当前位置:网站首页>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;
}
边栏推荐
- [notes of in-depth study paper]transbtsv2: wider instead of deep transformer for medical image segmentation
- 4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
- 这18个网站能让你的页面背景炫酷起来
- ELK 企业级日志分析系统
- Aikesheng sqle audit tool successfully completed the evaluation of "SQL quality management platform grading ability" of the Academy of communications and communications
- Fragmented knowledge management tool memos
- Solve the problem of "unable to open source file" xx.h "in the custom header file on vs from the source
- 【MySQL 使用秘籍】一網打盡 MySQL 時間和日期類型與相關操作函數(三)
- MySQL - database query - sort query, paging query
- Resttemplate details
猜你喜欢

Laravel框架运行报错:No application encryption key has been specified

Aikesheng sqle audit tool successfully completed the evaluation of "SQL quality management platform grading ability" of the Academy of communications and communications
![[public class preview]: basis and practice of video quality evaluation](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[public class preview]: basis and practice of video quality evaluation

嵌入式软件架构设计-消息交互
![[notes of in-depth study paper]transbtsv2: wider instead of deep transformer for medical image segmentation](/img/70/6de0346df8527af6c88db1ff89947b.png)
[notes of in-depth study paper]transbtsv2: wider instead of deep transformer for medical image segmentation

龙芯派2代烧写PMON和重装系统
![[深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接](/img/b6/f9da8a36167db10c9a92dabb166c81.png)
[深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接

一文详解ASCII码,Unicode与utf-8

Fragmented knowledge management tool memos

Can and can FD
随机推荐
FPGA 学习笔记:Vivado 2019.1 添加 IP MicroBlaze
Can and can FD
Idea set method annotation and class annotation
运筹说 第68期|2022年最新影响因子正式发布 快看管科领域期刊的变化
Could not set property ‘id‘ of ‘class XX‘ with value ‘XX‘ argument type mismatch 解决办法
Mmseg - Mutli view time series data inspection and visualization
Win10——轻量级小工具
go map
Apicloud studio3 WiFi real machine synchronization and WiFi real machine preview instructions
龙芯派2代烧写PMON和重装系统
Go pointer
What about data leakage? " Watson k'7 moves to eliminate security threats
这18个网站能让你的页面背景炫酷起来
【云资源】云资源安全管理用什么软件好?为什么?
Personal component - message prompt
Operational research 68 | the latest impact factors in 2022 were officially released. Changes in journals in the field of rapid care
Huawei push service content, read notes
asp.net 读取txt文件
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
Internal JSON-RPC error. {"code":-32000, "message": "execution reverted"} solve the error