Home Backend Development PHP Tutorial PHP and EasyWeChat: Practical Development of WeChat Mini Program Payment Function

PHP and EasyWeChat: Practical Development of WeChat Mini Program Payment Function

Jul 19, 2023 am 09:15 AM
Payment function easywechat Development practice

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:

  1. WeChat payment merchant number (mch_id)
  2. WeChat payment merchant secret Key
  3. App ID (appid) and App Secret (appsecret) of WeChat Pay
  4. 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
Copy after login

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',
    ],
];
Copy after login

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.

  1. 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.
  2. 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 login

    Replace USER_OPENID, YOUR_ORDER_ID, ORDER_TOTAL_FEE and PAYMENT_DESCRIPTION with actual values.

  3. 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 login

    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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Teach you to use EasyWeChat and PHP to build the voting function of WeChat mini program Teach you to use EasyWeChat and PHP to build the voting function of WeChat mini program Jul 18, 2023 am 09:53 AM

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 WeChat Payment Function Implementation Guide for Developing WeChat Mini Programs with EasyWeChat and PHP Jul 18, 2023 pm 03:12 PM

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

Use EasyWeChat and PHP to develop e-commerce functions of WeChat mini programs Use EasyWeChat and PHP to develop e-commerce functions of WeChat mini programs Jul 19, 2023 am 09:31 AM

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

Community function implementation skills for developing WeChat mini programs with EasyWeChat and PHP Community function implementation skills for developing WeChat mini programs with EasyWeChat and PHP Jul 18, 2023 pm 09:39 PM

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

Implementation Guide for File Upload and Download Functions in WeChat Mini Programs Developed with EasyWeChat and PHP Implementation Guide for File Upload and Download Functions in WeChat Mini Programs Developed with EasyWeChat and PHP Jul 18, 2023 pm 04:21 PM

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 Design and development guide for UniApp to realize the connection between payment function and payment interface Jul 04, 2023 pm 03:22 PM

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 PHP development of WeChat applet: EasyWeChat realizes data synchronization and backup functions Jul 18, 2023 pm 09:54 PM

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 for implementing accounting payments and settlements How to handle payment and settlement functions in accounting systems - Development methods for implementing accounting payments and settlements Sep 24, 2023 am 09:21 AM

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.

See all articles