• 异步调起微信支付


    include_once("./module/payment/lib/WxPayPubHelper/WxPayPubHelper.php");
    //使用jsapi接口
    $jsApi = new JsApi_pub();
    //=========步骤1:通过code获得openid============
    $openid = $_SESSION['openid'];
    //=========步骤2:使用统一支付接口,获取prepay_id============
    $unifiedOrder = new UnifiedOrder_pub();

    $unifiedOrder->setParameter("openid", $openid);//商品描述
    $unifiedOrder->setParameter("body", "test");//商品描述
    //自定义订单号,此处仅作举例

    $timeStamp = time();
    $out_trade_no = WxPayConf_pub::APPID . "$timeStamp";
    $unifiedOrder->setParameter("out_trade_no", $out_trade_no);//商户订单号
    $unifiedOrder->setParameter("total_fee", 100);//总金额
    $unifiedOrder->setParameter("notify_url", WxPayConf_pub::NOTIFY_URL);//通知地址
    $unifiedOrder->setParameter("trade_type", "JSAPI");//交易类型
    //非必填参数,商户可根据实际情况选填
    //$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号
    //$unifiedOrder->setParameter("device_info","XXXX");//设备号
    $unifiedOrder->setParameter("attach", $re_wx['id'] . "|" . $order_id);//附加数据

    $prepay_id = $unifiedOrder->getPrepayId();
    //=========步骤3:使用jsapi调起支付============
    $jsApi->setPrepayId($prepay_id);
    $jsApiParameters = $jsApi->getParameters();

    header('Content-Type:application/json; charset=utf-8');
    exit(json_encode($jsApiParameters));


    $.ajax({
    type: "post",
    url: url,
    dataType: "json",
    data: {price:price,order_id:id},
    success: function(msg){
    var data = $.parseJSON(msg);
    $('#wx').attr('data-appid',data.appId);
    $('#wx').attr('data-time',data.timeStamp);
    $('#wx').attr('data-nonce',data.nonceStr);
    $('#wx').attr('data-pack',data.package);
    $('#wx').attr('data-sign',data.paySign);
    callpay();
    }
    });

    <script type="text/javascript">


    //调用微信JS api 支付
    function jsApiCall()
    {

    WeixinJSBridge.invoke('getBrandWCPayRequest',{

    "appId":$('#wx').attr('data-appid'),
    "timeStamp":$('#wx').attr('data-time'),
    "nonceStr":$('#wx').attr('data-nonce'),
    "package":$('#wx').attr('data-pack'),
    "signType":"MD5",
    "paySign":$('#wx').attr('data-sign')
    },function(res){
    // WeixinJSBridge.log(res.err_msg);
    if(res.err_msg == "get_brand_wcpay_request:ok")// 支付成功
    {

    var order_id=$('#order_id').val();
    location.href = "<{$config.web_url}>/pay/?m=fenhong&s=evaluate&order_id="+order_id;

    }
    });
    }
    function callpay()
    {
    //$("#form1").submit();
    if (typeof WeixinJSBridge == "undefined"){
    if( document.addEventListener ){
    document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
    }else if (document.attachEvent){
    document.attachEvent('WeixinJSBridgeReady', jsApiCall);
    document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
    }
    }else{
    jsApiCall();
    }
    }
    </script>




    <div id="wx" data-appid="" data-time="" data-nonce="" data-pack="" data-sign="" style="display:none"></div>
  • 相关阅读:
    MD5 带salt 加密
    生成包含数字和大小写字母的随机码
    多读好代码助于提高
    Winform程序窗体间的跳转
    Sql Server 存储过程
    GDI+的学习
    管理人生的8个危机
    马云语录
    无边框窗体的拖动和拉伸
    安装oracle时遇到 环境变量path的值超过1023字符,无法设置该值
  • 原文地址:https://www.cnblogs.com/godLike7/p/6322315.html
Copyright © 2020-2023  润新知