search
HomeBackend DevelopmentPHP TutorialDetailed 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
phpinterfaceDetailed 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;
    }
  • //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'];

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;

$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;
        }

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);

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);  
        }

Main code:
[php] view plain copy

    $result = WxpayApi::notify(array($this, 'NotifyCallBack'), $msg);

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);  
        }

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);

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");

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());  
    }

[php] view plain copy

$this->GetReturn_code($return_code) == "SUCCESS")

Change to

[php] view plain copy

$this->GetReturn_code() == "SUCCESS")


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;
            }

        }
    }

in php and add

under

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

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);

相关推荐:

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
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment