当前位置:网站首页>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;
}
边栏推荐
- Introduction to Chapter 8 proof problem of njupt "Xin'an numeral base"
- Network security HSRP protocol
- Don't know these four caching modes, dare you say you understand caching?
- Multi person cooperation project to see how many lines of code each person has written
- 一文详解ASCII码,Unicode与utf-8
- 什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
- Cloudcompare - point cloud slice
- 如何把大的‘tar‘存档文件分割成特定大小的多个文件
- Shuttle INKWELL & ink components
- redis6事务和锁机制
猜你喜欢

Intranet penetration tool NetApp
![[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel](/img/b6/f9da8a36167db10c9a92dabb166c81.png)
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel

Go array and slice

Flutter 3.0更新后如何应用到小程序开发中

Self built shooting range 2022
![[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

真正的缓存之王,Google Guava 只是弟弟

什么是网络端口

Introduction to Chapter 8 proof problem of njupt "Xin'an numeral base"

Don't know these four caching modes, dare you say you understand caching?
随机推荐
mysql获得时间
【MySQL 使用秘籍】一网打尽 MySQL 时间和日期类型与相关操作函数(三)
Programmer growth Chapter 8: do a good job of testing
【 script secret pour l'utilisation de MySQL 】 un jeu en ligne sur l'heure et le type de date de MySQL et les fonctions d'exploitation connexes (3)
Internal JSON-RPC error. {"code":-32000, "message": "execution reverted"} solve the error
[deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging
Win10 - lightweight gadget
今年上半年,通信行业发生了哪些事?
Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
53. Maximum subarray sum: give you an integer array num, please find a continuous subarray with the maximum sum (the subarray contains at least one element) and return its maximum sum.
Jasypt configuration file encryption | quick start | actual combat
go 数组与切片
A detailed explanation of ASCII code, Unicode and UTF-8
With 4 years of working experience, you can't tell five ways of communication between multithreads. Dare you believe it?
Matlab paper chart standard format output (dry goods)
"Baidu Cup" CTF competition in September, web:upload
南理工在线交流群
Laravel框架运行报错:No application encryption key has been specified
leetcode 10. Regular expression matching regular expression matching (difficult)
What are the private addresses