Home  >  Article  >  Backend Development  >  PHP study notes: Alipay and WeChat payment integration

PHP study notes: Alipay and WeChat payment integration

PHPz
PHPzOriginal
2023-10-08 10:29:00735browse

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:

  1. Apply for an Alipay developer account
    Before starting, you first need to apply for an Alipay developer account. Register and apply through the Alipay Developer Platform (https://open.alipay.com/) to obtain a developer account and corresponding application key.
  2. Download Alipay SDK
    Alipay provides the official PHP SDK, which can be downloaded and referenced through GitHub (https://github.com/alipay/alipay-sdk-PHP).
  3. Introducing Alipay SDK files
    Extract the downloaded Alipay SDK files and introduce them into the project, making sure that the following files exist:
  4. AopSdk.php: The core file of Alipay SDK
  5. AopCertification.php: Certificate class file
  6. AopClient.php: Request class file
  7. Create Alipay instance
    Create an Alipay instance in the code and set the corresponding developer account and application Key:

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;

  1. Initiate a payment request
    Use the method in the Alipay instance to initiate a payment request. The following is a simple example to create an order for Alipay PC web 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'); // 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;

  1. Processing payment result notification
    After the Alipay payment is successful, the payment result will be sent to the merchant's backend through asynchronous notification. Code needs to be written in the background to handle Alipay's asynchronous notifications. The following is a simple code example:

$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:

  1. Apply for a WeChat developer account
    Before you start, you first need to apply for a WeChat developer account through the WeChat open platform (https://open .weixin.qq.com/) to register and apply, and obtain a developer account and corresponding application ID, merchant number and API key.
  2. Introducing WeChat Pay SDK files
    WeChat Pay provides the official PHP SDK, which can be downloaded and referenced through GitHub (https://github.com/wxpay/wechatpay-gysp-php).
  3. Create a WeChat payment instance
    Create a WeChat payment instance in the code and set the corresponding developer account and API key:

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);

  1. Initiate a payment request
    Use the method in the WeChat payment instance to initiate a payment request. The following is a simple example to create an order for payment through a WeChat official account:

$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']);

  1. Processing payment result notification
    After the WeChat payment is successful, the payment result will be sent to the merchant's backend through asynchronous notification. Code needs to be written in the background to handle asynchronous notifications for WeChat Pay. Here is a simple code example:

$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn