Payment of Alipay App payment access process (1)

Posted by koopkoop on Tue, 09 Jul 2019 20:06:49 +0200

It doesn't matter what merchant end, user side and payment platform end do not understand the payment function integration. This article takes Alipay as an example to explain Alipay's SDK integration process:
Generally, any technical learning should be done step by step by Demo.
This article takes Alipay Demo as an example to teach you how to integrate quickly.
Kind test can be used, so study hard!

This catalogue:
Import the corresponding library
2. Fast access mode:
3. Notes:

Import the corresponding library

1. Import the following system libraries
In the Link Binary With Libraries tab of Build Phases, add the following dependencies:

// Alipay SDK relies on
libc++.tbd
libz.tbd
SystemConfiguration.framework
CoreTelephony.framework
QuartzCore.framework
CoreText.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
CoreMotion.framework
CFNetwork.framework

// Alipay relies on it only after importing the AlipaySDK.framework library.
AlipaySDK.framework

2.2.a static libraries, 1 framework and 1 bundle file, a total of 4 libraries (found in Demo), as follows:

libcrypto.a
libssl.a
AlipaySDK.framework
AlipaySDK. bundle

3. Import two folders in demo, as follows:

openssl
Util

The import effect is as follows:


The imported external library, a total of 6 files.png

Build, the compiler finds the following error
Error reporting: #include <openssl/asn1.h> could not be found
Solution:
Search for header search in the Build Settings tab and add a new path to $(PROJECT_DIR)/FarmAndAnimal/Vendor/AliPaySDK/
The AliPay SDK here refers to the upper level file of openssl, not necessarily the AliPay SDK. Keep in mind!

2. Fast access mode:

  1. Appdelegate
    header file
    #import  <AlipaySDK/AlipaySDK.h>

Code
Add the following code to Appdelegate, and it's still intact! ____________ (for example, only Alipay is integrated, all the payment methods should be integrated in the later stage).

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    if ([url.host isEqualToString:@"safepay"]) {
        // Pay jump Alipay wallet to pay, deal with the result of payment.
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
        }];

        // Authorized to jump Alipay wallet to pay, deal with the result of payment.
        [[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            // Parsing auth code
            NSString *result = resultDic[@"result"];
            NSString *authCode = nil;
            if (result.length>0) {
                NSArray *resultArr = [result componentsSeparatedByString:@"&"];
                for (NSString *subResult in resultArr) {
                    if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
                        authCode = [subResult substringFromIndex:10];
                        break;
                    }
                }
            }
            NSLog(@"Authorization results authCode = %@", authCode?:@"");
        }];
    }
    return YES;
}

// NOTE: Use the new API interface after 9.0
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
    if ([url.host isEqualToString:@"safepay"]) {
        // Pay jump Alipay wallet to pay, deal with the result of payment.
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
        }];

        // Authorized to jump Alipay wallet to pay, deal with the result of payment.
        [[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);
            // Parsing auth code
            NSString *result = resultDic[@"result"];
            NSString *authCode = nil;
            if (result.length>0) {
                NSArray *resultArr = [result componentsSeparatedByString:@"&"];
                for (NSString *subResult in resultArr) {
                    if (subResult.length > 10 && [subResult hasPrefix:@"auth_code="]) {
                        authCode = [subResult substringFromIndex:10];
                        break;
                    }
                }
            }
            NSLog(@"Authorization results authCode = %@", authCode?:@"");
        }];
    }
    return YES;
}

2. Import the following code into the function class to be paid

Header file:

#import "RSADataSigner.h"
#import <AlipaySDK/AlipaySDK.h>

Code:

// NOTE: Invoke payment result to start payment
//Apply registration scheme to define URL types in AliSDKDemo-Info.plist
 NSString *appScheme = @"FarmBB";
 [[AlipaySDK defaultService] payOrder:result.data fromScheme:appScheme callback:^(NSDictionary * resultDic) {
                        NSLog(@"reslut = %@",resultDic);
                    }];

Note: Two parameter explanations:
After requesting the order, Alipay returns a string of characters, such as result.data and appScheme, which is callback to its application after successful payment. Then, according to the above, it will be accepted in resultDic if the payment is successful.

 NSString *resultStatus = resultDic[@"resultStatus"];
            NSString *errStr = resultDic[@"memo"];
            switch (resultStatus.integerValue) {
                case 9000:// Success
                    break;
                case 6001:// cancel
                    break;
                default:
                    break;
            }

3. Modify urlSchemes and add callback url schemes as shown in the following figure. Be sure to keep in line with the above appScheme so that payment success or failure can be returned to my application.


Modify my urlSchemes.png

3. Notes:

1. Principle Processing of Error Reporting

# Include <openssl/asn1.h> can not be found because no corresponding path has been found. Note that <> will be found from the root directory.
So if you set the path to the upper level of openssl, you can find it and solve this error!

2. Method Interpretation in Appdelegate:
The following method jumps to Alipay before returning to iOS9 to determine whether the payment is successful, or the order has been down, but has not yet been paid, and is processed in this way.

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation

The following method is used to jump back to Alipay after iOS9 to determine whether the payment is successful, or the order has been down, but has not yet been paid, and is processed in this way.

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options

3. Payment process description:

(1) First of all, the client sends out a request to place an order - > Our server responds and returns its request to the order information, indicating that the order is successful!
(2) Secondly, the client will continue to send the string returned by the server and its appScheme to Alipay's client App - - to tune up the Alipay client (or H5 page) - > client payment completion - > Alipay sends the information to - > Alipay server side - > Alipay server return payment to Alipay client.
(3) Alipay client tells our merchant App---> App whether the payment is successful.
(4) at the same time, Alipay server will notify our merchant server App to tell if the payment is successful.

Attached is a picture of the transaction as follows:


Alipay transaction flow chart.png

4. Be sure to remember to modify urlSchemes
Later on, we will write a complete Demo of Wechat Payment, UnionPayment, UnionPayment and the docking of the three for learning!

With Alipay iOS terminal integration process, it doesn't seem to be any use!
https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.sFUG6d&treeId=204&articleId=105295&docType=1

If you have any questions, you can add QQ: 1824496534. Note: Payment
Welcome to guide each other and make progress together!
From DaviD

Topics: OpenSSL SDK iOS