EasyWeChat and PHP WeChat Mini Program Development WeChat Payment Function Implementation Guide
In the current mobile Internet era, WeChat Pay has become a very popular payment method. For developers who develop WeChat mini programs, implementing the WeChat payment function is a very important part. WeChat payment can bring better business value to the mini programs. This guide will introduce how to use EasyWeChat and PHP to develop the WeChat payment function of the WeChat applet.
1. Understanding EasyWeChat
EasyWeChat is an unofficial open source project based on the WeChat public platform. It provides a simple and easy-to-use PHP SDK that can easily interact with WeChat public accounts. It provides a series of interfaces, including user authentication, message management, template messages, user tags, etc. At the same time, EasyWeChat also supports WeChat payment function.
2. Apply for WeChat payment
Before using WeChat payment, you first need to apply for binding a merchant account on the WeChat payment platform. After the application is successful, you will get a merchant number and related keys. This information will be used for subsequent payment interface calls.
3. Install EasyWeChat
Before starting development, you first need to install EasyWeChat in your project. It can be installed through Composer, open the command line and enter the root directory of the project, run the following command:
composer require overtrue/wechat
After the installation is completed, introduce EasyWeChat into the project:
use EasyWeChatFactory;
4. Configure payment related parameters
Before starting to use EasyWeChat for payment function development, you need to configure the payment parameters first. Suppose we have aconfig
directory in the root directory of the project, and create awechat.php
file in this directory:
return [ 'mini_program' => [ 'app_id' => 'your-mini-program-app-id', 'mch_id' => 'your-merchant-id', 'key' => 'your-merchant-key', 'cert_path' => 'your-cert-path', 'key_path' => 'your-key-path', 'notify_url' => 'your-notify-url', ], ];
Among them,app_id
is the App ID of the WeChat applet,mch_id
is the merchant number,key
is the merchant key,cert_path
andkey_path
are The path of the merchant certificate,notify_url
is the URL of the payment result notification.
5. Create a payment order
Using EasyWeChat, you can easily create a WeChat payment order. The following is a sample code:
$payment = Factory::payment(config('wechat.mini_program')); $result = $payment->order->unify([ 'body' => '商品描述', 'out_trade_no' => '商户订单号', 'total_fee' => '订单总金额', 'spbill_create_ip' => request()->ip(), 'trade_type' => 'JSAPI', 'openid' => '用户的openid', ]); if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') { $prepayId = $result['prepay_id']; // JSAPI支付需要的参数 $config = $payment->jssdk->sdkConfig($prepayId); // 将$config返回给前端,用于调起微信支付 } else { // 创建订单失败的处理逻辑 }
In the above code, theorder->unify
method is used to create an order, wherebody
is the product description,out_trade_no
is the merchant order number,total_fee
is the total order amount,spbill_create_ip
is the user’s IP address,trade_type
is the payment method,openid
is the user's openid. If the order is created successfully, you can obtainprepay_id
and use it for subsequent payment operations.
6. Activating WeChat payment
It is very simple to activate WeChat payment in the front-end applet. You only need to call thewx.requestPayment
method where payment is required. Just pass in the parameters required for payment. The following is a sample code:
wx.requestPayment({ 'timeStamp': '时间戳', 'nonceStr': '随机字符串', 'package': 'prepay_id=prepay_id', 'signType': 'MD5', 'paySign': '签名', 'success': function (res) { // 支付成功的处理逻辑 }, 'fail': function (res) { // 支付失败的处理逻辑 } })
In the above code,timeStamp
is the timestamp,nonceStr
is a random string,package
isprepay_id=prepay_id
,signType
is the signature type,paySign
is the signature.
7. Processing payment result notification
After the user completes the payment, WeChat will notify the developer server of the payment result through an asynchronous notification. Developers need to ensure that the server can normally receive payment result notifications. The following is a sample code for processing payment result notifications:
$payment = Factory::payment(config('wechat.mini_program')); $response = $payment->handlePaidNotify(function ($notify, $successful) { // 处理支付结果通知的逻辑 return true; }); return $response;
In the above code, thehandlePaidNotify
method is used to process payment result notifications, where the$notify
parameter is WeChat The content of the payment notification, the$successful
parameter indicates whether the payment was successful. Developers need to implement their own business logic in this callback function, such as updating order status, etc.
Through the above steps, we can use EasyWeChat and PHP to develop the WeChat payment function of the WeChat applet. I hope this guide can help developers successfully implement the WeChat payment function.
The above is the detailed content of WeChat Payment Function Implementation Guide for Developing WeChat Mini Programs with EasyWeChat and PHP. For more information, please follow other related articles on the PHP Chinese website!