Home >Backend Development >PHP Tutorial >How to develop Alipay and WeChat Pay in PHP?

How to develop Alipay and WeChat Pay in PHP?

PHPz
PHPzOriginal
2023-05-12 19:51:351980browse

PHP is a popular server-side programming language that is widely used in Internet applications. In transaction scenarios such as e-commerce, Alipay and WeChat Pay are currently one of the most popular payment methods. This article will introduce how to use PHP to develop Alipay and WeChat Pay.

1. Development of Alipay payment

  1. Register a developer account

Register a developer account on the Alipay official website and complete real-name authentication before payment can be made payment and refund operations. After the registration is completed, you need to create an application in the developer center and obtain the appid, RSA private key, RSA public key and other information.

  1. Integrated Payment SDK

Developers can integrate payment functions into their own applications through the SDK provided by Alipay, which is relatively simple to use. Specific steps include:

1) Download and unzip Alipay SDK, and copy the "alipay" directory inside to the project;

2) Introduce the core class files of Alipay SDK into the project:

require_once(dirname(dirname(__FILE__))."/alipay/config.php");
require_once(dirname(dirname(__FILE__))."/alipay/pagepay/service/AlipayTradeService.php" );
require_once(dirname(dirname(__FILE__))."/alipay/pagepay/buildermodel/AlipayTradePagePayContentBuilder.php");

3) Construct payment request parameters:

$payRequestBuilder = new AlipayTradePagePayContentBuilder();
$payRequestBuilder->setBody("test");
$payRequestBuilder->setSubject("test");
$payRequestBuilder->setTotalAmount("0.01") ;
$payRequestBuilder->setOutTradeNo(date('YmdHis'));
$payRequestBuilder->setProductCode("FAST_INSTANT_TRADE_PAY");

4) Initiate a payment request:

$alipayService = new AlipayTradeService();
$result = $alipayService->pagePay($payRequestBuilder,$config['return_url'],$config['notify_url']);

5 ) Process payment results:

if($response->trade_status == 'TRADE_SUCCESS'){

echo "支付成功";

} else {

echo "支付失败";

}

2. Development of WeChat Pay

  1. Register a developer account

Register a developer account on the WeChat Pay official website, bind the official account or mini program, and complete the certification. After registration is completed, you need to create an application in the developer center and obtain appid, merchant number, API key and other information.

  1. Integrated payment SDK

Developers need to download and introduce the WeChat payment SDK. Here we take the v3 version of the interface as an example. The integration steps are as follows:

1) Download and unzip the SDK, copy the "lib" directory inside to the project, and introduce the core class library file:

require_once "lib/WxPay.Api.php ";
require_once "lib/WxPay.Notify.php";

2) Construct payment request parameters:

$input = new WxPayUnifiedOrder();
$input-> ;SetBody("test");
$input->SetAttach("test");
$input->SetOut_trade_no(date('YmdHis'));
$input->SetTotal_fee ("1");
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() 600));
$input->SetNotify_url("http://www.example.com/wxpay/notify.php");
$input->SetTrade_type("NATIVE");
$input->SetProduct_id ("123456789");

3) Call the unified order API:

$result = WxPayApi::unifiedOrder($input);
$code_url = $result["code_url" ];

4) Generate payment QR code:

require_once "phpqrcode.php";
QRcode::png($code_url, 'qrcode.png');

5) Process payment results:

$notify = new PayNotifyCallBack();
$notify->Handle(false);

The above is using PHP for Alipay and WeChat Payment is the basic step in development, but there are of course other payment methods and dozens of different development languages. For developers, they should choose the payment method and development language that suits them based on specific business needs to provide a better payment experience and service quality.

The above is the detailed content of How to develop Alipay and WeChat Pay in PHP?. 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