| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- package com.izouma.awesomeAdmin.web;
- import com.alibaba.fastjson.JSONObject;
- import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
- import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
- import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
- import com.github.binarywang.wxpay.exception.WxPayException;
- import com.github.binarywang.wxpay.service.WxPayService;
- import io.swagger.annotations.ApiOperation;
- import lombok.AllArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import me.chanjar.weixin.common.bean.WxJsapiSignature;
- import me.chanjar.weixin.common.error.WxErrorException;
- import me.chanjar.weixin.mp.api.WxMpService;
- import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
- import me.chanjar.weixin.mp.bean.result.WxMpUser;
- import org.springframework.web.bind.annotation.*;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- @Slf4j
- @AllArgsConstructor
- @RestController
- @RequestMapping("/wx")
- public class WxController {
- private WxMpService wxMpService;
- private WxPayService wxPayService;
- @GetMapping("/greet")
- public String greetUser(@RequestParam String code) {
- try {
- WxMpOAuth2AccessToken accessToken = wxMpService.oauth2getAccessToken(code);
- WxMpUser user = wxMpService.oauth2getUserInfo(accessToken, null);
- return user.getNickname();
- } catch (WxErrorException e) {
- e.printStackTrace();
- return e.getMessage();
- }
- }
- @GetMapping("/getCode")
- public String getCode(@RequestParam String code) {
- return code;
- }
- @GetMapping("/redirect")
- public void redirect(HttpServletResponse response, @RequestParam String redirectUrl) throws IOException {
- String url = wxMpService.oauth2buildAuthorizationUrl(redirectUrl, "snsapi_userinfo", null);
- log.info("微信重定向: {}", url);
- response.sendRedirect(url);
- }
- @PostMapping(value = "/payNotify", produces = "application/xml")
- public String wxNotify(@RequestBody String xmlData) throws WxPayException {
- log.info("微信支付回调: {}", xmlData);
- final WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlData);
- notifyResult.checkResult(wxPayService, "MD5", true);
- JSONObject attach = JSONObject.parseObject(notifyResult.getAttach());
- String type = attach.getString("type");
- switch (type) {
- }
- return WxPayNotifyResponse.success("OK");
- }
- @PostMapping(value = "/refundNotify", produces = "application/xml")
- public String refundNotify(@RequestBody String xmlData) throws WxPayException {
- log.info("微信退款回调: {}", xmlData);
- final WxPayRefundNotifyResult notifyResult = wxPayService.parseRefundNotifyResult(xmlData);
- notifyResult.checkResult(wxPayService, "MD5", true);
- return WxPayNotifyResponse.success("OK");
- }
- @GetMapping("/jsapiSign")
- @ApiOperation(value = "jsapi签名")
- public WxJsapiSignature sign(@RequestParam String url) throws WxErrorException {
- return wxMpService.createJsapiSignature(url);
- }
- }
|