支付宝支付
2023年4月18日大约 2 分钟
准备
- pom.xml
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.31.24.ALL</version>
</dependency>- application.yml
#支付宝
alipay:
appId: xxx
format: json
charset: utf-8
signType: RSA2
# 支付宝公钥
aliAppPublicKey: xxx
privateKey: xxx
userInfoServerUrl: https://openapi.alipay.com/gateway.do
# 支付回调相关
# payOneNotifyUrl: https://abc.com/test1 #支付通知
# payTwoNotifyUrl: https://abc.com/test2 #支付通知
# 退款回调
# refundUrl: https://abc.com/test3- alipayConfig.java
@Data
@Component
@ConfigurationProperties(prefix = "alipay")
public class AlipayConfig {
private String appId;
private String format;
private String charset;
private String signType;
private String privateKey;
private String aliAppPublicKey;
private String userInfoServerUrl;
}- alipay 客户端
@Configuration
class AlipayClientInstance {
@Resource
AlipayConfig alipayConfig;
@Bean
public AlipayClient alipayClient() {
//String serverUrl, String appId, String privateKey, String format,String charset, String alipayPublicKey, String signType
//实例化客户端 参数:正式环境URL,Appid,商户私钥 PKCS8格式,字符编码格式,字符格式,支付宝公钥,签名方式
return new DefaultAlipayClient(
alipayConfig.getAppId(),
alipayConfig.getFormat(),
alipayConfig.getCharset(),
alipayConfig.getSignType(),
alipayConfig.getPrivateKey(),
alipayConfig.getAliAppPublicKey(),
alipayConfig.getUserInfoServerUrl());
}
}支付实现
@Component
public class AlipayService {
@Resource
private AlipayClient alipayClient;
/**
* 统一收单交易创建接口
*
* @param totalAmount 总金额,单位为分
* @param outTradeNo 商户订单号
* @param userId 支付宝用户id
* @param subject 订单标题。 注意:不可使用特殊字符,如 /,=,& 等。
* @param notifyUrl 通知地址
* @return 支付宝交易号以及商户订单号
*/
public AlipayTradeCreateResponse tradeCreate(Integer totalAmount, String outTradeNo, String userId, String subject, String notifyUrl) {
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.create.
AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数。
JSONObject json = new JSONObject();
//雪花算法订单号
// long l = IdUtil.getSnowflake().nextId();
// String outTradeNo =String.valueOf(l);
//订单号
json.set("out_trade_no", outTradeNo);
json.set("total_amount", BigDecimal.valueOf(totalAmount).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP).toString());
//描述
json.set("subject", subject);
//用户唯一标识id 这里必须使用buyer_id 参考文档
json.set("buyer_id", userId);
json.set("body", userId);
//对象转化为json字符串
String jsonStr = json.toString();
//商户通过该接口进行交易的创建下单
request.setBizContent(jsonStr);
//回调地址 是能够访问到的域名加上方法名
request.setNotifyUrl(notifyUrl);
try {
AlipayTradeCreateResponse response = alipayClient.execute(request);
if (response.isSuccess()) {
return response;
} else {
throw new BusinessException("调用支付宝创建订单接口失败,原因为:" + response.getMsg());
}
} catch (AlipayApiException e) {
throw new BusinessException("创建订单异常,原因为:" + e.getMessage());
}
}
/**
* @param outTradeNo 商户单号
* @param totalAmount 单位分
* @param refundReason 退款原因
* @return
*/
public AlipayTradeRefundResponse alipayRefund(String outTradeNo, Integer totalAmount, String refundReason, String notifyUrl) {
AlipayTradeRefundRequest alipayTradeRefundRequest = new AlipayTradeRefundRequest();
//构造退款的参数
AlipayTradeRefundModel model = new AlipayTradeRefundModel();
//订单号 这里的订单号是自己生成的 和支付宝系统的订单号TradeNo只能二选一
model.setOutTradeNo(outTradeNo);
//金额
model.setRefundAmount(BigDecimal.valueOf(totalAmount).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP).toString());
//退款原因
model.setRefundReason(refundReason);
//退款请求
//参数set到请求里
alipayTradeRefundRequest.setBizModel(model);
alipayTradeRefundRequest.setNotifyUrl(notifyUrl);
try {
return alipayClient.execute(alipayTradeRefundRequest);
} catch (AlipayApiException e) {
throw new BusinessException("退费异常:" + e.getMessage());
}
}
}
