Current Location: Home> Latest Articles> PHP Payment Integration Guide: Complete Implementation of Alipay and WeChat Pay

PHP Payment Integration Guide: Complete Implementation of Alipay and WeChat Pay

M66 2025-06-22

PHP Study Notes: Integration of Alipay and WeChat Pay

With the popularity of mobile payments and increasing user demand, Alipay and WeChat Pay have become the most mainstream online payment methods. Integrating these two payment options is essential when developing e-commerce platforms or online services. This article focuses on how to integrate Alipay and WeChat Pay using PHP and provides detailed code examples to help developers efficiently implement payment functions.

1. Alipay Payment Integration

Alipay is the most widely used third-party payment platform in China, offering comprehensive development interfaces and extensive documentation support. Below are the main steps for integrating Alipay payment with PHP:

  1. Apply for an Alipay developer account First, register a developer account on the Alipay open platform to obtain the application ID and keys.
  2. Download the Alipay PHP SDK The official SDK is available on GitHub and should be downloaded and included in the project.
  3. Include SDK core files Make sure the project contains these files:
    - AopSdk.php
    - AopCertification.php
    - AopClient.php
  4. Create an Alipay client instance Configure the application ID and keys in your code and initialize the Alipay client:
require_once 'path/to/AopSdk.php';

$appId = 'your_app_id'; // Replace with actual app ID
$privateKey = 'your_private_key'; // Replace with actual private key
$publicKey = 'alipay_public_key'; // Replace with Alipay public key

$alipay = new AopClient();
$alipay->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
$alipay->appId = $appId;
$alipay->rsaPrivateKey = $privateKey;
$alipay->alipayrsaPublicKey = $publicKey;
  1. Initiate payment request Use the Alipay client to create an order and initiate payment:
$bizContent = [
  'subject' => 'Test Order',
  'out_trade_no' => 'test_order_20220101',
  'total_amount' => '0.01',
  'product_code' => 'FAST_INSTANT_TRADE_PAY',
];

$request = new AlipayTradePagePayRequest();
$request->setNotifyUrl('your_notify_url');  // Async notification URL
$request->setReturnUrl('your_return_url');  // Sync redirect URL
$request->setBizContent(json_encode($bizContent, JSON_UNESCAPED_UNICODE));

$response = $alipay->pageExecute($request);
echo $response;
  1. Handle payment result notification After a successful payment, Alipay sends asynchronous notifications to your backend. You need to implement logic to verify and handle these notifications:
$alipayPublicKey = 'alipay_public_key'; // Alipay public key
$verifyResult = $alipay->rsaCheckV1($_POST, $alipayPublicKey, 'RSA2');

if ($verifyResult) {
  // Verification succeeded, process order logic
  echo 'success';
} else {
  // Verification failed
  echo 'fail';
}

2. WeChat Pay Integration

WeChat Pay, a Tencent payment platform, is especially popular on mobile devices. Below is the process for integrating WeChat Pay using PHP:

  1. Apply for a WeChat developer account Register on the WeChat open platform to obtain the app ID, merchant ID, and API key.
  2. Include the WeChat Pay PHP SDK Download the official SDK from GitHub and include it in your project.
  3. Create a WeChat Pay client instance Configure app details and initialize the WeChat Pay client:
require_once 'path/to/WxPay.Api.php';

$appId = 'your_app_id';         // App ID
$merchantId = 'your_merchant_id'; // Merchant ID
$apiKey = 'your_api_key';       // API key

$wxPayConfig = new WxPayConfig();
$wxPayConfig->SetAppId($appId);
$wxPayConfig->SetMerchantId($merchantId);
$wxPayConfig->SetKey($apiKey);

$wxPay = new WxPayApi($wxPayConfig);
  1. Initiate payment request Use the WeChat Pay API to create a payment order, for example, for a WeChat public account payment:
$wxPayData = new WxPayUnifiedOrder();
$wxPayData->SetBody('Test Order');
$wxPayData->SetOutTradeNo('test_order_20220101');
$wxPayData->SetTotalFee(1); // Amount in cents
$wxPayData->SetTradeType('JSAPI');
$wxPayData->SetOpenId('your_open_id'); // User’s OpenID

$result = $wxPay->unifiedOrder($wxPayData);
$jsApiParameters = $wxPay->getJsApiParameters($result['prepay_id']);
  1. Handle payment result notification WeChat Pay sends asynchronous notifications upon payment success. The backend should verify and process the order status accordingly:
$wxPayNotify = new WxPayNotify($wxPayConfig);
$wxPayNotify->Handle(false);
$orderInfo = $wxPayNotify->GetPayOrder();

if ($orderInfo['return_code'] === 'SUCCESS' && $orderInfo['result_code'] === 'SUCCESS') {
  // Payment verification succeeded, process order
  $wxPayNotify->ReplyNotify('SUCCESS');
} else {
  // Verification failed, return failure notification
  $wxPayNotify->ReplyNotify('FAIL');
}

Conclusion

This article systematically introduces how to integrate Alipay and WeChat Pay using PHP, including account registration, SDK downloads, payment request creation, and payment result handling, with practical code examples. In real development, adjust the process according to business needs. We hope this guide helps you successfully develop and launch payment features.