


Detailed explanation of payment steps for integrating Alipay APP into PHP server
This time I will bring you a detailed explanation of the payment steps of integrating Alipay APP on the php server. What are the precautions for integrating the Alipay APP payment on the php server. The following is a practical case, let's take a look.
Since the company's business is simple and only supports Alipay payment, there is no need to worry about refunds, inquiries and other additional functions. Therefore, this article only describes how the server prepares the APP to pull payment order information when using the Alipay payment interface. The general process is as follows
1. Create application and configuration
First, you need to register with the Ant Financial development platform (open.alipay.com) Apply, obtain the application ID, and configure the application. The configuration here mainly involves signing a contract, generating the RSA2 public and private keys of the application, and obtaining the payment public key provided by Alipay. There are prompts in the background of this part of the official website, which is relatively simple
2. Download the corresponding SDK
Here I am integrating the service in the PHP background, so I downloaded the PHP SDK, address: https:/ /docs.open.alipay.com/54/103419/
3. Prepare a real domain name that can be accessed
4. Case
After the above three steps are completed, we can now configure our own business code
4.1. Payment order information when organizing APP payment
<?php require_once (DIR.'/alipay-sdk-PHP-20171023143822/AopSdk.php'); class Alipay { /** * 应用ID */ const APPID = '你的应用ID'; /** *请填写开发者私钥去头去尾去回车,一行字符串 */ const RSA_PRIVATE_KEY = '应用对应开发者私钥'; /** *请填写支付宝公钥,一行字符串 */ const ALIPAY_RSA_PUBLIC_KEY = '支付宝提供的公钥'; /** * 支付宝服务器主动通知商户服务器里指定的页面 * @var string */ private $callback = "http://www.test.com/notify/alipay_notify.php"; /** *生成APP支付订单信息 * @param string $orderId 商品订单ID * @param string $subject 支付商品的标题 * @param string $body 支付商品描述 * @param float $pre_price 商品总支付金额 * @param int $expire 支付交易时间 * @return bool|string 返回支付宝签名后订单信息,否则返回false */ public function unifiedorder($orderId, $subject,$body,$pre_price,$expire){ try{ $aop = new \AopClient(); $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do"; $aop->appId = self::APPID; $aop->rsaPrivateKey = self::RSA_PRIVATE_KEY; $aop->format = "json"; $aop->charset = "UTF-8"; $aop->signType = "RSA2"; $aop->alipayrsaPublicKey = self::ALIPAY_RSA_PUBLIC_KEY; //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay $request = new \AlipayTradeAppPayRequest(); //SDK已经封装掉了公共参数,这里只需要传入业务参数 $bizcontent = "{\"body\":\"{$body}\"," //支付商品描述 . "\"subject\":\"{$subject}\"," //支付商品的标题 . "\"out_trade_no\":\"{$orderId}\"," //商户网站唯一订单号 . "\"timeout_express\":\"{$expire}m\"," //该笔订单允许的最晚付款时间,逾期将关闭交易 . "\"total_amount\":\"{$pre_price}\"," //订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000] . "\"product_code\":\"QUICK_MSECURITY_PAY\"" . "}"; $request->setNotifyUrl($this->callback); $request->setBizContent($bizcontent); //这里和普通的接口调用不同,使用的是sdkExecute $response = $aop->sdkExecute($request); //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题 return htmlspecialchars($response);//就是orderString 可以直接给客户端请求,无需再做处理。 }catch (\Exception $e){ return false; } } }
4.2. Asynchronous callback processing after successful Alipay payment
<?php /** * alipay_notify.php. * User: lvfk * Date: 2017/10/26 0026 * Time: 13:48 * Desc: 支付宝支付成功异步通知 */ include_once (DIR.'/../alipay-sdk-PHP-20171023143822/AopSdk.php'); //验证签名 $aop = new \AopClient(); $aop->alipayrsaPublicKey = \Comm\Pay\Alipay::ALIPAY_RSA_PUBLIC_KEY; $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2"); //验签 if($flag){ //处理业务,并从$_POST中提取需要的参数内容 if($_POST['trade_status'] == 'TRADE_SUCCESS' || $_POST['trade_status'] == 'TRADE_FINISHED'){//处理交易完成或者支付成功的通知 //获取订单号 $orderId = $_POST['out_trade_no']; //交易号 $trade_no = $_POST['trade_no']; //订单支付时间 $gmt_payment = $_POST['gmt_payment']; //转换为时间戳 $gtime = strtotime($gmt_payment); //此处编写回调处理逻辑 //处理成功一定要返回 success 这7个字符组成的字符串, //die('success');//响应success表示业务处理成功,告知支付宝无需在异步通知 } }
5. Encounter Problem
5.1. Keep reporting error 40001=>isv.invalid-signature
In order to find out the reason, I regenerated the application multiple times. The RSA2 public and private keys were found to have no effect. Finally, combined with online information, I discovered that turned out to be the Alipay callback address notifyUrl cannot have '?' and add parameters after ?
##5.2, Alipay asynchronous notification It succeeded, but $_POST was empty
It also took a while to find this. When I started doing it, I followed Alipay's suggestion and used HTTS to request it. But in this way, the application background keeps notifying that there is no parameter content. Finally, I remembered that because our application uses HTTS two-way authentication, the parameters of Alipay's server callback are empty. Finally, change the callback address to HTTP method, and verify that it passes If you encounter problems, first check Alipay's document description and the error code explanation provided by Alipay. If it doesn't work, use Baidu or Google, plus you continue to After testing and verification, the problem will definitely be solved in the endAt this point, the payment function of Alipay APP has been completed, and other APP refund, statement and other functions have not been continued. However, according to the official website documents of Alipay and the SDK provided by Alipay , it is only a matter of time before it is integrated into your own application. I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website! Recommended reading:Detailed explanation of the steps to connect the database with ThinkPHP framework PDO
Laravel5 implements fuzzy matching and multi-condition query function implementation Detailed explanation of steps
The above is the detailed content of Detailed explanation of payment steps for integrating Alipay APP into PHP server. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment