Home > Article > Backend Development > PHP study notes: Alipay and WeChat payment integration
PHP study notes: Integration of Alipay and WeChat payment requires specific code examples
Introduction:
With the popularity of mobile payment and the increase in user demand, Alipay and WeChat Pay have become the most common online payment methods. When developing projects such as online shopping malls and online payment platforms, the integration of Alipay and WeChat Pay is an essential part. This article will focus on how to use PHP language to integrate Alipay and WeChat Pay, and provide specific code examples.
1. Alipay payment integration
Alipay payment is currently the most widely used third-party payment platform in China, and it provides a wealth of interfaces and development documents. The following will introduce how to use PHP to integrate Alipay payment:
require_once 'path/to/AopSdk.php';
$appId = 'your_app_id'; // Replace with actual app ID
$privateKey = 'your_private_key'; // Replace with the actual application private key
$publicKey = 'alipay_public_key'; // Replace with the actual 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'); // Replace with the actual asynchronous notification address
$request->setReturnUrl('your_return_url'); // Replace with the actual synchronous jump Address
$request->setBizContent(json_encode($bizContent, JSON_UNESCAPED_UNICODE));
$response = $alipay->pageExecute($request);
echo $response;
$alipayPublicKey = 'alipay_public_key'; // Replace with the actual Alipay public key
$verifyResult = $alipay->rsaCheckV1($_POST, $ alipayPublicKey, 'RSA2');
if ($verifyResult) {
// Verification successful, order processing logic
// TODO: Order processing logic
echo 'success';
} else {
// Verification failed, output error message
echo 'fail';
}
2. WeChat payment integration
WeChat payment is an online payment platform owned by Tencent. Compared with Alipay payment, WeChat payment is more popular among mobile users. The following will introduce how to use PHP to integrate WeChat payment:
require_once 'path/to/WxPay. Api.php';
$appId = 'your_app_id'; // Replace with the actual application ID
$merchantId = 'your_merchant_id'; // Replace with the actual merchant number
$apiKey = 'your_api_key'; // Replace with actual 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);
$wxPayData->SetTradeType('JSAPI');
$wxPayData-> SetOpenId('your_open_id'); // Replace with actual user OpenID
$result = $wxPay->unifiedOrder($wxPayData);
$jsApiParameters = $wxPay->getJsApiParameters($result ['prepay_id']);
$wxPayNotify = new WxPayNotify($wxPayConfig);
$wxPayNotify->Handle(false);
$orderInfo = $wxPayNotify-> ;GetPayOrder();
// Verify payment result
if ($orderInfo['return_code'] === 'SUCCESS' && $orderInfo['result_code'] === 'SUCCESS') {
// Verification successful, processing order logic
// TODO: Processing order logic
$wxPayNotify->ReplyNotify('SUCCESS');
} else {
// Verification failed, Output error message
$wxPayNotify->ReplyNotify('FAIL');
}
Conclusion:
This article briefly introduces how to use PHP language to integrate Alipay and WeChat payment, and provides Specific code examples. In actual development, corresponding adjustments and expansions need to be made according to specific needs and business logic. I hope this article has helped you integrate Alipay and WeChat Pay. If you have any questions, please leave a message for discussion.
The above is the detailed content of PHP study notes: Alipay and WeChat payment integration. For more information, please follow other related articles on the PHP Chinese website!