支付宝实现预下单支付使用公钥证书(公钥证书方式)
支付宝预下单
public PreparePayResultVo preparePay(NftOrder nftOrder) {
private final AliPayProperties aliPayProperties;
AlipayClient alipayClient =null;
try {
alipayClient = new DefaultAlipayClient(this.getCertAlipay());
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
request.setNotifyUrl(aliPayProperties.getNotifyUrl());
request.setReturnUrl(aliPayProperties.getReturnUrl());
JSONObject bizContent = new JSONObject();
bizContent.put("out_trade_no", nftOrder.getOrderNo());
bizContent.put("total_amount", nftOrder.getPayAmount());
//utf编码防止出现特殊字符
bizContent.put("subject", URLEncoder.encode(nftOrder.getNftName(), "utf-8"));
bizContent.put("product_code", "QUICK_WAP_WAY");
bizContent.put("quit_url", aliPayProperties.getQuitUrl());
bizContent.put("time_expire", LocalDateTime.now().plusMinutes(orderProperties.getPayTimeoutMinutes()).toString("yyyy-MM-dd HH:mm:ss"));
// 商品明细信息,按需传入
JSONArray goodsDetail = new JSONArray();
JSONObject goods1 = new JSONObject();
goods1.put("goods_id", nftOrder.getNftId());
if (nftOrder.getNftImageType().equals(NftImageTypeEnum.DRM.getValue())) {
goods1.put("goods_name", URLEncoder.encode(nftOrder.getGoodsName(), "utf-8"));
} else {
goods1.put("goods_name", URLEncoder.encode(nftOrder.getNftName(), "utf-8"));
}
goods1.put("quantity", 1);
goods1.put("price", nftOrder.getPayAmount());
goodsDetail.add(goods1);
bizContent.put("goods_detail", goodsDetail);
request.setBizContent(bizContent.toString());
AlipayTradeWapPayResponse response = alipayClient.pageExecute(request, "GET");
if (response.isSuccess()) {
form = response.getBody();
} else {
log.error("调用手机网站支付接口失败:{},{}", nftOrder.getOrderNo(), JSON.toJSONString(response));
throw new ApplicationException(OrderResponseCode.ALIPAY_GET_TRADE_FAIL);
}
} catch (AlipayApiException e) {
log.error("获取支付表单失败", e);
throw new ApplicationException(OrderResponseCode.ALIPAY_GET_TRADE_FAIL);
} catch (UnsupportedEncodingException e) {
log.error("支付宝下单编码异常", e);
throw new ApplicationException(OrderResponseCode.ALIPAY_GET_TRADE_FAIL);
} catch (IOException e) {
log.error("支付宝下单异常", e);
throw new ApplicationException(OrderResponseCode.ALIPAY_GET_TRADE_FAIL);
}
}
支付宝配置文件
@Setter
@Getter
@ConfigurationProperties(prefix = "alipay")
public class AliPayProperties {
private String appId;
private String alipayPublicKey;
private String privateKey;
private String publicKey;
private String notifyUrl;
private String returnUrl;
private String quitUrl;
private String returnUrlApp;
private String quitUrlApp;
private String appToH5Pay;
private String charSet = "UTF-8";
private String serverUrl = "https://openapi.alipay.com/gateway.do";
private String signType = "RSA2";
private String format = "json";
private String appPublicCertPath="cert/appCertPublicKey.crt";
private String alipayPublicCertPath="cert/alipayCertPublicKey_RSA2.crt";
private String alipayRootCertPath="cert/alipayRootCert.crt";
读取支付宝配置文件和证书
private CertAlipayRequest getCertAlipay() {
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
certAlipayRequest.setServerUrl(aliPayProperties.getServerUrl());
certAlipayRequest.setAppId(aliPayProperties.getAppId());
certAlipayRequest.setPrivateKey(aliPayProperties.getPrivateKey());
certAlipayRequest.setFormat(aliPayProperties.getFormat());
certAlipayRequest.setCharset(aliPayProperties.getCharSet());
certAlipayRequest.setSignType(aliPayProperties.getSignType());
String appPublicCertPath = AliPayServiceImpl.class.getClassLoader().getResource(aliPayProperties.getAppPublicCertPath()).getPath();
certAlipayRequest.setCertPath(appPublicCertPath);
String alipayPublicCertPath = AliPayServiceImpl.class.getClassLoader().getResource(aliPayProperties.getAlipayPublicCertPath()).getPath();
certAlipayRequest.setAlipayPublicCertPath(alipayPublicCertPath);
String rootCertPath = AliPayServiceImpl.class.getClassLoader().getResource(aliPayProperties.getAlipayRootCertPath()).getPath();
certAlipayRequest.setRootCertPath(rootCertPath);
return certAlipayRequest;
}