注册支付宝支付功能需要个体工商户或企业才可以!需要有营业执照才能去申请哦!
进入支付宝开放平台 控制台
如果没有绑定APP支付就会报商家订单参数异常,请重新发起支付的错误
<dependency>
<groupId>com.alipay.sdkgroupId>
<artifactId>alipay-sdk-javaartifactId>
<version>4.22.32.ALLversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
# 支付宝支付
alipay:
server_url: https://openapi.alipay.com/gateway.do
app_id: 你的APPID
private_key: 应用私钥
format: json
charset: utf-8
alipay_public_key: 支付宝公钥
sign_type: RSA2
notifyUrl: 回调地址
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Getter
@Setter
@ToString
@Component
@ConfigurationProperties(prefix = "alipay")
public class AlipayConfig extends com.alipay.api.AlipayConfig {
private String serverUrl;
private String appId;
private String privateKey;
private String format;
private String charset;
private String alipayPublicKey;
private String signType;
private String notifyUrl;
}
import com.alipay.api.AlipayApiException;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 阿里云支付类
*/
@Service
public class BizAlipayService {
private static Logger logger = LoggerFactory.getLogger(BizAlipayService.class);
@Autowired
AlipayConfig alipayConfig;
private DefaultAlipayClient client() throws AlipayApiException {
return new DefaultAlipayClient(alipayConfig);
}
/**
* 预下单
*
* @param subject 订单标题
* @param outTradeNo 商家生成的订单号
* @param totalAmount 订单总价值
* @return
*/
public String appPay(String subject, String outTradeNo, String totalAmount) {
String source = "";
try {
DefaultAlipayClient client = client();
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setSubject(subject);
model.setOutTradeNo(outTradeNo);
model.setTotalAmount(totalAmount);
// alipay 封装的接口调用
AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
request.setBizModel(model);
request.setNotifyUrl(alipayConfig.getNotifyUrl());
AlipayTradeAppPayResponse response = client.sdkExecute(request);
source = response.getBody();
} catch (AlipayApiException e) {
logger.error("支付出现问题,详情:{}", e.getErrMsg());
e.printStackTrace();
}
return source;
}
}
接口代码
@RestController
@CrossOrigin // @CrossOrigin注解 解决uniapp跨域访问后端问题。
@RequestMapping("/productOrder")
public class UniProductOrderController {
@Autowired
private AlipayConfig alipayConfig;
@Autowired
private BizAlipayService alipayService;
/**
* 发起支付
*
* @return
*/
@GetMapping("/pay")
public Object pay() {
System.out.println("正在测试支付宝支付···");
String s = alipayService.appPay("测试支付", String.valueOf(System.currentTimeMillis()), new BigDecimal("0.01").toString());
System.out.println(s);
return s;
}
/**
* 订单回调
*
* @return
*/
@RequestMapping(method = RequestMethod.POST, value = "/notify")
public String orderNotify(HttpServletRequest request) {
Map<String, String> params = new HashMap<>();
Map<String, String[]> requestParams = request.getParameterMap();
for (String name : requestParams.keySet()) {
String[] values = requestParams.get(name);
String valueStr = "";
for (int i = 0; i < values.length; i++) {
valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
}
params.put(name, valueStr);
}
try {
boolean flag = AlipaySignature.rsaCheckV1(params, alipayConfig.getAlipayPublicKey(), alipayConfig.getCharset(), alipayConfig.getSignType());
if (flag) {
System.out.println("支付回调信息:"+ params);
return "success";
} else {
return "error";
}
} catch (AlipayApiException e) {
System.out.println("支付宝错误回调:"+e.getErrMsg());
e.printStackTrace();
return "error";
}
}
}
//发起支付
pay(){
let that = this
uni.request({
url: getApp().globalData.myurl + "/productOrder/pay",
data:{},
method: 'GET',
dataType: 'json',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
success(res) {
console.log(res);
uni.requestPayment({
provider: 'alipay',
orderInfo: res.data,
success(r) {
uni.showToast({
title:"支付成功",
icon: "success"
})
},
fail(e) {
uni.showToast({
title:"用户取消支付",
icon: "error"
})
},
complete: () => {
console.log("payment结束")
}
})
}
})
},
返回结果:
代码
/**
* 退款
*
* @param tradeNo
* @param totalAmount
* @return
*/
public AlipayTradeRefundResponse refund(String tradeNo, String totalAmount) {
try {
DefaultAlipayClient client = client();
AlipayTradeRefundModel alipayTradeRefundModel = new AlipayTradeRefundModel();
alipayTradeRefundModel.setTradeNo(tradeNo);
alipayTradeRefundModel.setRefundAmount(totalAmount);
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
request.setBizModel(alipayTradeRefundModel);
AlipayTradeRefundResponse response = client.execute(request);
return response;
} catch (AlipayApiException e) {
logger.error("退款出现问题,详情:{}", e.getErrMsg());
e.printStackTrace();
}
return null;
}
代码
/**
* 订单退款
*
* @return
* @TODO 仅实现了全部退款
*/
@RequestMapping(value = "/orderRefund", method = RequestMethod.GET)
public AlipayTradeRefundResponse orderRefund() {
AlipayTradeRefundResponse refund = alipayService.refund("2022020922001434041429269213", "0.01");
return refund;
}
本文仅供学习使用,本文参考博客园作者奔跑的砖头的文章感谢作者的详细说明以及代码 (*╯3╰) (*╯3╰) (*╯3╰)