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

王林
Release: 2023-07-19 09:16:02
Original
1697 people have browsed it

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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template