微信支付开发流程,信支付开发流程
微信支付开发流程,信支付开发流程
授人以鱼不如授人以渔
微信支付开发流程
下面以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信息如下:
1 2 3 4 5 6 7 8 9 10 11 |
|
如果是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。
完成上面的工作,就可以在微信内部进行支付了。
下面为相关支付的示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
代码中js_string即为我们生成的字符串。
HTML代码中调用callpay()函数发起支付。
这样微信支付的支付工作就完成了。
下面是回调工作,该功能确保订单支付成功后,有正确的状态显示给用户。
支付完成后,微信使用POST请求,将支付结果反馈给网站服务器,网站服务器获取POST信息,根据支付成功与否,来确定是否修改订单信息。
A:将POST参数中的sign去除,并且记录下来该值。
B:对剩余的参数进行签名
C:将签名结果和POST中的sign进行比对,相同说明签名正确,根据支付结果修改订单状态。
E:返回XML信息给微信,确保微信知道网站已经收到该通知,避免微信再次推送POST,示例如下:
1 2 3 4 |
|
如果失败,则返回
1 2 3 4 |
|
至此,微信支付的整个开发介绍完毕。
有任何疑问,请联系QQ:97695870

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 and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.
