1. 程式人生 > >微信小程式呼叫微信支付

微信小程式呼叫微信支付

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); } }) }

 

 

希望對你有所幫助!