WeChat Payment Function Implementation Guide for Developing WeChat Mini Programs with EasyWeChat and PHP

WBOY
Release: 2023-07-18 15:14:01
Original
2440 people have browsed it

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
Copy after login

After the installation is completed, introduce EasyWeChat into the project:

use EasyWeChatFactory;
Copy after login

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 a config directory in the root directory of the project, and create a wechat.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',
    ],
];
Copy after login

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 and key_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 {
    // 创建订单失败的处理逻辑
}
Copy after login

In the above code, the order->unify method is used to create an order, where body 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 obtain prepay_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 the wx.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) {
     // 支付失败的处理逻辑
  }
})
Copy after login

In the above code, timeStamp is the timestamp, nonceStr is a random string, package is prepay_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;
Copy after login

In the above code, the handlePaidNotify 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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!