当前主流 APP 内购支付方式为微信支付、支付宝支付,结合项目对两类支付做简单总结。
一、微信支付流程
微信支付(官方):
签名算法:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=4_3
APP 开发步骤:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5
API 列表:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1#
1.引入支付 SDK,完成相关配置
APP 开发步骤:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5
资源中心:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419319164&lang=zh_CN
2.APP 内购支付流程
2.1 APP 商品下单
商品下单需先调用后端接口
- 1.验证用户 token 接口
- 2.请求后端接口,返回微信
prepayid
2.2 调起支付
- 1.制作签名(也可以服务器端制作签名)
微信需要二次签名 - 2.调起支付接口
-(void)signWXPay:(NSString *)prepayid{
payRequsestHandler *req = [[payRequsestHandler alloc] init];
[req init:APP_ID mch_id:MCH_ID];
//设置密钥
[req setKey:PARTNER_ID];
NSMutableDictionary *signParams = [NSMutableDictionary dictionary];
[signParams setObject: APP_ID forKey:@"appid"];
NSString *time_stamp, *nonce_str;
time_t now;
time(&now);
time_stamp = [NSString stringWithFormat:@"%ld", now];
nonce_str = [WXUtil md5:time_stamp];
[signParams setObject: nonce_str forKey:@"noncestr"];
[signParams setObject: @"Sign=WXPay" forKey:@"package"];
[signParams setObject: MCH_ID forKey:@"partnerid"];
[signParams setObject: time_stamp forKey:@"timestamp"];
[signParams setObject: prepayid forKey:@"prepayid"];
//生成签名
NSString *sign = [req createMd5Sign:signParams];
//调起微信支付https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_12&index=2
if (sign != nil) {
PayReq* req1 = [[PayReq alloc] init];
req1.openID = APP_ID;
req1.partnerId = MCH_ID;
req1.prepayId = prepayid;
req1.nonceStr = nonce_str;
req1.timeStamp = time_stamp.intValue;
req1.package = @"Sign=WXPay";
req1.sign = sign;
[WXApi sendReq:req1];
}
}
2.3 处理回调
-(void)onResp:(BaseResp *)resp
{
#pragma mark 微信分享返回resp是SendMessageToWXResp类型
if ([resp isKindOfClass:[SendAuthResp class]]) {
SendAuthResp *aresp = (SendAuthResp *)resp;
if (aresp.errCode == 0)
{
}else if (aresp.errCode == -4){
// [self messageShow:@"用户拒绝"];
}else if (aresp.errCode == -2){
// [self messageShow:@"用户取消"];
}
}
if([resp isKindOfClass:[PayResp class]]){
//支付返回结果,实际支付结果需要去微信服务器端查询
switch (resp.errCode) {
case WXSuccess:
{
#支付成功-相关处理代码
}
break;
default:
if (resp.errCode == -4){
//[self messageShow:@"用户拒绝"];
}else if (resp.errCode == -2){
// [self messageShow:@"用户取消"];
}
#支付失败-相关处理代码
break;
}
}
}
二、支付宝支付
支付宝支付(官方):
APP 支付介绍:https://docs.open.alipay.com/204
iOS 集成流程:https://docs.open.alipay.com/204/105295/
参数说明:https://docs.open.alipay.com/204/105465/
签名专区:https://docs.open.alipay.com/291/
API 列表:https://docs.open.alipay.com/api/
1.引入支付 SDK,完成相关配置
APP 开发步骤:https://docs.open.alipay.com/204/105295/
资源中心:https://docs.open.alipay.com/54/104509/
2.APP 内购支付流程
2.1 APP 商品下单
商品下单需先调用后端接口
1.验证用户 token 接口
2.请求后端接口,返回支付宝签名(也可以 APP 做签名)
签名专区:https://docs.open.alipay.com/291/
签名返回示例:
请求参数说明:https://docs.open.alipay.com/204/105465/
charset=utf-8
&biz_content=%7B%2timeout_express%2%2230m%22%2Cproduct_code%22%3AQUICK_MSECURITY_PAY%2C%22total_amount%8A%A1%E8%B4%B5%E5%AE%BE%E5body%22%3A%22%E9%99%E8%85%BE%E4%B8%93%E4%BA%AB%22%2C%22out_trade_no%22%3A%22Z******%22%2C%22passback_params%22%3A%222
&method=alipay.****.app.pay
¬ify_url=http%3A%2F%2Fletapi.*******.net%2FretrunPay%2Fali
&app_id=201****890181
&sign_type=RSA2
&version=1.0
×tamp=****+11%3A52%3A18
&sign=ShkofgVliErCyPGXdmW%2BOcdjQPBSILSigZzLhy3jzj6dQZcQEqsOMvsSMmOQ0lWK3YzlrcnWMQmY71%2Fo3Es8icbNtkRaQAx8%2FjwANDzf50x0bD5AEFcGFDX4icaSKs%2BGk5VGhYbiZP6VeW8cYaWE8Gyg3A9fGbufN1yWA8uB5SrQaBo9EeieI%2Bj%2BqPY4CVtoP9acOvr2HPrR2DKmqLNO%2FSjmWPiVqxkg%3D%3D****;
2.2 调起支付
返回参数说明:https://docs.open.alipay.com/204/105301/
-(void)Alipay:(NSString *)orderString{
[[AlipaySDK defaultService] payOrder:orderString fromScheme:@"alisdk****" callback:^(NSDictionary *resultDic) {
NSLog(@"==注意这里是支付宝返回结果=====%@",resultDic);
NSNumber *restNum = [resultDic objectForKey:@"resultStatus"];
if ([restNum integerValue] == 9000) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"paySuccess" object:nil];
} else{
[[NSNotificationCenter defaultCenter] postNotificationName:@"payFailure" object:nil];
}
}];
}
2.3支付回调
#pragma mark - 支付宝支付回调
-(void)aliPay:(NSURL *)url{
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSNumber *restNum = [resultDic objectForKey:@"resultStatus"];
if ([restNum integerValue] == 9000) {
//支付成功处理代码
}else{
//支付失败处理代码
}
}];
}
其他
如果项目还引入了友盟分享的话还需要在代理方法里设置。
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSLog(@"%@", url.host);
if ([url.host isEqualToString:@"safepay"]){//支付宝支付
[self aliPay:url];
}else if ([url.host isEqualToString:@"pay"]) {//微信支付
return [WXApi handleOpenURL:url delegate:self];
}else{
//6.3的新的API调用,是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
if (!result) {
// 其他如支付等SDK的回调
}
return result;
}
return YES;
}