Ping++是一款非常不错的三方支付平台,简单,粗暴。
历经这么长时间,终于安下心来记录一下项目中Ping++支付的相关环节和内容,不得不说Ping++确实是一款很不错的三方支付平台,更重要的是方便快捷,简书App同样也运用了此三方平台。因为 Ping++官方给的接入指南 已经很详尽,所以这里就简单小结一下支付环节,附上项目中的部分代码。
1.导入SDK并添加相关设置
- 当前项目中用的CocoaPods依赖管理工具来管理所需的第三方开源库(这样可以节省设置和更新第三方开源库的时间),导入 pod ‘Pingpp’, ‘~> 2.1.0’ (默认会包含支付宝、微信、银联和百度钱包)。
- 由于项目中需要用到微信支付所以要添加
URL Schemes:,填入在微信平台上注册的应用程序的id(以wx开头的字符串)。如下图:

2.接入开启支付的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
- (void)buyCarAddOrderDataWithCarid_count:(NSString *)carid_count { typeof(self) _weakSelf = self; NSString *userID = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"]; NSString *URL = [NSString stringWithFormat:@"%@/App/Sylm/yclist",SERVERURL]; NSMutableDictionary *paramas = [NSMutableDictionary dictionary]; paramas[@"method"] = @"addorder"; paramas[@"buy_status"] = _buy_status; paramas[@"carid_count"] = carid_count; paramas[@"user_id"] = userID; paramas[@"ways"] = self.ways; paramas[@"addr_id"] = self.addressModel.address_id; [HXHttpTool post:URL params:paramas success:^(id json) { LXLog(@"%@", json); [_weakSelf wakeUpPingWithCharge:json]; } failure:^(NSError *error) { [SVProgressHUD showErrorWithStatus:@"调取支付失败" maskType:SVProgressHUDMaskTypeGradient]; LXLog(@"%@", error); }]; }
|
另:附上接口对照参数(仅供参考):

- 利用
charge对象唤起相应支付,其中kURLScheme为本应用的URL Schemes,在支付成功之后,正常跳转回应用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
- (void)wakeUpPingWithCharge:(NSString *)charge { typeof(self) _weakSelf = self; [Pingpp createPayment:charge viewController:self appURLScheme:kURLScheme withCompletion:^(NSString *result, PingppError *error) { if ([result isEqualToString:@"success"]) { [SVProgressHUD showSuccessWithStatus:@"支付成功" maskType:SVProgressHUDMaskTypeGradient]; } else { LXLog(@"Error: code=%lu msg=%@", error.code, [error getMsg]); [SVProgressHUD showErrorWithStatus:@"取消支付" maskType:SVProgressHUDMaskTypeGradient]; } }]; }
|
配置 kURLScheme,如下图:

- 接收并处理支付结果,必须实现
在AppDelegate.m文件中实现支付结果回调的方法,这里只做ios9以上的了。
1 2 3 4 5 6 7 8 9 10 11
| iOS 9 及以上 - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options { [Pingpp handleOpenURL:url withCompletion:^(NSString *result, PingppError *error) { LXLog(@"%@", result); if ([result isEqualToString:@"success"]) { [[NSNotificationCenter defaultCenter] postNotificationName:@"enterSuccessView" object:nil]; } }]; return YES; }
|
3.小结
- Ping++把支付订单的环节都放在服务器,所以客户端做的处理就少了许多,而且安全性更高(官方Demo用Swift写的),赞。
- 用微信支付的时候,要用真机进行测试,且安装的有微信客户端
- iOS9限制http协议的访问,需要在info.plist文件中添加访问权限,而且还要添加应用白名单,如下图:
