当前位置:   article > 正文

SpringBoot集成alipay-easysdk_alipay easy sdk 关闭订单

alipay easy sdk 关闭订单

1、引入maven依赖

	<!--支付宝-->
	<dependency>
	    <groupId>com.alipay.sdk</groupId>
	    <artifactId>alipay-easysdk</artifactId>
	    <version>2.1.2</version>
	</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2、SDK属性装配

package com.example.pay.uitls.ali;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * desc: 支付宝支付属性参数
 *
 * @author: 邢阳
 * @mail: xydeveloper@126.com
 * @create 2021-04-07 14:11
 */
@Data
@Component
@ConfigurationProperties(prefix = "alipay")
public class AliPayProperties {

    @ApiModelProperty("应用ID")  // @ApiModelProperty是Swagger的注解可以不必使用
    private String appId;

    @ApiModelProperty("请求的协议")
    private String protocol;

    @ApiModelProperty("请求网关的路径")
    private String gatewayHost;

    @ApiModelProperty("应用私钥")
    private String merchantPrivateKey;

    @ApiModelProperty("支付宝公钥")
    private String alipayPublicKey;

    @ApiModelProperty("是否忽略ssl")
    private Boolean ignoreSsl;

    @ApiModelProperty("签名加密类型")
    private String signType;

    @ApiModelProperty("支付回调地址")
    private String notifyUrl;

