微信小程序調用微信支付

1,首先咱們先縷清支付的整個流程,詳見https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_4&index=3,第一步在本身的服務器上吊起微信支付,獲取預支付訂單id,第二步根據獲取得APPID、隨機數、預支付訂單prepay_id、簽名類型、時間生成簽名,第三部將生成的簽名和第一步獲取獲得信息一同返回給小程序,第四步小程序接收值發起支付php

2.話很少說直接看代碼html

第一:吊起微信支付該方法返回的值有你須要獲得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; }

第三:微信小程序下單,去後臺獲取, 發起支付api

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

 

 

但願對你有所幫助!服務器

相關文章
相關標籤/搜索