专门写了一个demo,就叫升级会员吧,下面是小程序端代码,.xml文件如下:
.js文件代码如下:
//会员升级
setVipUser: function () {
console.log(wx.getStorageSync('token'));
console.log('选择升级会员,进行支付了哦');
var total_fee = 0.01;
var token = wx.getStorageSync('token');
wx.request({
url: 'https://***********/Order/doOrder', //你自己的接口地址
data: {
total_fee: total_fee//支付金额
},
method: 'POST',
header: {
'content-type': 'application/json' // 默认值
},
success(res) {
if (res.data.status == 1) {
console.log(res);
var data = res.data.data;
wx.requestPayment({
"timeStamp": data.timeStamp,
"nonceStr": data.nonceStr,
"package": data.package,
"signType": "MD5",
"paySign": data.paySign,
"success": function (res) {
//支付成功之后可以进行自己需要的操作。。。。
console.log('成功');
console.log(res);
},
"fail": function (res) {
console.log(res);
},
"complete": function (res) {
console.log('完成');
console.log(res);
}
});
}
}
})
},
use think\Db; class Order extends Base{ protected $appid = "";//小程序ID protected $mchid = '';//商户号id protected $key = '';//密钥 protected $appsecret = ''; /** * 小程序调起微信支付 */ public function doOrder(){ $total_fee = input('total_fee'); //支付金额 if(empty($total_fee)){ //一定要有支付金额 ajaxReturn(V(0, '参数错误!'));//该处为我自己封装,需要改成自己的 } $last_total_fee = $total_fee * 100; //支付金额单位是分的,所以要乘100,记住哦 $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";//固定的,不需要调整 $data['appid'] = $this->appid; //小程序appid $data['mch_id'] = $this->mchid; //商户号id $data['nonce_str'] = md5($this->mchid); //验证的支付 $data['openid'] = $openid; //用户openid $data['body'] = '这个地方自己设置哦'; //微信支付对应的商家/公司主体名 $out_trade_no = date("YmdHis").rand(0,1000); $data['out_trade_no'] = $out_trade_no; //订单号id,用于回调改订单状态 $data['total_fee'] = $last_total_fee; //支付金额,单位为分!! $data['notify_url'] = "https://********/Order/vip_notify"; //微信支付成功的回调路径,必须是小程序允许访问的路径 $data['trade_type'] = "JSAPI"; //小程序支付,所以是JSAPI ksort($data); $sign_str = $this->toUrlParams($data); $sign_str = $sign_str."&key=".$this->key; $data['sign'] = strtoupper(md5($sign_str)); $xml = $this->arrayToXml($data); $r = $this->postXmlCurl($xml,$url,true); $result = json_decode($this->xml_to_json($r)); if($result->return_code == 'SUCCESS'){ $sdata['appId'] = $this->appid; $sdata['timeStamp'] = (string)time(); $sdata['nonceStr'] = md5(time().rand().rand().$openid); $sdata['package'] = "prepay_id=".$result->prepay_id; $sdata['signType'] = "MD5"; ksort($sdata); $sign_str = $this->toUrlParams($sdata); $sign_str = $sign_str."&key=".$this->key; $sdata['paySign'] = strtoupper(md5($sign_str)); //创建未支付订单 //。。小伙伴们自己写哦 if($res){ ajaxReturn(V(1, '参数信息!',$sdata)); }else{ ajaxReturn(V(0, '操作失败!')); } } } /* * 会员升级回调 */ public function vip_notify(){ $data=file_get_contents('php://input'); $msg = (array)simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA); if($msg['result_code'] == "SUCCESS") { // 支付成功这里要做的操作! $out_trade_no = $msg['out_trade_no']; //将上面的未支付订单调整为已支付等等操作,自己写 } } /* * 获取随机字符串 */ private function getRandom($param){ $str="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $key = ""; for($i=0;$i<$param;$i++) { $key .= $str{mt_rand(0,32)}; //生成php随机数 } return $key; } /** * 用户post方法请求xml信息用的 */ function postXmlCurl($xml, $url, $useCert = false, $second = 10){ $ch = curl_init(); //设置超时 curl_setopt($ch, CURLOPT_TIMEOUT, $second); curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);//严格校验2 //设置header curl_setopt($ch, CURLOPT_HEADER, FALSE); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); //运行curl $data = curl_exec($ch); //返回结果 if($data){ curl_close($ch); return $data; } else { $error = curl_errno($ch); curl_close($ch); return $error; } } /* * 用于微信支付转换认证的信息用的 */ function toUrlParams($data){ $buff = ""; foreach ($data as $k => $v) { if($k != "sign" && $v != "" && !is_array($v)){ $buff .= $k . "=" . $v . "&"; } } $buff = trim($buff, "&"); return $buff; } /* * 微信支付-数组转xml */ function arrayToXml($arr){ $xml = ""; foreach ($arr as $key=>$val) { if (is_numeric($val)){ $xml.="<".$key.">".$val."".$key.">"; }else{ $xml.="<".$key.">".$key.">"; } } $xml.=" "; return $xml; } /* * 微信支付-数组转xml */ function xml_to_json($xmlstring) { return json_encode($this->xml_to_array($xmlstring),JSON_UNESCAPED_UNICODE); } /* * post方法 */ function post_url($post_data, $url){ $ch = curl_init(); //设置超时 curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);//严格校验2 curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $data = curl_exec($ch); curl_close($ch); return $data; } /* * 把xml转换成array */ function xml_to_array($xml) { return simplexml_load_string($xml,'SimpleXMLElement',LIBXML_NOCDATA); } }
很多朋友可能不大会处理微信支付回调,下面我截图一下,把支付成功后的返回数据给大家看一下,方便大家在回调中使用
希望这篇文章能够给写小程序支付的小伙伴一些帮助,如果有,记得给老七一个点赞和关注,谢谢!