作者:yuhemecy_883 | 来源:互联网 | 2024-11-27 11:48
微信小程序支付官方参数小程序中代码后端发起支付代码支付回调官方参数文档地址:https:developers.weixin.qq.comminiprogramdeva
官方参数
文档地址:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/payment/wx.requestPayment.html
小程序中代码
如果说你的小程序中有其他登陆方式,比如手机登陆,那么支付的时候就要先去获取用户的openid
get_user_openid:function(){let that = this;let openid = app.globalData.openid;if (openid){run_pay(that);}else{ app.get_user_openid(function (data) {run_pay(that);})}
},
下面看看发起支付
function run_pay(that){let bookReadData = wx.getStorageSync('bookReadData');let channel = app.globalData.QRXS_CHANNEL;let bid = bookReadData.bid;app.httpRequest(config.payOrderBe, {}, function (data) {let product = '开通畅读卡';var postdata = {product: product,detail: product,fee: that.data.pay_price * 100,reward: that.data.reward,bid: bid,channel: channel,timestamp: data.timestamp,nonceStr: data.nonceStr,signature: data.signature,}postdata['openid'] = app.globalData.openidpostdata['sign'] = md5.md5(postdata.product + postdata.detail + postdata.fee + postdata.timestamp + postdata.reward + config.APP_KEY);app.httpRequest(config.payOrder, postdata, function (data) {var res_data = data;var nonceStr = res_data.nonceStr;var outtradeno = res_data.OutTradeNo;var wxpackage = res_data.package;var paysign = res_data.paySign;var timestamp = res_data.timestamp;wx.requestPayment({'timeStamp': timestamp,'nonceStr': nonceStr,'package': wxpackage,'signType': 'MD5','paySign': paysign,'success': function (res) {console.log(res)if (res.errMsg === "requestPayment:ok") {if (that.data.show_back === 1) {app.getUserData(); wx.redirectTo({url: '/pages/buyCard/buyStatus/buyStatus?type=1',})} else {wx.switchTab({url: '/pages/my/my'})}}},'fail': function (res) {console.log(res)if (res.errMsg == "requestPayment:fail cancel") {wx.redirectTo({url: '/pages/buyCard/buyStatus/buyStatus?type=0',})}},'complete': function (res) {console.info('complete');}});});})
}
后端发起支付代码
发起支付第一步,获取支付所需信息,
跟网页端支付差不多
function wx_begin(){include_once "Application/Xcx4/ORG/WxPaySync/jssdk.php";$jssdk = new \JSSDK('wxabxxxxxb66bd', '647474836xxxxxxxxxxxxxxxx9221d724');$signPackage = $jssdk->GetSignPackage();return_json_data(1,'ok',$signPackage);}
发起支付
public function wx_pay(){$uid = $this->uid;if (!$uid){return_json_data(0,'请先登录');}$userInfo = $this->userInfo;$body = I('product','trim','购买书币'); $detail = I('detail','trim','购买书币'); $total_fee = I('fee','intval',0); $sign = I('sign','trim,addslashes',''); $month = I('month','trim,addslashes',''); $bid = I('bid','intval',0); $openid = I('openid','trim,addslashes',''); $timestamp = $_REQUEST['timestamp']; $nonceStr = trim(strip_tags($_REQUEST['nonceStr']));$out_trade_no = 'xcx'.$this->uid.date("mdHis").rand(2000,8000);if(!$out_trade_no ){return_json_data(0,'充值失败'.$insertId);}$notify_url = $this->notify_host.'/Xcx4/notify/wx'; include_once "Application/Xcx4/ORG/WxPaySync/WxPayPubHelper.php";$jsApi = new \JsApi_pub();$unifiedOrder = new \UnifiedOrder_pub();$unifiedOrder->setParameter("openid",$openid);$unifiedOrder->setParameter("body","$body");$unifiedOrder->setParameter("out_trade_no","$out_trade_no");COOKIE('pcode_trade_no',$out_trade_no,60*10);$unifiedOrder->setParameter("total_fee","$total_fee");$unifiedOrder->setParameter("notify_url",$notify_url);$unifiedOrder->setParameter("trade_type","JSAPI");$prepay_id = $unifiedOrder->getPrepayId();$jsApi->setPrepayId($prepay_id);$jsApiParameters = $jsApi->getNewParameters($timestamp,$nonceStr);$data = array("timestamp"=>($jsApiParameters["timestamp"]),"nonceStr"=>$jsApiParameters['nonceStr'],"package"=>$jsApiParameters['package'],"signType"=>$jsApiParameters['signType'],"paySign"=>$jsApiParameters['paySign'],'OutTradeNo'=>$out_trade_no);return_json_data(1,'ok',$data);
}
支付回调
public function wx(){include_once "Application/Xcx4/ORG/WxPaySync/WxPayPubHelper.php";$notify = new \Notify_pub();$xml = $GLOBALS['HTTP_RAW_POST_DATA'];$notify->saveData($xml);if($notify->checkSign() == FALSE){$notify->setReturnParameter("return_code","FAIL");$notify->setReturnParameter("return_msg","签名失败");}else{$notify->setReturnParameter("return_code","SUCCESS");}if($notify->checkSign() == true){$out_trade_no = $notify->data["out_trade_no"];$trade_no = $notify->data["transaction_id"];$result_code = $notify->data["result_code"];$bank_type = $notify->data["bank_type"];$is_subscribe = $notify->data["is_subscribe"];$openid = $notify->data["openid"];echo '';}else{echo '';}
}