支付宝实现预下单支付使用公钥证书(公钥证书方式)

支付宝预下单

  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;
    }

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值