release--微信支付整合springboot

release-2024-04-25
wangqing 2 months ago
parent c40ea62b30
commit cb673b9ca4

@ -4,5 +4,5 @@ spring:
name: eureka
profile: dev
label: master
# uri: http://116.62.145.60:12000
uri: http://127.0.0.1:12000
uri: http://116.62.145.60:12000
# uri: http://127.0.0.1:12000

@ -9,6 +9,11 @@
</parent>
<artifactId>luoo_user</artifactId>
<dependencies>
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-java</artifactId>
<version>0.2.14</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
@ -204,6 +209,14 @@
<dockerHost>http://116.62.145.60:2375</dockerHost>
</configuration>
</plugin>
<!--跳过测试的执行-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<!--因为QueryDsl是类型安全的所以还需要加上Maven APT plugin使用 APT 自动生成Q类:-->
<groupId>com.mysema.maven</groupId>

@ -52,4 +52,14 @@ public class WxPayConfig {
*
*/
private String platformCertPath;
/**
*
*/
private String publicKeyPath;
/**
* id
*/
private String publicKeyId;
}

@ -0,0 +1,26 @@
package com.luoo.user.config;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAPublicKeyConfig;
import com.wechat.pay.java.core.notification.NotificationConfig;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
/**
*
*/
@Configuration
public class WxPayNotificationConfig {
@Resource
private WxPayConfig wxPayConfig;
public NotificationConfig config() {
// 初始化商户配置
NotificationConfig config = new RSAPublicKeyConfig.Builder().merchantId(wxPayConfig.getMchId())
. privateKeyFromPath(wxPayConfig.getPrivateKeyPath()).publicKeyFromPath (wxPayConfig.getPublicKeyPath()).publicKeyId(wxPayConfig.getPublicKeyId()).merchantSerialNumber(wxPayConfig.getMchSerialNo()).apiV3Key(wxPayConfig.getApiV3Key()).build();
return config;
}
}

@ -0,0 +1,26 @@
package com.luoo.user.config;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAPublicKeyConfig;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
/**
*
*/
@Configuration
public class WxPayPublicKeyConfig {
@Resource
private WxPayConfig wxPayConfig;
public Config config() {
// 初始化商户配置
Config config = new RSAPublicKeyConfig.Builder().merchantId(wxPayConfig.getMchId())
. privateKeyFromPath(wxPayConfig.getPrivateKeyPath()).publicKeyFromPath (wxPayConfig.getPublicKeyPath()).publicKeyId(wxPayConfig.getPublicKeyId()).merchantSerialNumber(wxPayConfig.getMchSerialNo()).apiV3Key(wxPayConfig.getApiV3Key()).build();
return config;
}
}

@ -0,0 +1,127 @@
package com.luoo.user.controller;
import api.Result;
import com.ijpay.core.kit.WxPayKit;
import com.luoo.user.config.WxPayConfig;
import com.luoo.user.config.WxPayNotificationConfig;
import com.luoo.user.config.WxPayPublicKeyConfig;
import com.luoo.user.service.WxPayApiV3Service;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.RSAPublicKeyConfig;
import com.wechat.pay.java.core.exception.HttpException;
import com.wechat.pay.java.core.exception.MalformedMessageException;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.core.exception.ValidationException;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RequestParam;
import com.wechat.pay.java.service.payments.app.AppService;
import com.wechat.pay.java.service.payments.app.model.*;
import controller.BaseController;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import util.IdWorker;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("/wechat")
@Api(tags = "WeChatApiV3PayController")
public class WeChatApiV3PayController extends BaseController {
// public static AppService service;
@Autowired
private WxPayNotificationConfig wxPayNotificationConfig;
@Autowired
private WxPayConfig wxPayConfig;
@Autowired
private WxPayApiV3Service wxPayApiV3Service;
// 下单接口
@GetMapping("/appPay")
public Result<Object> appPay() {
try {
PrepayResponse response = wxPayApiV3Service.prepay();
Map<String, String> map = WxPayKit.appCreateSign(wxPayConfig.getAppId(), wxPayConfig.getMchId(), response.getPrepayId(), wxPayConfig.getPrivateKeyPath());
return Result.success(map);
} catch (HttpException e) { // 发送HTTP请求失败
// 调用e.getHttpRequest()获取请求打印日志或上报监控更多方法见HttpException定义
e.getHttpRequest();
System.out.println( e.getHttpRequest());
} catch (ServiceException e) { // 服务返回状态小于200或大于等于300例如500
// 调用e.getResponseBody()获取返回体打印日志或上报监控更多方法见ServiceException定义
e.getResponseBody();
System.out.println(e.getResponseBody());
} catch (MalformedMessageException e) { // 服务返回成功,返回体类型不合法,或者解析返回体失败
// 调用e.getMessage()获取信息打印日志或上报监控更多方法见MalformedMessageException定义
e.getMessage();
System.out.println(e.getMessage());
} catch (Exception e) {
throw new RuntimeException(e);
}
return Result.success("");
}
@PostMapping("/payNotify")
public Result<Object> payNotify(@RequestBody String requestBody, HttpServletRequest request, HttpServletResponse response) {
String wechatTimestamp = request.getHeader("Wechatpay-Timestamp");
String wechatpayNonce = request.getHeader("Wechatpay-Nonce");
String wechatPaySerial = request.getHeader("Wechatpay-Serial");
String wechatSignature = request.getHeader("Wechatpay-Signature");
// request.
System.out.println("body:_____" + requestBody);
// 构造 RequestParam
RequestParam requestParam = new RequestParam.Builder().serialNumber(wechatPaySerial).nonce(wechatpayNonce).signature(wechatSignature).timestamp(wechatTimestamp).body(requestBody).build();
// 如果已经初始化了 RSAAutoCertificateConfig可直接使用
// 没有的话,则构造一个
// 初始化 NotificationParser
NotificationParser parser = new NotificationParser(wxPayNotificationConfig.config());
try {
// 以支付通知回调为例,验签、解密并转换成 Transaction
Transaction transaction = parser.parse(requestParam, Transaction.class);
System.out.println("transaction:_____" + transaction.toString());
} catch (ValidationException e) {
// 签名验证失败,返回 401 UNAUTHORIZED 状态码
// logger.error("sign verification failed", e);
System.out.println(e.getMessage());
return null;
}
return null;
}
}

