WechatPay.java
4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.aigeo.util.pay;
import com.aigeo.entity.ApiConfig;
import com.aigeo.entity.PayDto;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.Amount;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
@Component
public class WechatPay implements PaymentStrategy{
/** 全局单例配置 */
//private static volatile Config globalConfig;
//@Autowired
//private ApiConfigDao apiConfigDao;
private static String HOST = "https://api.mch.weixin.qq.com";
private static String METHOD = "POST";
private static String PATH = "/v3/pay/partner/transactions/out-trade-no/{out_trade_no}/close";
/**
* 初始化完毕(所有 @Value 已注入)后,构造一次并缓存
*/
//@PostConstruct
//public void initConfig() throws Exception {
// if (globalConfig == null) {
// synchronized (WechatPay.class) {
// if (globalConfig == null) {
// ApiConfig wechatapy = apiConfigDao.findByApiCode(7,"wechatpay");
// if(wechatapy==null){
// throw new RuntimeException("微信支付初始化失败!");
// }
// globalConfig = new RSAAutoCertificateConfig.Builder()
// .merchantId(wechatapy.getMerchantId())
// .privateKeyFromPath(wechatapy.getPrivateKeyPath())
// .merchantSerialNumber(wechatapy.getApiCode())
// .apiV3Key(wechatapy.getApiKey())
// .build();
// }
// }
// }
//}
@Override
public String pay(PayDto payDto) {
ApiConfig apiConfig = payDto.getApiConfig();
String path = this.getClass().getResource("/").getPath();
path = path.replace("/", "//");
path = path.replace("//target//classes//", "");
path = path.replace("//WEB-INF//classes//", "");
Config globalConfig=new RSAAutoCertificateConfig.Builder()
.merchantId(apiConfig.getMerchantId())
.privateKeyFromPath(path+apiConfig.getPrivateKeyPath())
.merchantSerialNumber(apiConfig.getApiCode())
.apiV3Key(apiConfig.getApiKey())
.build();
NativePayService service = new NativePayService.Builder()
.config(globalConfig) // 复用全局配置
.build();
// request.setXxx(val)设置所需参数,具体参数可见Request定义
PrepayRequest request = new PrepayRequest();
Amount amount = new Amount();
BigDecimal money = new BigDecimal(payDto.getAmount()).multiply(new BigDecimal("100"));
amount.setTotal(money.setScale(0, RoundingMode.FLOOR).intValueExact());
request.setAmount(amount);
request.setAppid(payDto.getApiConfig().getApiId());
request.setMchid(payDto.getApiConfig().getMerchantId());
request.setDescription(payDto.getDesc());
request.setNotifyUrl(payDto.getApiConfig().getApiRedirectUri()+"/"+payDto.getCompanyId());
request.setOutTradeNo(payDto.getOrderId());
request.setTimeExpire(getTime());
// 调用下单方法,得到应答
PrepayResponse response = service.prepay(request);
// 使用微信扫描 code_url 对应的二维码,即可体验Native支付
System.out.println("**-**-*--*-*-**--*-*-*-*-*-*-*"+response);
System.out.println("*-*-*-*-*-*-*-*-*-*-*-"+response.getCodeUrl()+"-*-**-*-*--**--*-*");
return response.getCodeUrl();
}
/**
* 关闭订单
* @param orderId 订单号
* @param mchId 商户号
*/
@Override
public void closeTrade(String orderId, String mchId,ApiConfig apiConfig) {
}
/**
* 退款申请
*/
@Override
public void refund() {
}
private String getTime(){
// 获取当前时间
ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault());
// 加上十分钟
ZonedDateTime thirtyMinutesLater = now.plusMinutes(10);
// 定义目标格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");
// 格式化为字符串
String formattedTime = thirtyMinutesLater.format(formatter);
System.out.println("订单结束时间:"+formattedTime);
// 输出结果
return formattedTime;
}
@Override
public String getChannel() {
return "wechatpay";
}
}