This time I will bring you an analysis of the case of PHP implementing APP WeChat payment. What are the precautions for PHP to implement APP WeChat payment. The following is a practical case, let's take a look.
1. The PHP background generates a prepayment transaction order, returns the correct prepayment transaction response ID, and then calls up the payment in the APP!
Official document:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1
Need to splice WeChat according to the document Parameters, several methods are needed here, go directly to the code!
The parameters transmitted to WeChat must be assembled into xml format and sent as a parameter array!
public function ToXml($data=array()) { if(!is_array($data) || count($data) "; foreach ($data as $key=>$val) { if (is_numeric($val)){ $xml.="".$val."".$key.">"; }else{ $xml.="".$key.">"; } } $xml.=""; return $xml; }
2. Generate a random string, WeChat Required parameters! There are many methods here, it all depends on your hobbies!
function rand_code(){ $str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';//62个字符 $str = str_shuffle($str); $str = substr($str,0,32); return $str; }
3. This is an important step for WeChat. This method will be used many times! Generate signature
private function getSign($params) { ksort($params); //将参数数组按照参数名ASCII码从小到大排序 foreach ($params as $key => $item) { if (!empty($item)) { //剔除参数值为空的参数 $newArr[] = $key.'='.$item; // 整合新的参数数组 } } $stringA = implode("&", $newArr); //使用 & 符号连接参数 $stringSignTemp = $stringA."&key="."************************"; //拼接key // key是在商户平台API安全里自己设置的 $stringSignTemp = MD5($stringSignTemp); //将字符串进行MD5加密 $sign = strtoupper($stringSignTemp); //将所有字符转换为大写 return $sign; }
4 . Pass the parameters to WeChat and generate a pre-payment order! Receive the data returned by WeChat and send it back to the APP. The APP calls the payment interface to complete the payment! For the parameters required on the APP, please see the WeChat documentation: https://pay.weixin.qq .com/wiki/doc/api/app/app.php?chapter=9_12&index=2
public function wx_pay() { $nonce_str = $this->rand_code(); //调用随机字符串生成方法获取随机字符串 $data['appid'] ='wxdbc5dc*******'; //appid $data['mch_id'] = '1493*****' ; //商户号 $data['body'] = "APP支付测试"; $data['spbill_create_ip'] = $_SERVER['HTTP_HOST']; //ip地址 $data['total_fee'] = 1; //金额 $data['out_trade_no'] = time().mt_rand(10000,99999); //商户订单号,不能重复 $data['nonce_str'] = $nonce_str; //随机字符串 $data['notify_url'] = 'http://xxx.xxx.com/wx_notify'; //回调地址,用户接收支付后的通知,必须为能直接访问的网址,不能跟参数 $data['trade_type'] = 'APP'; //支付方式 //将参与签名的数据保存到数组 注意:以上几个参数是追加到$data中的,$data中应该同时包含开发文档中要求必填的剔除sign以外的所有数据 $data['sign'] = $this->getSign($data); //获取签名 $xml = $this->ToXml($data); //数组转xml //curl 传递给微信方 $url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; //header("Content-type:text/xml"); $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $url); if(stripos($url,"https://")!==FALSE){ curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); } else { curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,TRUE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,2);//严格校验 } //设置header curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); curl_setopt($ch, CURLOPT_HEADER, FALSE); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //设置超时 curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_POST, TRUE); //传输文件 curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); //运行curl $data = curl_exec($ch); //返回结果 if($data){ curl_close($ch); //返回成功,将xml数据转换为数组. $re = $this->FromXml($data); if($re['return_code'] != 'SUCCESS'){ json("201",'签名失败'); } else{ //接收微信返回的数据,传给APP! $arr =array( 'prepayid' =>$re['prepay_id'], 'appid' => 'wxdbc5dc*****', 'partnerid' => '14937****', 'package' => 'Sign=WXPay', 'noncestr' => $nonce_str, 'timestamp' =>time(), ); //第二次生成签名 $sign = $this->getSign($arr); $arr['sign'] = $sign; json('200','签名成功',$arr); } } else { $error = curl_errno($ch); curl_close($ch); json('201',"curl出错,错误码:$error"); } }
5. Convert xml data into an array, used when receiving data returned by WeChat.
public function FromXml($xml) { if(!$xml){ echo "xml数据异常!"; } //将XML转为array //禁止引用外部xml实体 libxml_disable_entity_loader(true); $data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $data; }
2. After the APP payment is successful, it will call the callback address you filled in.
For details of the return parameters, please see the WeChat document: https://pay.weixin.qq .com/wiki/doc/api/app/app.php?chapter=9_7&index=3
// 微信支付回调 function wx_notify(){ //接收微信返回的数据数据,返回的xml格式 $xmlData = file_get_contents('php://input'); //将xml格式转换为数组 $data = $this->FromXml($xmlData); //用日志记录检查数据是否接受成功,验证成功一次之后,可删除。 $file = fopen('./log.txt', 'a+'); fwrite($file,var_export($data,true)); //为了防止假数据,验证签名是否和返回的一样。 //记录一下,返回回来的签名,生成签名的时候,必须剔除sign字段。 $sign = $data['sign']; unset($data['sign']); if($sign == $this->getSign($data)){ //签名验证成功后,判断返回微信返回的 if ($data['result_code'] == 'SUCCESS') { //根据返回的订单号做业务逻辑 $arr = array( 'pay_status' => 1, ); $re = M('order')->where(['order_sn'=>$data['out_trade_no']])->save($arr); //处理完成之后,告诉微信成功结果! if($re){ echo '<xml> <return_code></return_code> <return_msg></return_msg> </xml>';exit(); } } //支付失败,输出错误信息 else{ $file = fopen('./log.txt', 'a+'); fwrite($file,"错误信息:".$data['return_msg'].date("Y-m-d H:i:s"),time()."\r\n"); } } else{ $file = fopen('./log.txt', 'a+'); fwrite($file,"错误信息:签名验证失败".date("Y-m-d H:i:s"),time()."\r\n"); } }
Here, the WeChat APP payment process is successfully completed! Thank you for your support!
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
PHP’s RSA encryption, decryption and development interface case usage analysis
PHP long connection usage case analysis
The above is the detailed content of PHP implementation of APP WeChat payment case analysis. For more information, please follow other related articles on the PHP Chinese website!

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

Tostoreauser'snameinaPHPsession,startthesessionwithsession_start(),thenassignthenameto$_SESSION['username'].1)Usesession_start()toinitializethesession.2)Assigntheuser'snameto$_SESSION['username'].Thisallowsyoutoaccessthenameacrossmultiplepages,enhanc

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
