Table of Contents
Various parameters of the account
Order information
Request prepay_id
Return client information
WeChat callback
Home Backend Development PHP Tutorial Detailed explanation of the php background interface of App WeChat Payment

Detailed explanation of the php background interface of App WeChat Payment

Mar 08, 2018 am 10:34 AM
php interface Detailed explanation

This article will introduce to you the PHP (7.0) background payment and callback interface of the App WeChat Pay (2016.10.11). The framework is Thinkphp5.0: I hope it can help you.

  • Various parameters of the account

  • Order information

  • Request prepay_id

  • *Return to APP data processing

  • WeChat callback

  • Modify order status


Various parameters of the account

The various parameters of the account are like when WeChat applies for app payment, an email will be sent to your account mailbox, and there will be the corresponding WeChat payment allocation. The merchant number (MCHID), APPID and APPSECRET are returned when applying for app payment permissions, and the KEY needs to be set by the user in the merchant backend on WeChat. This is very important! Avoid leaks!

Order information

  • **The client will transfer the product data in the shopping cart to the backend, including user information, etc. After getting the data, you first need to verify the data. , the verification here generally determines the transmission rules with the client at the beginning of the project, such as transmission method, parameter name, and method of verifying parameters. The focus here is verification, and signature authentication is generally used:

  • //Signature Step 1: Sort parameters in lexicographic order;

  • The code example is given here (formatting parameters are formatted into url parameters):

/**
     * 格式化参数格式化成url参数
     */
    public function ToUrlParams()
    {
        $buff = "";
        foreach ($this->values as $k => $v)
        {            if($k != "sign" && $v != "" && !is_array($v)){
                $buff .= $k . "=" . $v . "&";
            }
        }

        $buff = trim($buff, "&");        return $buff;
    }
Copy after login
  • //Signature step 2: Add KEY after the string (this KEY is negotiated with the front-end personnel);


  • //Signature step three: MD5 encryption;


  • // Signature Step 4: Convert all characters to uppercase


It is best to encapsulate these in the early stages of the project, and you will only need to call them later. Just fine, for example:
(You only need $param = $this->request('parameter name')); later), and then pre-store the order information.

Request prepay_id

Here I will go directly to the code, (here is the interface document download address:), although there are many self-written request interfaces on the Internet, but since WeChat has been packaged, just use it:

$input = new \app\wxpay\WxPayUnifiedOrder();//这里引用微信的统一下单接口

$input->SetBody($data['gname']['g_name']);//商品或支付单简要描述
$input->SetAttach($data['gname']['g_name']);//置附加数据 
$input->SetOut_trade_no($order_sn); // 商户订单号
$input->SetTotal_fee(intval($data['data']['order_price']*100)); 
$input->SetTime_start(date("YmdHis"));//订单生成时间
$input->SetTime_expire(date("YmdHis", time() + 600));//订单失效时间
$input->SetGoods_tag($data['gname']['g_name']); //商品标记  
$input->SetNotify_url("http://www.weixin.qq.com/wxpay/notify.php"); // 支付成功后的回调地址,
$input->SetTrade_type("APP");
$order = \app\wxpay\WxPayApi::unifiedOrder($input);return $order['prepay_id'];
Copy after login

Here is the WeChat official unified ordering interface description address:
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1

Return client information

$info = array();
//账号的信息一般都放在配置文件里面,用到的地方也很多
$info['appid'] = config('APP_APPID');
$info['partnerid'] = config('APP_MCHID');
$info['package'] = config('APP_PACKAGE');
$info['noncestr'] = $this->random_number();//生成随机数,下面有生成实例,统一下单接口需要
$info['timestamp'] = time();
$info['prepayid'] = $prepay_id;
$info['sign'] = self::_makeSign($info);//生成签名return $info;
Copy after login

$info is the information the client needs
Generate random number instance

