php小程序支付
程序员文章站
2022-07-13 17:08:15
...
1.基本逻辑
小程序端先请求后端接口
后端接口生成相应参数,返回给小程序端
小程序端调用wx.requestPayment发起支付
小程序示例代码:
wx.requestPayment(
{
'timeStamp': '', //时间戳
'nonceStr': '', //随机字符串,长度为32个字符以下。
'package': '', //后端返回的 prepay_id 参数值,格式如:prepay_id=*
'signType': 'MD5', //签名类型默认MD5
'paySign': '', //签名
//以上参数由后端生成返回
'success':function(res){ //支付成功
},
'fail':function(res){ //支付失败
},
'complete':function(res){ //支付完成,不管成功失败都会响应
}
})
2. tp5代码
$appid = $res['appid']; //小程序appid
$body = '订单支付';
$mch_id = // 商户号 公众平台商户号需绑定到小程序上
$nonce_str = $this->nonce_str(); // 随机字符串
$notify_url = '' ; //回调地址
$openid = $this->openID; //用户openid
$out_trade_no = ''; //订单号
$spbill_create_ip = $_SERVER['REMOTE_ADDR'];
$total_fee = $money*100; //支付金额需要乘以100
$trade_type = 'JSAPI';
//按照顺序生成签名
$post1['appid'] = $appid;
$post1['body'] = $body;
$post1['mch_id'] = $mch_id;
$post1['nonce_str'] = $nonce_str;
$post1['notify_url'] = $notify_url;
$post1['openid'] = $openid;
$post1['out_trade_no'] = $out_trade_no;
$post1['spbill_create_ip'] = $spbill_create_ip;
$post1['total_fee'] = $total_fee;//
$post1['trade_type'] = $trade_type;
$sign = $this->sign($post1); //签名
//生成xml数据
$post_xml =
'<xml>
<appid>'.$appid.'</appid>
<body>'.$body.'</body>
<mch_id>'.$mch_id.'</mch_id>
<nonce_str>'.$nonce_str.'</nonce_str>
<notify_url>'.$notify_url.'</notify_url>
<openid>'.$openid.'</openid>
<out_trade_no>'.$out_trade_no.'</out_trade_no>
<spbill_create_ip>'.$spbill_create_ip.'</spbill_create_ip>
<total_fee>'.$total_fee.'</total_fee>
<trade_type>'.$trade_type.'</trade_type>
<sign>'.$sign.'</sign>
</xml> ';
//调用微信接口下单 得到prepay_id
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
//请求成功后微信会返回一个xml 里面包含prepay_id
$xml = $this->http_request($url,$post_xml);
$array = $this->xml($xml);//全要大写
//结果分析
if( $array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS'){
$tmp = []; //临时数组用于签名
$tmp['appId'] = $appid;
$tmp['nonceStr'] = $nonce_str;
$tmp['package'] = 'prepay_id='.$array['PREPAY_ID'];
$tmp['signType'] = 'MD5';
$tmp['timeStamp'] = strval($time);
$data['prepay_id'] = $array['PREPAY_ID'];
$data['timeStamp'] = strval(time()); //时间戳 此处转化为字符串模式返回
$data['nonceStr'] = $nonce_str; //随机字符串
$data['signType'] = 'MD5'; //签名算法,暂支持 MD5
$data['package'] = 'prepay_id='.$array['PREPAY_ID'];// 接口返回的 prepay_id 参数值,
$data['paySign'] = $this->sign($tmp); //签名
$data['out_trade_no'] = $out_trade_no;
}else{
$data['state'] = 0;
$data['text'] = "错误";
$data['RETURN_CODE'] = $array['RETURN_CODE'];
$data['RETURN_MSG'] = $array['RETURN_MSG'];
}
echo json_encode($data);
获取nonce_str
public function nonce_str(){
$result = '';
$str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';
for ($i=0;$i<32;$i++){
$result .= $str[rand(0,48)];
}
return $result;
}
获取openid
public function openID(){
$code= 小程序端通过wx.login获得
//
wx.login({
success: function(res) {
// 成功的话会返回:
// {errMsg: "login:ok", code: "获取用户OpenID的ticket"}
var code =res.code //将code发送给后端
}
})
//
$WX_APPID = ''; //小程序appid
$WX_SECRET = ''; //小程序appsecret
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $WX_APPID . "&secret=" . $WX_SECRET . "&js_code=" . $code . "&grant_type=authorization_code";
$infos = json_decode(file_get_contents($url));
$openid = $infos->openid;
return $openid;
}
获取签名
public function sign($data){
$stringA = '';
foreach ($data as $key=>$value){
if(!$value) continue;
if($stringA){
$stringA .= '&'.$key."=".$value;
} else{
$stringA = $key."=".$value;
}
}
$wx_key = ''; //支付**
$stringSignTemp = $stringA.'&key='.$wx_key;
return strtoupper(md5($stringSignTemp));
}
http请求
function http_request($url,$data = null,$headers=array())
{
$curl = curl_init();
if( count($headers) >= 1 ){
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
得到xml
public function xml($xml){
$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $vals, $index);
xml_parser_free($p);
$data = "";
foreach ($index as $key=>$value) {
if($key == 'xml' || $key == 'XML') continue;
$tag = $vals[$value[0]]['tag'];
$value = $vals[$value[0]]['value'];
$data[$tag] = $value;
}
return $data;
}
上一篇: PHP 小程序支付