


About the WeChat public account payment implementation code of WeChat payment PHP SDK
This article mainly introduces the relevant information about the WeChat public account payment implementation code of WeChat payment PHP SDK. Friends in need can refer to it
It is assumed that you have already applied for WeChat payment
1. WeChat background configuration As shown in the figure
We will test first, so first put the test authorization directory and Test whitelist added. The test authorization directory is the directory where the file you want to initiate a WeChat request is located.
For examplejsapi The request is usually made to the directory where jsapi.php is located, which is the test directory. The test whitelist is the developer's WeChat ID.
The official payment authorization directory cannot be the same as the one tested, otherwise an error will be reported. Failure to fill in or fill in the wrong authorization directory and test whitelist will result in an error.
Error report example:
NaNsystem:access_denied
2. Configure the lib/WxPay.Config.php file
The most important configuration is the following four items:const MCHID = '';
const
KEY = '';const APPSECRET = '';
APPID and APPSECRET can both be found in the WeChat backend .
MCHID can be found in the email sent after applying for WeChat payment. KEY can be configured on the merchant platform according to the email prompt
3. Access the starting index.php
First access index.php and you can see the interface1 2 3 4 5 6 7 8 9 |
|
4. JSAPI payment
Necessary code analysis:1 2 |
|
Log class You can print debugging information through $log->DEBUG('test');. In fact, you can also use $Log::DEBUG('test'); directly to debug
1 2 |
|
Function Defined in the file WxPay.JsApiPay.php file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
interface.
After obtaining the Openid, you can call the unified ordering interface of WeChat Pay. Go back to the file jsapi.php and the following code1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
##
1 |
|
If you change the value to $input->SetAttach("test this is attach"); There will be a bug. I will talk about it later. In fact, this parameter is not necessary and can be simply removed.
Code:
1 |
|
is the Url set to receive payment result notifications. Here is the default demo link. We can set it to ours:
1 |
|
当然你也可以选择直接写死。
其中的函数 unifiedOrder($input) 可以到WxPay.Api.php 中文件跟踪,其实就是调用统一下单接口。
在 WxPay.Api.php 中需要更改的一处代码是:
1 2 3 4 |
|
就是当没设置 notifyUrl 的时候回去配置文件中找,但是配置文件中根本没有设置。
所以你可以选择在 配置文件WxPay.Config.php 中加上这个配置,也可以直接写一个默认的notify链接。
函数 GetJsApiParameters() 是获取jsApi支付的参数给变量 $jsApiParameters 方便在下面的Js中调用
jsapi.php 中js的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
其中点击立即支付按钮调用的就是 callpay() 函数,他有会调用jsApiCall() 函数打开支付程序。
此后输入密码完成支付。
在完成支付页面点击完成会回到这个支付页面,并弹出 支付成功的提示框
这个其实就是 js函数 jsApiCall 里面的alter 弹出的对话框
其中 res.err_msg 为get_brand_wcpay_request:ok 表明前端判断的支付成功,我们可以根据这个将支付跳转到成功页面。
但是这个并不可信。确认是否支付成功还是应当 通过notify.php 处理业务逻辑。
5. 支付结果通知 notify.php
其实这个页面最主要的代码就两行
1 2 |
|
其中大部分逻辑在 Handle 函数中处理 文件 WxPay.Notify.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
主要代码:
1 |
|
跟踪函数 notify 文件WxPay.Api.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
通过 $GLOBALS[‘HTTP_RAW_POST_DATA‘]; 获取同志数据 然后 Init 函数验证签名等。验签成功运行代码
1 |
|
即调用了一个回调函数,NotifyCallBack() 函数并传递参数 $result 在NotifyCallBack函数中会调用我们重写的NotifyProcess()函数(此函数在notify.php 中被重写)
NotifyProcess() 判断也没有问题就会 设置返回 success的xml信息
1 2 |
|
并最终调用函数 $this->ReplyNotify($needSign); echo success的结果
函数ReplyNotify 需要修改一处代码:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
改为
1 |
|
即可。
这样整个流程就结束了。上面提到了 传递订单参数
1 |
|
如果我设置 值为 test this is attach (其实只要有空格就会存在bug)
如图 传递的订单信息
可以看到 attach 信息正常,当然支付也是正常的没有任何问题。
但是发现总是会收到notify 通知,即意味着没有返回给微信服务器正确的结果通知。
打印服务器发来的通知数据
可以看到 attach 是 test+this+is+attach 即空格被转化为加号
打印接收到的签名和程序算出来的签名发现 签名不同,即认为接收结果异常。
所以我们要是想使用attach 这个值就不能有空格,要么干脆不使用这个参数
(等待微信修复这个bug, 也可能是我这边有哪个地方不会? - -#)
这样 微信支付的 JsApi支付就大致分析完成了。
The above is the detailed content of About the WeChat public account payment implementation code of WeChat payment PHP SDK. 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

AI Hentai Generator
Generate AI Hentai for free.

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