    /**
     * 初始化工厂配置参数
     */
    @PostConstruct
    public void initOptions() {

        Config config = new Config();

        config.protocol = protocol;
        config.gatewayHost = gatewayHost;
        config.signType = signType;
        config.appId = appId;
        config.merchantPrivateKey = merchantPrivateKey;
        config.alipayPublicKey = alipayPublicKey;
        config.notifyUrl = notifyUrl;

        Factory.setOptions(config);

    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

3、yml

# 支付宝支付属性参数
alipay:
  appId: 用户的appId
  protocol: https
  gatewayHost: openapi.alipaydev.com
  alipayPublicKey: 支付宝公钥
  ignoreSSL: false
  signType: RSA2
  notifyUrl: 回调地址(必须外网可以调用,开发阶段建议使用内网穿透工具)
  merchantPrivateKey: 商户私钥
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

4、AliPayUtils工具类

package com.example.pay.uitls.ali;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.app.models.AlipayTradeAppPayResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeCloseResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeQueryResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeRefundResponse;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.alipay.easysdk.payment.wap.models.AlipayTradeWapPayResponse;
import com.example.pay.common.*;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * desc: 支付宝支付工具类
 *
 * @author: 邢阳
 * @mail: xydeveloper@126.com
 * @create 2021-04-07 14:11
 */
@Component
public class AliPayUtils {

    /**
     * app支付
     *
     * @param subject     订单标题
     * @param outTradeNo  交易创建时传入的商户订单号
     * @param totalAmount 订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000]
     * @return
     */
    public AlipayTradeAppPayResponse appPay(String subject, String outTradeNo, String totalAmount) {

        AlipayTradeAppPayResponse alipayTradeAppPayResponse = null;

        try {
			
			// 主要操作 Factory.Payment.App().pay(subject, outTradeNo, totalAmount); 其他代码是一些判空以及异常处理
            alipayTradeAppPayResponse = Factory.Payment.App().pay(subject, outTradeNo, totalAmount);

        } catch (Exception e) {

            e.printStackTrace();

        }

        if (Objects.isNull(alipayTradeAppPayResponse)) {

            MyRuntimeException.ThrowRunTimeException.throwException(ExceptionEnum.PAY_FAIL);

        }

        return alipayTradeAppPayResponse;

    }

    /**
     * 手机网站支付
     *
     * @param subject:     订单标题
     * @param outTradeNo:  交易创建时传入的商户订单号
     * @param totalAmount: 订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000]
     * @param quitUrl:     用户付款中途退出返回商户网站的地址
     * @param returnUrl    支付成功后同步跳转的页面,是一个http/https开头的字符串
     * @return
     */
    public AlipayTradeWapPayResponse wapPay(String subject, String outTradeNo, String totalAmount,
                                            String quitUrl, String returnUrl) {

        AlipayTradeWapPayResponse alipayTradeWapPayResponse = null;

        try {
			
			// 主要操作 Factory.Payment.App().pay(subject, outTradeNo, totalAmount); 其他代码是一些判空以及异常处理
            alipayTradeWapPayResponse = Factory.Payment.Wap().pay(subject, outTradeNo, totalAmount, quitUrl, returnUrl);

        } catch (Exception e) {

            e.printStackTrace();

        }

        if (Objects.isNull(alipayTradeWapPayResponse)) {

            MyRuntimeException.ThrowRunTimeException.throwException(ExceptionEnum.PAY_FAIL);

        }

        return alipayTradeWapPayResponse;

    }

    /**
     * 扫码支付
     *
     * @param subject:     订单标题
     * @param outTradeNo:  交易创建时传入的商户订单号
     * @param totalAmount: 订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000]
     * @param returnUrl    支付成功后同步跳转的页面,是一个http/https开头的字符串
     * @return
     */
    public AlipayTradePagePayResponse pagePay(String subject, String outTradeNo, String totalAmount, String returnUrl) {

        AlipayTradePagePayResponse alipayTradePagePayResponse = null;

        try {
        
			// 主要操作 Factory.Payment.App().pay(subject, outTradeNo, totalAmount); 其他代码是一些判空以及异常处理
            alipayTradePagePayResponse = Factory.Payment.Page().pay(subject, outTradeNo, totalAmount, returnUrl);

        } catch (Exception e) {

            e.printStackTrace();

        }

        if (Objects.isNull(alipayTradePagePayResponse)) {

            MyRuntimeException.ThrowRunTimeException.throwException(ExceptionEnum.PAY_FAIL);

        }

        return alipayTradePagePayResponse;

    }


    /**
     * 支付查询
     *
     * @param outTradeNo 交易创建时传入的商户订单号
     * @return
     */
    public AlipayTradeQueryResponse query(String outTradeNo) {

        AlipayTradeQueryResponse alipayTradeQueryResponse = null;

        try {
        
			// 主要操作 Factory.Payment.App().pay(subject, outTradeNo, totalAmount); 其他代码是一些判空以及异常处理
            alipayTradeQueryResponse = Factory.Payment.Common().query(outTradeNo);

        } catch (Exception e) {

            e.printStackTrace();

        }

        Assert.isTrue(ResponseChecker.success(alipayTradeQueryResponse), ExceptionEnum.SYSTEM_EXCEPTION);

        return alipayTradeQueryResponse;

    }

    /**
     * 退款
     *
     * @param outTradeNo   交易创建时传入的商户订单号
     * @param refundAmount 退款金额
     * @return
     */
    public AlipayTradeRefundResponse refund(String outTradeNo, String refundAmount) {

        AlipayTradeRefundResponse alipayTradeRefundResponse = null;

        try {

			// 主要操作 Factory.Payment.App().pay(subject, outTradeNo, totalAmount); 其他代码是一些判空以及异常处理
            alipayTradeRefundResponse = Factory.Payment.Common().refund(outTradeNo, refundAmount);

        } catch (Exception e) {

            e.printStackTrace();

        }

        Assert.isTrue(ResponseChecker.success(alipayTradeRefundResponse), ExceptionEnum.SYSTEM_EXCEPTION);

        return alipayTradeRefundResponse;

    }

    /**
     * 取消交易
     *
     * @param outTradeNo 交易创建时传入的商户订单号
     * @return
     */
    public AlipayTradeCloseResponse close(String outTradeNo) {

        AlipayTradeCloseResponse alipayTradeCloseResponse = null;

        try {

			// 主要操作 Factory.Payment.App().pay(subject, outTradeNo, totalAmount); 其他代码是一些判空以及异常处理
            alipayTradeCloseResponse = Factory.Payment.Common().close(outTradeNo);

        } catch (Exception e) {

            e.printStackTrace();

        }

        Assert.isTrue(ResponseChecker.success(alipayTradeCloseResponse), ExceptionEnum.SYSTEM_EXCEPTION);

        return alipayTradeCloseResponse;

    }

}
;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213

5、服务层调用

package com.example.pay.service.impl;

import cn.hutool.core.net.URLDecoder;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.example.pay.common.Constant;
import com.example.pay.common.ExceptionEnum;
import com.example.pay.common.MyRuntimeException;
import com.example.pay.config.UserMain;
import com.example.pay.entity.PayOrder;
import com.example.pay.entity.vo.QueryVo;
import com.example.pay.entity.vo.AliPagePayVo;
import com.example.pay.enums.PayEnum;
import com.example.pay.enums.PayOrderEnum;
import com.example.pay.service.PayService;
import com.example.pay.uitls.ali.AliPayUtils;
import com.example.pay.uitls.fuiou.FuiouPayUtils;
import com.example.pay.uitls.wechat.WeChatUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.*;

import static com.example.pay.common.Result.success;

/**
 * desc: 聚合支付服务实现类
 *
 * @author: 邢阳
 * @mail: xydeveloper@126.com
 * @create 2021-04-07 14:11
 */
@Slf4j
@Service
public class PayServiceImpl implements PayService {

    @Autowired
    private AliPayUtils aliPayUtils;
    
    /**
     * 手机网站支付
     *
     * @param aliPagePayVo
     * @return
     */
    @Override
    public String pagePay(AliPagePayVo aliPagePayVo, UserMain userMain) {

        String outTradeNo = String.valueOf(UUID.randomUUID());
        AlipayTradePagePayResponse alipayTradePagePayResponse = aliPayUtils.pagePay(aliPagePayVo.getSubject(),
                    outTradeNo, aliPagePayVo.getTotalAmount(), aliPagePayVo.getReturnUrl());
		
		String body = alipayTradePagePayResponse.getBody();

        return body;

    }
    
	/**
     * 回调通知
     *
     * @param httpServletRequest
     * @return
     */
    @Override
    public Map notice(HttpServletRequest httpServletRequest) {

        try {

            Map<String, String> params = getAllRequestParam(httpServletRequest);

            if (!Factory.Payment.Common().verifyNotify(params)) {

                MyRuntimeException.ThrowRunTimeException.throwException(ExceptionEnum.PARAM_PARSING_EXCPTION);

            }

            if (params.get("trade_status").equals(Constant.TRADE_SUCCESS)) {

                String orderId = URLDecoder.decode(params.get("passback_params"), StandardCharsets.UTF_8);



				// todo 业务处理...
				
                return null;

            }

        } catch (Exception e) {

            log.error(e.getMessage(), e);

        }

        return null;

    }
    
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/125661
推荐阅读
相关标签
  

闽ICP备14008679号