| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package com.izouma.nineth.web;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.alipay.api.AlipayApiException;
- import com.alipay.api.internal.util.AlipaySignature;
- import com.izouma.nineth.config.AlipayProperties;
- import com.izouma.nineth.service.OrderService;
- import lombok.AllArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.collections.MapUtils;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
- import javax.servlet.http.HttpServletRequest;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Set;
- import static com.alibaba.fastjson.serializer.SerializerFeature.PrettyFormat;
- @Slf4j
- @Controller
- @RequestMapping("/notify")
- @AllArgsConstructor
- public class OrderNotifyController {
- private final AlipayProperties alipayProperties;
- private final OrderService orderService;
- @PostMapping("/order/alipay")
- @ResponseBody
- public String notify(HttpServletRequest request) throws AlipayApiException {
- Map<String, String> params = new HashMap<>();
- Set<Map.Entry<String, String[]>> entrySet = request.getParameterMap().entrySet();
- for (Map.Entry<String, String[]> entry : entrySet) {
- String name = entry.getKey();
- String[] values = entry.getValue();
- int valLen = values.length;
- if (valLen == 1) {
- params.put(name, values[0]);
- } else if (valLen > 1) {
- StringBuilder sb = new StringBuilder();
- for (String val : values) {
- sb.append(",").append(val);
- }
- params.put(name, sb.substring(1));
- } else {
- params.put(name, "");
- }
- }
- log.info("支付宝回调 {}", JSON.toJSONString(params, PrettyFormat));
- AlipaySignature.rsaCheckV1(params, alipayProperties.getAliPublicKey(), "UTF-8", "RSA2");
- if (MapUtils.getString(params, "trade_status").equals("TRADE_SUCCESS")) {
- JSONObject body = JSON.parseObject(params.get("body"));
- String action = body.getString("action");
- switch (action) {
- case "payOrder":
- Long orderId = body.getLong("orderId");
- orderService.notifyAlipay(orderId, params);
- break;
- }
- return "success";
- }
- return "error";
- }
- }
|