JAVA集成支付宝支付 周期扣费 (证书方式)

这篇博客详细介绍了如何使用Java进行支付宝周期扣费的集成,包括所需的依赖引入、核心代码展示以及配置类的设置,帮助开发者实现后端支付功能。
摘要由CSDN通过智能技术生成

依赖

<!-- <dependency>
     <groupId>com.alipay.sdk</groupId>
     <artifactId>alipay-sdk-java</artifactId>
     <version>4.11.33.ALL</version>
 </dependency>-->

上代码

package com.yuren.yoga.common.vendor.template;

import com.alibaba.fastjson.JSON;
import com.alipay.api.*;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author YSM
 * @user Admin
 * @data 2021/10/13
 * @ProjectName 新建文件夹
 */
@Slf4j
public class AliPayTemplate {

    private AliPayProperties aliPayProperties;

    private String PRODUCT_CODE = "CYCLE_PAY_AUTH";
    private String SIGN_SCENE = "INDUSTRY|CARRENTAL";
    private String url = "https://openapi.alipay.com/gateway.do";

    public AliPayTemplate(AliPayProperties aliPayProperties) {
        this.aliPayProperties = aliPayProperties;
    }


    public CertAlipayRequest getCertAlipayRequest() {
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
        certAlipayRequest.setAppId(aliPayProperties.getAppId());
        certAlipayRequest.setPrivateKey(aliPayProperties.getPrivateKey());
        certAlipayRequest.setFormat("json");
        certAlipayRequest.setCharset("UTF-8");
        certAlipayRequest.setSignType("RSA2");
        certAlipayRequest.setCertPath(aliPayProperties.getCertPath() + "appCertPublicKey_" + aliPayProperties.getAppId() + ".crt");
        certAlipayRequest.setAlipayPublicCertPath(aliPayProperties.getCertPath() + "alipayCertPublicKey_RSA2.crt");
        certAlipayRequest.setRootCertPath(aliPayProperties.getCertPath() + "alipayRootCert.crt");
        return certAlipayRequest;
    }


    public String getOrder(String name, double price, Double originPrice, String courseId, String userId, String type) {
        return getOrder(name, price, originPrice, courseId, userId, type, null);
    }

    public String getOrder(String name, double price, Double originPrice, String courseId, String userId, String type, String contactPhone) {
        CertAlipayRequest certAlipayRequest = getCertAlipayRequest();
        AlipayClient alipayClient = null;
        try {
            alipayClient = new DefaultAlipayClient(certAlipayRequest);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        if (originPrice == null || 0 == originPrice) {
            originPrice = price;
        }

        if (StringUtils.isEmpty(contactPhone)) {
            contactPhone = "";
        }
        model.setBody(courseId + "-" + userId + "-" + type + "-" + originPrice + "-" + contactPhone);

        model.setSubject("YUREN-" + name);
        model.setGoodsType("0");//虚拟类商品

        String outTradeNo = DateUtils.format(new Date(), "yyyyMMddHHmmss")
                + new Random().nextInt(8999) + 1000;
        model.setOutTradeNo(outTradeNo);
        model.setTimeoutExpress("30m");
        model.setTotalAmount(price + "");
        model.setProductCode("QUICK_MSECURITY_PAY");

        request.setBizModel(model);
        request.setNotifyUrl(aliPayProperties.getCallbackUrl());
        try {
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            return response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }

    public AlipayTradePrecreateResponse getQROrder(String name, double price, Double originPrice, String goodsId, String userId, String type) {
        CertAlipayRequest certAlipayRequest = getCertAlipayRequest();
        AlipayClient alipayClient = null;
        try {
            alipayClient = new DefaultAlipayClient(certAlipayRequest);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest(); //创建API对应的request类

        AlipayTradePrecreateModel model = new AlipayTradePrecreateMode
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值