支付宝回调验证签名失败怎么解决?
支付宝回调验证签名失败的解决方法:1、确保使用的验证签名是正确的;2、确保传入的参数是正确的;3、要在支付宝中给你的回调域名授权;3、确保加密解密类型为RSA2。
回调接口是支持扫码支付方式的回调的,最近业务需要又需要支持移动app的支付方式,回调时却签名验证失败。
支付宝回调验证签名失败怎么解决?
签名验证错误的检查顺序(这里是基于使用官方给的demo,自己封装的请绕道):
1:检查一下你使用的验证签名的方法是否正确?
bool signVerified = AlipaySignature.RSACheckV1(dic, alipay_public_key, config.charset);
2:检查一下你传入的参数是否正确?
参数1:dic,把回调的参数保存到key,value集合中
Dictionary<string, string> dic = new Dictionary<string, string>(); var form = HttpContext.Current.Request.Form; string str = "异步通知:\r\n"; foreach (var key in form) { dic[key.ToString()] = HttpContext.Current.Request.Form[key.ToString()]; var value = HttpContext.Current.Request.Form[key.ToString()]; //记录日志使用 str += $"{key.ToString()}:{value}\r\n"; }
参数2:alipay_public_key
这个参数是 支付宝公钥!! 很多小伙伴都写成了应用公钥,瞎几把写。
参数3:编码格式,UTF-8,这个一般没人会错。
3:检查一下你的环境
沙盒环境还是线上环境,沙盒环境会出错,具体为什么我不知道,百度来的。要在支付宝中给你的回调域名授权,不授权人家懒得回调给你。
4:检查一下你的加密解密类型
我从官网下载下来的demo里面的解密类型默认是RSA,但是官方文档已经明确说明现在都要用RSA2了,所以记得检查demo的源码
public static bool RSACheckV1(IDictionary<string, string> parameters, string publicKeyPem, string charset) { string sign = parameters["sign"]; string sign_type = parameters["sign_type"]; parameters.Remove("sign"); parameters.Remove("sign_type"); string signContent = GetSignContent(parameters); return RSACheckContent(signContent, sign, publicKeyPem, charset, sign_type); }
sign_type,这个就是解码类型,demo写的好像“RSA”,我这里改成动态获取了,我们在前期配置的地方也会配置加密类型,从哪获取都可以,别弄错了就行。
5:这里不检查了,回忆一下你的支付宝公钥,是直接存在文本中的,还是写在代码里的(区别:公钥.txt,string 公钥 = “巴拉巴拉巴拉一大堆”),一个是文件,一个是直接代码(我就是代码,所以我一直到最后才解决)(下面的解决方案只针对代码保存支付宝公钥的骚年)
string alipay_public_key = "-----BEGIN PUBLIC KEY-----\r\n" + config.alipay_public_key + "-----END PUBLIC KEY-----\r\n\r\n"; bool signVerified = AlipaySignature.RSACheckV1(dic, alipay_public_key, config.charset);
如果是直接写在代码中的,要给支付宝公钥的头跟尾加上标识,具体标识看我贴出来的代码,如果是文件,请自动忽略
还没结束,官方给的demo也是默认找的文件,可是我用的代码存的,哪有文件,所以找不到文件是会报错的,报错直接返回false了,在修改一下源码(自己到AlipaySignature这个类里面去找)
public static bool RSACheckContent(string signContent, string sign, string publicKeyPem, string charset, string signType) { try { if (string.IsNullOrEmpty(charset)) { charset = DEFAULT_CHARSET; } if ("RSA2".Equals(signType)) { //这里就是要改的地方 //从参数获取 string sPublicKeyPEM = publicKeyPem; //从文件获取 //string sPublicKeyPEM = File.ReadAllText(publicKeyPem); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.PersistKeyInCsp = false; RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM); bool bVerifyResultOriginal = rsa.VerifyData(Encoding.GetEncoding(charset).GetBytes(signContent), "SHA256", Convert.FromBase64String(sign)); return bVerifyResultOriginal; } else { //这里就是要改的地方 //从参数获取 string sPublicKeyPEM = publicKeyPem; //从文件获取 //string sPublicKeyPEM = File.ReadAllText(publicKeyPem); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.PersistKeyInCsp = false; RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM); SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider(); bool bVerifyResultOriginal = rsa.VerifyData(Encoding.GetEncoding(charset).GetBytes(signContent), sha1, Convert.FromBase64String(sign)); return bVerifyResultOriginal; } } catch (Exception e) { NLogGetter.NLog.ErrorLog(e); return false; } }
好了,差不多就总结了这么多,基本上可以让你签名验证成功了。
更多相关知识,请访问 PHP中文网!!

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Alipay PHP...

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.

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

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.
