当前位置:网站首页>Wechat app payment callback processing method PHP logging method, notes. 2020/5/26
Wechat app payment callback processing method PHP logging method, notes. 2020/5/26
2022-07-05 13:43:00 【Php_ XiaoZhi】
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2020/5/23 0023
* Time: 16:06
*/
function wxQueryPay_1($order){
$app_id = 'app_id '; // Applied appid
$mch_id = 'mch_id '; // Your merchant account number
$nonce_str = nonce_str_1(); // Random string
$out_trade_no = $order['orderid']; // Merchant order number
// Here is in order Because the signatures below are in order Sort error There must be something wrong
$post['appid'] = $app_id;
$post['mch_id'] = $mch_id;
$post['nonce_str'] = $nonce_str; // Random string
$post['out_trade_no'] = $out_trade_no;
$sign = wx_sign_1($post); // Signature
$post_xml = "<xml><appid><![CDATA[$app_id]]></appid><mch_id><![CDATA[$mch_id]]></mch_id><nonce_str><![CDATA[$nonce_str]]></nonce_str><out_trade_no><![CDATA[$out_trade_no]]></out_trade_no><sign><![CDATA[$sign]]></sign></xml>";
// Unified interface prepay_id
$url = 'https://api.mch.weixin.qq.com/pay/orderquery';
$xml =wx_http_request_1($url, $post_xml);
// return $xml;
$array = wx_xml_1($xml); // All in capital letters
// return $array;
if ($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS') {
$data =$array;
}else {
$data['status'] = 0;
$data['text'] = " error ";
$data['RETURN_CODE'] = $array['RETURN_CODE'];
$data['RETURN_MSG'] = $array['RETURN_MSG'];
}
return $data;
}
/// Generate signature
function wx_sign_1($data){
// Signature $data Order first
$stringA = '';
ksort($data);
foreach($data as $key => $value) {
if (!$value) continue;
if ($stringA)
$stringA.= '&'.$key."=".$value;
else
$stringA = $key."=".$value;
}
$stringSignTemp = $stringA.'&key='.'key'; // After applying for payment, a merchant account number and password are given , Set up your own after logging in key
return strtoupper(md5($stringSignTemp));
}
function nonce_str_1() {
$result = '';
$str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';
for ($i = 0; $i < 32; $i++) {
$result.= $str[rand(0, 48)];
}
return $result;
}
function wx_xml_1($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;
}
function wx_http_request_1($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;
}
$payXml = file_get_contents("php://input");
// take xml Format into json Format
$jsonXml = json_encode(simplexml_load_string($payXml, 'SimpleXMLElement', LIBXML_NOCDATA));
// take json Format to array format
$result = json_decode($jsonXml, true);
$myfile = "order_log.txt";
$paytype = "paytype.txt";
file_put_contents($myfile, $jsonXml.'</br>',FILE_APPEND);
if($result){
if($result['result_code'] == 'SUCCESS' && $result['return_code'] == 'SUCCESS'){
$order['orderid'] =$result['out_trade_no'];
$rs = wxQueryPay_1($order);
file_put_contents($paytype, json_encode($rs).'</br>',FILE_APPEND);
if($rs['TRADE_STATE_DESC'] ==' Successful payment '){
$servername = "0.0.0.0";/// database ip Address
$username = "username"; /// Database user name
$password = "password"; Database password
$tablename ="tablename"; Database table name
// Create connection
$link = mysqli_connect($servername,$username,$password,$tablename) or die(mysqli_errno());
// Dealing with databases
$order_sql ='select * from cmf_users_charge where orderno = "'.$rs['out_trade_no'].'";';
$order_type =mysqli_query($link,$order_sql);
$order_result = mysqli_fetch_array($order_type);
$user_sql ='select * from cmf_users where id = "'.$order_result['touid'].'";';
$user_type =mysqli_query($link,$user_sql);
$user_result = mysqli_fetch_array($user_type);
if($order_result['power']==4){
$order_status_sql ='UPDATE cmf_users_charge SET status = 1 where orderno =' .$rs['out_trade_no'].'; ';
mysqli_query($link,$sql);
$coin = $user_result['coin']+$order_result['coin'];
$coin_sql = 'UPDATE cmf_users SET coin =.'.$coin.' where id =' .$order_result['touid'].'and banlc_type = 0; ';
mysqli_query($link,$coin_sql);
$banle_type = 'UPDATE cmf_users_charge SET banlc_type = 1 where orderno =' .$rs['out_trade_no'].'; ';
mysqli_query($link,$banle_type);
}
mysqli_close($link);
}
/// Handle wechat callback repetition
$return = ['return_code'=>'SUCCESS','return_msg'=>'OK'];
$xml_t = '<xml>';
foreach($return as $k=>$v){
$xml_t.='<'.$k.'><![CDATA['.$v.']]></'.$k.'>';
}
$xml_t.='</xml>';
echo $xml_t;
}else{
$return = ['return_code'=>'SUCCESS','return_msg'=>'OK'];
$xml_t = '<xml>';
foreach($return as $k=>$v){
$xml_t.='<'.$k.'><![CDATA['.$v.']]></'.$k.'>';
}
$xml_t.='</xml>';
echo $xml_t;
}
}else{
$return = ['return_code'=>'SUCCESS','return_msg'=>'OK'];
$xml_t = '<xml>';
foreach($return as $k=>$v){
$xml_t.='<'.$k.'><![CDATA['.$v.']]></'.$k.'>';
}
$xml_t.='</xml>';
echo $xml_t;
}
边栏推荐
猜你喜欢
一网打尽异步神器CompletableFuture
搭建一个仪式感点满的网站,并内网穿透发布到公网 2/2
[深度学习论文笔记]TransBTSV2: Wider Instead of Deeper Transformer for Medical Image Segmentation
The "Baidu Cup" CTF competition was held in February 2017, Web: explosion-2
What are the private addresses
Fragmented knowledge management tool memos
今年上半年,通信行业发生了哪些事?
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
Scientific running robot pancakeswap clip robot latest detailed tutorial
Could not set property ‘id‘ of ‘class XX‘ with value ‘XX‘ argument type mismatch 解决办法
随机推荐
ELFK部署
Go pointer
Kafaka log collection
Shuttle INKWELL & ink components
NFT value and white paper acquisition
53. 最大子数组和:给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
“百度杯”CTF比赛 九月场,Web:SQL
Flutter InkWell & Ink组件
MySQL --- 数据库查询 - 排序查询、分页查询
Shandong University Summer Training - 20220620
[server data recovery] a case of RAID5 data recovery stored in a brand of server
go 数组与切片
多人合作项目查看每个人写了多少行代码
Integer = = the comparison will unpack automatically. This variable cannot be assigned empty
法国学者:最优传输理论下对抗攻击可解释性探讨
What is a network port
个人组件 - 消息提示
asp. Net read TXT file
Huawei push service content, read notes
Could not set property ‘id‘ of ‘class XX‘ with value ‘XX‘ argument type mismatch 解决办法