iOS开发常用知识点

ios 支付宝h5页面支付,支付完成后回调实现记录

2018-12-10  本文已影响0人  第二沦陷区

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为:

<form id='alipaysubmit' name='alipaysubmit' action='https://openapi.alipay.com/gateway.do?charset=UTF-8' method='post'><input type='hidden'  name='app_id' value='app_id值'/><input type='hidden'  name='biz_content' value='{\"body\":\"\",\"out_trade_no\":\"ts_20181210105054227474\",\"product_code\":\"QUICK_WAP_WAY\",\"quit_url\":\"\",\"subject\":\"test alipay\",\"total_amount\":\"0.01\"}'/><input type='hidden'  name='charset' value='UTF-8'/><input type='hidden'  name='format' value='json'/><input type='hidden'  name='method' value='alipay.trade.wap.pay'/><input type='hidden'  name='notify_url' value='http://域名/wap/Notify_url.aspx'/><input type='hidden'  name='return_url' value='http://域名/wap/Return_url.aspx'/><input type='hidden'  name='sign_type' value='RSA'/><input type='hidden'  name='timestamp' value='2018-12-10 10:50:54'/><input type='hidden'  name='version' value='1.0'/><input type='hidden'  name='sign' value='PT9+xaDcwa8/omrvweIWexW+kdwVya7As1cnAfYgRhwkoPKB0H/pFNm6HdIiTLaQFjoiRONB7yYc8rKeY04+yCtqkanrO0WZO6/QVZzpWtHVttykRNtdBdXTAmQALXBepf2EbKgIY02/M1szr7UrUTc0QVrfkZ+7ODAWFGT7QW17zu5lV5JOZCZtcfTRk5CfVzDJMTsQlDItarWJkzpLFsncGW44wkn8+UmF/DHvRrgI87ILmQyhXcocrpV2Sf0vHzyQqcnHh64r3rIdzqVUGqzATfTzuN+Dg64UGN0bQDFZz5Arj+xMqak62b+PZN/ciPdf+Cw1HZkKcMNF/ahKIA=='/><input type='submit' value='post' style='display:none;'></form><script>document.forms['alipaysubmit'].submit();</script>

将上述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


image.png

注意这个地方设置的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
大家也可以去参考一下

上一篇下一篇

猜你喜欢

热点阅读