服务器之家

服务器之家 > 正文

php微信扫码支付 php公众号支付

时间:2021-07-27 14:59     来源/作者:奇葩也是花

本文实例为大家分享了php微信扫码支付,公众号支付的具体代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
 
# 微信统一下单接口
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
 
 
$param = [
 'appid' => '公众号id',
 'mch_id' => '商户id',
 'nonce_str' =>uniqid(),
 'sign_type' => 'MD5',
 'body' => 'test',
 'detail' => 'test detail',
 'out_trade_no' => date('Ymd').rand(10000,99999),
 'total_fee' => 1,
 'notify_url' => 'http://www.test.top/testpay/pay.php',
 'trade_type' =>'JSAPI',
];
 
 
ksort( $param );
 
$sign_str = urldecode(http_build_query( $param ));
 
 
$sign_str .= '&key=商户密钥';
 
 
//echo $sign_str;exit;
 
$sign_str = md5( $sign_str );
 
 
$param['sign'] = strtoupper( $sign_str );
 
function CurlPost($url, $param = [], $is_post = 1, $timeout = 5 )
{
 
 //初始化curl
 $curl = curl_init();
 
 // 设置请求的路径
 curl_setopt($curl, CURLOPT_URL, $url);
 
 if ($is_post == 1) {
  //设置POST提交
  curl_setopt($curl, CURLOPT_POST, 0);
 }
 
 //显示输出结果 1代表 把接口返回的结果当作一个字符串处理
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 
 // 设置请求超时时间
 curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
 
 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 
 
 if ($is_post == 1) {
  //提交数据 -- 设置post提交的数据
  if (is_array($param)) {
 
   //http_build_query
   curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($param));
  } else {
   curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
  }
 }
 
 //执行请求
 $data = $data_str = curl_exec($curl);
 //处理错误
 if ($error = curl_error($curl)) {
  $log_data = array(
   'url' => $url,
   'param' => $param,
   'error' => '<span style="color:red;font-weight: bold">' . $error . '</span>',
  );
 
  var_dump($log_data);
  exit;
 }
 
 # 关闭CURL
 curl_close($curl);
 
 
 //json数据转换为数组
 $data = json_decode($data, true);
 
 if (!is_array($data)) {
  $data = $data_str;
 }
 
 #调用玩接口之后写一个日志
 $log = [
  'url' => $url,
  'param' => $param,
  'response' => $data_str
 ];
 file_put_contents(__DIR__ . '/wechat.log', print_r($log, true), 8);
 
 return $data;
 
}
function arr2Xml( $arr ){
 
 $xml = '<xml version="1.0" encoding="UTF-8"> ';
 foreach( $arr as $key => $value ){
  if (is_numeric($value)){
   $xml.="<".$key.">".$value."</".$key.">";
  }else{
   $xml.="<".$key."><![CDATA[".$value."]]></".$key.">";
  }
 
 }
 $xml .= '</xml>';
 return $xml;
}
 
$xml = arr2Xml( $param );
 
$result = CurlPost( $url , arr2Xml($param) );
 
 
$api_arr = json_decode( json_encode(simplexml_load_string( $result , 'SimpleXMLElement', LIBXML_NOCDATA) ), true );
 
if( $api_arr['return_code'] == 'SUCCESS' ){
 
 include __DIR__ . '/phpqrcode.php';
 header('content-type:image/png');
 echo Qrcode::png( $api_arr['code_url'] , false , 'H' , 6 ,2 );
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
 
#微信统一下单接口
$url='https://api.mch.weixin.qq.com/pay/unifiedorder';
 
$param=[
 #公众账号ID
 'appid'=>'****',
 'mch_id'=>'***',
 'nonce_str'=>uniqid(),
 'sign_type'=>'MD5',
 'body'=>'test',
 'detail'=>'detail',
 'out_trade_no'=>date('Ymd').rand(10000,99999),
 'total_fee'=>1,
 'spbill_create_ip'=>$_SERVER['SERVER_ADDR'],
 'notify_url'=>'http://****/test.php',
 'trade_type'=>'NATIVE',
];
ksort($param);
 
$sign_str=urldecode(http_build_query($param));
 
$sign_str.='&key=8934e7d15453e97507ef794cf7b0519d';
 
$sign_str=md5($sign_str);
 
$param['sign']=strtoupper($sign_str);
 
//print_r($param);exit;
function CurlPost($url, $param = [], $is_post = 1, $timeout = 5 )
{
 //初始化curl
 $curl = curl_init();
 
 // 设置请求的路径
 curl_setopt($curl, CURLOPT_URL, $url);
 
 if ($is_post == 1) {
  //设置POST提交
  curl_setopt($curl, CURLOPT_POST, 0);
 }
 
 //显示输出结果 1代表 把接口返回的结果当作一个字符串处理
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 
 // 设置请求超时时间
 curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
 
 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
 
 
 if ($is_post == 1) {
  //提交数据 -- 设置post提交的数据
  if (is_array($param)) {
 
   //http_build_query
   curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($param));
  } else {
   curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
  }
 }
 
 //执行请求
 $data = $data_str = curl_exec($curl);
 //处理错误
 if ($error = curl_error($curl)) {
  $log_data = array(
   'url' => $url,
   'param' => $param,
   'error' => '<span style="color:red;font-weight: bold">' . $error . '</span>',
  );
 
  var_dump($log_data);
  exit;
 }
 
 # 关闭CURL
 curl_close($curl);
 
 
 //json数据转换为数组
 $data = json_decode($data, true);
 
 if (!is_array($data)) {
  $data = $data_str;
 }
 
 #调用玩接口之后写一个日志
 $log = [
  'url' => $url,
  'param' => $param,
  'response' => $data_str
 ];
 file_put_contents(__DIR__ . '/wechat.log', print_r($log, true), 8);
 
 return $data;
 
}
 
function arrzxml($arr){
 $xml='<xml version="1.0" encoding="UTF-8">';
 foreach($arr as $key=>$value){
  if(is_numeric($value)){
   $xml.="<".$key.">".$value."</".$key.">";
  }else{
   $xml.="<".$key."><![CDATA[".$value."]]></".$key.">";
  }
 
 }
 $xml.='</xml>';
// var_dump($xml);exit;
 return $xml;
 
}
 
$xml=arrzxml($param);
$result=CurlPost($url,arrzxml($param));
//echo '<pre/>';
var_dump($result);
exit;
$api_arr=json_decode(simplexml_load_string($result,'SimpleXMLElement',LIBXML_NOCDATA),true);
if($result['return_code']=='SUCCESS'){
 include __DIR__.'/phpqrcode.php';
 header('content-type:image/png');
 echo Qrcode::png($api_arr['code_url'],false,'H',6,2);
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国 2021-05-08
返回顶部

606