- 
首先有自己的小程序以及商户平台;
 - 
进入小程序后台,绑定商户平台,获得appid和mch_id,两者需具备绑定关系,以此来使用微信支付提供的开放接口;

根据图操作,进入商户平台:


商户平台中添加小程序appid,绑定成功后,进入小程序后台确定;
 - 
微信官方文档 https://pay.weixin.qq.com/static/pay_setting/appid_protocol.shtml
 - 
代码操作:
在小程序的代码中调用wx.login()来向微信获取到code,拿到了之后把code通过request传给商户服务器,再由服务器来跟微信服务器要session_key和openID
 
小程序中 ,buy.js
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31  | 
						<script>     buyVip: function () {         var a = this;         dataApi.wxPay({             uid: wx.getStorageSync("uid"),//用户id                   id: 15,             type: 2,//类型                   auth_selected_str: a.data.auth_selected_str,//选择的支付套餐标识             }).then(function (t) {             console.log(t);//获取设置的支付参数                  var orderid = t.data.orderid;             wx.requestPayment({                 timeStamp: String(t.data.timeStamp),//时间戳                       nonceStr: t.data.nonceStr,//随机字符串                        package: t.data.package,//统一下单接口返回的 prepay_id 参数值                        signType: t.data.signType,//签名算法                        paySign: t.data.paySign,//签名                        success: function (t) {                     wx.showToast({icon: "success", title: "支付成功"});                     wx.switchTab({//支付成功,跳转                                    url: "../payOk/payOk"                     });                 }, fail: function (t) {                     wx.showToast({icon: "none", title: "支付失败,请重试~"});                 }             });         }).catch(function (t) {             console.log(t);         })     } </script>  | 
					
服务器后端:
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74  | 
						public function dopageWxPay() {     global $_W;     global $_GPC;     $uid               = intval($_GPC['uid']);     $type              = intval($_GPC['type']);     $id                = intval($_GPC['id']);     $auth_selected_str = $_GPC['auth_selected_str'];     //获取支付设置     $setting  = pdo_get('g_setting', ['weid' => $_W['uniacid']], ['pay_open', 'auth']);     $pay_open = $setting ? $setting['pay_open'] : '';     if ($pay_open != 1) {         return $this->result(1, '未开启支付', []);     }    //支付套餐数组     $auth  = $setting ? unserialize($setting['auth']) : '';     $price = 0;//根据标识获取价格     if ($auth) {         foreach ($auth as $k => $v) {             if ($k == $auth_selected_str) {                 $price = $v;             }         }     }     if ($price > 0) {         $openid                 = pdo_getcolumn('g_user', ['weid' => $_W['uniacid'], 'id' => $uid], 'openid');         $order_sn               = getNonceStr(8) . date('his') . getNonceStr(8);//随机订单号         $insert                 = [];         $insert['weid']         = $_W['uniacid'];         $insert['type']         = $type;         $insert['out_trade_no'] = $order_sn;         $insert['userid']       = $uid;         $insert['openid']       = $openid;         $insert['order_status'] = 0;//支付状态         $insert['all_money']    = $price;         $insert['true_money']   = $price;         $insert['createtime']   = TIMESTAMP;         $insert['msg']          = '微信支付';         $insert['dataid']       = $id;         pdo_insert('g_order', $insert);//添加订单记录         $orderid           = pdo_insertid();         $sys               = pdo_fetch('SELECT mchid,pay_secret FROM ' . tablename('g_setting') . ' WHERE weid = :weid ', [':weid' => $_W['uniacid']]);         $nonce_str         = getNonceStr(32);         $fee               = $price * 100;         $spbill_create_ip  = $_SERVER['REMOTE_ADDR'];         $notify            = $_W['siteroot'] . 'addons/go/wxresult.php';         $orderinfo         = ['appid' => $_W['account']['key'], 'mch_id' => $sys['mchid'], 'nonce_str' => $nonce_str, 'body' => '资料购买', 'out_trade_no' => $order_sn, 'total_fee' => $fee, 'spbill_create_ip' => $spbill_create_ip, 'notify_url' => $notify, 'trade_type' => 'JSAPI', 'openid' => $openid];         $sign              = getSign($orderinfo, $sys['pay_secret']);         $orderinfo['sign'] = $sign;         $xml               = arrayToXml($orderinfo);         $url               = 'https://api.mch.weixin.qq.com/pay/unifiedorder';         $res               = api_notice_increment($url, $xml);         libxml_disable_entity_loader(true);         $xmlstring = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);         $returnarr = json_decode(json_encode($xmlstring), true);         $data      = [];         if (($returnarr['return_code'] == 'SUCCESS') && ($returnarr['result_code'] == 'SUCCESS')) {             $prepay_id                = $returnarr['prepay_id'];             $update                   = [];             $update['transaction_sn'] = $prepay_id;             pdo_update('g_order', $update, ['id' => $orderid, 'weid' => $_W['uniacid']]);             $data            = ['appId' => $_W['account']['key'], 'timeStamp' => TIMESTAMP, 'nonceStr' => $nonce_str, 'package' => 'prepay_id=' . $prepay_id, 'signType' => 'MD5'];             $paySign         = getSign($data, $sys['pay_secret']);             $data['paySign'] = $paySign;             $data['orderid'] = $orderid;             $data['price']   = $price;             return $this->result(0, '获取支付参数', $data);         } else {             $data['msg'] = $returnarr['return_msg'];             return $this->result(1, '支付失败,请重试', $data);         }     } else {         return $this->result(1, '不需要购买', []);     } }  |