创建应用
这个得先去登录 - 支付宝创建应用
相关配置设置
maven配置
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.38.10.ALL</version>
</dependency>
支付服务代码
/**
* 支付宝支付
* @author leell
* @date 2023/7/21 16:39:58
*/
public interface AliPayService {
/**
* 预下单
* @return 返回二维码链接
*/
String createPrecreateTrade(AliPrecreateParam param) throws RuntimeException;
/**
* 检查支付状态
* @param orderNo 订单号
* @return 支付状态 true:已支付 false:未支付
*/
boolean checkAlipayStatus(String orderNo) throws RuntimeException;
/**
* 取消支付
* @param orderNo 订单号
* @return 取消状态
*/
boolean cancelPay(String orderNo) throws RuntimeException;
/**
* 退款
* @param orderNo 订单号
* @return 取消状态
*/
boolean refundPay(String orderNo, BigDecimal money) throws RuntimeException;
/**
* 验证签名
* @param params 参数
* @return 状态
*/
boolean rsaCheckV1(Map<String,String> params) throws RuntimeException;
/**
* 检查回调支付状态
* @param params 参数
* @return 状态
* @throws RuntimeException 异常
*/
boolean checkNotifyPayStatus(Map<String,String> params) throws RuntimeException;
/**
* 获取订单号
* @param params 支付宝返回参数
* @return 订单号
*/
String getOrderNo(Map<String,String> params);
}
/**
* 支付宝支付
*
* @author leell
* @date 2023/7/21 16:46:16
*/
@Service
public class AliPayServiceImpl implements AliPayService {
@Autowired
private PayHelper payHelper;
@Autowired
private ConfigService configService;
@Override
public String createPrecreateTrade(AliPrecreateParam param) throws RuntimeException {
try {
AlipayClient alipayClient = payHelper.getAlipayClient();
AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
//异步接收地址,仅支持http/https,公网可访问
request.setNotifyUrl(param.getNotifyUrl());
JSONObject bizContent = new JSONObject();
bizContent.put("out_trade_no", param.getOutTradeNo());
bizContent.put("total_amount", param.getTotalAmount());
bizContent.put("subject", param.getSubject());
request.setBizContent(bizContent.toString());
AlipayTradePrecreateResponse response = alipayClient.execute(request);
AssertUtil.isTrue(response.isSuccess(), response.getMsg());
return response.getQrCode();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public boolean checkAlipayStatus(String orderNo) throws RuntimeException {
try {
AlipayClient alipayClient = payHelper.getAlipayClient();
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
JSONObject bizContent = new JSONObject();
bizContent.put("out_trade_no", orderNo);
bizContent.put("query_options", new String[]{"trade_settle_info"});
request.setBizContent(bizContent.toString());
AlipayTradeQueryResponse response = alipayClient.execute(request);
JSONObject tradeResponse = JSONObject.parseObject(response.getBody()).getJSONObject("alipay_trade_query_response");
String result = tradeResponse.getString("trade_status");
return "TRADE_SUCCESS".equals(result);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public boolean cancelPay(String orderNo) throws RuntimeException {
try {
AlipayClient alipayClient = payHelper.getAlipayClient();
AlipayTradeCancelRequest request = new AlipayTradeCancelRequest();
JSONObject bizContent = new JSONObject();
bizContent.put("out_trade_no", orderNo);
request.setBizContent(bizContent.toString());
AlipayTradeCancelResponse response = alipayClient.execute(request);
return response.isSuccess();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public boolean refundPay(String orderNo, BigDecimal money) throws RuntimeException {
try {
AlipayClient alipayClient = payHelper.getAlipayClient();
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
JSONObject bizContent = new JSONObject();
bizContent.put("refund_amount", money);
bizContent.put("out_trade_no", orderNo);
request.setBizContent(bizContent.toString());
AlipayTradeRefundResponse response = alipayClient.execute(request);
return response.isSuccess();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public boolean rsaCheckV1(Map<String, String> params) throws RuntimeException {
try {
String alipayPublicKey = configService.getValue("pay_alipay_public_key");
String charset = configService.getValue("pay_alipay_charset");
String signType = configService.getValue("pay_alipay_signType");
return AlipaySignature.rsaCheckV1(params, alipayPublicKey, charset, signType);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public boolean checkNotifyPayStatus(Map<String, String> params) throws RuntimeException {
// 取得支付宝POST过来的异步通知参数,进行验签
// 注意:调用SDK验证签名需要使用支付宝公钥,不要使用自己生成的RSA公私钥对
boolean verifyResult = this.rsaCheckV1(params);
if (verifyResult) {
// 验签通过
String tradeStatus = params.get("trade_status");
return "TRADE_SUCCESS".equals(tradeStatus) || "TRADE_FINISHED".equals(tradeStatus);
}
return false;
}
@Override
public String getOrderNo(Map<String, String> params) {
// 处理交易成功的逻辑
return params.get("out_trade_no");
}
}
支付宝相关配置参数
我的后台服务采用的都是参数配置型,并没有放到yaml文件中。上面的appid primarykey和publickey signtype按照支付宝官网上找到自己的,对于特定的publickey和primarykey可以下载支付宝证书生成工具本地生成,然后同步到支付宝
当面付线上案例:
模板码-网站源码,源码下载,模板插件,商业源码平台 (mubanma.com)
惠集会-专业的宠物用品在线购物平台 (huijihui.com)