Table of Contents
浅谈使用PHP开发微信支付的流程,浅谈php支付流程
Home php教程 php手册 浅谈使用PHP开发微信支付的流程,浅谈php支付流程

浅谈使用PHP开发微信支付的流程,浅谈php支付流程

Jun 13, 2016 am 08:53 AM
php WeChat Pay

浅谈使用PHP开发微信支付的流程,浅谈php支付流程

下面以PHP语言为例,对微信支付的开发流程进行一下说明。

1.获取订单信息

2.根据订单信息和支付相关的账号生成sign,并且生成支付参数

3.将支付参数信息POST到微信服务器,获取返回信息

4.根据返回信息生成相应的支付代码(微信内部)或是支付二维码(非微信内),完成支付。

下面分步骤的讲一下:

1.微信支付中相关的必须的订单参数有三个,分别是:body(商品名或订单描述),out_trade_no(一般为订单号)和total_fee(订单金额,单位“分”,要注意单位问题),在不同的应用中,首先要做的就是获取订单中的相关信息,为支付参数生成做准备。

2.其他必须的支付参数有 appid(微信appid),mch_id(申请成功后告知),device_info(web端和微信端该参数都是统一的,为大写的”WEB“),trade_type(根据使用场景不同,该值也是不同的,微信外部为”NATIVE“,微信内部为”JSAPI“),nonce_str(32位随机字符串),spbill_create_ip(发起支付的终端IP,即服务器IP),notify_url(支付回调地址,微信服务器通知网站支付完成与否,修改订单状态),sign(签名),还有一个需要说明的地方,如果trade_type为JSAPI的话,openid为必填的参数。

签名算法是比较容易出错的地方,在于签名步骤繁琐,其实很关键的是,sign不参与签名

A:将1、2中提到的除sign外的参数赋值,放到一个数组array里面,按照字典顺序排序,其实就是键值按照A—Z的顺序进行排序。

B:将数组转换成字符串string,格式为 k1=v1&k2=v2&...kN=vN

C:在此string后加上KEY值(在微信支付商户后台用户自己设定的)现在string = k1=v1&k2=v2&...kN=vN&key=KEY。

D:string = md5(string)

E: sign = strtoupper(string)

至此,sign生成完毕。

将sign添加到array数组里面生成新的数组。将该数组转换为XML。至此,微信支付的参数准备工作完成。

3.将2中生成的XML,使用POST的方式发送请求到微信(https://api.mch.weixin.qq.com/pay/unifiedorder),获取返回的XML信息,将该信息转换成数组格式方便操作。返回的XML信息如下:

<xml>
 <return_code><![CDATA[SUCCESS]]></return_code>
 <return_msg><![CDATA[OK]]></return_msg>
 <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
 <mch_id><![CDATA[10000100]]></mch_id>
 <nonce_str><![CDATA[IITRi8Iabbblz1Jc]]></nonce_str>
 <sign><![CDATA[7921E432F65EB8ED0CE9755F0E86D72F]]></sign>
 <result_code><![CDATA[SUCCESS]]></result_code>
 <prepay_id><![CDATA[wx201411101639507cbf6ffd8b0779950874]]></prepay_id>
 <trade_type><![CDATA[JSAPI]]></trade_type>
</xml> 

Copy after login


如果是trade_type==native支付的话,还会多一个参数code_url,该URL为微信扫码支付的地址。

4.下面就是支付的过程了。

如果trade_type==native,那么使用一些方式将code_url转换成二维码,使用微信扫码就可以了,如果是微信内部点击支付的话,需要调用微信js-sdk中的相关东西,这一步中最关键是生成一个json格式的字符串。

首先要生成转换json字符串的数组array_jsapi。

A:该数组的参数包括:appId,timeStamp,nonceStr,package,signType(默认为”MD5“),要注意大小写和上面的数组里面是不一样的。

B:使用该数组生成paySign参数,签名方式同上。

C:将paySign参数追加到array_jsapi数组中。

D:将该数组使用json_encode格式化为字符串js_string。

完成上面的工作,就可以在微信内部进行支付了。

下面为相关支付的示例代码:

<script type='text/javascript'>
         function jsApiCall()
     {
      WeixinJSBridge.invoke(
       'getBrandWCPayRequest',
       $js_string,
       function(res){
        WeixinJSBridge.log(res.err_msg);
         if(res.err_msg=='get_brand_wcpay_request:ok')
         {
          alert('支付成功');
         }
         else
         {
          alert('支付失败');
         }
       }
      );
     }
     function callpay()
     {
      if (typeof WeixinJSBridge == 'undefined'){
       if( document.addEventListener ){
        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
       }else if (document.attachEvent){
        document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
       }
      }else{
       jsApiCall();
      }
     }
    </script> 



Copy after login

代码中js_string即为我们生成的字符串。

HTML代码中调用callpay()函数发起支付。

这样微信支付的支付工作就完成了。

下面是回调工作,该功能确保订单支付成功后,有正确的状态显示给用户。

支付完成后,微信使用POST请求,将支付结果反馈给网站服务器,网站服务器获取POST信息,根据支付成功与否,来确定是否修改订单信息。

A:将POST参数中的sign去除,并且记录下来该值。

B:对剩余的参数进行签名

C:将签名结果和POST中的sign进行比对,相同说明签名正确,根据支付结果修改订单状态。

E:返回XML信息给微信,确保微信知道网站已经收到该通知,避免微信再次推送POST,示例如下:

<xml>
 <return_code><![CDATA[SUCCESS]]></return_code>
 <return_msg><![CDATA[OK]]></return_msg>
</xml> 



Copy after login

如果失败,则返回

<xml>
 <return_code><![CDATA[FAIL]]></return_code>
 <return_msg><![CDATA[失败原因]]></return_msg>
</xml> 

Copy after login

至此,微信支付的整个开发介绍完毕。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

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

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

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

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