1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
| package com.alipay.controller;
import com.alibaba.fastjson.JSONObject; import com.alipay.service.*; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest; import java.math.BigDecimal; import java.util.Map;
@Slf4j @RestController @RequestMapping("/api/alipay") public class AlipayController { @Autowired private WapPayService wapPayService; @Autowired private AppPayService appPayService; @Autowired private FaceToFacePayService faceToFacePayService; @Autowired private AlipayNotifyService notifyService; @Autowired private AlipayRefundService refundService;
@PostMapping("/wap/create") public ResponseEntity<String> createWapOrder(@RequestBody JSONObject params) { try { String userId = params.getString("userId"); String subject = params.getString("subject"); String body = params.getString("body"); BigDecimal totalAmount = params.getBigDecimal("totalAmount"); String payForm = wapPayService.createWapOrder(userId, subject, body, totalAmount); return ResponseEntity.ok(payForm); } catch (Exception e) { log.error("手机网站支付下单失败", e); return ResponseEntity.internalServerError().body("支付下单失败: " + e.getMessage()); } }
@PostMapping("/app/create") public ResponseEntity<JSONObject> createAppOrder(@RequestBody JSONObject params) { try { String userId = params.getString("userId"); String subject = params.getString("subject"); String body = params.getString("body"); BigDecimal totalAmount = params.getBigDecimal("totalAmount"); String payInfo = appPayService.createAppOrder(userId, subject, body, totalAmount); JSONObject result = new JSONObject(); result.put("code", "SUCCESS"); result.put("payInfo", payInfo); return ResponseEntity.ok(result); } catch (Exception e) { log.error("APP支付下单失败", e); JSONObject error = new JSONObject(); error.put("code", "FAIL"); error.put("message", e.getMessage()); return ResponseEntity.internalServerError().body(error); } }
@PostMapping("/face-to-face/create") public ResponseEntity<JSONObject> createFaceToFaceOrder(@RequestBody JSONObject params) { try { String userId = params.getString("userId"); String subject = params.getString("subject"); String body = params.getString("body"); BigDecimal totalAmount = params.getBigDecimal("totalAmount"); String qrCode = faceToFacePayService.createFaceToFaceOrder(userId, subject, body, totalAmount); JSONObject result = new JSONObject(); result.put("code", "SUCCESS"); result.put("qrCode", qrCode); return ResponseEntity.ok(result); } catch (Exception e) { log.error("当面付下单失败", e); JSONObject error = new JSONObject(); error.put("code", "FAIL"); error.put("message", e.getMessage()); return ResponseEntity.internalServerError().body(error); } }
@PostMapping("/notify") public String payNotify(HttpServletRequest request) { try { Map<String, String[]> parameterMap = request.getParameterMap(); Map<String, String> params = new java.util.HashMap<>(); for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) { String key = entry.getKey(); String[] values = entry.getValue(); if (values != null && values.length > 0) { params.put(key, values[0]); } } return notifyService.handlePayNotify(params); } catch (Exception e) { log.error("处理支付回调失败", e); return "failure"; } }
@GetMapping("/query/{orderNo}") public ResponseEntity<JSONObject> queryOrder(@PathVariable String orderNo) { try { JSONObject result = wapPayService.queryOrder(orderNo); return ResponseEntity.ok(result); } catch (Exception e) { log.error("查询订单失败", e); JSONObject error = new JSONObject(); error.put("code", "FAIL"); error.put("message", e.getMessage()); return ResponseEntity.internalServerError().body(error); } }
@PostMapping("/close/{orderNo}") public ResponseEntity<JSONObject> closeOrder(@PathVariable String orderNo) { try { wapPayService.closeOrder(orderNo); JSONObject result = new JSONObject(); result.put("code", "SUCCESS"); result.put("message", "订单关闭成功"); return ResponseEntity.ok(result); } catch (Exception e) { log.error("关闭订单失败", e); JSONObject error = new JSONObject(); error.put("code", "FAIL"); error.put("message", e.getMessage()); return ResponseEntity.internalServerError().body(error); } }
@PostMapping("/refund") public ResponseEntity<JSONObject> refund(@RequestBody JSONObject params) { try { String orderNo = params.getString("orderNo"); BigDecimal refundAmount = params.getBigDecimal("refundAmount"); String reason = params.getString("reason"); String refundNo = refundService.refund(orderNo, refundAmount, reason); JSONObject result = new JSONObject(); result.put("code", "SUCCESS"); result.put("refundNo", refundNo); result.put("message", "退款申请成功"); return ResponseEntity.ok(result); } catch (Exception e) { log.error("申请退款失败", e); JSONObject error = new JSONObject(); error.put("code", "FAIL"); error.put("message", e.getMessage()); return ResponseEntity.internalServerError().body(error); } } }
|