PayController.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.example.xiaoshiweixinback.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.example.xiaoshiweixinback.business.common.Constants;
  4. import com.example.xiaoshiweixinback.business.common.Response;
  5. import com.example.xiaoshiweixinback.business.common.base.Records;
  6. import com.example.xiaoshiweixinback.business.exception.BusinessException;
  7. import com.example.xiaoshiweixinback.business.utils.JsonUtils;
  8. import com.example.xiaoshiweixinback.business.utils.LoginUtils;
  9. import com.example.xiaoshiweixinback.entity.weixinPay.*;
  10. import com.example.xiaoshiweixinback.service.OrderService;
  11. import com.example.xiaoshiweixinback.service.VipService;
  12. import com.example.xiaoshiweixinback.service.weixinpay.AuthorizationService;
  13. import com.example.xiaoshiweixinback.service.weixinpay.WeixinPayService;
  14. import io.swagger.v3.oas.annotations.Operation;
  15. import jakarta.servlet.http.HttpServletRequest;
  16. import org.apache.commons.compress.utils.IOUtils;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.web.bind.annotation.*;
  19. import java.io.IOException;
  20. import java.nio.charset.StandardCharsets;
  21. @RequestMapping(Constants.XIAOSHI_WEIXINBACK + "/weixinpay")
  22. @RestController
  23. public class PayController {
  24. @Autowired
  25. private WeixinPayService weixinPayService;
  26. @Autowired
  27. private AuthorizationService authorizationService;
  28. @Autowired
  29. private OrderService orderService;
  30. @Operation(summary = "回调成功")
  31. @PostMapping("/success")
  32. public String success(@RequestBody WeiXinSuccessDTO weiXinSuccessDTO) throws IOException {
  33. try {
  34. WeiXinSuccessDTO.Resource resource = weiXinSuccessDTO.getResource();
  35. WeixinSuccessVO weixinSuccessVO = authorizationService.decryptMessage(resource.getAssociated_data(), resource.getNonce(), resource.getCiphertext());
  36. String tradeNo = weixinSuccessVO.getOut_trade_no();
  37. orderService.payBack(tradeNo);
  38. } catch (Exception e) {
  39. return "";
  40. }
  41. return "success";
  42. }
  43. @Operation(summary = "下单并获取订单信息")
  44. @PostMapping("/getPayTickets")
  45. public Response getPayTickets(@RequestBody WeiXinPayDTO weiXinPayDTO) {
  46. Records records = new Records();
  47. try {
  48. GetPayTicketVO getPayTicketVO = weixinPayService.getPayTickets(weiXinPayDTO);
  49. if(getPayTicketVO==null){
  50. return Response.error("901","支付成功");
  51. }
  52. records.setData(getPayTicketVO);
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. if(e instanceof BusinessException) {
  56. BusinessException e2 =(BusinessException) e;
  57. return Response.error(e2.getErrorCode(), e2.getMessage());
  58. }
  59. }
  60. return Response.success(records);
  61. }
  62. @Operation(summary = "回调成功")
  63. @PostMapping("/testAuthorization")
  64. public String testAuthorization() {
  65. try {
  66. String associatedData = "transaction";
  67. String nonce = "yxul9eBG7PQp";
  68. String ciphertext = "UivUAmQ3r289Wv1q3Hbi7CmnQseCD6EHmuAE1301YMFU41IPkE/98NsTBb7pygTK7dc1Z4AS05sxlHcUMSeJB5KXn7H3l9vfvW538WhoxbbmBqafH9hTUT32A0dUz9LB4Kuw7yqyJl2af70VaZ7O/XDlYR0wAzNFLjsfvJMx0hEIGQQVnuBuCgkZitI5vwvBZN8WIuFdldGqjbRDcCUe7NHVAho2Ph+PRuSbocO7ijtbqoVS8NHfTKVl23bDrHoopuAH/7W6UGX6BD2QMBk4FTm3HmGO0TPmIxrgwIkDz91HyZ3mNmc3A4Fd4p7y7XqOfABGcaSzb0ZY0M2o/gmIDCXS9l3ZY0Rz8D4eo7ddWLeK5hdwWQmVIMqNh+bqt0OEA9KiYFWqfqU9G2DTZbd6r3MYPAAm7NTacu+/iI9mittq0DfsGn66MFGL8WFqMQCpUP9CqsCHQPxnCW80s7m1YJBL5SA8SwogvnIz5FK6bad+own1JCQmuwsmqNkdcnAr48u6OesmUa/OFdOSL+pb1UDaea5O4K/Zp/GhmUFC4JNpreBoZS4SYhs5OTfdgiKV4wEBjrz4";
  69. WeixinSuccessVO weixinSuccessVO = authorizationService.decryptMessage(associatedData, nonce, ciphertext);
  70. System.out.println(weixinSuccessVO);
  71. return "a";
  72. } catch (Exception e) {
  73. e.printStackTrace();
  74. }
  75. return "success";
  76. }
  77. @Operation(summary = "回调成功")
  78. @GetMapping("/testSuccess")
  79. public String testSuccess(String tradeNo) throws IOException {
  80. try {
  81. orderService.payBack(tradeNo);
  82. } catch (Exception e) {
  83. return "";
  84. }
  85. return "success";
  86. }
  87. }