1

我正在用 PHP 开发中国电子商务网站。并且我已经完成了微信支付url的二维码生成。它在PC上正常工作。

现在如果是平板电脑或移动网站版本,我如何从网页打开微信应用程序并将二维码数据发送到微信应用程序。

4

3 回答 3

4

Each time you're generating the URL you put inside the QRCode you're creating a Wechat Order calling the unifiedorder Url provided by WechatPay, right? Using trade_type = NATIVE.

What you need is make the same call to that URL but with trade_type = JSAPI, also adding the parameter openid = wechatUserOpenId.

Then, using the returned data $orderResult you need to generate an string in json format, with the following data, like in this code:

    $timeStamp = time();
    $jsOrder['appid']     = $orderResult['appid'];
    $jsOrder['timeStamp'] = "$timeStamp";
    $jsOrder['nonce_str'] = $this->randomGenerator->getRandomString(32);
    $jsOrder['package']   = "prepay_id=" . $orderResult['prepay_id'];
    $jsOrder['signType']  = "MD5";
    $jsOrder['paySign']   = $this->makeSignature($jsOrder);

    $parameters = json_encode($jsOrder);

Once you have that string, inside the page you're showing in the Wechat WebBrowser you need to make an ajax call to get it and use it in a code like this:

Execute callpay() in an onClick event:

function callpay()
{
    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();
    }
}


function jsApiCall()
{
       var parameters = result[2];//fill it with the previous $parameters, with your preferred ajax call
       WeixinJSBridge.invoke(
       'getBrandWCPayRequest',
       parameters,
       function(res){
           console.log(res);
           switch(res.err_msg)
           {
               case "ok":
                     console.log('payment made'); 
                     paymentWasMade = true;                         
                     break;
               case "cancel":
                     break;
               case "fail":
                     break;
            }
     });
}

The parameters is the formatted result of the unifiedorder WechatPay API call. The function(res) is called after the user get out the WechatPay Gateway with those possibles results.

I hope this is helpful for you.

UPDATE:

I realized you don't need the file jweixin-1.0.0.js, the Wechat Web Browser it's gonna recognize the JS call.

Another comment, only Wechat version 5.0 and greater supports the payment feature, so users with versions prior to 5.0 can't access Wechat Payment. But you can check the version in the user agent, it should say something like this: Mozilla/5.0(iphone;CPU iphone OS 5_1_1 like Mac OS X) AppleWebKit/534.46(KHTML,like Gecko) Mobile/9B206 MicroMessenger/5.0

于 2016-04-19T07:12:52.020 回答
0

微信支付有4种交易类型:

  1. JSAPI - 用于微信App内嵌浏览器
  2. NATIVE - 生成二维码,用户必须使用微信应用嵌入式浏览器扫描二维码
  3. MWEB - 注意:用于手机或平板系统浏览器,但会调用微信App完成支付流程。
  4. APP——用在APP里,不是你的case

所以,我认为 MWEB 是您所需要的并且最适合您的场景。

但是,如果您的平板电脑或手机没有安装微信APP,则必须恢复为NATIVE(二维码)方式,因此用户必须取出安装了微信APP的手机扫描二维码并完成付款。

于 2018-01-14T14:15:57.010 回答
0

这是微信的一大痛点。如果用户已经在微信浏览器中,您可以指示他们长按二​​维码,他们会弹出一个选项来扫描图像中的二维码。

如果他们在另一个移动浏览器中,您几乎只能告诉他们截屏然后从微信中扫描图像。

于 2016-04-09T00:24:22.923 回答