工行小程序支付 Java生成的 RSA2签名验证不通过

1. 流程概述

在实现工行小程序支付时,需要生成 RSA2 签名,并将其用于请求接口并验证返回结果。下面是整个流程的概述:

journey
    title 工行小程序支付流程概述
    section 生成 RSA2 签名
    GenerateRSA2Signature --> RequestInterface: 请求接口
    RequestInterface --> VerifySignature: 验证签名
    VerifySignature --> Result: 返回结果

2. 详细步骤

2.1 生成 RSA2 签名

为了生成 RSA2 签名,需要使用支付宝提供的 SDK 和一些关键信息,具体步骤如下:

  1. 引入支付宝 SDK
import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeAppPayRequest;
  1. 创建 AlipayClient 实例
String gatewayUrl = "
String appId = "your_app_id";
String privateKey = "your_private_key";
String format = "json";
String charset = "UTF-8";
String alipayPublicKey = "alipay_public_key";
String signType = "RSA2";

DefaultAlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, appId, privateKey, format, charset, alipayPublicKey, signType);
  1. 创建 AlipayTradeAppPayRequest 实例并设置参数
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
request.setBizContent("{" +
    "    \"out_trade_no\":\"20150320010101001\"," +
    "    \"total_amount\":\"88.88\"," +
    "    \"subject\":\"Iphone6 16G\"," +
    "    \"body\":\"Iphone6 16G\"," +
    "    \"product_code\":\"QUICK_MSECURITY_PAY\"" +
    "  }");
  1. 生成签名并设置到请求中
String signedString = alipayClient.sdkExecute(request).getBody();
request.setNotifyUrl("your_notify_url");
request.setSign(signedString);

2.2 请求接口

  1. 发送请求
String response = alipayClient.pageExecute(request).getBody();
  1. 解析返回结果
JSONObject responseJson = JSON.parseObject(response);
String resultCode = responseJson.getString("code");
String resultMsg = responseJson.getString("msg");

2.3 验证签名

  1. 获取返回结果中的签名
String responseSign = responseJson.getString("sign");
  1. 验证签名
boolean signVerified = AlipaySignature.rsaCheckContent(response, responseSign, alipayPublicKey, charset, signType);

3. 代码注释

下面是每一步所使用的代码,并对其进行了注释:

import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeAppPayRequest;

String gatewayUrl = "
String appId = "your_app_id";
String privateKey = "your_private_key";
String format = "json";
String charset = "UTF-8";
String alipayPublicKey = "alipay_public_key";
String signType = "RSA2";

// 创建 AlipayClient 实例
DefaultAlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, appId, privateKey, format, charset, alipayPublicKey, signType);

// 创建 AlipayTradeAppPayRequest 实例并设置参数
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
request.setBizContent("{" +
    "    \"out_trade_no\":\"20150320010101001\"," +
    "    \"total_amount\":\"88.88\"," +
    "    \"subject\":\"Iphone6 16G\"," +
    "    \"body\":\"Iphone6 16G\"," +
    "    \"product_code\":\"QUICK_MSECURITY_PAY\"" +
    "  }");

// 生成签名并设置到请求中
String signedString = alipayClient.sdkExecute(request).getBody();
request.setNotifyUrl("your_notify_url");
request.setSign(signedString);

// 发送请求
String response = alipayClient.pageExecute(request).getBody();

// 解析返回结果
JSONObject responseJson = JSON.parseObject(response);
String resultCode = responseJson.getString("code");
String resultMsg = responseJson.getString("msg");

// 获取返回结果中的签名
String responseSign = responseJson.getString("sign");

// 验证签名
boolean signVerified = AlipaySignature.rsaCheckContent(response, responseSign, alipayPublicKey, charset