小程序交流群:528215729,加入和小伙伴们一起探讨开发疑难、运营idea吧

admin

admin

发表于 2018-05-13 14:11:03

摘要:小程序调起支付 (微信小程序开发 - 微信 小程序外包开发-微信小程序 开发教程-微信小程序开发者社区,尽在 www.mntuku.cn)

小程序端:


checkout: function () {
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: app.globalData.requestUrl_pay,
            data: {
              code: res.code
            },
            header: {
              Content-Type: application/x-www-form-urlencoded
            },
            method: "POST",
            success: function (res) {
              //console.log(res)
              that.sendpay(res.data)
            }
          })
        } else {
          console.log(获取用户登录态失败! + res.errMsg)
        }
      }
    });
  },
  sendpay: function (res) {
    console.log(res)
    wx.requestPayment(
      {
        timeStamp: res.timeStamp,
        nonceStr: res.nonceStr,
        package: res.package,
        signType: MD5,
        paySign: res.paySign,
        success: function (res) { console.log(success)},
        fail: function (res) { console.log(fail)},
        complete: function (res) { console.log(complete)}
      }) 
  },
ini_set(date.timezone,Asia/Shanghai);
require_once "pay/WxPay.Api.php";
require_once "pay/WxPay.JsApiPay.php";

define("APPID", wxaabb732e9df8ea54);
define("SECRET", e89c0e4f7bcd45a178202700c6a47d0b);

$code = $_REQUEST[code];
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=".APPID."&secret=".SECRET."&js_code=".$code."&grant_type=authorization_code";
$res = curlget($url);
$res = json_decode($res,true);
    
$input = new WxPayUnifiedOrder();
$input->SetBody("test");
$input->SetAttach("test");
$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
$input->SetTotal_fee("1");
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("test");
$input->SetNotify_url("http://www.tongrentangsxls.com/wxxcx/pay/notify.php");
$input->SetTrade_type("JSAPI");
$input->SetOpenid($res[openid]);
$order = WxPayApi::unifiedOrder($input);
$timeStamp = (string)time();
$sign[appId] = $order[appid];
$sign[timeStamp] = $timeStamp;
$sign[nonceStr] = $order[nonce_str];
$sign[package] = prepay_id=.$order[prepay_id];
$sign[signType] = MD5;

ksort($sign);
$buff = "";
foreach ($sign as $k => $v)
{
    if($k != "sign" && $v != "" && !is_array($v)){
        $buff .= $k . "=" . $v . "&";
    }
}
$buff = trim($buff, "&");
$string = $buff . "&key=".WxPayConfig::KEY;
$string = md5($string);
$paySign = strtoupper($string);

$res = array(
    timeStamp    =>$timeStamp,
    nonceStr    =>$order[nonce_str],
    package    =>prepay_id=.$order[prepay_id],
    signType    =>MD5,
    paySign    =>$paySign
);
echo json_encode($res);

function curlget($url=){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    $r = curl_exec($ch);
    curl_close($ch);
    return $r;
}

 


 

返回数据:

<xml>
<appid>wxaabb732e9df8ea54]]>appid>
<attach>test]]>attach>
<bank_type>CFT]]>bank_type>
<cash_fee>1]]>cash_fee>
<fee_type>CNY]]>fee_type>
<is_subscribe>N]]>is_subscribe>
<mch_id>1463088702]]>mch_id>
<nonce_str>9h8rddqy7fcaiedycgddwd41a6412nf9]]>nonce_str>
<openid>o6tz-0NjjVB0IClMnmuiqZEL7ynI]]>openid>
<out_trade_no>146308870220180223144717]]>out_trade_no>
<result_code>SUCCESS]]>result_code>
<return_code>SUCCESS]]>return_code>
<sign>1EA58B42E987099A883C2BE1AE2E4141]]>sign>
<time_end>20180223144721]]>time_end>
<total_fee>1total_fee>
<trade_type>JSAPI]]>trade_type>
<transaction_id>4200000079201802236963042982]]>transaction_id>
xml> 

 

104阅读 | 0评论
你的回应