支付宝退款功能
参考地址:https://opendocs.alipay.com/open/4b7cc5db_alipay.trade.refund?scene=common&pathHash=d98b006d
1、引入依赖
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
</dependency>
2、创建一个配置文件放支付宝支付的《必需参数》
alipay:
# alipay_url: https://openapi.alipay.com/gateway.do # 真实支付的url
alipay_url: https://openapi-sandbox.dl.alipaydev.com/gateway.do # 沙箱的url
app_id: # APPID
app_private_key: # 应用私钥
alipay_public_key: # 支付宝公钥
format: json # 数据格式
charset: utf-8 # 编码格式
sign_type: RSA2 # 加密方式
return_payment_url: # 通知地址,支付成功后点击完成后,会调用该路径接口。
notify_payment_url: # 异步通知地址,用于接收支付宝推送给商户的支付/退款成功的消息。
return_order_url: # 重定向地址,支付成功后点击完成后,会《重定向自动跳转回》商家页面地址。
3、创建一个类调用支付宝支付API,并获取配置文件值
package com.spzx.payment.configure;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AlipayConfig {
// 支付宝网关地址
@Value("${alipay.alipay_url}")
private String alipay_url;
// 应用的私钥
@Value("${alipay.app_private_key}")
private String app_private_key;
// 支付宝的appId
@Value("${alipay.app_id}")
private String app_id;
public final static String format="json";
public final static String charset="utf-8";
public final static String sign_type="RSA2";
public static String return_payment_url;
public static String notify_payment_url;
public static String return_order_url;
public static String alipay_public_key;
@Value("${alipay.alipay_public_key}")
public void setAlipay_public_key(String alipay_public_key) {
AlipayConfig.alipay_public_key = alipay_public_key;
}
@Value("${alipay.return_payment_url}")
public void setReturn_url(String return_payment_url) {
AlipayConfig.return_payment_url = return_payment_url;
}
@Value("${alipay.notify_payment_url}")
public void setNotify_url(String notify_payment_url) {
AlipayConfig.notify_payment_url = notify_payment_url;
}
@Value("${alipay.return_order_url}")
public void setReturn_order_url(String return_order_url) {
AlipayConfig.return_order_url = return_order_url;
}
// 创建一个client对象
@Bean
public AlipayClient alipayClient(){
AlipayClient alipayClient=new DefaultAlipayClient(alipay_url,app_id,app_private_key,format,charset, alipay_public_key,sign_type );
return alipayClient;
}
}
4、使用支付宝退款
https://opendocs.alipay.com/open/4b7cc5db_alipay.trade.refund?scene=common&pathHash=d98b006d
@Autowired
private AlipayConfig alipauConfig
@Override
public String AlipayTradeRefund(String orderNo){
//调用支付宝的退款接口
//支付宝退款接口对应的request AlipayTradeRefundRequest
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
//用于封装参数
AlipayTradeRefundModel model = new AlipayTradeRefundModel();
// 设置商户订单号
model.setOutTradeNo("20150320010101001");
// 设置支付宝交易号
model.setTradeNo("2014112611001004680073956707");
// 设置退款金额
model.setRefundAmount("200.12");
// 设置退款原因说明
model.setRefundReason("正常退款");
// 设置退款请求号
model.setOutRequestNo("HZ01RF001");
request.setBizModel(model);
try {
//调用支付宝退款接口
AlipayTradeRefundResponse response = alipayClient.execute(request);
boolean success = response.isSuccess();
if (!success){
throw new ServiceException("退款接口调用失败");
}
} catch (AlipayApiException e) {
throw new RuntimeException(e);
}
}
标签:alipay,支付宝,功能,return,String,url,payment,退款,public
From: https://www.cnblogs.com/21CHS/p/18528872