OrderNotifyController.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.github.kevinsawicki.http.HttpRequest;
  11. import com.huifu.adapay.core.AdapayCore;
  12. import com.huifu.adapay.core.util.AdapaySign;
  13. import com.izouma.nineth.config.AlipayProperties;
  14. import com.izouma.nineth.enums.PayMethod;
  15. import com.izouma.nineth.service.AssetService;
  16. import com.izouma.nineth.service.GiftOrderService;
  17. import com.izouma.nineth.service.OrderService;
  18. import com.izouma.nineth.utils.SnowflakeIdWorker;
  19. import lombok.AllArgsConstructor;
  20. import lombok.extern.slf4j.Slf4j;
  21. import org.apache.commons.collections.MapUtils;
  22. import org.springframework.stereotype.Controller;
  23. import org.springframework.web.bind.annotation.*;
  24. import javax.servlet.http.HttpServletRequest;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import static com.alibaba.fastjson.serializer.SerializerFeature.PrettyFormat;
  29. @Slf4j
  30. @Controller
  31. @RequestMapping("/notify")
  32. @AllArgsConstructor
  33. public class OrderNotifyController {
  34. private final AlipayProperties alipayProperties;
  35. private final OrderService orderService;
  36. private final WxPayService wxPayService;
  37. private final AssetService assetService;
  38. private final GiftOrderService giftOrderService;
  39. private final SnowflakeIdWorker snowflakeIdWorker;
  40. @PostMapping("/order/alipay")
  41. @ResponseBody
  42. public String notify(HttpServletRequest request) throws AlipayApiException {
  43. Map<String, String> params = new HashMap<>();
  44. Set<Map.Entry<String, String[]>> entrySet = request.getParameterMap().entrySet();
  45. for (Map.Entry<String, String[]> entry : entrySet) {
  46. String name = entry.getKey();
  47. String[] values = entry.getValue();
  48. int valLen = values.length;
  49. if (valLen == 1) {
  50. params.put(name, values[0]);
  51. } else if (valLen > 1) {
  52. StringBuilder sb = new StringBuilder();
  53. for (String val : values) {
  54. sb.append(",").append(val);
  55. }
  56. params.put(name, sb.substring(1));
  57. } else {
  58. params.put(name, "");
  59. }
  60. }
  61. log.info("支付宝回调 {}", JSON.toJSONString(params, PrettyFormat));
  62. AlipaySignature.rsaCheckV1(params, alipayProperties.getAliPublicKey(), "UTF-8", "RSA2");
  63. if (MapUtils.getString(params, "trade_status").equals("TRADE_SUCCESS")) {
  64. JSONObject body = JSON.parseObject(params.get("body"));
  65. String action = body.getString("action");
  66. switch (action) {
  67. case "payOrder": {
  68. Long orderId = body.getLong("orderId");
  69. orderService.notifyOrder(orderId, PayMethod.ALIPAY, MapUtils.getString(params, "trade_no"));
  70. break;
  71. }
  72. case "payGiftOrder": {
  73. Long orderId = body.getLong("orderId");
  74. giftOrderService.giftNotify(orderId, PayMethod.WEIXIN, MapUtils.getString(params, "trade_no"));
  75. break;
  76. }
  77. }
  78. return "success";
  79. }
  80. return "error";
  81. }
  82. @PostMapping(value = "/order/weixin", produces = "application/xml")
  83. @ResponseBody
  84. public String wxNotify(@RequestBody String xmlData) throws WxPayException {
  85. log.info("微信支付回调: {}", xmlData);
  86. final WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlData);
  87. notifyResult.checkResult(wxPayService, "MD5", true);
  88. JSONObject attach = JSONObject.parseObject(notifyResult.getAttach());
  89. String action = attach.getString("action");
  90. switch (action) {
  91. case "payOrder": {
  92. Long orderId = attach.getLong("orderId");
  93. orderService.notifyOrder(orderId, PayMethod.WEIXIN, notifyResult.getTransactionId());
  94. break;
  95. }
  96. case "payGiftOrder": {
  97. Long orderId = attach.getLong("orderId");
  98. giftOrderService.giftNotify(orderId, PayMethod.WEIXIN, notifyResult.getTransactionId());
  99. break;
  100. }
  101. }
  102. return WxPayNotifyResponse.success("OK");
  103. }
  104. @PostMapping(value = "/order/iap")
  105. @ResponseBody
  106. public String iap(@RequestParam String receiptData, @RequestParam Long orderId) {
  107. String data = "{\"receipt-data\":\"" + receiptData + "\"}";
  108. String body = HttpRequest.post("https://buy.itunes.apple.com/verifyReceipt")
  109. .contentType("application/json")
  110. .send(data)
  111. .body();
  112. JSONObject jsonObject = JSON.parseObject(body);
  113. int status = jsonObject.getInteger("status");
  114. if (status == 21007) {
  115. jsonObject = JSON.parseObject(HttpRequest.post("https://sandbox.itunes.apple.com/verifyReceipt")
  116. .contentType("application/json")
  117. .send(data)
  118. .body());
  119. status = jsonObject.getInteger("status");
  120. }
  121. if (status == 0) {
  122. orderService.notifyOrder(orderId, PayMethod.WEIXIN, snowflakeIdWorker.nextId() + "");
  123. }
  124. return "ok";
  125. }
  126. @PostMapping("/adapay/order/{orderId}")
  127. @ResponseBody
  128. public void adapayNotify(@PathVariable Long orderId, HttpServletRequest request) {
  129. log.info("adapay notify: \n{}", JSON.toJSONString(request.getParameterMap(), PrettyFormat));
  130. try {
  131. String data = request.getParameter("data");
  132. String sign = request.getParameter("sign");
  133. String type = request.getParameter("type");
  134. if ("payment.succeeded".equals(type)) {
  135. boolean checkSign = AdapaySign.verifySign(data, sign, AdapayCore.PUBLIC_KEY);
  136. log.info("checkSign {}", checkSign);
  137. if (checkSign) {
  138. JSONObject jsonObject = JSON.parseObject(data);
  139. String channel = jsonObject.getString("pay_channel");
  140. String id = jsonObject.getString("id");
  141. orderService.notifyOrder(orderId, channel.startsWith("wx") ? PayMethod.WEIXIN : PayMethod.ALIPAY, id);
  142. }
  143. }
  144. } catch (Exception e) {
  145. e.printStackTrace();
  146. }
  147. }
  148. @PostMapping("/adapay/giftOrder/{orderId}")
  149. @ResponseBody
  150. public void adapayGiftNotify(@PathVariable Long orderId, HttpServletRequest request) {
  151. log.info("adapay gift notify: \n{}", JSON.toJSONString(request.getParameterMap(), PrettyFormat));
  152. try {
  153. String data = request.getParameter("data");
  154. String sign = request.getParameter("sign");
  155. String type = request.getParameter("type");
  156. if ("payment.succeeded".equals(type)) {
  157. boolean checkSign = AdapaySign.verifySign(data, sign, AdapayCore.PUBLIC_KEY);
  158. log.info("checkSign {}", checkSign);
  159. if (checkSign) {
  160. JSONObject jsonObject = JSON.parseObject(data);
  161. String channel = jsonObject.getString("pay_channel");
  162. String id = jsonObject.getString("id");
  163. giftOrderService.giftNotify(orderId, channel.startsWith("wx") ? PayMethod.WEIXIN : PayMethod.ALIPAY, id);
  164. }
  165. }
  166. } catch (Exception e) {
  167. e.printStackTrace();
  168. }
  169. }
  170. }