@ -38,9 +38,9 @@ import java.util.Map;
* @author: yawei.huang
* @create: 2024-11-12 11:59
**/
@RestController
//@RestController
@CrossOrigin
@RequestMapping("/wx/pay")
//@RequestMapping("/wx/pay")
@Slf4j
public class WxPayController {

@ -0,0 +1,95 @@
package com.luoo.user.service;
import api.Result;
import com.ijpay.core.kit.WxPayKit;
import com.luoo.user.config.WxPayConfig;
import com.luoo.user.config.WxPayPublicKeyConfig;
import com.wechat.pay.java.core.exception.HttpException;
import com.wechat.pay.java.core.exception.MalformedMessageException;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.service.payments.app.AppService;
import com.wechat.pay.java.service.payments.app.model.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.IdWorker;
import javax.annotation.Resource;
import java.util.Map;
/**
* @program: luoo_parent
* @description:
* @author: wangqing
* @create: 2024-11-12 12:00
**/
@Slf4j
@Service
public class WxPayApiV3Service {
@Resource
private WxPayConfig wxPayConfig;
@Autowired
private IdWorker idWorker;
@Autowired
private WxPayPublicKeyConfig wxPayPublicKeyConfig;
public static AppService service;
/** APP支付下单 */
public PrepayResponse prepay() {
PrepayRequest request = new PrepayRequest();
// 初始化服务
service = new AppService.Builder().config(wxPayPublicKeyConfig.config()).build();
// 调用request.setXxx(val)设置所需参数具体参数可见Request定义
Amount amount = new Amount();
amount.setTotal(1);
request.setAmount(amount);
request.setAppid("wxae6fb76efa147314");
request.setMchid("1676389229");
request.setDescription("测试商品标题");
request.setNotifyUrl(wxPayConfig.getNotifyUrl());
request.setOutTradeNo(idWorker.nextId() + "");
// 调用接口
PrepayResponse response = service.prepay(request);
return service.prepay(request);
}
/** 关闭订单 */
public static void closeOrder() {
CloseOrderRequest request = new CloseOrderRequest();
// 调用request.setXxx(val)设置所需参数具体参数可见Request定义
// 调用接口
service.closeOrder(request);
}
/** 微信支付订单号查询订单 */
public static Transaction queryOrderById() {
QueryOrderByIdRequest request = new QueryOrderByIdRequest();
// 调用request.setXxx(val)设置所需参数具体参数可见Request定义
// 调用接口
return service.queryOrderById(request);
}
/** 商户订单号查询订单 */
public static Transaction queryOrderByOutTradeNo() {
QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
// 调用request.setXxx(val)设置所需参数具体参数可见Request定义
// 调用接口
return service.queryOrderByOutTradeNo(request);
}
}

@ -6,8 +6,8 @@ spring:
# profile: prod
label: master
# uri: http://116.62.145.60:13000
# uri: http://116.62.145.60:12000
uri: http://127.0.0.1:12000
uri: http://116.62.145.60:12000
# uri: http://127.0.0.1:12000
oauth2:
weixin:
@ -42,9 +42,16 @@ wx:
appId: wxae6fb76efa147314
mchId: 1676389229
mchSerialNo: 541E42E46254E026DE1E1D13602865CE049866C2
# mchSerialNo: 6A7BA1555FC56FBFAB2086527E88AA7FCA4A566F
apiV3Key: Indiecn2024181189288661529541569
notifyUrl: https://beta.indie.cn/user/wx/pay/payNotify
# privateKeyPath: /home/wxcert/apiclient_key.pem
privateKeyPath: D:\apiclient_key.pem
# privateCertPath: /home/wxcert/apiclient_cert.pem
privateCertPath: D:\apiclient_cert.pem
# platformCertPath: /home/wxcert/apiclient_cert.pem
platformCertPath: D:\apiclient_cert.pem
# publicKeyPath: /home/wxcert/pub_key.pem
publicKeyPath: D:\pub_key.pem
publicKeyId: PUB_KEY_ID_0116763892292024111200648800000198

Loading…
Cancel
Save