Current Location: Home> Latest Articles> How to Implement WeChat QR Code Payment with ThinkPHP 5

How to Implement WeChat QR Code Payment with ThinkPHP 5

gitbox 2025-06-26

1. Introduction

WeChat Pay, as a convenient payment method, is widely used across e-commerce platforms and mobile apps. This article will walk you through how to implement WeChat QR code payment using the ThinkPHP 5 framework.

2. Preparation

2.1 Register a WeChat Open Platform Account

Before you start, you need to register a WeChat Open Platform account. Go to the WeChat Open Platform website, click the "Register" button, and complete the registration process by filling in the necessary details.

2.2 Create a Project

To use ThinkPHP 5, first create a project. Run the following command in the command line to create a project named "myproject":

composer create-project topthink/think myproject --prefer-dist

3. Configure WeChat Payment Parameters

To enable WeChat QR code payment, you need to configure the related payment parameters. Open the config folder in the root directory of your project, find and edit the wechatpay.php file, and add the following code:

return [
    // WeChat Official Account APPID
    'appid' => 'your_appid',
    // Merchant ID
    'mch_id' => 'your_mch_id',
    // WeChat Pay API Key
    'key' => 'your_api_key',
];

Replace your_appid, your_mch_id, and your_api_key with the actual values you obtained from the WeChat Open Platform.

4. Create a Payment Controller

Next, you need to create a controller to handle WeChat payment logic. Run the following command in the project root directory to generate a controller named "Pay":

php think make:controller Pay

Then, open the generated Pay controller file and add the following code:

namespace app\index\controller;
use think\Controller;
use think\Request;
use think\facade\Config;
<p>class Pay extends Controller<br>
{<br>
// Handle WeChat payment callback<br>
public function notify()<br>
{<br>
// Get WeChat payment configuration parameters<br>
$wechatpayConfig = Config::get('wechatpay');</p>
    // ...

    // Return result to WeChat
    return 'success';
}

}

5. Generate Payment QR Code

To allow users to make payments via QR code, you need to generate the QR code. Add the following method to the Pay controller:

public function scan()
{
    // Get WeChat payment configuration parameters
    $wechatpayConfig = Config::get('wechatpay');
// ...

}

You can generate the payment QR code by calling the WeChat Pay API. For more details, refer to the WeChat Pay developer documentation.

6. Complete the Payment Process

The final step is to display the payment QR code and allow users to complete the payment. In the scan method of the Pay controller, add the following code to render the payment page:

public function scan()
{
    // Get WeChat payment configuration parameters
    $wechatpayConfig = Config::get('wechatpay');
// ...

// Render payment page and display the QR code to the user
return $this->fetch('pay/scan', ['qrcode' => $qrcode]);

}

7. Conclusion

This article explains how to implement WeChat QR code payment with the ThinkPHP 5 framework. We covered the steps from registering a WeChat Open Platform account, configuring payment parameters, creating payment controllers, generating QR codes, to completing the payment process. With this knowledge, developers can easily integrate WeChat Pay into their applications.