iOS development-Alipay integration process

iOS development-Alipay integration process

1. See how the demo looks like

Search for "iOS Alipay SDK" on the Internet and download the SDK (Figure 1)


figure 1

Go to Figure 2 and click on the mobile payment Demo&SDK on the left, then click on the iOS&Android version resources on the right, click on the right SDK&DEMO to download the SDK


figure 2


After the download is complete, unzip it, and you get this (Figure 3)


image 3

Then unzip the "Alipay Wallet Payment Interface Development Kit 2.0 Standard Edition (iOS2.2.3)" to get this (Figure 4)



Figure 4

Open the folder and run it to see. Get this very dazzling interface (Figure 5)


Figure 5

Let's take a look at the source code:


Figure 6

As shown in Figure 6, there are three string variables in APViewController, which are the same as the prompt in Figure 5. This thing is a series of identifiers obtained after the merchant signs an agreement with Ali. Ask the company at that time.

2. Integrate Alipay by yourself

Create a new project (note: the project must be in English, otherwise a mysterious bug will appear).

Back to the demo downloaded just now, there are two files AlipaySDK.Bundle and AlipaySDK.framework. Copy these two files to the project


Figure 7

Import the header file in ViewController


Figure 8

Introduce the following code in the viewdidload method

Order *order = [[Order alloc] init];

order.partner = partner;

order.seller = seller;

order.tradeNO = [self generateTradeNO];//Order ID (determined by the merchant)

order.productName = product.subject;//Product title

order.productDescription = product.body;//Product description

order.amount = [NSString stringWithFormat:@"%.2f",product.price];//Business

Product price


Picture 9

You will find that there is an error.

Go back to the downloaded folder


Picture 10

You will find two files, Order.h and Order.m, copy these two files to the project, and then import the header file.


Picture 11

When you enter Order.h, you will find that many attribute names have been changed, so I changed it decisively.


Picture 12

Then copy the following code:

order.notifyURL = @"http://www.xxx.com";//Callback URL order.service = @"mobile.securitypay.pay"; order.paymentType = @"1";

order.inputCharset = @"utf-8";

order.itBPay = @"30m";

//Apply the registration scheme, define URL types in AlixPayDemo-Info.plist

NSString *appScheme = @"alisdkdemo";

//Combine the product information into a string

NSString *orderSpec = [order description];

NSLog(@"orderSpec = %@",orderSpec);

After copying NSString *appScheme = @"alisdkdemo"; after this sentence, you need to click info in the project configuration and add a type to the URLType below, and the value of URLscheme is alisdkdemo;


Figure 13

After the integration is complete, you will find that there are still some errors


Figure 14

The comment shows that it is a signature error. Find the signature code in the development document to solve the problem


Figure 15

Find these two folders and two files in the downloaded demo, copy the files to the project, import the header file "DataSigner.h", recompile and find that there will be a bunch of missing framework errors


Figure 15

At this time, you need to import the foundation framework on the header file of the error file. After compiling, there is finally one error left.


Figure 16

Open the project configuration file BulidSetting, enter "search" in the search box to find HeaderSearchPath, add a path with the content "$(SRCROOT)/project name" and press Enter and recompile.


Figure 17

There will be an error as shown in Figure 17, and some static libraries need to be imported at this time

libz.tbd

libc++.tbd

Security.framework

CoreMotion.Framework

CFNetwork.framework

CoreTelephony.framework

SystemConfiguration.framework



Figure 18

After the addition is complete, the compilation will not report an error.

Finally

//Format the successful signature string as an order string, please strictly follow the format

NSString *orderString = nil;

if (signedString != nil) {

orderString = [NSString

stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",

orderSpec, signedString, @"RSA"];

[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme

callback:^(NSDictionary *resultDic) {

NSLog(@"reslut = %@",resultDic);

}];

}

Copy this code into Viewdidload.

Copy the following code in AppDelegate.m and import it

#import <AlipaySDK\AlipaySDK.h>

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url

sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

{

//If the minimalist development kit is not available, it will jump to the Alipay wallet for payment, and the payment result of the Alipay wallet needs to be returned to the open

Send out

if ([url.host isEqualToString:@"safepay"]) {

[[AlipaySDK defaultService] processOrderWithPaymentResult:url

standbyCallback:^(NSDictionary *resultDic) {

NSLog(@"result = %@",resultDic);

}];

}

if ([url.host isEqualToString:@"platformapi"]){//Alipay wallet quick login authorization to return authCode

[[AlipaySDK defaultService] processAuthResult:url

standbyCallback:^(NSDictionary *resultDic) {

NSLog(@"result = %@",resultDic);

}];

}

return YES;

}

At this point, you're done, and you can make specific changes in conjunction with the project.