微擎小程序支付功能

1,要使用微擎特定的小程序框架

https://gitee.com/we7coreteam/wxapp

微擎小程序支付功能

2,必须正确的配置siteinfo

https://www.kancloud.cn/qq188872170/xcx/673488

var siteinfo = {
“m”: ‘yhjd_fhjs‘,
“uniacid”: “2”,
“acid”: “2”,
“multiid”: “0”,
“version”: “1.01”,
“siteroot”: “https://02.zcwlkj.cn/app/index.php”,
‘method_design‘: ‘3‘
};
module.exports = siteinfo;
 
3,发起支付的页面,必须有m参数
appInfo.util.request({
‘url‘: ‘entry/wxapp/pay‘, //调用wxapp.php中的doPagePay方法获取支付参数
data: {
orderid: params.ordertid,
fee: params.fee,
openid: appInfo.globalData.openid,
m: appInfo.siteInfo.m
},
‘cachetime‘: ‘0‘,
success(res) {
console.log(res);
}
});
4,PHP端生成支付参数——————wxapp.php文件就是专门用来写这个小程序支付逻辑的

class We7WxappDemoModuleWxapp extends WeModuleWxapp {

    public function doPagePay() {

        global $_GPC, $_W;

        //获取订单号,保证在业务模块中唯一即可

        $orderid = intval($_GPC[‘orderid‘]);

        //构造支付参数

        $order = array(

            ‘tid‘ => $orderid,

            ‘user‘ => $_W[‘openid‘], //用户OPENID

            ‘fee‘ => floatval($fee), //金额

            ‘title‘ => ‘小程序支付示例‘,

        );

        //生成支付参数,返回给小程序端

        $pay_params = $this->pay($order);

        if (is_error($pay_params)) {

            return $this->result(1, ‘支付失败,请重试‘);

        }

        return $this->result(0, ‘‘, $pay_params);

    }

}

小程序端发起支付

app.util.request({

    ‘url‘: ‘entry/wxapp/pay‘, //调用wxapp.php中的doPagePay方法获取支付参数

    data: {

        orderid: options.orderid,

    },

    ‘cachetime‘: ‘0‘,

    success(res) {

        if (res.data && res.data.data && !res.data.errno) {

            //发起支付

            wx.requestPayment({

                ‘timeStamp‘: res.data.data.timeStamp,

                ‘nonceStr‘: res.data.data.nonceStr,

                ‘package‘: res.data.data.package,

                ‘signType‘: ‘MD5‘,

                ‘paySign‘: res.data.data.paySign,

                ‘success‘: function (res) {

                    //执行支付成功提示

                },

                ‘fail‘: function (res) {

                    backApp()

                }

            })

        }

    },

    fail(res) {

        wx.showModal({

            title: ‘系统提示‘,

            content: res.data.message ? res.data.message : ‘错误‘,

            showCancel: false,

            success: function (res) {

                if (res.confirm) {

                    backApp()

                }

            }

        })

    }

})

验证支付结果

和模块一样,验证代码写在 payResult() 函数中即可。