SoFunction
Updated on 2025-03-09

Example code that integrates PHP with WeChat red envelope function

To integrate PHP with WeChat red envelope function, you can use the red envelope API provided by WeChat Pay to achieve it. Here are some steps for your reference:

Get WeChat Pay Merchant Number and API Key: First, you need to register with the WeChat Pay Merchant Platform and obtain the Merchant Number (MCHID) and API Key (API Key). These credentials will be used to connect to the WeChat payment interface.

  • Download the WeChat Pay SDK: Visit the official WeChat Pay website, download and import the PHP SDK of WeChat Pay. The SDK encapsulates communication and data processing functions with the WeChat payment interface, making it easier for you to develop.
  • Configure SDK and initialization: Unzip the downloaded WeChat Payment SDK file and introduce relevant files into your PHP code. Then, according to the document description, initialize the SDK, including setting the merchant number, API key and other information.
  • Build red envelope request parameters: Build the parameters of red envelope request according to your needs. These parameters include information such as the sender of the red envelope, the recipient of the red envelope, the red envelope blessings, etc.
  • Send red envelope request: Use the method provided by the SDK to send the constructed red envelope request parameters to the red envelope interface of WeChat Pay. You need to call the relevant method and pass the request parameters to the SDK.
  • Processing payment results: The WeChat payment interface will return the payment results of the red envelope. You can use the methods provided by the SDK to parse the returned results and process them accordingly. You can check the status of payment results, update database records, and other operations.

Please note that the above steps are only an overview, and the specific implementation steps will vary according to the WeChat payment version and SDK documentation you are using. Make sure to follow the specific steps provided by the SDK document to ensure the correct integration of WeChat red envelope functionality.

In addition, integrated WeChat payment involves sensitive issues such as funding and payment security. Please make sure that you have a full understanding of the payment process and relevant legal provisions and comply with relevant regulations. It is recommended to read the WeChat payment documentation and developer guide carefully to ensure proper use and security.

The following is a simple example code to use the WeChat Pay SDK to send WeChat red envelopes:

<?php
// Introduce WeChat Pay SDKrequire_once 'path/to/wechatpay-sdk-php/';

use WechatPay\GuzzleMiddleware\Util\PemUtil;
use WechatPay\GuzzleMiddleware\WechatPayMiddleware;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;

// WeChat Payment Configuration$merchantId = 'YOUR_MERCHANT_ID'; // Replace with your merchant number (MCHID)$apiKey = 'YOUR_API_KEY'; // Replace with your API key$certPath = 'path/to/apiclient_cert.pem'; // Replace with your certificate file path$keyPath = 'path/to/apiclient_key.pem'; // Replace with your private key file path
// Initialize WeChat Pay SDK$wechatPayMiddleware = WechatPayMiddleware::builder()
    ->withMerchant($merchantId, $apiKey)
    ->withCert(PemUtil::loadCertificate($certPath), PemUtil::loadPrivateKey($keyPath))
    ->build();

$stack = HandlerStack::create();
$stack->push($wechatPayMiddleware, 'wechatpay');

$client = new Client(['handler' => $stack]);

//Construct red envelope request parameters$data = [
    'mch_billno' => 'YOUR_BILL_NO', // Replace with your merchant order number    'send_name' => 'The name of the sender of the red envelope',
    're_openid' => 'RECEIVER_OPENID', // User openid replaced with red envelope recipient    'total_amount' => 100, // Replace with red envelope amount (unit: points)    'total_num' => 1, // Replace with the number of red packets sent    'wishing' => 'Blessings',
    'act_name' => 'Activity Name',
    'remark' => 'Remark',
];

// Send a red envelope request$response = $client->request('POST', '/mmpaymkttransfers/sendredpack', [
    'json' => $data,
]);

// Process payment results$result = json_decode($response->getBody(), true);

if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') {
    echo 'The red envelope was sent successfully';
} else {
    echo 'Red envelope failed to send:' . $result['return_msg'];
}
?>

Please note that the paths and parameters in the above example code need to be replaced according to actual conditions. Make sure to replace the placeholders in the sample code such as YOUR_MERCHANT_ID, YOUR_API_KEY, certPath, keyPath, YOUR_BILL_NO, and RECEIVER_OPENID with your actual values ​​to enable the code to run correctly.

This example uses the WeChat Pay SDK to handle communication with the WeChat Pay interface and uses the Guzzle HTTP client library to send requests. You need to install the Guzzle HTTP client library first and import the SDK-related files according to the actual situation.

The above is the detailed content of the example code that integrates PHP with WeChat red envelope function. For more information about the integration of PHP with WeChat red envelope function, please pay attention to my other related articles!