iOS Alipay payment development (updated version)

1 Introduction

This article is a summary of the previous projects. Due to the relevant payment SDK iteration, the original text has not met the needs. The following updates are made for your reference. In addition, a summary of common problems has been added.

Today we will mainly introduce Alipay payment, and other payment introductions will be updated as soon as possible.

Before making payment, I also searched for information on the Internet. Most of them said that there are too many payment access pits, the most WeChat pits, and the UnionPay documents are too complicated.
In fact, if there are more accesses, those routines can go around.
Most of the online experience is relatively old, and there are not many articles that can be used in relatively new ones (but there are still good articles, which are also very helpful to me). Let me give you a detailed introduction to help you avoid the pit.

Source code acquisition

Follow the official account " Wangluo Development " and reply " Alipay Pay " to receive

2. Interactive process

It is recommended to read the development documents carefully first. Be sure to read it. At the beginning, you did not read it honestly. As a result, you encountered a lot of pits and wasted a lot of time. Therefore, it is recommended to read it carefully, especially This part of the interactive process.

2.1 Functional process

2.2 Data interaction

Compared with WeChat Pay, the functional process of Alipay payment is very simple. As shown in the above two pictures, our App (that is, the merchant client) only has three steps:

  • Generate orders
  • Call Alipay interface to send order
  • Return the order payment result and process

3. Download Alipay SDK

Alipay has many businesses. It takes a lot of effort to find the payment SDK you want. Here is the latest SDK address.
Note that there is no legendary development document in the downloaded SDK package. You need to find it elsewhere or Look at the webpage.

Public key, private key, PID, sellerID, key are all explained in detail in the document, so I won’t go into details here. You must make the concept clear before doing it, otherwise it will be messed up for a while. If we encounter problems, we can discuss them together.

4. Import library integration SDK

4.1 Import files and key libraries

Import via CocoaPods

pod  'AlipaySDK-iOS' 

Manual import

Official demo screenshot

Add framework and other files
Open the iOS project, the new version of the SDK file Order file, you need to copy and paste into your own project:

  • AlipaySDK.framework
  • AlipaySDK.bundle
  • Until folder
  • openssl folder
  • libcrypto.a 和 libssl.a

Some of the above files are not visible in the open project, you need to open the project folder to find it.

In the Link Binary With Libraries of the Build Phases tab, add the following dependencies:

Among them, it should be noted that:

  • If it is a version after Xcode 7.0, you need to add libc++.tbd, libz.tbd;
  • If it is a version before Xcode 7.0, you need to add libc++.dylib, libz.dylib (as shown in the figure below).

At this point, if you start the project, you will most likely find an error report.

4.2 Introducing header files

In the files that need to call AlipaySDK, add header file references.

import <AlipaySDK/AlipaySDK.h>

4.3 Call payment interface

In Alipay's access document, the step of generating an order is done on the client side, but this is best done on the server side.

The order is generated in the background, then spliced, signed, and then the server-side directly transmits an encrypted and signed parameter to the client-side, which is more secure.

All order information, merchant information, etc. are in your own hands. In this way, the APP is not afraid of data being intercepted, and it is particularly simple to call. You only need to call the payment interface.

If you only need to send orders and process payment and return results, you only need to add AlipaySDK.bundle and AlipaySDK.framework, which are easy to find in the downloaded SDK.
The quick payment method is this:

-(void)payOrder:(NSString *)orderStr fromScheme:(NSString *)schemeStr callback:(CompletionBlock)completionBlock;

In the payment button, use the Alipay class and call this method again! As follows:

// NOTE: 调用支付结果开始支付
[[AlipaySDK defaultService] payOrder:orderString fromScheme:@"FBYAlipayDemo" callback:^(NSDictionary *resultDic) {
    NSLog(@"reslut = %@",resultDic);
}];

4.4 Configure Alipay client to return url processing method

For example, in the sample AliSDKDemo\APAppDelegate.m file, add the reference code:

import <AlipaySDK/AlipaySDK.h>

In *@*implementation AppDelegate, the NSLog in the following code is changed to the actual business processing code:

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
    if ([url.host isEqualToString:@"safepay"]) {
        //跳转支付宝钱包进行支付,处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
        }];
    }
    return YES;
}

4.5 Project configuration

Finally, don't forget to write a URL Scheme. You can find the last one under Targets -> Info.
Click the "Info" tab, and in the "URL Types" option, click "+".

5. Summary of common problems

5.1 Cannot return to the App after payment

Generally, the whitelist is not set correctly

// NOTE: 调用支付结果开始支付
[[AlipaySDK defaultService] payOrder:orderString fromScheme:@"FBYAlipayDemo" callback:^(NSDictionary *resultDic) {
    NSLog(@"reslut = %@",resultDic);
}];

The appScheme parameter in the code above needs to be the same as the URL Schemes set in the article 4.5 project configuration , so that the App can be returned correspondingly.

5.2 #include <openssl/opensslconf.h> not find

This is a magical pit. I Googled it for a long time, but I couldn't understand it. Then after being reminded by netizens, I remembered the difference between #import "" and #import <>.

Solution : Targets -> Header Search Paths under Build Settings.
Add the following directory "$(SRCROOT)/project name/absolute address of the file"
as shown in the figure:

3.png

5.3 ‘openssl/asn1.h’ file not found

Due to the introduction of the openssl library into the project, this problem occurs because the library file project cannot be found. You only need to add $(PROJECT_DIR)/project name/openssl in the Header Search Paths, as shown in the following figure:

5.4 Undefined symbol: OBJC_METACLASS$_WKWebView

When the above problem occurs, you need to add the system class library in the project configuration: WebKit.framework

5.5 If an error is reported after running, it is similar to the following prompt message:

Cannot find interface declaration for 'NSObject', superclass of 'Base64'

Then you need to open the file that reported the error and add the header file.

# import <Foundation/Foundation.h>

5.6 Problems encountered when accessing SDK in Swift

If the project uses Swift as the development language, you need to add a bridge file, such as Bridging-Header.h

At the same time, set the location of the bridge file in the project Build Settings.

If the following error occurs during runtime, write #import <UIKit/UIKit.h> in the bridge file

5.7 Click to pay to jump to the Alipay card in the start page

When this happens, the page stuck on the page will show a corresponding error message. Generally, there are the following situations:

  • Product information splicing string error
  • The payment account is expecting renewal status
  • Merchant ID error
  • Failed to verify the order information

At this point, Alipay payment is basically completed. If you encounter any problems during the integration process, you can leave a message to me.

Guess you like

Origin blog.csdn.net/qq_36478920/article/details/113363062