


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:
- WeChat payment merchant number (mch_id)
- WeChat payment merchant secret Key
- App ID (appid) and App Secret (appsecret) of WeChat Pay
- A legal certificate file
The above materials can be obtained by applying on the WeChat Pay Developer Platform.
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.
- Receive payment request
In your applet, after the user clicks the payment button, a payment request will be sent to the server. You need to write an interface for receiving payment requests and configure the URL of this interface to the background of the mini program. 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)); }
Copy after loginReplace 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();
Copy after loginReplace 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!

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



Teach you to use EasyWeChat and PHP to build the voting function of WeChat mini programs. Introduction: With the popularity of WeChat mini programs, more and more companies have begun to try to develop their own mini programs to interact with users. Among them, the voting function is a very common and interesting application scenario. This article will teach you how to use EasyWeChat and PHP to build the voting function of the WeChat applet and provide corresponding code examples. 1. Introduction to EasyWeChat EasyWeChat is a WeChat development based on PHP

WeChat Payment Function Implementation Guide for Developing WeChat Mini Programs with EasyWeChat and PHP In the current mobile Internet era, WeChat Payment 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 EasyWeChatEasyWe

Using EasyWeChat and PHP to develop e-commerce functions of WeChat mini programs. In recent years, with the rapid development of WeChat mini programs, more and more companies have begun to use them as an important channel for e-commerce. In order to realize the e-commerce function of WeChat applet, we can use EasyWeChat and PHP development tools to build a complete e-commerce platform. This article will introduce how to use EasyWeChat and PHP to develop the e-commerce function of WeChat applet, and provide some code samples for reference. Build environment

Tips for implementing community functions in developing WeChat mini programs with EasyWeChat and PHP. With the continuous development of WeChat mini programs, more and more companies and developers are beginning to pay attention to and use WeChat mini programs. WeChat mini programs provide rich development interfaces and functions, allowing developers to easily build a variety of applications. Among them, the community function is a very common and important function in WeChat mini programs. It allows users to communicate, share and interact, improving user experience and stickiness. This article will introduce how to use EasyWeCha

EasyWeChat (ECW for short) is a WeChat development toolkit based on PHP. It provides developers with a series of convenient API interfaces for developing WeChat public accounts, WeChat applets and other applications. In this article, we will introduce how to use EasyWeChat and PHP to develop the file upload and download functions of WeChat applet. First, we need to configure the relevant information of the mini program in EasyWeChat and obtain the appID and appSecret of the mini program. The specific configuration method can be

Design and development guide for UniApp to realize the connection between payment function and payment interface 1. Introduction With the rapid development of mobile payment, payment function has become one of the necessary functions in mobile application development. UniApp is a cross-platform application development framework that supports writing once and publishing on multiple platforms, and can efficiently implement payment functions. This article will introduce how to implement the payment function in UniApp and connect it with the payment interface. 2. Design and Development of Payment Function 1. Preparation Before starting, please make sure that you have completed the following preparations

PHP development of WeChat applet: EasyWeChat realizes data synchronization and backup functions WeChat applet has become an important part of the mobile Internet field, and more and more developers are beginning to pay attention to and use it. In the process of developing WeChat applet, data synchronization and backup is a very important function. In this article, we will introduce how to use PHP to develop WeChat applet and use the EasyWeChat library to implement data synchronization and backup functions. 1. Introduction to EasyWeChatEasyWeCh

How to handle payment and settlement functions in accounting systems - development methods to implement accounting payment and settlement, specific code examples are required. With the development of Internet technology and cloud computing, accounting systems have become an indispensable and important tool for many enterprises. In a complete accounting system, payment and settlement are two core functions, and the development methods and specific code examples to achieve these two functions will be discussed in detail in this article. 1. Payment function development method To realize the payment function in the accounting system, it first needs to be connected with the payment platform to ensure smooth payment.
