添加支付宝支付模块
This commit is contained in:
parent
29acbd6e19
commit
e499eff391
2
pom.xml
2
pom.xml
@ -32,7 +32,7 @@
|
|||||||
<oshi.version>6.4.11</oshi.version>
|
<oshi.version>6.4.11</oshi.version>
|
||||||
<commons.io.version>2.11.0</commons.io.version>
|
<commons.io.version>2.11.0</commons.io.version>
|
||||||
<commons.collections.version>3.2.2</commons.collections.version>
|
<commons.collections.version>3.2.2</commons.collections.version>
|
||||||
<poi.version>4.1.2</poi.version>
|
<poi.version>5.2.3</poi.version>
|
||||||
<httpclient.version>4.5.2</httpclient.version>
|
<httpclient.version>4.5.2</httpclient.version>
|
||||||
<velocity.version>2.3</velocity.version>
|
<velocity.version>2.3</velocity.version>
|
||||||
<jwt.version>0.9.1</jwt.version>
|
<jwt.version>0.9.1</jwt.version>
|
||||||
|
@ -5,4 +5,9 @@ pay:
|
|||||||
terminalKey: "terminalKey"
|
terminalKey: "terminalKey"
|
||||||
appId: "appId"
|
appId: "appId"
|
||||||
vendorSn: "vendorSn"
|
vendorSn: "vendorSn"
|
||||||
vendorKey: "vendorKey"
|
vendorKey: "vendorKey"
|
||||||
|
alipay:
|
||||||
|
appId: "appId"
|
||||||
|
privateKey: "privateKey"
|
||||||
|
publicKey: "publicKey"
|
||||||
|
notifyUrl: "notifyUrl"
|
@ -22,6 +22,16 @@
|
|||||||
<artifactId>ruoyi-pay-common</artifactId>
|
<artifactId>ruoyi-pay-common</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alipay.sdk</groupId>
|
||||||
|
<artifactId>alipay-easysdk</artifactId>
|
||||||
|
<version>2.2.0</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alipay.sdk</groupId>
|
||||||
|
<artifactId>alipay-sdk-java</artifactId>
|
||||||
|
<version>4.22.110.ALL</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -0,0 +1,66 @@
|
|||||||
|
package com.ruoyi.pay.alipay.config;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import com.alipay.easysdk.factory.Factory;
|
||||||
|
import com.alipay.easysdk.kernel.Config;
|
||||||
|
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class AliPayConfig {
|
||||||
|
@Value("${pay.alipay.appId}")
|
||||||
|
private String appId;
|
||||||
|
@Value("${pay.alipay.privateKey}")
|
||||||
|
private String privateKey;
|
||||||
|
@Value("${pay.alipay.publicKey}")
|
||||||
|
private String publicKey;
|
||||||
|
@Value("${pay.alipay.notifyUrl}")
|
||||||
|
private String notifyUrl;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
Config config = new Config();
|
||||||
|
config.protocol = "https";
|
||||||
|
config.gatewayHost = "openapi-sandbox.dl.alipaydev.com";
|
||||||
|
config.signType = "RSA2";
|
||||||
|
config.appId = this.appId;
|
||||||
|
config.merchantPrivateKey = this.privateKey;
|
||||||
|
config.alipayPublicKey = this.publicKey;
|
||||||
|
config.notifyUrl = this.notifyUrl;
|
||||||
|
Factory.setOptions(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAppId() {
|
||||||
|
return appId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAppId(String appId) {
|
||||||
|
this.appId = appId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPrivateKey() {
|
||||||
|
return privateKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrivateKey(String privateKey) {
|
||||||
|
this.privateKey = privateKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPublicKey() {
|
||||||
|
return publicKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPublicKey(String publicKey) {
|
||||||
|
this.publicKey = publicKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNotifyUrl() {
|
||||||
|
return notifyUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNotifyUrl(String notifyUrl) {
|
||||||
|
this.notifyUrl = notifyUrl;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,84 @@
|
|||||||
|
package com.ruoyi.pay.alipay.controller;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.alipay.easysdk.factory.Factory;
|
||||||
|
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
|
||||||
|
import com.ruoyi.common.annotation.Anonymous;
|
||||||
|
import com.ruoyi.pay.alipay.config.AliPayConfig;
|
||||||
|
import com.ruoyi.pay.domain.PayOrder;
|
||||||
|
import com.ruoyi.pay.mapper.PayOrderMapper;
|
||||||
|
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/alipay")
|
||||||
|
public class AliPayController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PayOrderMapper payOrderMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AliPayConfig aliPayConfig;
|
||||||
|
|
||||||
|
@GetMapping("/init")
|
||||||
|
public String init() {
|
||||||
|
aliPayConfig.init();
|
||||||
|
return "success";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Anonymous
|
||||||
|
@GetMapping("/pay")
|
||||||
|
public String pay(PayOrder payOrder) {
|
||||||
|
AlipayTradePagePayResponse response;
|
||||||
|
try {
|
||||||
|
// 发起API调用(以创建当面付收款二维码为例)
|
||||||
|
response = Factory.Payment.Page()
|
||||||
|
.pay(payOrder.getOrderContent(), payOrder.getOrderNumber(), payOrder.getTotalAmount(), "");
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("调用遭遇异常,原因:" + e.getMessage());
|
||||||
|
throw new RuntimeException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
return response.getBody();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/notify") // 注意这里必须是POST接口
|
||||||
|
public String payNotify(HttpServletRequest request) throws Exception {
|
||||||
|
if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
|
||||||
|
System.out.println("=========支付宝异步回调========");
|
||||||
|
|
||||||
|
Map<String, String> params = new HashMap<>();
|
||||||
|
Map<String, String[]> requestParams = request.getParameterMap();
|
||||||
|
for (String name : requestParams.keySet()) {
|
||||||
|
params.put(name, request.getParameter(name));
|
||||||
|
// System.out.println(name + " = " + request.getParameter(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
String orderNumber = params.get("out_trade_no");
|
||||||
|
// 支付宝验签
|
||||||
|
if (Factory.Payment.Common().verifyNotify(params)) {
|
||||||
|
// 验签通过
|
||||||
|
System.out.println("交易名称: " + params.get("subject"));
|
||||||
|
System.out.println("交易状态: " + params.get("trade_status"));
|
||||||
|
System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
|
||||||
|
System.out.println("商户订单号: " + params.get("out_trade_no"));
|
||||||
|
System.out.println("交易金额: " + params.get("total_amount"));
|
||||||
|
System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));
|
||||||
|
System.out.println("买家付款时间: " + params.get("gmt_payment"));
|
||||||
|
System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));
|
||||||
|
|
||||||
|
PayOrder payOrder = payOrderMapper.selectPayOrderByOrderNumber(orderNumber);
|
||||||
|
payOrder.setOrderStatus("已支付");
|
||||||
|
payOrderMapper.updatePayOrder(payOrder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "success";
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user