iOS Integration
1.DownloadSDK package, and extract it. What you will get are:
.
|____S4P_SDK // SDK directroy
| |____ScanForPaySDK.framework // ScanForPay Framework
| |____Channels // Dependencies: Alipay SDK
|____ScanForPay_Demo // Demo project
2.Drag the SDK directroy into your project
3.Add dependencies,Target --> Build Phases --> Link Binary With Libraries
4.If your App needs to visit the http address,following code should be added to the Info.plist
,or add NSExceptionDomains
according to your requirements.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
5.Add URL Scheme,Target --> Info --> URL Types, and add your URL Scheme
6.Call SDK methods to create payment.
1)Include header file:
#import <ScanForPaySDK/ScanForPayTask.h>
2)Create payment.
pay
method in ScanForPayTask need a parameter orderInfo
, in which the signature should be generated by server side. The format of this orderInfo
can be found in iOS Api.
If the Alipay App is not installed or call it fails, payment will go on with the wap page, and after the payment is completed or cancelled, the result will be returned in the callback of the pay
method.
If the Alipay App is opened for the payment, the callback will enter the App's OpenUrl
callback.
// Get ScanForPayTask instance. It's a Singleton class
ScanForPayTask* task = [ScanForPayTask shareInstance];
// Building order information. This must be done on the server, here is just an example, you can not generate a signature on the client.
// See API documentation for the format, or refer to the Demo.
NSString *request = [self buildRequest:@"Alipay"
orderTitle:@"test"
price:0.01f];
// Create payment
[task pay:request // request string
appScheme:@"scanforpaySdkDemo" // registered URL Scheme in merchant's app
callback:^(NSDictionary *dictResult){
// NOTE: If the Alipay App is not installed or opened fails, this block will be executed.
NSLog(@"reslut test= %@",dictResult);
if([dictResult[@"code"] intValue] == 1 && [dictResult[@"data"] [@"status"] intValue] == 1){
// NOTE: If the request is successful and the payment status is successful, the signature in params should be verified at the app server first.
NSLog(@"pay success, params =%@", dictResult[@"data"][@"params"]);
// validate signature
// success, and display something
} else{
if([dictResult[@"code"] intValue] < 0){
// Errors Alipay returns are transformed to negative. It's usually caused by user cancellation, so left it unprocessed.
}else{
// Failed, display something.
}
}
}];
3)Register openURL
in AppDelegate.h
, and then call handleOpenURL
of ScanForPayTask in it to get response, and pass the response to app server to validate signature and determin the success of the order.
// NOTE: 9.0以后使用新API接口
// NOTE: After 9.0,use the new API
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
// Send the url SDK, After processing, the relevant order information and results will be returned to the merchant.
return [[ScanForPayTask shareInstance] handleOpenURL:url callback:^(NSDictionary *dictResult){
// NOTE: After getting the result, you need to send the order content to the server of the APP, verify the signature or query the result to confirm the payment result.
NSLog(@"reslut test= %@",dictResult);
if([dictResult[@"code"] intValue] == 1 && [dictResult[@"data"] [@"status"] intValue] == 1){
// NOTE: If the request is successful and the payment status is successful, the signature in params should be verified at the app server first.
NSLog(@"pay success, params =%@", dictResult[@"data"][@"params"]);
// validate signature
// success, and display something
} else{
if([dictResult[@"code"] intValue] < 0){
// Errors Alipay returns are transformed to negative. It's usually caused by user cancellation, so left it unprocessed.
}else{
// Failed, display something.
}
NSLog(@"pay failed, code =%@, msg=%@", dictResult[@"code"], dictResult[@"msg"]);
}
}];
}