WxController.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package com.izouma.awesomeAdmin.web;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
  4. import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
  5. import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
  6. import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
  7. import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
  8. import com.github.binarywang.wxpay.constant.WxPayConstants;
  9. import com.github.binarywang.wxpay.exception.WxPayException;
  10. import com.github.binarywang.wxpay.service.WxPayService;
  11. import com.izouma.awesomeAdmin.exception.BusinessException;
  12. import io.swagger.annotations.ApiOperation;
  13. import lombok.AllArgsConstructor;
  14. import lombok.extern.slf4j.Slf4j;
  15. import me.chanjar.weixin.common.bean.WxJsapiSignature;
  16. import me.chanjar.weixin.common.error.WxErrorException;
  17. import me.chanjar.weixin.mp.api.WxMpService;
  18. import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
  19. import me.chanjar.weixin.mp.bean.result.WxMpUser;
  20. import org.apache.commons.lang3.RandomStringUtils;
  21. import org.springframework.core.env.Environment;
  22. import org.springframework.web.bind.annotation.*;
  23. import org.springframework.web.servlet.ModelAndView;
  24. import javax.servlet.http.HttpServletRequest;
  25. import javax.servlet.http.HttpServletResponse;
  26. import java.io.IOException;
  27. @Slf4j
  28. @AllArgsConstructor
  29. @RestController
  30. @RequestMapping("/wx")
  31. public class WxController {
  32. private WxMpService wxMpService;
  33. private WxPayService wxPayService;
  34. private Environment env;
  35. @RequestMapping("/testMp")
  36. public ModelAndView testPay(@RequestParam(required = false) String code, HttpServletRequest request,
  37. HttpServletResponse response) throws IOException {
  38. try {
  39. WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code);
  40. WxMpUser wxMpUser = wxMpService.oauth2getUserInfo(accessToken, null);
  41. ModelAndView mav = new ModelAndView("WxMpTest");
  42. mav.addObject("user", wxMpUser);
  43. return mav;
  44. } catch (WxErrorException e) {
  45. String url = wxMpService.oauth2buildAuthorizationUrl(env.getProperty("general.host")
  46. + request.getServletPath(), "snsapi_userinfo", null);
  47. log.info("微信重定向: {}", url);
  48. response.sendRedirect(url);
  49. }
  50. ModelAndView mav = new ModelAndView("Error");
  51. mav.addObject("title", "redirect");
  52. mav.addObject("body", "redirect");
  53. return mav;
  54. }
  55. @GetMapping("/testPay")
  56. public WxPayMpOrderResult testPayApi(@RequestParam String openId, @RequestParam Integer totalFee) {
  57. WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
  58. request.setBody("测试微信支付");
  59. request.setDetail("测试微信支付");
  60. request.setOutTradeNo(RandomStringUtils.randomAlphanumeric(32));
  61. request.setTotalFee(totalFee);
  62. request.setSpbillCreateIp("180.102.110.170");
  63. request.setNotifyUrl(env.getProperty("wx.pay.notifyUrl"));
  64. request.setTradeType(WxPayConstants.TradeType.JSAPI);
  65. request.setOpenid(openId);
  66. request.setSignType("MD5");
  67. try {
  68. return wxPayService.createOrder(request);
  69. } catch (WxPayException e) {
  70. log.error("unifiedOrder error", e);
  71. throw new BusinessException(e.getMessage());
  72. }
  73. }
  74. @GetMapping("/greet")
  75. public String greetUser(@RequestParam String code) {
  76. try {
  77. WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code);
  78. WxMpUser user = wxMpService.oauth2getUserInfo(accessToken, null);
  79. return user.getNickname();
  80. } catch (WxErrorException e) {
  81. e.printStackTrace();
  82. return e.getMessage();
  83. }
  84. }
  85. @GetMapping("/getCode")
  86. public String getCode(@RequestParam String code) {
  87. return code;
  88. }
  89. @GetMapping("/redirect")
  90. public void redirect(HttpServletResponse response, @RequestParam String redirectUrl) throws IOException {
  91. String url = wxMpService.oauth2buildAuthorizationUrl(redirectUrl, "snsapi_userinfo", null);
  92. log.info("微信重定向: {}", url);
  93. response.sendRedirect(url);
  94. }
  95. @PostMapping(value = "/payNotify", produces = "application/xml")
  96. public String wxNotify(@RequestBody String xmlData) throws WxPayException {
  97. log.info("微信支付回调: {}", xmlData);
  98. final WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlData);
  99. notifyResult.checkResult(wxPayService, "MD5", true);
  100. JSONObject attach = JSONObject.parseObject(notifyResult.getAttach());
  101. String type = attach.getString("type");
  102. switch (type) {
  103. }
  104. return WxPayNotifyResponse.success("OK");
  105. }
  106. @PostMapping(value = "/refundNotify", produces = "application/xml")
  107. public String refundNotify(@RequestBody String xmlData) throws WxPayException {
  108. log.info("微信退款回调: {}", xmlData);
  109. final WxPayRefundNotifyResult notifyResult = wxPayService.parseRefundNotifyResult(xmlData);
  110. notifyResult.checkResult(wxPayService, "MD5", true);
  111. return WxPayNotifyResponse.success("OK");
  112. }
  113. @GetMapping("/jsapiSign")
  114. @ApiOperation(value = "jsapi签名")
  115. public WxJsapiSignature sign(@RequestParam String url) throws WxErrorException {
  116. return wxMpService.createJsapiSignature(url);
  117. }
  118. }