1,首先我们先缕清支付的整个流程,详见https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_4&index=3,第一步在自己的服务器上吊起微信支付,获取预支付订单id,第二步根据获取得APPID、随机数、预支付订单prepay_id、签名类型、时间生成签名,第三部将生成的签名和第一步获取得到信息一同返回给小程序,第四步小程序接收值发起支付
2.话不多说直接看代码
第一:吊起微信支付该方法返回的值有你需要得到prepay_id、随机字符串、签名类型、时间。还有一个签名切记这个签名不是我们给小程序的
protected function weixin_pay($order) { Loader::import("weixinpay.lib.WxPay", EXTEND_PATH, ".Api.php"); Loader::import("weixinpay.example.log", EXTEND_PATH, ".php"); Loader::import("weixinpay.example.WxPay", EXTEND_PATH, ".Config.php"); $notify=new NativePay(); $logHandler= new \CLogFileHandler(EXTEND_PATH."weixinpay/logs/".date('Y-m-d').'.log'); $log = \Log::Init($logHandler, 15); $input = new \WxPayUnifiedOrder(); $input->SetBody("xiaoasdhjasd"); $input->SetAttach("asdasd"); $input->SetOut_trade_no($order["order_number"]); // $input->SetTotal_fee($order["money"]*100); $input->SetTotal_fee("1"); $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis", time() + 600)); $input->SetGoods_tag("test"); $input->SetNotify_url("https://test.gdzhisheng.top/index.php/index/index/notify.html"); $input->SetTrade_type("JSAPI"); $input->SetProduct_id("407038"); $openid=$this->openid; $input->SetOpenid($openid); $api=new \WxPayApi(); $config=new \WxPayConfig(); $order = $api->unifiedOrder($config,$input); return $order; }
第二:将上一个方法得到的参数进行签名
private function genPaySign($unifiedOrder, $time) {
//$unifiedOrder是上一个方法返回的参数$time是一个时间戳
$appId = "你的appid";
$key="你的商户密钥"; $nonceStr = $unifiedOrder['nonce_str']; $package = 'prepay_id='.$unifiedOrder['prepay_id']; $signType = "MD5"; $timeStamp = $time; $sign = MD5("appId={$appId}&nonceStr={$nonceStr}&package={$package}&signType={$signType}&timeStamp={$timeStamp}&key={$key}"); return $sign; }
第三:微信小程序下单,去后台获取, 发起支付
xiadan: function (options) {var that = this; var http = getApp(); var url = http.globalData.url var openid = http.globalData.openid wx.request({ url: url + '/index.php/index/index/buy_oneorder', method: 'POST', data: { openid: openid, order_id: options.order_id }, success: function (res) { that.setData({ order_id: options.order_id, timeStamp: res.data.time, nonceStr: res.data.pay.nonce_str, package: res.data.pay.prepay_id, paySign: res.data.sign, }) } }) },
buybuy:function(){
var that=this;
var http = getApp();
var openid = http.globalData.openid;
var url = http.globalData.url;
wx.requestPayment({
timeStamp: that.data.timeStamp,
nonceStr: that.data.nonceStr,
package: 'prepay_id=' + that.data.package,
signType: 'MD5',
paySign: that.data.paySign,
success: function (res) {
console.log('支付成功');
},
complete: function (res) {
console.log(res);
}
})
}
希望对你有所帮助!