你的位置:首页 > 软件开发 > 操作系统 > iOS的Ping++支付接入步骤(详细)

iOS的Ping++支付接入步骤(详细)

发布时间:2016-02-14 17:00:06
Ping++ SDK1.简介Ping++ SDK包括Server和Client两部分。其中Server目前支持 PHP,Java,Python,Node.js,Ruby,Go,C# 七种语言。Client目前支持 iOS终端,Android终端,手机网页和PC网页这四种平台, ...

iOS的Ping++支付接入步骤(详细)

Ping++ SDK

1.简介

Ping++ SDK包括Server和Client两部分。

其中Server目前支持 PHP,Java,Python,Node.js,Ruby,Go,C# 七种语言。

Client目前支持 iOS终端,Android终端,手机网页PC网页这四种平台,分别对应iOS,Android,HTML5PC这四种Client SDK。

2.环境

为了提高接入效率,Ping++提供了Live和Test两个工作模式提供开发者接入时使用,这两种模式切换非常简单,只需要在使用Server SDK 的过程中设置 API Key 时根据自己的需要使用相应的Key即可,设置为test key 表明使用Test 模式,设置为live key 则使用Live模式。Test Key 在你注册Ping++后即可自动获得,Live key 则是在你完成签约后获得。

1⃣️Test 模式

Test模式提供开发测试时使用。因为Test模式吧支付流程与渠道草书隔离开,所以开发可以与渠道申请同时进行,从而缩短接入调试时间,Test 模式中发起虚拟交易不会调用真实支付控件,支付时客户端会调转到Ping++提供的支付页面

2⃣️Live 模式

Live 时应用上线的模式。该模式下会发生真实的交易,调起真实的支付控件产生真实的资金流动,所以请确保在Test模式下所有的攻能逗测试通过后,在切换到Live模式。

3.参数

应用在接入 Ping++ SDK 时,需要使用以下三个参数,这三个参数你可以在管理平台中获取:

1⃣️API Key:是 Ping++ 分配给你的唯一身份标识,即上面说到的Test KeyLive Key。

2⃣️应用ID:是 Ping++ 分配给你的应用的唯一标识。

3⃣️Notify URL:是 Ping++ 系统用来向你的应用后台推送异步通知时使用的地址,该地址必须是一个互联网可以访问的地址。你可以在 Ping++ 管理平台中对应的应用内进行设置。

下面介绍下Ping++如何发起并完成支付

iOS的Ping++支付接入步骤(详细)

 

1. Client 发送支付要素给 Server

用户选择渠道点击交易按钮, Client 收集交易所需的相关参数传递给 Server (服务器的地址为代码中的 URL)

 

NSMutableURLRequest * postRequest=[NSMutableURLRequest requestWithURL:url];

    NSDictionary* dict = @{

                           @"channel" : self.channel,

                           @"amount"  : amountStr

                           };

[postRequest setHTTPMethod:@"POST"];

    [postRequest setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    NSData* data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];

    NSString *bodyData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

    [postRequest setHTTPBody:[NSData dataWithBytes:[bodyData UTF8String] length:strlen([bodyData UTF8String])]];

    ViewController * __weak weakSelf = self;

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    [self showAlertWait];

    [NSURLConnection sendAsynchronousRequest:postRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        dispatch_async(dispatch_get_main_queue(), ^{

            NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;

            [weakSelf hideAlert];

            if (httpResponse.statusCode != 200) {

                NSLog(@"statusCode=%ld error = %@", (long)httpResponse.statusCode, connectionError);

                [weakSelf showAlertMessage:kErrorNet];

                return;

            }

            if (connectionError != nil) {

                NSLog(@"error = %@", connectionError);

                [weakSelf showAlertMessage:kErrorNet];

                return;

            }

            NSString* charge = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

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

            //客户端从服务器端拿到charge对象后,调用下面的方法

           //请看3.Client 调起支付控件完成支付

       });

    }];

 

2. Server 发送支付请求并将返回的支付凭据传给 Client

Server 接收并处理 Client 传过来的数据,使用 Ping++ 提供的方法向 Ping++ 发起交易,并将从 Ping++ 获得的带支付凭据的 Charge 对象返回给 Client。

3. Client 调起支付控件完成支付

Client 接收 Server 返回的带支付凭据的 Charge 对象并用之调起支付插件完成交易.

//客户端从服务器端拿到charge对象后,调用下面的方法

            [Pingpp createPayment:charge viewController:weakSelf appURLScheme:kUrlScheme withCompletion:^(NSString *result, PingppError *error) {

                NSLog(@"completion block: %@", result);

                if (error == nil) {

                    NSLog(@"支付成功");

                } else {

                    NSLog(@"PingppError: code=%lu msg=%@", (unsigned  long)error.code, [error getMsg]);

                    NSLog(@"支付失败");

                }

                [weakSelf showAlertMessage:result];

            }];

4. 渠道同步返回支付结果给 Client

在上一步中用户完成了支付,渠道会返回一个支付结果给客户端,这里 Client 需要做的是处理此结果。

渠道为银联、百度钱包或者渠道为支付宝但未安装支付宝钱包时,交易结果会在调起插件时的 Completion 中返回。 渠道为微信、支付宝且安装了支付宝钱包时,请实现 UIApplicationDelegate 的 - application:openURL:sourceApplication:annotation: 方法:

#warning 渠道为微信、支付宝且安装了支付宝钱包时实现方法

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

    [Pingpp handleOpenURL:[NSURL URLWithString:kUrl] withCompletion:^(NSString *result, PingppError *error) {

        if ([result isEqualToString:@"success"]) {

            //...

        }else{

            NSLog(@"PingppError: code=%lu msg=%@", error.code, [error getMsg]);

        }

    }];

    return YES;

}

5. Server 收到 Ping++ 发送的交易结果的异步通知

Ping++ 会把从渠道收到的异步通知告诉商户 Server,客户 Server 接收到异步通知是一个带支付状态的完整的 Charge 对象,客户在接收到异步通知后需要回复 success 给 Ping++ 表明成功收到异步通知。所有的交易结果,商户均须以异步通知结果为准。关于异步通知具体请参见 API Reference 文档。

 

参考链接: https://www.pingxx.com/guidance/client/sdk/ios

 


原标题:iOS的Ping++支付接入步骤(详细)

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。