1、调用服务器端接口,支付宝返回一段HTML,然后将HTML用UIWebview加载一下
注意:服务器端接口一定是调用支付宝h5接口,和pc端有区别,要不然下面的操作不会再进行了
下面是服务器端接口主要代码
private string mobile_submit_alipay(HttpContext context, string WIDout_trade_no, string WIDsubject
, string WIDtotal_amount, string WIDshow_url, string WIDbody)
{
DefaultAopClient client = new DefaultAopClient(config.gatewayUrl, config.app_id, config.private_key, "json", "1.0", config.sign_type, config.alipay_public_key, config.charset, false);
// 外部订单号,商户网站订单系统中唯一的订单号
string out_trade_no = WIDout_trade_no.Trim();
// 订单名称
string subject = WIDsubject.Trim();
// 付款金额
string total_amout = WIDtotal_amount.Trim();
// 商品描述
string body = WIDbody.Trim();
// 支付中途退出返回商户网站地址
string quit_url = WIDshow_url.Trim();
// 组装业务参数model
AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
model.Body = body;
model.Subject = subject;
model.TotalAmount = total_amout;
model.OutTradeNo = out_trade_no;
model.ProductCode = "QUICK_WAP_WAY";
model.QuitUrl = quit_url;
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
// 设置支付完成同步回调地址
request.SetReturnUrl("http://域名/pay/alipay/wap/Return_url.aspx");
// 设置支付完成异步通知接收地址
request.SetNotifyUrl("http://域名/pay/alipay/wap/Notify_url.aspx");
// 将业务model载入到request
request.SetBizModel(model);
AlipayTradeWapPayResponse response = null;
try
{
response = client.pageExecute(request, null, "post");
return response.Body;
}
catch (Exception ex)
{
}
return "";
}
服务端接口返回具体html为:
将上述html加载到webview里面后,webview会进行几次跳转,最终可以获取到一段跳转到支付宝的代码如下:
alipay://alipayclient/?%7B%22requestType%22%3A%22SafePay%22%2C%22fromAppUrlScheme%22%3A%22alipays%22%2C%22dataString%22%3A%22h5_route_token%3D%5C%22RZ24aCETVKzE9Sm6Y9I5ROlaBO9xhImobilecashierRZ24%5C%22%26is_h5_route%3D%5C%22true%5C%22%22%7D
替换跳转代码
[url stringByReplacingOccurrencesOfString:@"%22alipays%22" withString:@"%22testmobilepay%22"]
testmobilepay为你项目中设置的urlschemes
注意这个地方设置的urlscheme不能有特殊字符
对上述代码进行[UrlEncode编码/解码]后得到:
alipay://alipayclient/?{"requestType":"SafePay","fromAppUrlScheme":"alipays","dataString":"h5_route_token=\"RZ24OeCaraWz233HQyQ7yavoAFEJaQmobilecashierRZ24\"&is_h5_route=\"true\""}
字符替换完之后代码:
alipay://alipayclient/?%7B%22requestType%22%3A%22SafePay%22%2C%22fromAppUrlScheme%22%3A%22testmobilepay%22%2C%22dataString%22%3A%22h5_route_token%3D%5C%22RZ24aCETVKzE9Sm6Y9I5ROlaBO9xhImobilecashierRZ24%5C%22%26is_h5_route%3D%5C%22true%5C%22%22%7D
然后用
BOOL bSucc = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:new_url]];
跳转到支付宝,支付宝支付完成后,即会跳转回你的app了
具体参考了里面一篇文章
https://www.jianshu.com/p/0d8dd04fe94e
大家也可以去参考一下