Implement APP payment function on the server side
This time I will bring you the precautions for implementing the APP payment function on the server side and the APP payment function on the server side. The following is a practical case, let's take a look.
Alipay payment is divided into many scenarios. Here we only describe the integration of Alipay APP payment functions. During the process, we encountered particularly big pitfalls, so I will briefly describe the integration process and outline the problems encountered and solutions 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, and the general process As follows1. Create application and configuration
First, you need to register the application on the Ant Financial development platform (open.alipay.com). And 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 backend of this part of the official website, which is relatively simple2. 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 an accessible real domain name
4. Case
After the above three steps are completed, we can now configure our own business code4.1. Organization APP Payment order information during 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. Problems encountered
##5.1. Keep reporting error 40001=>isv.invalid-signatureIn order to find out the reason, I regenerated the applied RSA2 multiple times. Public and private keys are 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 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 use of PHP callback functions and anonymous functions
phpstudy2018 access directory service permissions
ThinkPHP implements WeChat payment (jsapi payment) process tutorial detailed explanation_php example
The above is the detailed content of Implement APP payment function on the server side. 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

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

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

Baidu Map APP has now become the preferred travel navigation software for many users, so some of the functions here are comprehensive and can be selected and operated for free to solve some of the problems that you may encounter in daily travel. You can all check some of your own travel routes and plan some of your own travel plans. After checking the corresponding routes, you can choose appropriate travel methods according to your own needs. So whether you choose some public transportation, Cycling, walking or taking a taxi can all satisfy your needs. There are corresponding navigation routes that can successfully lead you to a certain place. Then everyone will feel more convenient if they choose to take a taxi. There are many drivers They are all able to take orders online, and taxi-hailing has become super

Both vivox100s and x100 mobile phones are representative models in vivo's mobile phone product line. They respectively represent vivo's high-end technology level in different time periods. Therefore, the two mobile phones have certain differences in design, performance and functions. This article will conduct a detailed comparison between these two mobile phones in terms of performance comparison and function analysis to help consumers better choose the mobile phone that suits them. First, let’s look at the performance comparison between vivox100s and x100. vivox100s is equipped with the latest

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

With the rapid development of the Internet, the concept of self-media has become deeply rooted in people's hearts. So, what exactly is self-media? What are its main features and functions? Next, we will explore these issues one by one. 1. What exactly is self-media? We-media, as the name suggests, means you are the media. It refers to an information carrier through which individuals or teams can independently create, edit, publish and disseminate content through the Internet platform. Different from traditional media, such as newspapers, television, radio, etc., self-media is more interactive and personalized, allowing everyone to become a producer and disseminator of information. 2. What are the main features and functions of self-media? 1. Low threshold: The rise of self-media has lowered the threshold for entering the media industry. Cumbersome equipment and professional teams are no longer needed.

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

As Xiaohongshu becomes popular among young people, more and more people are beginning to use this platform to share various aspects of their experiences and life insights. How to effectively manage multiple Xiaohongshu accounts has become a key issue. In this article, we will discuss some of the features of Xiaohongshu account management software and explore how to better manage your Xiaohongshu account. As social media grows, many people find themselves needing to manage multiple social accounts. This is also a challenge for Xiaohongshu users. Some Xiaohongshu account management software can help users manage multiple accounts more easily, including automatic content publishing, scheduled publishing, data analysis and other functions. Through these tools, users can manage their accounts more efficiently and increase their account exposure and attention. In addition, Xiaohongshu account management software has

PHP Tips: Quickly implement the function of returning to the previous page. In web development, we often encounter the need to implement the function of returning to the previous page. Such operations can improve the user experience and make it easier for users to navigate between web pages. In PHP, we can achieve this function through some simple code. This article will introduce how to quickly implement the function of returning to the previous page and provide specific PHP code examples. In PHP, we can use $_SERVER['HTTP_REFERER'] to get the URL of the previous page

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.
