[微信小程序支付]
#登陆API https:
#可参考微信小程序登录逻辑整理 http:
#获取登录凭证(code) @注意code 必须确认在小程序开发者绑定之后才能获取
app.js
App({
onLaunch: function () {
var that = this;
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
var user = wx.getStorageSync('user') || {};
var userInfo = wx.getStorageSync('userInfo') || {};
if ((!user.openid || (user.expires_in || Date.now()) < (Date.now() + 600)) && (!userInfo.nickName)) {
wx.login({
success: function (res) {
#获取code
var d = that.globalData;
var url_ = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
wx.request({
url: url_,
data: { code: res.code },
method: 'GET',
success: function (sessionKey) {
if (sessionKey!='') {
var obj = {};
obj.openid = sessionKey.data.openid;
obj.expires_in = Date.now() + sessionKey.data.expires_in;
wx.setStorageSync('user', obj);
console.log(obj);
}else{
console.log('请求服务器失败');
}
console.log(sessionKey);
}
});
}
})
}else {
console.log('获取用户登录态失败!' + res.errMsg)
}
},
getUserInfo:function(cb){
var that = this
if(this.globalData.userInfo){
typeof cb == "function" && cb(this.globalData.userInfo)
}
},
#设置全局变量
globalData:{
appid: 'b01ddedddddddddd1ee1b',
secret: '0cc594acdfdsafsaddfdb56c5',
userInfo:null,
nav:{"channelList":[
{"id":"1","name":"推荐","orderId":"1"},
{"id":"2","name":"热点","orderId":"2"},
}
})
var app = getApp()
Page({
formSubmit: function (e) {
var obj = wx.getStorageSync('user');
console.log(obj.openid);
var url_ = "https://xxxxxxxxxxx/index.php?controller=WxAppletPay&action=weChatPay";
wx.request({
url: url_,
data: { goods_name: e.detail.value.goods_name, order_sn: e.detail.value.order_sn, order_amount: e.detail.value.order_amount, openid: obj.openid },
method: 'GET',
success: function (res) {
console.log(res);
if (res.data.error == 200){
wx.requestPayment({
'timeStamp': res.data.result.timeStamp,
'nonceStr': res.data.result.nonceStr,
'package': res.data.result.package,
'signType': 'MD5',
'paySign': res.data.result.sign,
'success': function (success_) {
console.log(success_);
},
'fail': function (fail_) {
console.log(fail_)
},
'complete': function (complete_) {
console.log(complete_)
}
})
}
}
});
},
formReset: function (e) {
console.log('form发生了reset事件,携带数据为:', e.detail.value)
this.setData({
chosen: ''
})
}
})
require_once PROJECT_PATH."/WxApplet/wxPay/lib/WxPay.Config.php";
require_once PROJECT_PATH."/WxApplet/wxPay/lib/WxPay.Data.php";
require_once PROJECT_PATH."/WxApplet/wxPay/lib/WxPay.Api.php";
error_reporting(E_ALL);
ini_set('display_errors',true);
class WxAppletController extends Controller
{
public function __construct($params)
{
parent::__construct($params);
}
public function weChatPay()
{
$goods_name = empty($this->goods_name) ? NULL : $this->goods_name;
$order_sn = empty($this->order_sn) ? NULL : $this->order_sn;
$order_amount = empty($this->order_amount) ? NULL : $this->order_amount;
$openid = empty($this->openid) ? NULL : $this->openid;
if(!empty($goods_name) && !empty($order_sn) && !empty($order_amount)){
$input = new WxPayUnifiedOrder();
$input->SetAppid(WxPayConfig::APPID);
$input->SetMch_id(WxPayConfig::MCHID);
$input->SetNonce_str(WxPayApi::getNonceStr());
$input->SetBody($goods_name);
$input->SetOut_trade_no($order_sn);
$input->SetTotal_fee($order_amount);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetFee_type("CNY");
$input->SetSpbill_create_ip(get_client_ip());
$input->SetNotify_url(WxPayConfig::NOTIFY_URL);
$input->SetTrade_type('JSAPI');
$input->SetOpenid($openid);
$results = WxPayApi::unifiedOrder($input);
if (!empty($results['prepay_id'])) {
$new_results= $this->sign($results['prepay_id']);
if (!empty($new_results)) {
$new_results['callback'] = WxPayConfig::CALLBACK;
exit(json_response(0, "200200", "", "请求成功123", $new_results));
}
}
}
}
public function sign($prepay_id = '')
{
$result = [];
if (!empty($prepay_id)) {
$input = new WxApplet();
$input->SetAppid(WxPayConfig::APPID);
$input->SetNonce_str(WxPayApi::getNonceStr());
$input->SetPrepayid('prepay_id='.$prepay_id);
$input->SetTimeStamp(time());
$input->SetSign();
$xml = $input->ToXml();
$result = $input->FromXml($xml);
return $result;
}
return $result;
}
}