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

WBOY
Release: 2023-07-18 15:14:01
Original
2416 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 aconfigdirectory in the root directory of the project, and create awechat.phpfile 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_idis the App ID of the WeChat applet,mch_idis the merchant number,keyis the merchant key,cert_pathandkey_pathare The path of the merchant certificate,notify_urlis 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, theorder->unifymethod is used to create an order, wherebodyis the product description,out_trade_nois the merchant order number,total_feeis the total order amount,spbill_create_ipis the user’s IP address,trade_typeis the payment method,openidis the user's openid. If the order is created successfully, you can obtainprepay_idand 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 thewx.requestPaymentmethod 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,timeStampis the timestamp,nonceStris a random string,packageisprepay_id=prepay_id,signTypeis the signature type,paySignis 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, thehandlePaidNotifymethod is used to process payment result notifications, where the$notifyparameter is WeChat The content of the payment notification, the$successfulparameter 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
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!