PHP微信三方平台-微信支付(扫码支付)

1、官方文档地址:

https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_1

2、逻辑分析:

  生成支付二维码-》用户扫码支付-》成功后支付回调完成业务功能

3、准备工作

一个生成图片二维码的文件(phpqrcode)

 

二、代码


require_once '../librarys/wx/wx_pay/lib/WxPay.Api.php';
require_once '../librarys/wx/wx_pay/lib/WxPay.Config.php';
require_once '../librarys/wx/wx_pay/lib/WxPay.NativePay.php';
 1  /**
 2      * 微信支付,扫码支付
 3      * @return string
 4      * @throws \yii\base\Exception
 5      */
 6     public function actionWxPayNative()
 7     {
 8         $body = $this->_getRequestValue('body');//主题
 9         $attach = $this->_getRequestValue('attach');//备注信息
10         $trade_no = $this->_getRequestValue('trade_no', Yii::$app->security->generateRandomString(8));//订单号
11         $total_fee = $this->_getRequestValue('total_fee');//支付金额
12         $notify_url = $this->_getRequestValue('notify_url');//支付成功后的回调地址
13         $app_id = $this->_getRequestValue('app_id');//公众号的app_id
14         $mch_id = $this->_getRequestValue('mch_id');//商户id
15         $mch_key = $this->_getRequestValue('mch_key');//商户key
16 
17         $pay_config = ['APPID' => $app_id, 'MCHID' => $mch_id, 'MCHKEY' => $mch_key];
18 
19         $attach = json_encode($attach);
20         $notify = new \NativePay();
21         $input = new \WxPayUnifiedOrder();
22         $input->SetBody($body);
23         $input->SetAttach($attach);
24         $input->SetOut_trade_no($trade_no);
25         $input->SetTotal_fee($total_fee);
26         $input->SetTime_start(date("YmdHis"));
27         $input->SetTime_expire(date("YmdHis", time() + 600));
28 //        $input->SetGoods_tag("test");
29         $input->SetNotify_url($notify_url);
30         $input->SetTrade_type("NATIVE");
31         $input->SetProduct_id($trade_no);
32 
33 
34         $result = $notify->GetPayUrl($input, $pay_config);
35 
36         if (isset($result['result_code']) == 'SUCCESS' && $result['return_code'] == 'SUCCESS') {
37             $data = ['app_id' => $result['appid'], 'mch_id' => $result['mch_id'], 'code_url' => $result['code_url'], 'trade_type' => $result['trade_type']];
38             return $this->renderJson($data);
39         }
40 
41         $data = ['return_msg' => $result['return_msg']];
42 
43         $log_id = Yii::$app->session->get(self::RequestBaseApiLogId);45 
46 
47         return $this->renderJson($data, 1, '生成扫码支付失败');
48     }

页面

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-01-10
 * Time: 17:52
 */
?>

<div class="message-cz">
    <div class="top">支付金额 <b class="font-red">¥0.01</b></div>
    <div class="zxzf">
        <div class="ewm">
            <img alt="扫码支付" src="<?php echo \yii\helpers\Url::to(['common/qr-code', 'qr_code' => $code_url]); ?>"
                 style="width:220px;height:220px;"/>
        </div>
    </div>
    <p>在微信右上角点击"扫一扫"</p>
    <a href="javascript:;" class="btn_close"></a>
</div>


<script type="text/javascript">
    $('.btn_close').click(function () {
        $('.inside-alert-wrapper').hide();
        clearInterval(ref);
    });

    var data = {};
    data.record_id = "<?= $record_info['id'] ?>";

    var count = 0;
    ref = setInterval(function () {
        count++;
        if(count >= 300) {
            clearInterval(ref);
        }

        requestJson('check-recharge-status', data, false, function (responseJson) {
            if (responseJson.data == '1') {
                infoDialog('支付成功');
                clearInterval(ref);
                $('.inside-alert-wrapper').hide();
                window.location.reload();
            }
        })
    }, 2000);
</script>

 

posted @ 2020-05-19 15:22  鲜花满月楼  阅读(960)  评论(0编辑  收藏  举报