WeChat Pay offers an enterprise payment to wallet interface that allows merchants to transfer funds directly to users' WeChat Wallet balances programmatically. This feature is commonly used for user withdrawals, reward distribution, and similar scenarios. This article explains the complete integration process using PHP.
Before development, complete the following preparation steps:
Visit the WeChat Pay Open Platform to register and verify your merchant account.
Log into the merchant platform, go to the account center, and set your API key. Keep this key secure and do not disclose it.
Download the PHP SDK from the WeChat Pay developer documentation and include it in your project directory to prepare for API calls.
Include the WeChat payment SDK file in your PHP project as follows:
require_once 'path/to/wxpay.sdk.php';
Before making API calls, configure the merchant information as shown:
$config = new WxPayConfig();
$config->SetMerchantId('your_merchant_id');
$config->SetAppId('your_app_id');
$config->SetAppKey('your_app_key');
Replace the placeholders with your actual merchant ID, AppID, and API key.
After configuration, use the following PHP code to initiate an enterprise payment to a user's WeChat Wallet:
$input = new WxPayEnterprisePay();
$input->SetOpenId('user_openid');
$input->SetAmount(100); // Amount in cents
$input->SetDesc('Enterprise Payment');
$result = WxPayApi::enterprisePay($config, $input);
if ($result['success']) {
// Payment succeeded, proceed with business logic
} else {
// Payment failed, handle failure
}
Here, SetOpenId sets the recipient's WeChat OpenID, SetAmount specifies the payment amount in cents, and SetDesc provides the payment description.
This guide has walked you through the basic process of implementing WeChat enterprise payment to wallet using PHP. From preparation to coding, each step is essential. After development, conduct thorough testing in the production environment to ensure fund security and a smooth user experience.