小程序支付

小程序调用接口生成预付订单,接收prepay_id

    unified(){
        wx.request({url: "http://localhost/personalities/server/?head=unified",
            method: GET,
            data:{
                body : this.data.body,
                out_trade_no : this.random(),
                total_fee : this.data.total_fee,
                openid : this.data.openid,
                report_id : this.data.report_id
            },
            success: res => {

                if(res.data.err_code_des && res.data.err_code_des==="该订单已支付") return console.log(res.data);

                this.setData({
                    prepay_id : res.data.prepay_id
                });

                this.again_sign();
            }
        })
    },

php

/**
 * 调用统一下单接口,获取prepay_id
 */
function unified()
{
    $app = Factory::payment(WX_PAY);

    $result = $app->order->unify([
        ‘body‘ => $_REQUEST[‘body‘], //充值说明
        ‘out_trade_no‘ => $_REQUEST[‘out_trade_no‘], //订单号
        ‘total_fee‘ => $_REQUEST[‘total_fee‘], //金额
        ‘trade_type‘ => ‘JSAPI‘, //支付类型
        ‘openid‘ => $_REQUEST[‘openid‘], //openid
    ]);

    $user = json_decode(query("SELECT id FROM user WHERE openid=‘{$_REQUEST[‘openid‘]}‘","json"));
    $user_id = $user -> id;
    $total_fee = $_REQUEST[‘total_fee‘] / 100;

    //保存订单号
    query("INSERT INTO `order` (`user_id`, `out_trade_no`, `total_fee`, `report_id`) VALUES ({$user_id}, {$_REQUEST[‘out_trade_no‘]}, {$total_fee}, {$_REQUEST[‘report_id‘]})","json");

    return json_encode($result, JSON_UNESCAPED_UNICODE);
}

 

小程序

    /**
     * 根据预付单号,获取签名信息
     */
    again_sign(){
        wx.request({
            url: "http://localhost/personalities/server/?head=pay",
            method: ‘GET‘,
            data:{
                prepay_id : this.data.prepay_id
            },
            success: res => {

                this.setData({
                    appId: res.data.appId,
                    nonceStr: res.data.nonceStr,
                    package: res.data.package,
                    paySign: res.data.paySign,
                    signType: res.data.signType,
                    timeStamp: res.data.timeStamp
                });

                this.start_pay();
            }
        })
    },

 

php

// 返回支付签名信息
function pay()
{
    $payment = Factory::payment(WX_PAY);

    $jssdk = $payment -> jssdk;

    return $jssdk->bridgeConfig($_REQUEST[‘prepay_id‘], true);
}

 

小程序根据获取的签名信息,发起支付

    start_pay() {

        wx.requestPayment({

            timeStamp: this.data.timeStamp, //注意 timeStamp 的格式
            nonceStr: this.data.nonceStr,
            package: this.data.package,
            signType: this.data.signType,
            paySign: this.data.paySign, // 支付签名

            success: function (res) {
                // 支付成功后的回调函数
                console.log(‘支付成功了‘, res);

                wx.showToast({
                    title: ‘支付成功‘,
                    icon: ‘success‘,
                })

                setTimeout(() => {
                    wx.navigateBack()
                },1300)
            },

            fail: res => {

                wx.showModal({
                    title: ‘警告‘,
                    content: ‘您拒绝了付款,将无法解锁详细报告‘,
                    showCancel: false,
                    confirmText: ‘确认‘,

                    success: res => {
                        setTimeout(() => {
                            wx.navigateBack()
                        },1300)
                    }

                });
            }
        });
    },

 

php处理支付完成回调......

小程序支付

上一篇:互联网公司如何使用数十亿人的个人数据?


下一篇:禾匠微信小程序商城验证码图片不存在bug