Implementation of php online payment function (Alipay)
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

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

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.

Login account payment
Step 3: Buyer selects payment method

Select payment method
Step 4: Payment successful

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.

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

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:
Configuration code
(3) Find the following parameters in config file for corresponding configuration:

Parameters

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.

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.

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!
Hot AI Tools
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undress AI Tool
Undress images for free
Clothoff.io
AI clothes remover
AI Hentai Generator
Generate AI Hentai for free.
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
1378
52
Alipay PHP SDK transfer error: How to solve the problem of 'Cannot declare class SignData'?
Apr 01, 2025 am 07:21 AM
Alipay PHP...
Explain JSON Web Tokens (JWT) and their use case in PHP APIs.
Apr 05, 2025 am 12:04 AM
JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,
Explain late static binding in PHP (static::).
Apr 03, 2025 am 12:04 AM
Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases?
Apr 03, 2025 am 12:03 AM
What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
How to solve the problem of 'Undefined array key 'sign'' error when calling Alipay EasySDK using PHP?
Mar 31, 2025 pm 11:51 PM
Problem Description When calling Alipay EasySDK using PHP, after filling in the parameters according to the official code, an error message was reported during operation: "Undefined...
Explain the match expression (PHP 8 ) and how it differs from switch.
Apr 06, 2025 am 12:03 AM
In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.
Sesame Open Door Login Registration Entrance gate.io Exchange Registration Official Website Entrance
Mar 04, 2025 pm 04:51 PM
Gate.io (Sesame Open Door) is the world's leading cryptocurrency trading platform. This article provides a complete tutorial on spot trading of Gate.io. The tutorial covers steps such as account registration and login, KYC certification, fiat currency and digital currency recharge, trading pair selection, limit/market transaction orders, and orders and transaction records viewing, helping you quickly get started on the Gate.io platform for cryptocurrency trading. Whether a beginner or a veteran, you can benefit from this tutorial and easily master the Gate.io trading skills.
Ouyi Exchange app domestic download tutorial
Mar 21, 2025 pm 05:42 PM
This article provides a detailed guide to safe download of Ouyi OKX App in China. Due to restrictions on domestic app stores, users are advised to download the App through the official website of Ouyi OKX, or use the QR code provided by the official website to scan and download. During the download process, be sure to verify the official website address, check the application permissions, perform a security scan after installation, and enable two-factor verification. During use, please abide by local laws and regulations, use a safe network environment, protect account security, be vigilant against fraud, and invest rationally. This article is for reference only and does not constitute investment advice. Digital asset transactions are at your own risk.


