1.去支付宝官网下载官方Demo,把appid和私钥写入,验证拿到的账号信息是否正确。之前接入支付宝的时候,就是因为拿到的appid不对,在接入过程中找这个问题花费了很多不必要的时间。
2.清楚支付宝app支付流程,流程图如下:
app支付架构
app支付流程
3.导入支付宝SDK和在Build Phases选项卡的Link Binary With Libraries下增加相应依赖
4.开始支付宝的接入,访问后台接口,传入相关订单信息参数,后台使用私钥对订单信息进行RSA加密,并将签名字符串base64编码和UrlEncode后返回到客户端,签名示例如下:
charset=utf-8&biz_content=%7B%22out_trade_no%22%3A%22170224144622309845%22%2C%22total_amount%22%3A%220.01%22%2C%22subject%22%3A%22%E6%AD%BC20%E6%88%98%E6%96%97%E6%9C%BA%22%2C%22timeout_express%22%3A%2230m%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22body%22%3A%22%E6%AD%BC20%E6%88%98%E6%96%97%E6%9C%BA%22%2C%22passback_params%22%3A%2210%22%2C%22seller_id%22%3A%222088021544263245%22%7D&method=alipay.trade.app.pay¬ify_url=http%3A%2F%2Fhseschool.app360.cn%2Fapp%2FpingNotify%2FaliPayNotify&app_id=2016040501268128&sign_type=RSA&version=1.0×tamp=2017-02-24+14%3A46%3A22&sign=W8zMEj%2FCtjafRcLW9mZXdd34YQgg53RHqhTZChBd6NvJNDxNqyPgCS8mhGrqOksU%2BzjkjA%2FrqpKo2b4T4Km7%2BTyqG30cVYIqTBweu5bGONiQvIizIQma%2Fbs5a0hw7ZEnI92w6K4N0ylShMoJpS4TRgYSn%2Fe%2Fh9fkMfFMQF5yXvs%3D
5.拿到签名之后,对签名中的支付宝appid和本地进行校验,成功之后才进行支付。
6.调起支付宝SDK进行支付,示例代码如下:
/**
* 支付宝支付接口
*
* @param orderStr 订单信息
* @param schemeStr 调用支付的app注册在info.plist中的scheme
* @param compltionBlock 支付结果回调Block,用于wap支付结果回调(非跳转钱包支付)
*/
- (void)createAliPayment:(NSString *)orderStr appURLScheme:(NSString *)schemeStr withCompletion:(CompletionBlock)completionBlock
{
[[AlipaySDK defaultService]payOrder:orderStr fromScheme:schemeStr callback:^(NSDictionary *resultDic) {
completionBlock(resultDic);
}];
}
7.处理支付宝回调结果,示例结果如下:
{
"memo" : "xxxxx",
"result" : "{
\"alipay_trade_app_pay_response\":{
\"code\":\"10000\",
\"msg\":\"Success\",
\"app_id\":\"2014072300007148\",
\"out_trade_no\":\"081622560194853\",
\"trade_no\":\"2016081621001004400236957647\",
\"total_amount\":\"0.01\",
\"seller_id\":\"2088702849871851\",
\"charset\":\"utf-8\",
\"timestamp\":\"2016-10-11 17:43:36\"
},
\"sign\":\"NGfStJf3i3ooWBuCDIQSumOpaGBcQz+aoAqyGh3W6EqA/gmyPYwLJ2REFijY9XPTApI9YglZyMw+ZMhd3kb0mh4RAXMrb6mekX4Zu8Nf6geOwIa9kLOnw0IMCjxi4abDIfXhxrXyj********\",
\"sign_type\":\"RSA2\"
}",
"resultStatus" : "9000"
}
当resultStatus==9000说明,支付完成,此时调用后台结果,查询实际支付结果。因为实际支付是否成功,完全依赖服务端异步通知。
否则,根据支付宝提供的错误码,显示相应的错误信息。最后附上支付宝官网文档连接:app支付宝文档