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.
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:
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;
$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;
$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';
}
WeChat Pay, a Tencent payment platform, is especially popular on mobile devices. Below is the process for integrating WeChat Pay using PHP:
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);
$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']);
$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');
}
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.