How to integrate Alipay APP payment into PHP server

不言
Release: 2023-04-02 09:32:02
Original
1425 people have browsed it

Let me share with you an example of PHP server integrating Alipay APP payment. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together

Alipay payment is divided into many scenarios. Here we only describe the integration of Alipay APP payment function. We encountered particularly big pitfalls during the process, so I will briefly describe the integration process and outline the problems encountered. And solution

Since the company's business is simple and only supports Alipay payment, there is no need to care about refunds, inquiries and other additional functions. Therefore, this article only describes how the server prepares the APP to pull payment orders when using the Alipay payment interface. The general process is as follows

1. Create application and configuration

First, you need to go to the Ant Financial development platform (open.alipay. com) to register the application, obtain the application ID, and configure the application. The configuration here mainly involves signing a contract, generating the application's RSA2 public and private keys, 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 an accessible real domain name

4. Case

After the above three steps are completed, we can now configure our own business code

4.1. Organize APP payment Payment order information at the time

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; } } }
Copy after login

4.2. Asynchronous callback processing after successful Alipay payment

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表示业务处理成功,告知支付宝无需在异步通知 } }
Copy after login

5. Problems Encountered

##5.1. Keep reporting error 40001=>isv.invalid- signature

In order to find out the reason, I regenerated the application's RSA2 public and private keys several times, but found that it had 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 end

At 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.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

PDO preprocessing statement PDOStatement object


PHP full-featured non-deformation image cropping operation class and Introduction to usage


The above is the detailed content of How to integrate Alipay APP payment into PHP server. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!