WxController.java 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.exception.WxPayException;
  7. import com.github.binarywang.wxpay.service.WxPayService;
  8. import io.swagger.annotations.ApiOperation;
  9. import lombok.AllArgsConstructor;
  10. import lombok.extern.slf4j.Slf4j;
  11. import me.chanjar.weixin.common.bean.WxJsapiSignature;
  12. import me.chanjar.weixin.common.error.WxErrorException;
  13. import me.chanjar.weixin.mp.api.WxMpService;
  14. import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
  15. import me.chanjar.weixin.mp.bean.result.WxMpUser;
  16. import org.springframework.web.bind.annotation.*;
  17. import javax.servlet.http.HttpServletResponse;
  18. import java.io.IOException;
  19. @Slf4j
  20. @AllArgsConstructor
  21. @RestController
  22. @RequestMapping("/wx")
  23. public class WxController {
  24. private WxMpService wxMpService;
  25. private WxPayService wxPayService;
  26. @GetMapping("/greet")
  27. public String greetUser(@RequestParam String code) {
  28. try {
  29. WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code);
  30. WxMpUser user = wxMpService.oauth2getUserInfo(accessToken, null);
  31. return user.getNickname();
  32. } catch (WxErrorException e) {
  33. e.printStackTrace();
  34. return e.getMessage();
  35. }
  36. }
  37. @GetMapping("/getCode")
  38. public String getCode(@RequestParam String code) {
  39. return code;
  40. }
  41. @GetMapping("/redirect")
  42. public void redirect(HttpServletResponse response, @RequestParam String redirectUrl) throws IOException {
  43. String url = wxMpService.oauth2buildAuthorizationUrl(redirectUrl, "snsapi_userinfo", null);
  44. log.info("微信重定向: {}", url);
  45. response.sendRedirect(url);
  46. }
  47. @PostMapping(value = "/payNotify", produces = "application/xml")
  48. public String wxNotify(@RequestBody String xmlData) throws WxPayException {
  49. log.info("微信支付回调: {}", xmlData);
  50. final WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlData);
  51. notifyResult.checkResult(wxPayService, "MD5", true);
  52. JSONObject attach = JSONObject.parseObject(notifyResult.getAttach());
  53. String type = attach.getString("type");
  54. switch (type) {
  55. }
  56. return WxPayNotifyResponse.success("OK");
  57. }
  58. @PostMapping(value = "/refundNotify", produces = "application/xml")
  59. public String refundNotify(@RequestBody String xmlData) throws WxPayException {
  60. log.info("微信退款回调: {}", xmlData);
  61. final WxPayRefundNotifyResult notifyResult = wxPayService.parseRefundNotifyResult(xmlData);
  62. notifyResult.checkResult(wxPayService, "MD5", true);
  63. return WxPayNotifyResponse.success("OK");
  64. }
  65. @GetMapping("/jsapiSign")
  66. @ApiOperation(value = "jsapi签名")
  67. public WxJsapiSignature sign(@RequestParam String url) throws WxErrorException {
  68. return wxMpService.createJsapiSignature(url);
  69. }
  70. }