PayController.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. package com.ylx.web.controller.massage;
  2. import cn.hutool.core.date.DatePattern;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.io.file.FileWriter;
  5. import cn.hutool.core.util.StrUtil;
  6. import cn.hutool.http.ContentType;
  7. import cn.hutool.json.JSONArray;
  8. import cn.hutool.json.JSONObject;
  9. import cn.hutool.json.JSONUtil;
  10. import com.ijpay.core.IJPayHttpResponse;
  11. import com.ijpay.core.enums.AuthTypeEnum;
  12. import com.ijpay.core.enums.RequestMethodEnum;
  13. import com.ijpay.core.kit.AesUtil;
  14. import com.ijpay.core.kit.HttpKit;
  15. import com.ijpay.core.kit.PayKit;
  16. import com.ijpay.core.kit.WxPayKit;
  17. import com.ijpay.core.utils.DateTimeZoneUtil;
  18. import com.ijpay.wxpay.WxPayApi;
  19. import com.ijpay.wxpay.WxPayApiConfigKit;
  20. import com.ijpay.wxpay.enums.WxDomainEnum;
  21. import com.ijpay.wxpay.enums.v3.BasePayApiEnum;
  22. import com.ijpay.wxpay.enums.v3.CertAlgorithmTypeEnum;
  23. import com.ijpay.wxpay.enums.v3.TransferApiEnum;
  24. import com.ijpay.wxpay.model.v3.*;
  25. import com.ylx.common.config.WxPayConfig;
  26. import com.ylx.common.core.domain.R;
  27. import com.ylx.massage.domain.TRecharge;
  28. import com.ylx.massage.enums.BillTypeEnum;
  29. import com.ylx.massage.service.RefundVoucherService;
  30. import com.ylx.massage.service.TOrderService;
  31. import com.ylx.massage.service.TRechargeService;
  32. import io.swagger.annotations.Api;
  33. import io.swagger.annotations.ApiOperation;
  34. import lombok.extern.slf4j.Slf4j;
  35. import org.springframework.beans.factory.annotation.Autowired;
  36. import org.springframework.web.bind.annotation.*;
  37. import javax.annotation.Resource;
  38. import javax.servlet.http.HttpServletRequest;
  39. import javax.servlet.http.HttpServletResponse;
  40. import java.io.ByteArrayInputStream;
  41. import java.math.BigDecimal;
  42. import java.nio.charset.StandardCharsets;
  43. import java.security.cert.X509Certificate;
  44. import java.util.*;
  45. import static com.ylx.common.constant.HttpStatus.SUCCESS;
  46. /**
  47. * @author jianlong
  48. * @date 2024-04-03 15:27
  49. */
  50. @RestController
  51. @Slf4j
  52. @RequestMapping("/wx/pay")
  53. @Api(tags = {"微信支付"})
  54. public class PayController {
  55. @Autowired
  56. private WxPayConfig wxPayProperties;
  57. @Resource
  58. private TRechargeService rechargeService;
  59. @Resource
  60. private TOrderService orderService;
  61. @Resource
  62. private RefundVoucherService refundVoucherService;
  63. String serialNo;
  64. String platSerialNo;
  65. /**
  66. * 小程序微信支付的第一步,统一下单
  67. */
  68. @PostMapping("/pay")
  69. @ApiOperation("AIPV3微信支付充值")
  70. public R createUnifiedOrder(@RequestBody TRecharge recharge) throws Exception {
  71. TRecharge rechargeResp = rechargeService.recharge(recharge);
  72. return rechargeService.getPay(rechargeResp.getRechargeNo(), recharge.getdMoney(), recharge.getcOpenId(), BillTypeEnum.RECHARGE.getInfo(), BillTypeEnum.RECHARGE.getCode().toString());
  73. }
  74. public R<String> getPay(String setOutTradeNo, BigDecimal amount, String openId, String description) throws Exception {
  75. String timeExpire = DateTimeZoneUtil.dateToTimeZone(System.currentTimeMillis() + 1000 * 60 * 3);
  76. UnifiedOrderModel unifiedOrderModel = new UnifiedOrderModel()
  77. .setAppid(wxPayProperties.getAppId())
  78. .setMchid(wxPayProperties.getMchId())
  79. //商品描述
  80. .setDescription(description)
  81. //订单号
  82. .setOut_trade_no(setOutTradeNo)
  83. //交易结束时间
  84. .setTime_expire(timeExpire)
  85. //附加数据
  86. .setAttach("夜来香")
  87. //通知地址异步接收微信支付结果通知的回调地址,通知url必须为外网可访问的url,不能携带参数。 公网域名必须为https,如果是走专线接入,使用专线NAT IP或者私有回调域名可使用http
  88. //示例值:https://www.weixin.qq.com/wxpay/pay.php
  89. .setNotify_url(wxPayProperties.getNotifyUrl())
  90. //支付金额以分为单位
  91. .setAmount(new Amount().setTotal(amount.multiply(new BigDecimal(100)).intValue()))
  92. //交易人
  93. .setPayer(new Payer().setOpenid(openId));
  94. log.info("统一下单参数 {}", JSONUtil.toJsonStr(unifiedOrderModel));
  95. IJPayHttpResponse response = WxPayApi.v3(
  96. RequestMethodEnum.POST,
  97. WxDomainEnum.CHINA.toString(),
  98. BasePayApiEnum.JS_API_PAY.toString(),
  99. wxPayProperties.getMchId(),
  100. getSerialNumber(),
  101. null,
  102. wxPayProperties.getCertKeyPath(),
  103. JSONUtil.toJsonStr(unifiedOrderModel)
  104. );
  105. log.info("统一下单响应 {}", response);
  106. // 根据证书序列号查询对应的证书来验证签名结果
  107. boolean verifySignature = WxPayKit.verifySignature(response, wxPayProperties.getPlatFormPath());
  108. log.info("verifySignature: {}", verifySignature);
  109. if (response.getStatus() == SUCCESS && verifySignature) {
  110. String body = response.getBody();
  111. JSONObject jsonObject = JSONUtil.parseObj(body);
  112. String prepayId = jsonObject.getStr("prepay_id");
  113. Map<String, String> map = WxPayKit.jsApiCreateSign(wxPayProperties.getAppId(), prepayId, wxPayProperties.getCertKeyPath());
  114. log.info("唤起支付参数:{}", map);
  115. return R.ok(JSONUtil.toJsonStr(map));
  116. }
  117. return R.ok(JSONUtil.toJsonStr(response));
  118. }
  119. private String getSerialNumber() {
  120. if (StrUtil.isEmpty(serialNo)) {
  121. // 获取证书序列号
  122. X509Certificate certificate = PayKit.getCertificate(wxPayProperties.getCertPath());
  123. if (null != certificate) {
  124. serialNo = certificate.getSerialNumber().toString(16).toUpperCase();
  125. // 提前两天检查证书是否有效
  126. boolean isValid = PayKit.checkCertificateIsValid(certificate, wxPayProperties.getMchId(), -2);
  127. log.info("证书是否可用 {} 证书有效期为 {}", isValid, DateUtil.format(certificate.getNotAfter(), DatePattern.NORM_DATETIME_PATTERN));
  128. }
  129. // System.out.println("输出证书信息:\n" + certificate.toString());
  130. // // 输出关键信息,截取部分并进行标记
  131. // System.out.println("证书序列号:" + certificate.getSerialNumber().toString(16));
  132. // System.out.println("版本号:" + certificate.getVersion());
  133. // System.out.println("签发者:" + certificate.getIssuerDN());
  134. // System.out.println("有效起始日期:" + certificate.getNotBefore());
  135. // System.out.println("有效终止日期:" + certificate.getNotAfter());
  136. // System.out.println("主体名:" + certificate.getSubjectDN());
  137. // System.out.println("签名算法:" + certificate.getSigAlgName());
  138. // System.out.println("签名:" + certificate.getSignature().toString());
  139. }
  140. System.out.println("serialNo:" + serialNo);
  141. return serialNo;
  142. }
  143. @RequestMapping(value = "/payNotify", method = {org.springframework.web.bind.annotation.RequestMethod.POST, org.springframework.web.bind.annotation.RequestMethod.GET})
  144. @ResponseBody
  145. @ApiOperation("微信支付回调接口")
  146. public void payNotify(HttpServletRequest request, HttpServletResponse response) {
  147. log.info("微信支付回调接口====================================>>>>微信支付回调接口");
  148. Map<String, String> map = new HashMap<>(12);
  149. try {
  150. String timestamp = request.getHeader("Wechatpay-Timestamp");
  151. String nonce = request.getHeader("Wechatpay-Nonce");
  152. String serialNo = request.getHeader("Wechatpay-Serial");
  153. String signature = request.getHeader("Wechatpay-Signature");
  154. log.info("timestamp:{} nonce:{} serialNo:{} signature:{}", timestamp, nonce, serialNo, signature);
  155. String result = HttpKit.readData(request);
  156. log.info("支付通知密文 {}", result);
  157. // 需要通过证书序列号查找对应的证书,verifyNotify 中有验证证书的序列号
  158. String plainText = WxPayKit.verifyNotify(serialNo, result, signature, nonce, timestamp,
  159. wxPayProperties.getMchKey(), wxPayProperties.getPlatFormPath());
  160. log.info("支付通知明文 {}", plainText);
  161. if (StrUtil.isNotEmpty(plainText)) {
  162. response.setStatus(200);
  163. map.put("code", "SUCCESS");
  164. map.put("message", "SUCCESS");
  165. // 处理业务逻辑
  166. JSONObject jsonObject = new JSONObject(plainText);
  167. if (jsonObject.get("attach").equals(BillTypeEnum.WX_PAY.getCode().toString())) {
  168. // 订单支付成功
  169. orderService.payNotifyOrder(jsonObject.get("out_trade_no").toString());
  170. } else {
  171. TRecharge outTradeNo = rechargeService.increaseAmount(jsonObject.get("out_trade_no").toString());
  172. }
  173. } else {
  174. response.setStatus(500);
  175. map.put("code", "ERROR");
  176. map.put("message", "签名错误");
  177. }
  178. response.setHeader("Content-type", ContentType.JSON.toString());
  179. response.getOutputStream().write(JSONUtil.toJsonStr(map).getBytes(StandardCharsets.UTF_8));
  180. response.flushBuffer();
  181. } catch (Exception e) {
  182. log.error("系统异常", e);
  183. }
  184. }
  185. @RequestMapping(value = "/test", method = {org.springframework.web.bind.annotation.RequestMethod.POST, org.springframework.web.bind.annotation.RequestMethod.GET})
  186. @ResponseBody
  187. @ApiOperation("测试")
  188. public void test(HttpServletRequest request, HttpServletResponse response) {
  189. System.out.println("test=======================>");
  190. }
  191. @RequestMapping("/get")
  192. @ResponseBody
  193. public String v3Get() throws Exception {
  194. // 获取平台证书列表
  195. try {
  196. IJPayHttpResponse response = WxPayApi.v3(
  197. RequestMethodEnum.GET,
  198. WxDomainEnum.CHINA.toString(),
  199. CertAlgorithmTypeEnum.getCertSuffixUrl(CertAlgorithmTypeEnum.RSA.getCode()),
  200. wxPayProperties.getMchId(),
  201. getSerialNumber(),
  202. null,
  203. wxPayProperties.getCertKeyPath(),
  204. "",
  205. AuthTypeEnum.RSA.getCode()
  206. );
  207. Map<String, List<String>> headers = response.getHeaders();
  208. log.info("请求头: {}", headers);
  209. String timestamp = response.getHeader("Wechatpay-Timestamp");
  210. String nonceStr = response.getHeader("Wechatpay-Nonce");
  211. String serialNumber = response.getHeader("Wechatpay-Serial");
  212. String signature = response.getHeader("Wechatpay-Signature");
  213. String body = response.getBody();
  214. int status = response.getStatus();
  215. log.info("serialNumber: {}", serialNumber);
  216. log.info("status: {}", status);
  217. log.info("body: {}", body);
  218. int isOk = 200;
  219. if (status == isOk) {
  220. JSONObject jsonObject = JSONUtil.parseObj(body);
  221. JSONArray dataArray = jsonObject.getJSONArray("data");
  222. // 默认认为只有一个平台证书
  223. JSONObject encryptObject = dataArray.getJSONObject(0);
  224. JSONObject encryptCertificate = encryptObject.getJSONObject("encrypt_certificate");
  225. String associatedData = encryptCertificate.getStr("associated_data");
  226. String cipherText = encryptCertificate.getStr("ciphertext");
  227. String nonce = encryptCertificate.getStr("nonce");
  228. String algorithm = encryptCertificate.getStr("algorithm");
  229. String serialNo = encryptObject.getStr("serial_no");
  230. final String platSerialNo = savePlatformCert(associatedData, nonce, cipherText, algorithm, wxPayProperties.getPlatFormPath());
  231. log.info("平台证书序列号: {} serialNo: {}", platSerialNo, serialNo);
  232. // 根据证书序列号查询对应的证书来验证签名结果
  233. boolean verifySignature = WxPayKit.verifySignature(response, wxPayProperties.getPlatFormPath());
  234. log.info("verifySignature:{}", verifySignature);
  235. }
  236. return body;
  237. } catch (Exception e) {
  238. log.error("获取平台证书列表异常", e);
  239. return null;
  240. }
  241. }
  242. private String savePlatformCert(String associatedData, String nonce, String cipherText, String algorithm, String certPath) {
  243. try {
  244. String key3 = wxPayProperties.getMchKey();
  245. String publicKey;
  246. if (StrUtil.equals(algorithm, AuthTypeEnum.SM2.getPlatformCertAlgorithm())) {
  247. publicKey = PayKit.sm4DecryptToString(key3, cipherText, nonce, associatedData);
  248. } else {
  249. AesUtil aesUtil = new AesUtil(wxPayProperties.getMchKey().getBytes(StandardCharsets.UTF_8));
  250. // 平台证书密文解密
  251. // encrypt_certificate 中的 associated_data nonce ciphertext
  252. publicKey = aesUtil.decryptToString(
  253. associatedData.getBytes(StandardCharsets.UTF_8),
  254. nonce.getBytes(StandardCharsets.UTF_8),
  255. cipherText
  256. );
  257. }
  258. if (StrUtil.isNotEmpty(publicKey)) {
  259. // 保存证书
  260. FileWriter writer = new FileWriter(certPath);
  261. writer.write(publicKey);
  262. // 获取平台证书序列号
  263. X509Certificate certificate = PayKit.getCertificate(new ByteArrayInputStream(publicKey.getBytes()));
  264. return certificate.getSerialNumber().toString(16).toUpperCase();
  265. }
  266. return "";
  267. } catch (Exception e) {
  268. log.error("保存平台证书异常", e);
  269. return e.getMessage();
  270. }
  271. }
  272. @RequestMapping("/batchTransfer")
  273. @ApiOperation("微信批量提现")
  274. @ResponseBody
  275. public String batchTransfer(@RequestParam(value = "openId", required = false, defaultValue = "o-_-itxuXeGW3O1cxJ7FXNmq8Wf8") String openId) {
  276. try {
  277. BatchTransferModel batchTransferModel = new BatchTransferModel()
  278. .setAppid(wxPayProperties.getAppId())
  279. .setOut_batch_no(PayKit.generateStr())
  280. .setBatch_name("IJPay 测试微信转账到零钱")
  281. .setBatch_remark("IJPay 测试微信转账到零钱")
  282. .setTotal_amount(1)
  283. .setTotal_num(1)
  284. .setTransfer_detail_list(Collections.singletonList(
  285. new TransferDetailInput()
  286. .setOut_detail_no(PayKit.generateStr())
  287. .setTransfer_amount(1)
  288. .setTransfer_remark("IJPay 测试微信转账到零钱")
  289. .setOpenid(openId)));
  290. log.info("发起商家转账请求参数 {}", JSONUtil.toJsonStr(batchTransferModel));
  291. IJPayHttpResponse response = WxPayApi.v3(
  292. RequestMethodEnum.POST,
  293. WxDomainEnum.CHINA.toString(),
  294. TransferApiEnum.TRANSFER_BATCHES.toString(),
  295. wxPayProperties.getMchId(),
  296. getSerialNumber(),
  297. null,
  298. wxPayProperties.getCertKeyPath(),
  299. JSONUtil.toJsonStr(batchTransferModel)
  300. );
  301. log.info("发起商家转账响应 {}", response);
  302. // 根据证书序列号查询对应的证书来验证签名结果
  303. boolean verifySignature = WxPayKit.verifySignature(response, wxPayProperties.getPlatFormPath());
  304. log.info("verifySignature: {}", verifySignature);
  305. if (response.getStatus() == SUCCESS && verifySignature) {
  306. return response.getBody();
  307. }
  308. return JSONUtil.toJsonStr(response);
  309. } catch (Exception e) {
  310. log.error("系统异常", e);
  311. return e.getMessage();
  312. }
  313. }
  314. @RequestMapping("/refund")
  315. @ResponseBody
  316. public String refund(@RequestParam(required = false) String outRefundNo, @RequestParam(required = false) BigDecimal amount,@RequestParam(required = false) String transactionId, @RequestParam(required = false) String outTradeNo) {
  317. return rechargeService.refund(outRefundNo,transactionId,outTradeNo,amount);
  318. }
  319. // /**
  320. // * 退款通知
  321. // */
  322. // @RequestMapping(value = "/refundNotify", method = {RequestMethod.POST, RequestMethod.GET})
  323. // @ResponseBody
  324. // @ApiOperation("微信退款回调接口")
  325. // public String refundNotify(HttpServletRequest request) {
  326. // String xmlMsg = HttpKit.readData(request);
  327. // log.info("退款通知=" + xmlMsg);
  328. // Map<String, String> params = WxPayKit.xmlToMap(xmlMsg);
  329. // log.info("退款通知parms:{}",params);
  330. // String returnCode = params.get("event_type");
  331. // // 注意重复通知的情况,同一订单号可能收到多次通知,请注意一定先判断订单状态
  332. // if (returnCode.equals("REFUND.SUCCESS")) {
  333. // String reqInfo = params.get("resource");
  334. // String decryptData = WxPayKit.decryptData(reqInfo, WxPayApiConfigKit.getWxPayApiConfig().getPartnerKey());
  335. // log.info("退款通知解密后的数据=" + decryptData);
  336. //// refundVoucherService.
  337. // // 更新订单信息
  338. // // 发送通知等
  339. // Map<String, String> xml = new HashMap<String, String>(2);
  340. // xml.put("return_code", "SUCCESS");
  341. // xml.put("return_msg", "OK");
  342. // return WxPayKit.toXml(xml);
  343. // }
  344. // return null;
  345. // }
  346. @ApiOperation("微信退款回调接口")
  347. @RequestMapping(value = "/refundNotify", method = {org.springframework.web.bind.annotation.RequestMethod.POST, org.springframework.web.bind.annotation.RequestMethod.GET})
  348. public void refundWechatCallback(HttpServletRequest request, HttpServletResponse response) {
  349. log.info("微信退款回调接口====================================>>>>微信退款回调接口");
  350. Map<String, String> map = new HashMap<>(12);
  351. try {
  352. String timestamp = request.getHeader("Wechatpay-Timestamp");
  353. String nonce = request.getHeader("Wechatpay-Nonce");
  354. String serialNo = request.getHeader("Wechatpay-Serial");
  355. String signature = request.getHeader("Wechatpay-Signature");
  356. log.info("timestamp:{} nonce:{} serialNo:{} signature:{}", timestamp, nonce, serialNo, signature);
  357. String result = HttpKit.readData(request);
  358. log.info("退款通知密文 {}", result);
  359. // 需要通过证书序列号查找对应的证书,verifyNotify 中有验证证书的序列号
  360. String plainText = WxPayKit.verifyNotify(serialNo, result, signature, nonce, timestamp,
  361. wxPayProperties.getMchKey(), wxPayProperties.getPlatFormPath());
  362. log.info("退款通知明文 {}", plainText);
  363. if (StrUtil.isNotEmpty(plainText)) {
  364. response.setStatus(200);
  365. map.put("code", "SUCCESS");
  366. map.put("message", "SUCCESS");
  367. // 处理业务逻辑
  368. } else {
  369. response.setStatus(500);
  370. map.put("code", "ERROR");
  371. map.put("message", "签名错误");
  372. }
  373. response.setHeader("Content-type", ContentType.JSON.toString());
  374. response.getOutputStream().write(JSONUtil.toJsonStr(map).getBytes(StandardCharsets.UTF_8));
  375. response.flushBuffer();
  376. } catch (Exception e) {
  377. log.error("系统异常", e);
  378. }
  379. }
  380. }