1. import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
    2. import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
    3. import com.github.binarywang.wxpay.exception.WxPayException;
    4. import com.github.binarywang.wxpay.service.WxPayService;
    5. import lombok.extern.slf4j.Slf4j;
    6. import org.springframework.web.bind.annotation.RequestMapping;
    7. import org.springframework.web.bind.annotation.RestController;
    8. import javax.servlet.http.HttpServletRequest;
    9. import java.io.ByteArrayOutputStream;
    10. import java.io.IOException;
    11. import java.io.InputStream;
    12. /**
    13. * @author admin
    14. */
    15. @Slf4j
    16. @RestController
    17. @RequestMapping("pay")
    18. public class PayController {
    19. /**
    20. * 微信服务回调
    21. */
    22. @RequestMapping("wxCallPack")
    23. public String wxCallPack(HttpServletRequest request) throws WxPayException, IOException {
    24. // 将异步通知的参数转化为xml
    25. String xmlData = getXml(request);
    26. log.info("微信支付回调:xmlData:{}", xmlData);
    27. WxPayService wxPayService = WxPayAutoConfiguration.getWxPayService();
    28. WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlData);
    29. // 获取商户订单号
    30. String outTradeNo = notifyResult.getOutTradeNo();
    31. // 通过 outTradeNo 查询系统订单数据
    32. // orderService.updateOrderStatus(outTradeNo, notifyResult.getTransactionId());
    33. return WxPayNotifyResponse.success("成功");
    34. }
    35. private String getXml(HttpServletRequest request) throws IOException {
    36. InputStream inputStream = null;
    37. try {
    38. inputStream = request.getInputStream();
    39. } catch (IOException e) {
    40. e.printStackTrace();
    41. }
    42. //获取请求输入流
    43. ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    44. byte[] buffer = new byte[1024];
    45. int len = 0;
    46. while ((len=inputStream.read(buffer))!=-1){
    47. outputStream.write(buffer,0,len);
    48. }
    49. outputStream.close();
    50. inputStream.close();
    51. return new String(outputStream.toByteArray(), "utf-8");
    52. }
    53. }

    #