飞狐爷

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

小程序端:

<button bindtap='checkout' style='margin-top:20px'>微信支付</button>
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", 'xxx');
define("SECRET", 'xxx');

$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.xxx.com/xxx.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><![CDATA[dfdf]]></appid>
<attach><![CDATA[fdf]]></attach>
<bank_type><![CDATA[CFT]]></bank_type>
<cash_fee><![CDATA[1]]></cash_fee>
<fee_type><![CDATA[CNY]]></fee_type>
<is_subscribe><![CDATA[N]]></is_subscribe>
<mch_id><![CDATA[]]></mch_id>
<nonce_str><![CDATA[dfdfdfdf]]></nonce_str>
<openid><![CDATA[o6tz-dfdfdf]]></openid>
<out_trade_no><![CDATA[ddfffdfdf]]></out_trade_no>
<result_code><![CDATA[SUCCESS]]></result_code>
<return_code><![CDATA[SUCCESS]]></return_code>
<sign><![CDATA[dfdfdf]]></sign>
<time_end><![CDATA[dfdfdfdf]]></time_end>
<total_fee>1</total_fee>
<trade_type><![CDATA[JSAPI]]></trade_type>
<transaction_id><![CDATA[dfdfdfd]]></transaction_id>
</xml> 

 

posted on 2018-03-01 11:52  飞狐爷  阅读(1774)  评论(0编辑  收藏  举报