Home > Article > Backend Development > PHP and EasyWeChat: Practical Development of WeChat Mini Program Payment Function
PHP and EasyWeChat: Practical Development of WeChat Mini Program Payment Function
WeChat Payment is one of the most commonly used mobile payment methods, and WeChat Mini Program has become the choice of more and more enterprises and developers platform. Integrating payment functions into WeChat mini programs can bring more business opportunities and convenience to enterprises. This article will introduce how to use PHP and EasyWeChat to develop the payment function of WeChat mini program.
1. Preparation
Before starting development, we need to prepare the following materials:
2. Install EasyWeChat
EasyWeChat is a PHP-based WeChat development toolkit that can simplify the process of interacting with WeChat official accounts and mini programs. We can use Composer to install EasyWeChat and execute the following command:
composer require overtrue/wechat
3. Configure EasyWeChat
After the installation is complete, create a new file config.php in your project and configure EasyWeChat according to the following example:
<?php return [ 'payment' => [ 'sandbox' => false, 'app_id' => 'YOUR_APPID', 'mch_id' => 'YOUR_MCHID', 'key' => 'YOUR_KEY', 'cert_path' => 'CERT_PATH', 'key_path' => 'KEY_PATH', ], ];
Replace YOUR_APPID, YOUR_MCHID, YOUR_KEY with your actual values. CERT_PATH and KEY_PATH are the paths to your certificate files.
4. Implement the payment function
Using EasyWeChat to help us encapsulate the WeChat applet payment class, we can easily implement the payment function. This can be achieved by following the steps below.
Processing payment requests
After receiving the payment request, we can use the following code to process the payment request and return a prepayment information to the mini program for the mini program to call the WeChat payment interface to initiate payment :
<?php require 'vendor/autoload.php'; $config = require 'config.php'; use EasyWeChatFactory; $options = [ // ... ]; $app = Factory::miniProgram($options); $response = $app->payment->prepare([ 'openid' => 'USER_OPENID', 'out_trade_no' => 'YOUR_ORDER_ID', 'total_fee' => 'ORDER_TOTAL_FEE', 'body' => 'PAYMENT_DESCRIPTION', 'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], // ... 可选参数 ]); if ($response->return_code === 'SUCCESS' && $response->result_code === 'SUCCESS') { // 返回预支付信息给小程序 echo json_encode($app->payment->configForPayment($response->prepay_id)); }
Replace USER_OPENID, YOUR_ORDER_ID, ORDER_TOTAL_FEE and PAYMENT_DESCRIPTION with actual values.
Processing payment callbacks
After the user completes the payment, WeChat will call back to our server asynchronously. We need to write an interface to handle the callback of successful payment and perform corresponding business processing. . Here is the sample code:
<?php use EasyWeChatPaymentNotify; $options = [ // ... ]; $app = Factory::miniProgram($options); $payment = $app->payment; $notice = $payment->notify(); $notice->setAttr('sub_appid', 'SUB_APPID'); $notice->handle(function ($notify, $successful) { // 处理支付成功的业务逻辑 $outTradeNo = $notify->out_trade_no; // ... return true; // 返回 true 表示已处理完成,不会再异步通知 }); $response = $notice->reply(); $response->send();
Replace SUB_APPID with the App ID of your applet.
So far, we have completed the development of the WeChat mini program payment function. Through the packaging of EasyWeChat, we can easily implement the payment function, which greatly simplifies the development process. I hope this article will be helpful to you who are developing the payment function of WeChat mini program.
The above is the detailed content of PHP and EasyWeChat: Practical Development of WeChat Mini Program Payment Function. For more information, please follow other related articles on the PHP Chinese website!