//生成随机数
    public function random_number($len=21,$format='ALL' ){
        $is_abc = $is_numer = 0;
        $password = $tmp ='';
        switch($format){
            case 'ALL':
            $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';            break;
            case 'CHAR':
            $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';            break;
            case 'NUMBER':
            $chars='0123456789';            break;
            default :
            $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';            break;
        } // www.jb51.net
        mt_srand((double)microtime()*1000000*getmypid());        while(strlen($password)<$len){
            $tmp =substr($chars,(mt_rand()%strlen($chars)),1);            if(($is_numer <> 1 && is_numeric($tmp) && $tmp >0 )|| $format == &#39;CHAR&#39;){
                $is_numer = 1;
            }            if(($is_abc <> 1 && preg_match(&#39;/[a-zA-Z]/&#39;,$tmp)) || $format == &#39;NUMBER&#39;){
                $is_abc = 1;
            }
            $password.= $tmp;
        }        if($is_numer <> 1 || $is_abc <> 1 || empty($password) ){
            $password = $this->random_number($len,$format);
        }        return $password;
        }
Copy after login

WeChat callback

Payment result notification notify.php (here The address is the callback address filled in when placing an order, WeChat has already encapsulated it), the document download address
http://mch.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1
In fact, the most important code of this page is only two lines

[php] view plain copy

    $notify = new PayNotifyCallBack();  
    $notify->Handle(false);
Copy after login

Most of the logic is processed in the Handle function to process the file WxPay.Notify.php

[php] view plain copy

    final public function Handle($needSign = true)  
        {  
            $msg = "OK";  
            //当返回false的时候,表示notify中调用NotifyCallBack回调失败获取签名校验失败,此时直接回复失败  
            $result = WxpayApi::notify(array($this, &#39;NotifyCallBack&#39;), $msg);  
            if($result == false){  
                $this->SetReturn_code("FAIL");  
                $this->SetReturn_msg($msg);  
                $this->ReplyNotify(false);  
                return;  
            } else {  
                //该分支在成功回调到NotifyCallBack方法,处理完成之后流程  
                $this->SetReturn_code("SUCCESS");  
                $this->SetReturn_msg("OK");  
            }  
            $this->ReplyNotify($needSign);  
        }
Copy after login

Main code:
[php] view plain copy

    $result = WxpayApi::notify(array($this, &#39;NotifyCallBack&#39;), $msg);
Copy after login

Track function notify file WxPay.Api.php

[ php] view plain copy

    public static function notify($callback, &$msg)  
        {  
            //获取通知的数据  
            $xml = $GLOBALS[&#39;HTTP_RAW_POST_DATA&#39;];  
            //如果返回成功则验证签名  
            try {  
                $result = WxPayResults::Init($xml);  
            } catch (WxPayException $e){  
                $msg = $e->errorMessage();  
                return false;  
            }  

            return call_user_func($callback, $result);  
        }
Copy after login

Get gay data through $GLOBALS['HTTP_RAW_POST_DATA']; and then use the Init function to verify the signature, etc. The signature verification is successful and the code is run.
It needs to be explained here that php7 itself does not support $GLOBALS['HTTP_RAW_POST_DATA']. You need to download a plug-in. You can Baidu for details. What I want to say is that you can use file_get_contents('php:/ /input'), for specific reasons, please refer to the blog below, which is very detailed (https://my.oschina.net/jiec/blog/485359)
[php] view plain copy

return call_user_func($callback, $result);
Copy after login

That is, a callback function is called, the NotifyCallBack() function and the parameter $result is passed. In the NotifyCallBack function, our rewritten NotifyProcess() function will be called (this function is rewritten in notify.php)

NotifyProcess( ) If there is no problem, it will set the xml information to return success

[php] view plain copy

$this->SetReturn_code("SUCCESS");  
$this->SetReturn_msg("OK");
Copy after login

and finally call the function this−>ReplyNotify(this−>ReplyNotify(needSign) ; echo success result

Function ReplyNotify needs to modify one code:

[php] view plain copy

final private function ReplyNotify($needSign = true)  
    {  
        //如果需要签名  
        if($needSign == true &&   
            $this->GetReturn_code($return_code) == "SUCCESS")  
        {  
            $this->SetSign();  
        }  
        WxpayApi::replyNotify($this->ToXml());  
    }
Copy after login

[php] view plain copy

$this->GetReturn_code($return_code) == "SUCCESS")
Copy after login

Change to

[php] view plain copy

$this->GetReturn_code() == "SUCCESS")
Copy after login


Then modify the order status based on the returned information, mainly where to modify, I am notifying. Create a new method

//修改订单状态
    public function updateState($data){        if($data){
            $order_sn = $data[&#39;out_trade_no&#39;];\

            $data = array();
            $data[&#39;order_id&#39;] = $order_id;
            //修改订单状态(用curlpost方法请求至thinkphp目录下的Controller里面控制器里面的方法,修改状态)
            $url = &#39;www.test.com&#39;;
            header(&#39;content-type:text/html;charset=utf8&#39;);
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            $result = curl_exec($curl);
            curl_close($curl);            if($result == &#39;success&#39;){                return true;
            }else{                return false;
            }

        }
    }
Copy after login

in php and add

under

$notify = new PayNotifyCallBack();
$notify->Handle(false);
Copy after login

in notify.php
//接受参数,修改状态
$xml = file_get_contents("php://input");
$data = json_decode(json_encode(simplexml_load_string($xml, &#39;SimpleXMLElement&#39;, LIBXML_NOCDATA)), true);
$notify->updateState($data);
Copy after login

相关推荐:

PHP后台接口的一些问题

5 分钟实现微信支付接入教程

php初学者如何学习实现微信支付和支付宝支付的相关教程

The above is the detailed content of Detailed explanation of the php background interface of App WeChat Payment. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles