WxController.java 5.5 KB

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