小程序端 触发调用微信支付接口:
// 微信支付 goPay : function(e){ var that = this; var pay = e.currentTarget.dataset.pay; var uid = that.data.uid; var address = that.data.select_id; wx.request({ url: getApp().globalData.server +'index/index/wxPay', data: { uid: uid, gid: that.data.id, address: address, pay: pay, num: that.data.num, }, method: 'POST', header: { 'content-type': 'application/json' // 默认值 }, success: function (res) { console.log(res.data) if(res.data == 'fail') { console.log('生成订单失败'); return false; }else{ that.setData({ orders: res.data.orders, }) } // 调起支付API wx.requestPayment({ 'timeStamp': res.data.timeStamp, 'nonceStr': res.data.nonceStr, 'package': res.data.package, 'signType': res.data.signType, 'paySign': res.data.paySign, 'success': function (ee) { console.log('suc'); wx.showToast({ title: '支付成功', icon: 'success', duration: 3000 }); // 更改支付状态 wx.request({ url: getApp().globalData.server + 'index/index/notify', data: { order_id: that.data.orders, state: 1, }, header: { 'content-type': 'application/json' // 默认值 }, success: function (res) { console.log(res.data); } }) }, 'fail': function (res) { console.log(res); if (res.errMsg == 'requestPayment:fail cancel'){ wx.showToast({ title: '您已取消支付', icon: 'none', duration: 3000 }); }else{ console.log('res.errMsg'); wx.showToast({ title: '支付失败', icon: 'none', duration: 3000 }); // 更改支付状态 wx.request({ url: getApp().globalData.server +'index/index/notify', data: { order_id: that.data.orders, state: -1, }, header: { 'content-type': 'application/json' // 默认值 }, success: function (res) { console.log(res.data); } }) } }, }); } }) },后端:创建商户订单 统一下单 构建需要参数返回小程序 调起支付
注:其中用到的请求函数 XML转换函数 参见我的另一篇 微信支付xml处理与请求(我这里是小程序支付)
// 微信支付接口 public function wxPay() { $input = input();// $input = json_decode($input,true); $uid = $input['uid']; $gid = $input['gid']; $address = $input['address']; $money = $input['pay']; $num = $input['num'];// 创建我的支付订单 $orderCode = $this-setOrderCode(); $order = Db::table('orders')-insertGetId([ 'order_code' = $orderCode, 'goods_id' = $gid, 'pay_price' = $money, 'number' = $num, 'address_id' = $address ]); if ($order == false) { return 'fail'; }// 准备统一下单参数(你可以调用wx.login获取) $openid = Db::table('users')-where('id',$uid)-value('openid');// halt($openid);// 随机字符串 $str="QWERTYUIPADGHJKLZXCVNM1234567890"; $nonce = str_shuffle($str);// 小程序id - 商户平台密钥 $appid = '你的小程序appid'; $key = "你的商户密钥"; $pay['appid'] = $appid; $pay['body'] = '商品描述'; //商品描述 $pay['mch_id'] = '你的商户号'; //商户号 $pay['nonce_str'] = $str; //随机字符串 $pay['notify_url'] = '你的地址'; //异步接收微信支付结果通知的回调地址 $pay['openid'] = $openid; $pay['out_trade_no'] = $orderCode; //订单号 $pay['spbill_create_ip'] = '你的IP'; // 终端IP $pay['total_fee'] = $money*100; //支付金额 $pay['trade_type'] = 'JSAPI'; //交易类型// 组建签名(不可换行 空格 否则哭吧) $stringA="appid=".$pay['appid']."&body=".$pay['body']."&mch_id=".$pay['mch_id']."&nonce_str=".$pay['nonce_str']."¬ify_url=".$pay['notify_url']."&openid=". $pay['openid']."&out_trade_no=".$pay['out_trade_no']."&spbill_create_ip=". $pay['spbill_create_ip']."&total_fee=".$pay['total_fee']."&trade_type=".$pay['trade_type'];// return $stringA; $stringSignTemp=$stringA."&key=".$key; //注:key为商户平台设置的密钥key(这个还需要再确认一下) $sign= strtoupper(md5($stringSignTemp)); //注:MD5签名方式 $pay['sign'] = $sign; //签名// 统一下单请求 $url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; $data = $this-arrayToXml($pay);// return $data; $res = $this-wxpost($url,$data);// return $res; //返回统一下单结果// 对 统一下单返回得参数进行处理 $pay_arr = $this-xmlToArray($res); //这里是数组 if ($pay_arr['return_code'] == 'FAIL' || $pay_arr['result_code'] == 'FAIL') { return $res; }// 组建调起支付参数// 调起支付数据签名字段// $appid; $timeStamp = time(); $nonce_pay = str_shuffle($str); $package = $pay_arr['prepay_id']; $signType = "MD5"; $stringPay = "appId=".$appid."&nonceStr=".$nonce_pay."&package=prepay_id=".$package."&signType=".$signType."&timeStamp=".$timeStamp."&key=".$key; $paySign = strtoupper(md5($stringPay)); $rpay['timeStamp'] = (string)$timeStamp; $rpay['nonceStr'] = $nonce_pay; $rpay['package'] = "prepay_id=".$package; $rpay['signType'] = $signType; $rpay['paySign'] = $paySign; $rpay['orders'] = $order; //订单id return json_encode($rpay); }这是订单号得函数:
/** * 生成订单号 */ function setOrderCode(){ $sql = Db::table('orders')-where('pay_state',1)-column('order_code'); $ORDERSN = $sql; //静态变量 $ors = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);// $ors=date('Ymd').substr(time(),-5).substr(microtime(),2,5); //生成16位数字基本号 if (isset($ORDERSN[$ors])) { //判断是否有基本订单号 $ORDERSN[$ors]++; //如果存在,将值自增1 }else{ $ORDERSN[$ors]=1; } return $ors.str_pad($ORDERSN[$ors],2,'0',STR_PAD_LEFT); //链接字符串 }













