Alipay payment PHP background signature implementation method
Signing and signature verification can also be completed on the APP side. Considering security issues, signing and signature verification are best completed on the server side. This is also the official recommendation of Alipay, so the PHP side needs to pass the signed parameters to the APP. end. This article mainly shares with you the implementation method of PHP background signature for Alipay payment. I hope it can help everyone.
1. Download php Alipay sdk
https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.eCtVsf&treeId=54&articleId=103419&docType= 1 (old)
https://docs.open.alipay.com/54/103419/ (new)
2. View the Alipay App payment request parameter document, splice the request parameters, and sign
App payment request Parameter description
https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.wM4mV1&treeId=204&articleId=105465&docType=1 (old)
https://docs. open.alipay.com/204/105465/ (New)
First, the parameters are spliced to generate a signature, and then the previous parameters and signature are assembled. The core code is as follows:
require_once '/Alipay/aop/AopClient.php'; $private_path = "/Alipay/key/rsa_private_key.pem";//私钥路径 //构造业务请求参数的集合(订单信息) $content = array(); $content['subject'] = "商品的标题/交易标题/订单标题/订单关键字等"; $content['out_trade_no'] = "商户网站唯一订单号"; $content['timeout_express'] = "该笔订单允许的最晚付款时间"; $content['total_amount'] = "订单总金额(必须定义成浮点型)"; $content['product_code'] = "QUICK_MSECURITY_PAY";/销售产品码,固定值 $con = json_encode($content);//$content是biz_content的值,将之转化成json字符串
//公共参数 $Client = new \AopClient();//实例化支付宝sdk里面的AopClient类,下单时需要的操作,都在这个类里面 $param['app_id'] = '支付宝分配给开发者的应用ID'; $param['method'] = 'alipay.trade.app.pay';//接口名称,固定值 $param['charset'] = 'utf-8';//请求使用的编码格式 $param['sign_type'] = 'RSA2';//商户生成签名字符串所使用的签名算法类型 $param['timestamp'] = date("Y-m-d Hi:i:s");//发送请求的时间 $param['version'] = '1.0';//调用的接口版本,固定为:1.0 $param['notify_url'] = '支付宝服务器异步回调地址'; $param['biz_content'] = $con;//业务请求参数的集合,长度不限,json格式,即前面一步得到的 $paramStr = $Client->getSignContent($param);//组装请求签名参数 $sign = $Client->alonersaSign($paramStr, $private_path, 'RSA2', true);//生成签名 $param['sign'] = $sign; $str = $Client->getSignContentUrlencode($param);//最终请求参数
The description of the request, Alipay said very clearly, here is a screenshot again:
3. Signature verification
App will return a string after successful payment. The customer service side also needs to make a judgment, so I won’t be too verbose here, as shown below:
The next step is to verify the signature on the PHP server. Alipay asynchronously returns the data to the asynchronous callback address in post mode:
function notify() { require_once('/alipay/aop/AopClient.php'); $aop = new \AopClient; //$public_path = "key/rsa_public_key.pem";//公钥路径 $aop->alipayrsaPublicKey = "支付宝公钥"; //此处验签方式必须与下单时的签名方式一致 $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2"); //验签通过后再实现业务逻辑,比如修改订单表中的支付状态。 /** * ①验签通过后核实如下参数out_trade_no、total_amount、seller_id * ②修改订单表 **/ //打印success,应答支付宝。必须保证本界面无错误。只打印了success,否则支付宝将重复请求回调地址。 echo 'success'; }
The signature verification failed all the time before. After searching for a long time, it was finally solved. The document says that the Alipay public key is used for signature verification, not the RSA2 public key. Special attention needs to be paid here to avoid using the wrong one.
Please see the screenshot:
Related recommendations:
realize WeChat scan code payment PHP code sharing
PHP realizes QQ, WeChat and Alipay payment collection The codes are consistent
Detailed explanation of UnionPay payment and refund examples on the PHP backend
The above is the detailed content of Alipay payment PHP background signature implementation method. For more information, please follow other related articles on the PHP Chinese website!

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.


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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
