Home  >  Article  >  Backend Development  >  Alipay Development Guide in PHP

Alipay Development Guide in PHP

WBOY
WBOYOriginal
2023-05-23 08:01:351189browse

In modern society, Alipay has become an electronic payment tool used by more and more people in their daily lives. The first thing to understand is that Alipay’s payment function is implemented through the API interface. In PHP, if you want to use Alipay API, you need to use the SDK provided by Alipay for development. This article will introduce the Alipay development guide in PHP.

1. Download Alipay SDK

To start using Alipay SDK, you need to first go to the official website of Alipay Open Platform for registration and certification. After the certification is completed, you can download the Alipay SDK in the "Developer Center". It includes files such as interface parameter definitions, tool classes, sample codes, etc. You can choose different SDK versions according to your own needs.

2. Initial configuration

After the download is completed, you need to configure it in your own PHP project. First, you need to import the SDK file into the project and introduce the corresponding class library in the project configuration file.

Next, Alipay configuration needs to be initialized, including application ID, private key, public key, gateway and other information. The initialization code is as follows:

<?php
$config = [
    'app_id' => '支付宝应用ID',
    'merchant_private_key' => '应用私钥',
    'alipay_public_key' => '支付宝公钥',
    'gatewayUrl' => 'https://openapi.alipay.com/gateway.do',
    'charset' => 'UTF-8',
    'sign_type' => 'RSA2',
];

The application ID, private key, and public key in the above configuration information can be obtained in the Developer Center of the Alipay Open Platform.

3. Initiate a payment request

Alipay SDK provides a variety of payment interfaces, the most commonly used of which are computer website payment and mobile payment. The following takes computer website payment as an example to introduce how to initiate a payment request.

First, you need to construct an array of Alipay payment request parameters, including merchant order number, total order amount, product name, callback address and other information. The construction code is as follows:

<?php
$requestConfigs = [
    'out_trade_no' => '商户订单号',
    'total_amount' => '订单总金额',
    'subject' => '商品名称',
    'return_url' => '回调地址',
    'notify_url' =>'异步通知地址',
];

Among them, 'out_trade_no' is the merchant's own order number, and 'total_amount' is the total amount of the order, in yuan. These parameters need to be filled in by the merchants themselves according to their business needs.

Next, you need to instantiate the Alipay SDK class and pass in the initialization configuration and construction parameter array. The sample code is as follows:

<?php
require_once './vendor/autoload.php';

use AlipayEasySDKFactory;

$config = [
    'app_id' => '支付宝应用ID',
    'merchant_private_key' => '应用私钥',
    'alipay_public_key' => '支付宝公钥',
    'gatewayUrl' => 'https://openapi.alipay.com/gateway.do',
    'charset' => 'UTF-8',
    'sign_type' => 'RSA2',
];
$pay = Factory::payment($config);
$order = $pay->preCreate($requestConfigs);

Among them, 'preCreate' is the method in Alipay SDK to initiate a computer website payment request, and the returned $order is all the information returned by Alipay, including payment QR code, etc.

Finally, the returned information is displayed to the user, allowing the user to scan the Alipay QR code to complete the payment.

4. Processing payment results

Alipay SDK supports two methods of synchronous notification and asynchronous notification to process payment results. Synchronous notification means that after the user pays, the Alipay page will automatically jump to the merchant's customized page. The merchant can display the payment results on this page, and then the user can confirm the payment results. Asynchronous notification means that the Alipay server sends a notification of the payment result to the merchant server immediately after the payment is completed. The merchant can promptly update the order status and other information after receiving the notification.

For synchronous notification, return_url needs to be added to the request parameters to specify which page the user will jump to after successful payment. For asynchronous notification, notify_url needs to be added to the payment request parameters to specify the payment result notification address. After receiving the payment result notification, you need to verify the validity of the payment result and process the order and other related information.

5. Summary

This article introduces the steps of using Alipay SDK for payment development in PHP. Through steps such as initializing configuration, constructing payment request parameters, initiating payment requests, and processing payment results, you can quickly access the Alipay payment function in your own PHP project. At the same time, during the development process using Alipay SDK, you need to pay attention to protecting the application private key and Alipay public key to avoid leakage to illegal channels.

The above is the detailed content of Alipay Development Guide 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