IOS UnionPay payment

Official SDk download address https://open.unionpay.com/ajweb/index
Test Demo: http://pan.baidu.com/s/1kUKMaqz

1. Import the official SDK. Directly drag into the project
2. Add system package: Target - > build phases - > link binary with libraries add 4 packages: CFNetwork.framework, SystemConfiguration.framework, Libz.tbd, libPaymentControl.a (automatically imported when dragging into the project). Effect map

3. In info.plist setting of the project. Add a URl Types callback protocol. Just copy the project name to the URL Schemes

4. Configure the info.plist file to paste the two fields into the source Code mode of info.plist as follows:

LSApplicationQueriesSchemes

uppaysdk
uppaywallet
uppayx1
uppayx2
uppayx3

NSAppTransportSecurity NSAllowsArbitraryLoads

<1> The first Array represents the protocol white list. <2> The second dictionary represents ios 9 https access network protocol field

6. Add ObjC macro: select project targets - > build settings - > linking - > other linker flags

7. Change the suffix of the source file that refers to UPPaymentControl.h to. mm

Copy the following code in the appdelete. M file:

------------------------APPDelegate.m--------------------------------------------

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


    [[UPPaymentControl defaultControl] handlePaymentResult:url completeBlock:^(NSString *code, NSDictionary *data) {

        //If the result code is successful, verify the signature first, and then do subsequent processing
        if([code isEqualToString:@"success"]) {

            //Determine whether signature data exists
            if(data == nil){
                //If there is no signature data, it is recommended that the merchant app query the transaction results in the background
                return;
            }

            //Data conversion from NSDictionary to NSString
            NSData *signData = [NSJSONSerialization dataWithJSONObject:data
                                                               options:0
                                                                 error:nil];
            NSString *sign = [[NSString alloc] initWithData:signData encoding:NSUTF8StringEncoding];

            //The signature verification certificate is the same as the background signature verification certificate
            //verify here, the merchant needs to send it to the merchant background for signature verification
            if([self verify:sign]) {
                NSLog(@"Payment successful");
                //The payment is successful and the signature verification is successful. The prompt of successful payment is displayed
            }
            else {

                NSLog(@"Signature verification failed");
                //Failed to verify the signature, the transaction result data was tampered, and the merchant app background inquired the transaction result
            }
        }
        else if([code isEqualToString:@"fail"]) {
            //Transaction failed
        }
        else if([code isEqualToString:@"cancel"]) {
            //Cancellation of transaction
        }
    }];

    return YES;
}

-(BOOL) verify:(NSString *) resultStr {

    //The signature verification certificate is the same as the background signature verification certificate
    //verify here, the merchant needs to send it to the merchant background for signature verification
    return NO;
}
------------------------APPDelegate.mm--------------------------------------------

------------------------ViewController.mm--------------------------------------------
#import "UPPaymentControl.h"

#define kURL_TN_Normal                @"http://101.231.204.84:8091/sim/getacptn"


@interface ViewController (){
    NSMutableData* _responseData;

}

@end
- (IBAction)Pay:(UIButton *)sender {
    // Check whether the interface of UnionPay App - (BOOL)isPaymentAppInstalled
    [self startNetWithURL:[NSURL URLWithString:kURL_TN_Normal]];


}


- (void)startNetWithURL:(NSURL *)url
{

    NSURLRequest * urlRequest=[NSURLRequest requestWithURL:url];
    NSURLConnection* urlConn = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
    [urlConn start];
}

#pragma mark - connection
//Receive docking information with UnionPay payment
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response
{
    NSHTTPURLResponse *rsp = (NSHTTPURLResponse*)response;
    NSInteger code = [rsp statusCode];
    if (code != 200)
    {
        NSLog(@"network error");
        [connection cancel];
    }
    else
    {

        _responseData = [[NSMutableData alloc] init];
    }
}

//Splicing message
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_responseData appendData:data];
}

//Connection completed importing
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString  *tn = [[NSMutableString alloc] initWithData:_responseData encoding:NSUTF8StringEncoding];
    if (tn != nil && tn.length > 0)
    {

        NSLog(@"tn=%@",tn);
        //tn: transaction No
        //scheme: agreement returned after payment: generally, we fill in the APP name
        //Mode 01: test environment 00: production environment
        //
        [[UPPaymentControl defaultControl] startPay:tn fromScheme:Here's your APP project name mode:@"01" viewController:self];

    }


}
//connection failed
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"network error");
}

------------------------ViewController.mm--------------------------------------------

Finally, I would like to thank Mr. Fan for his strong support.

Tags: network SDK encoding iOS

Posted on Sat, 02 May 2020 08:05:01 -0700 by leafface