


PHP WeChat payment development method of callback after scanning payment (mode 2)
In fact, when I wrote this article, I felt that I was already behind the times. However, when I searched for "WeChat payment development - how to call back after scanning payment (mode 2)" on Baidu to find the answer, I found that many friends still did not solve this problem. problem, so I will share my solution ideas with everyone.
1. Download the WeChat payment SDK (the author uses PHP development as an example, the sdk package is WxpayAPI_php_v3.zip)
After downloading the SDK package, unzip it. In the unzipped directory, we will see the following Directory
2. After consulting the WeChat payment developer documentation, we learned that the demo of WeChat scan code payment is the native.php file in the example directory
For convenience, what we have to do is to put the entire decompressed file into the wxpay (can be named according to personal preference) folder in the root directory of the local environment
3. Taking the author as an example, when browsing Enter http://localhost/wxpay/example/native.php
into the browser. After opening the above URL, we found that there are two QR codes. As the title says, what we are studying today is the mode two scan code (also recommended by the official Mode 2 scan code to pay)
4. We log in to WeChat with our mobile phone, scan the QR code of Mode 2 on the page above, and pay
Here we find an interesting problem, when After your payment is successful, there are no changes in the PC page, so the main issue we consider is how to call back after payment.
I won’t talk too much nonsense here. The author referred to many methods on the Internet and summarized them as follows:
1. Delete the scan code mode 1 in the native.php file. Some html, only some related html codes in scan code mode 2 are left.
2. As the official document also states that the payment result of scan code mode 2 is an asynchronous response and will not actively return the payment result, so we use javascript to monitor the payment result from time to time, and then based on the request result , making the next page callback. The author’s final code is as follows, interested friends can refer to it:
native.php file
<?php ini_set('date.timezone','Asia/Shanghai'); //error_reporting(E_ERROR); require_once "../lib/WxPay.Api.php"; require_once "WxPay.NativePay.php"; require_once 'log.php'; //模式一 /** * 流程: * 1、组装包含支付信息的url,生成二维码 * 2、用户扫描二维码,进行支付 * 3、确定支付之后,微信服务器会回调预先配置的回调地址,在【微信开放平台-微信支付-支付配置】中进行配置 * 4、在接到回调通知之后,用户进行统一下单支付,并返回支付信息以完成支付(见:native_notify.php) * 5、支付完成之后,微信服务器会通知支付成功 * 6、在支付成功通知中需要查单确认是否真正支付成功(见:notify.php) */ $notify = new NativePay(); $url1 = $notify->GetPrePayUrl("123456789"); //模式二 /** * 流程: * 1、调用统一下单,取得code_url,生成二维码 * 2、用户扫描二维码,进行支付 * 3、支付完成之后,微信服务器会通知支付成功 * 4、在支付成功通知中需要查单确认是否真正支付成功(见:notify.php) */ $input = new WxPayUnifiedOrder(); $input->SetBody("1分钱购买何宁"); $input->SetAttach("1分钱购买何宁"); $num=WxPayConfig::MCHID.date("YmdHis"); $input->SetOut_trade_no($num); $input->SetTotal_fee("1"); $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis", time() + 600)); $input->SetGoods_tag("test"); $input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php"); $input->SetTrade_type("NATIVE"); $input->SetProduct_id("123456789"); $result = $notify->GetPayUrl($input); $url2 = $result["code_url"]; ?> <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>微信支付样例</title> </head> <body> <div style="margin-left: 10px;color:#556B2F;font-size:30px;font-weight: bolder;">扫描支付模式二</div><br/> <img src="/static/imghw/default1.png" data-src="qrcode.php?data=<?php echo urlencode($url2);? alt="PHP WeChat payment development method of callback after scanning payment (mode 2)" >" class="lazy" alt="模式二扫码支付" style="max-width:90%"/> <div id="myDiv"></div><div id="timer">0</div> <script> //设置每隔1000毫秒执行一次load() 方法 var myIntval=setInterval(function(){load()},1000); function load(){ document.getElementById("timer").innerHTML=parseInt(document.getElementById("timer").innerHTML)+1; var xmlhttp; if (window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); }else{ // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ trade_state=xmlhttp.responseText; if(trade_state=='SUCCESS'){ document.getElementById("myDiv").innerHTML='支付成功'; //alert(transaction_id); //延迟3000毫秒执行tz() 方法 clearInterval(myIntval); setTimeout("location.href='success.php'",3000); }else if(trade_state=='REFUND'){ document.getElementById("myDiv").innerHTML='转入退款'; clearInterval(myIntval); }else if(trade_state=='NOTPAY'){ document.getElementById("myDiv").innerHTML='请扫码支付'; }else if(trade_state=='CLOSED'){ document.getElementById("myDiv").innerHTML='已关闭'; clearInterval(myIntval); }else if(trade_state=='REVOKED'){ document.getElementById("myDiv").innerHTML='已撤销'; clearInterval(myIntval); }else if(trade_state=='USERPAYING'){ document.getElementById("myDiv").innerHTML='用户支付中'; }else if(trade_state=='PAYERROR'){ document.getElementById("myDiv").innerHTML='支付失败'; clearInterval(myIntval); } } } //orderquery.php 文件返回订单状态,通过订单状态确定支付状态 xmlhttp.open("POST","orderquery.php",false); //下面这句话必须有 //把标签/值对添加到要发送的头文件。 xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send("out_trade_no=<?php echo $num;?>"); } </script> </body> </html>
## The #orderquery.php code has also been adjusted accordingly:
<?php ini_set('date.timezone','Asia/Shanghai'); error_reporting(E_ERROR); require_once "../lib/WxPay.Api.php"; require_once 'log.php'; //初始化日志 $logHandler= new CLogFileHandler("./logs/".date('Y-m-d').'.log'); $log = Log::Init($logHandler, 15); function printf_info($data) { foreach($data as $key=>$value){ echo "<font color='#f00;'>$key</font> : $value <br/>"; } } if(isset($_REQUEST["transaction_id"]) && $_REQUEST["transaction_id"] != ""){ $transaction_id = $_REQUEST["transaction_id"]; $input = new WxPayOrderQuery(); $input->SetTransaction_id($transaction_id); //printf_info(WxPayApi::orderQuery($input)); $result=WxPayApi::orderQuery($input); echo $result['trade_state']; exit(); } if(isset($_REQUEST["out_trade_no"]) && $_REQUEST["out_trade_no"] != ""){ $out_trade_no = $_REQUEST["out_trade_no"]; $input = new WxPayOrderQuery(); $input->SetOut_trade_no($out_trade_no); //printf_info(WxPayApi::orderQuery($input)); $result=WxPayApi::orderQuery($input); echo $result['trade_state']; exit(); } ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>微信支付成功</title> </head> <body> <br /><br /><br /><br /><br /><br /><br /> <h1 id="微信支付成功">微信支付成功</h1> </body> </html>
The above is the code summarized by the author. After debugging, it was found that there is no problem. Interested friends can refer to it.
The above is the detailed content of PHP WeChat payment development method of callback after scanning payment (mode 2). For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is an open source scripting language that is widely used in web development and server-side programming, especially in WeChat development. Today, more and more companies and developers are starting to use PHP for WeChat development because it has become a truly easy-to-learn and easy-to-use development language. In WeChat development, message encryption and decryption are a very important issue because they involve data security. For messages without encryption and decryption methods, hackers can easily obtain the data, posing a threat to users.

