Home > Backend Development > PHP Tutorial > Implementation of php online payment function (Alipay)

Implementation of php online payment function (Alipay)

PHPz
Release: 2023-03-07 09:58:02
Original
7748 people have browsed it
In the process of implementing e-commerce websites, we often involve the payment function. Currently, the more popular third-party payments are Alipay and WeChat. The so-called third-party payments are those that have signed contracts with major banks and have A transaction support platform provided by a third-party independent institution with certain strength and credibility. In transactions through a third-party payment platform, after the buyer purchases the goods, he uses the account provided by the third-party platform to pay for the goods, and the third party notifies the seller of the arrival of the payment. This article takes Alipay access as a case.

1. Basic flow chart

Implementation of php online payment function (Alipay)

Payment process

2. Detailed step analysis:

( 1) The user initiates a request to confirm the order to the mall website (for example, click Buy Now)
(2) The mall website receives the request and saves the order data to the database or other storage media
(3) Returns to the order confirmation page, on the page The order amount and other information should be displayed (the order interface after clicking Buy Now)
(4) The user confirms the payment and initiates a payment request. Note: The payment request is sent to the payment gateway (such as Alipay, online banking) rather than to the mall website.
(5) Display the payment page (this interface is the same interface as Alipay)
(6) The user fills in the authentication information and submits it (scanning the QR code is relatively convenient)
(7) There are two steps here. One is After the deduction is successful, the page jumps to the payment result page (displayed to the user), and the other is the payment notification. These two steps may be executed at the same time in no particular order. After receiving the payment notification, the mall website verifies according to Verification Rules Validity of the information, and make corresponding changes (for example: if valid, change the order to paid status , if invalid, change the order to unpaid status).

Take Alipay as an example: If you want to integrate Alipayinterface into your website, you must first have an Alipay account, then apply for online payment business with Alipay and sign an agreement. After the agreement comes into effect, Alipay will give the website a partner ID and a Security verification code. With these two things, you can develop the Alipay interface according to the Alipay interface document, as shown in the steps above. Only steps 4 and 7 have information exchange between the mall and the payment gateway. In step 4, it means sending the data to the payment gateway (Alipay). In step 7, it is the notification verification part. The verification gateway requests a certain address of the website. The website verifies the information according to the verification rules, records and responds. We are developing almost any payment When it comes to the interface, the focus is on the development of these two parts. If you understand the principles of the payment interface, it will not be difficult to develop the payment interface. It should be emphasized that if we want to test the entire process, then we need to have a merchant account (signing account), which means we need to submit a business license and other materials to Alipay. After passing the Alipay review, we can proceed The account number for payment. (This is rarely available to individuals. You can apply for one in the name of your company during development)

3. Alipay interface access

InterfaceIntroduction and testing
Alipay is currently Several interfaces are provided, such as guaranteed transactions, standard instant payment, and dual functions. There are only some differences in functions, but the website integration method is the same. Taking the standard instant account interface as an example, after signing an agreement with Alipay (that is, after becoming an official merchant), several steps are needed to complete the integration. For each function, Alipay's developer platform has listed detailed steps and case studies. For information on how to sign a contract, please visit Alipay Verification

Let’s show you the business logic process of instant payment:

Step 1: Buyer chooses the product of his choice

Implementation of php online payment function (Alipay)

Step One: Click Buy Now

Step Two: Jump to the cashier page

1. Users can open the mobile wallet and select “Scan Scan", aim at the screen to scan the QR code, wait until the mobile phone prompts for payment, select the payment tool and enter the password to pay;

Implementation of php online payment function (Alipay)

Scan the code


2. If you don’t want to use mobile payment, you can click “Login Account Payment” on the right side of the page as shown above, enter your Alipay account and password to log in.

Implementation of php online payment function (Alipay)

Login account payment

Step 3: Buyer selects payment method

Implementation of php online payment function (Alipay)

Select payment method

Step 4: Payment successful

Implementation of php online payment function (Alipay)

Payment successful

Let’s show you the steps to access:

Step 1: Get the PID, the developer logs in to the open platform, click "Account and Key Management" in the upper right corner.

Implementation of php online payment function (Alipay)

Get PID

Step 2: Select "Partner Key" to Query to the partner Identity (PID), a 16-digit pure number starting with 2088.

Implementation of php online payment function (Alipay)

Key

Step 3: Configure the secret key

Please refer to Alipay DSA, RSA, MD5 Three signature methods, it is recommended to use the official tool provided by Alipay, click here to go.

Step 4: Access the project

(1) Download the official file (demo) and click to download.
(2) Configure the developer information in DEMO, find the alipay.config.php file in the directory and open it, the content is as follows:

Implementation of php online payment function (Alipay)

Configuration code


(3) Find the following parameters in config file for corresponding configuration:

Implementation of php online payment function (Alipay)

Parameters

Implementation of php online payment function (Alipay)

Find this part


(4) If you choose the MD5 signature method, please Configure according to the MD5 method: See step 3
(5) for details. Connect with our php code. We can see that there is another file named alipayapi.php. , this file is used to accept order information, and the order parameters are passed to this file in the form of post. This file has referencedalipay.config.php, so everyone’s basic configuration must be correct. When the parameters are successfully accepted, , will jump to the payment interface pre-written by Alipay.

Implementation of php online payment function (Alipay)

alipayapi.php file


(6) Accept payment results: configure synchronous return (synchronous jump notification) and asynchronous Callback (asynchronous notification). There are two parameters return_url and notification_url in the configuration file, which can be configured to the return_url page and notify_url page in DEMO respectively. Synchronous returns are obtained using the GET method, and asynchronous notifications are obtained using the POST method. There is a 1-minute timeout for synchronous return verification, and there is no time limit for asynchronous notification verification. Both the return_url page and the notify_url page will verify the data using the AlipayNotify.verify() signature verification method.
(7) Final judgment: judge by the trade_status field (transaction status) in the return parameter and write the execution business logic code.

Implementation of php online payment function (Alipay)

Screenshot 2016-12-15 16.30.28.png

Note: notify_url page can only return success, asynchronous notification There cannot be any HTML code on the page. Please refer to the asynchronous notification for payment results.

The above is the detailed content of Implementation of php online payment function (Alipay). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template