OrderNotifyController.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package com.izouma.nineth.web;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alipay.api.AlipayApiException;
  5. import com.alipay.api.internal.util.AlipaySignature;
  6. import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
  7. import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
  8. import com.github.binarywang.wxpay.exception.WxPayException;
  9. import com.github.binarywang.wxpay.service.WxPayService;
  10. import com.izouma.nineth.config.AlipayProperties;
  11. import com.izouma.nineth.enums.PayMethod;
  12. import com.izouma.nineth.service.AssetService;
  13. import com.izouma.nineth.service.GiftOrderService;
  14. import com.izouma.nineth.service.OrderService;
  15. import lombok.AllArgsConstructor;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.apache.commons.collections.MapUtils;
  18. import org.springframework.stereotype.Controller;
  19. import org.springframework.web.bind.annotation.PostMapping;
  20. import org.springframework.web.bind.annotation.RequestBody;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. import org.springframework.web.bind.annotation.ResponseBody;
  23. import javax.servlet.http.HttpServletRequest;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import java.util.Set;
  27. import static com.alibaba.fastjson.serializer.SerializerFeature.PrettyFormat;
  28. @Slf4j
  29. @Controller
  30. @RequestMapping("/notify")
  31. @AllArgsConstructor
  32. public class OrderNotifyController {
  33. private final AlipayProperties alipayProperties;
  34. private final OrderService orderService;
  35. private final WxPayService wxPayService;
  36. private final AssetService assetService;
  37. private final GiftOrderService giftOrderService;
  38. @PostMapping("/order/alipay")
  39. @ResponseBody
  40. public String notify(HttpServletRequest request) throws AlipayApiException {
  41. Map<String, String> params = new HashMap<>();
  42. Set<Map.Entry<String, String[]>> entrySet = request.getParameterMap().entrySet();
  43. for (Map.Entry<String, String[]> entry : entrySet) {
  44. String name = entry.getKey();
  45. String[] values = entry.getValue();
  46. int valLen = values.length;
  47. if (valLen == 1) {
  48. params.put(name, values[0]);
  49. } else if (valLen > 1) {
  50. StringBuilder sb = new StringBuilder();
  51. for (String val : values) {
  52. sb.append(",").append(val);
  53. }
  54. params.put(name, sb.substring(1));
  55. } else {
  56. params.put(name, "");
  57. }
  58. }
  59. log.info("支付宝回调 {}", JSON.toJSONString(params, PrettyFormat));
  60. AlipaySignature.rsaCheckV1(params, alipayProperties.getAliPublicKey(), "UTF-8", "RSA2");
  61. if (MapUtils.getString(params, "trade_status").equals("TRADE_SUCCESS")) {
  62. JSONObject body = JSON.parseObject(params.get("body"));
  63. String action = body.getString("action");
  64. switch (action) {
  65. case "payOrder": {
  66. Long orderId = body.getLong("orderId");
  67. orderService.notifyOrder(orderId, PayMethod.ALIPAY, MapUtils.getString(params, "trade_no"));
  68. break;
  69. }
  70. case "payGiftOrder": {
  71. Long orderId = body.getLong("orderId");
  72. giftOrderService.giftNotify(orderId, PayMethod.ALIPAY, MapUtils.getString(params, "trade_no"));
  73. break;
  74. }
  75. }
  76. return "success";
  77. }
  78. return "error";
  79. }
  80. @PostMapping(value = "/order/weixin", produces = "application/xml")
  81. @ResponseBody
  82. public String wxNotify(@RequestBody String xmlData) throws WxPayException {
  83. log.info("微信支付回调: {}", xmlData);
  84. final WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlData);
  85. notifyResult.checkResult(wxPayService, "MD5", true);
  86. JSONObject attach = JSONObject.parseObject(notifyResult.getAttach());
  87. String action = attach.getString("action");
  88. switch (action) {
  89. case "payOrder": {
  90. Long orderId = attach.getLong("orderId");
  91. orderService.notifyOrder(orderId, PayMethod.WEIXIN, notifyResult.getTransactionId());
  92. break;
  93. }
  94. case "payGiftOrder": {
  95. Long orderId = attach.getLong("orderId");
  96. giftOrderService.giftNotify(orderId, PayMethod.WEIXIN, notifyResult.getTransactionId());
  97. break;
  98. }
  99. }
  100. return WxPayNotifyResponse.success("OK");
  101. }
  102. }