With the popularity of WeChat, more and more companies are beginning to use it as a marketing tool. The WeChat group messaging function is one of the important means for enterprises to conduct WeChat marketing. However, if you only rely on manual sending, it is an extremely time-consuming and laborious task for marketers. Therefore, it is particularly important to develop a WeChat mass messaging tool. This article will introduce how to use PHP to develop WeChat mass messaging tools. 1. Preparation work To develop WeChat mass messaging tools, we need to master the following technical points: Basic knowledge of PHP WeChat public platform development Development tools: Sub

In the development of WeChat public accounts, user tag management is a very important function, which allows developers to better understand and manage their users. This article will introduce how to use PHP to implement the WeChat user tag management function. 1. Obtain the openid of the WeChat user. Before using the WeChat user tag management function, we first need to obtain the user's openid. In the development of WeChat public accounts, it is a common practice to obtain openid through user authorization. After the user authorization is completed, we can obtain the user through the following code

As WeChat becomes an increasingly important communication tool in people's lives, its agile messaging function is quickly favored by a large number of enterprises and individuals. For enterprises, developing WeChat into a marketing platform has become a trend, and the importance of WeChat development has gradually become more prominent. Among them, the group sending function is even more widely used. So, as a PHP programmer, how to implement group message sending records? The following will give you a brief introduction. 1. Understand the development knowledge related to WeChat public accounts. Before understanding how to implement group message sending records, I

WeChat is currently one of the social platforms with the largest user base in the world. With the popularity of mobile Internet, more and more companies are beginning to realize the importance of WeChat marketing. When conducting WeChat marketing, customer service is a crucial part. In order to better manage the customer service chat window, we can use PHP language for WeChat development. 1. Introduction to PHP WeChat development PHP is an open source server-side scripting language that is widely used in the field of Web development. Combined with the development interface provided by WeChat public platform, we can use PHP language to conduct WeChat

In the development of WeChat public accounts, the voting function is often used. The voting function is a great way for users to quickly participate in interactions, and it is also an important tool for holding events and surveying opinions. This article will introduce you how to use PHP to implement WeChat voting function. Obtain the authorization of the WeChat official account. First, you need to obtain the authorization of the WeChat official account. On the WeChat public platform, you need to configure the API address of the WeChat public account, the official account, and the token corresponding to the public account. In the process of our development using PHP language, we need to use the PH officially provided by WeChat

How to use PHP to develop WeChat public accounts WeChat public accounts have become an important channel for promotion and interaction for many companies, and PHP, as a commonly used Web language, can also be used to develop WeChat public accounts. This article will introduce the specific steps to use PHP to develop WeChat public accounts. Step 1: Obtain the developer account of the WeChat official account. Before starting the development of the WeChat official account, you need to apply for a developer account of the WeChat official account. For the specific registration process, please refer to the official website of WeChat public platform

With the development of the Internet and mobile smart devices, WeChat has become an indispensable part of the social and marketing fields. In this increasingly digital era, how to use PHP for WeChat development has become the focus of many developers. This article mainly introduces the relevant knowledge points on how to use PHP for WeChat development, as well as some of the tips and precautions. 1. Development environment preparation Before developing WeChat, you first need to prepare the corresponding development environment. Specifically, you need to install the PHP operating environment and the WeChat public platform
