后端:增加部分退款逻辑

This commit is contained in:
YunaiV
2019-04-26 22:08:26 +08:00
parent 957b2eb893
commit 1be40cb195
50 changed files with 1180 additions and 371 deletions

View File

@@ -1,99 +0,0 @@
package cn.iocoder.mall.pay.application.controller.users;
import cn.iocoder.common.framework.util.HttpUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.pay.api.PayTransactionService;
import cn.iocoder.mall.pay.api.bo.PayTransactionBO;
import cn.iocoder.mall.pay.api.dto.PayTransactionCreateDTO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
/**
* 示例 Controller
*/
@RestController
@RequestMapping("users/demo")
public class PayDemoController {
@Reference(validation = "true")
private PayTransactionService payTransactionService;
@PostMapping("/create_order")
public void createOrder(HttpServletRequest request,
@RequestParam("orderId") String orderId) {
// 创建业务订单
// ...
// 调用【支付服务】,创建交易订单
PayTransactionCreateDTO payTransactionCreateDTO = new PayTransactionCreateDTO()
.setAppId("POd4RC6a")
.setCreateIp(HttpUtil.getIp(request))
.setOrderId(orderId)
.setOrderSubject("商品名" )
.setOrderDescription("商品描述")
.setOrderMemo("商品备注")
.setPrice(10)
.setExpireTime(new Date());
CommonResult<PayTransactionBO> result = payTransactionService.createTransaction(payTransactionCreateDTO);
Assert.isTrue(result.isSuccess(), "一定会成功的");
}
@PostMapping("/pingxx")
public String pingxx() {
return "{\n" +
" \"id\": \"ch_n5COmHGG8iX5TWf5qDynvTaP\",\n" +
" \"object\": \"charge\",\n" +
" \"created\": 1552445643,\n" +
" \"livemode\": false,\n" +
" \"paid\": false,\n" +
" \"refunded\": false,\n" +
" \"reversed\": false,\n" +
" \"app\": \"app_aTyfXDjrvzDSbLuz\",\n" +
" \"channel\": \"wx_pub\",\n" +
" \"order_no\": \"1552445643093\",\n" +
" \"client_ip\": \"127.0.0.1\",\n" +
" \"amount\": 1,\n" +
" \"amount_settle\": 1,\n" +
" \"currency\": \"cny\",\n" +
" \"subject\": \"测试商品\",\n" +
" \"body\": \"测试描述\",\n" +
" \"time_paid\": null,\n" +
" \"time_expire\": 1552452843,\n" +
" \"time_settle\": null,\n" +
" \"transaction_no\": null,\n" +
" \"refunds\": {\n" +
" \"object\": \"list\",\n" +
" \"url\": \"/v1/charges/ch_n5COmHGG8iX5TWf5qDynvTaP/refunds\",\n" +
" \"has_more\": false,\n" +
" \"data\": []\n" +
" },\n" +
" \"amount_refunded\": 0,\n" +
" \"failure_code\": null,\n" +
" \"failure_msg\": null,\n" +
" \"metadata\": {},\n" +
" \"credential\": {\n" +
" \"object\": \"credential\",\n" +
" \"wx_pub\": {\n" +
" \"appId\": \"wxytom5krtuf54qjff\",\n" +
" \"timeStamp\": \"1552445643\",\n" +
" \"nonceStr\": \"5cc0206f78d8bf931980f5132d5ce394\",\n" +
" \"package\": \"prepay_id=1101000000190313rx9y5oahkkcsm5gg\",\n" +
" \"signType\": \"MD5\",\n" +
" \"paySign\": \"9F6E80E89439575B8414FA56ADB07228\"\n" +
" }\n" +
" },\n" +
" \"extra\": {\n" +
" \"open_id\": \"just_for_test\"\n" +
" },\n" +
" \"description\": null\n" +
"}";
}
}

View File

@@ -0,0 +1,47 @@
package cn.iocoder.mall.pay.application.controller.users;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.pay.api.PayRefundService;
import cn.iocoder.mall.pay.api.constant.PayChannelEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
@RestController
@RequestMapping("users/refund") // TODO 芋艿,理论来说,是用户无关的。这里先酱紫先~
public class PayRefundController {
private Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private PayRefundService payRefundService;
@PostMapping(value = "pingxx_refund_success", consumes = MediaType.APPLICATION_JSON_VALUE)
public String pingxxRefundSuccess(HttpServletRequest request) throws IOException {
logger.info("[pingxxRefundSuccess][被回调]");
// 读取 webhook
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = request.getReader()) {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
}
CommonResult<Boolean> result = payRefundService.updateRefundSuccess(PayChannelEnum.PINGXX.getId(), sb.toString());
if (result.isError()) {
logger.error("[pingxxRefundSuccess][message({}) result({})]", sb, result);
return "failure";
}
return "success";
}
}

View File

@@ -25,12 +25,12 @@ public class PayTransactionController {
private Logger logger = LoggerFactory.getLogger(getClass());
@Reference(validation = "true")
private PayTransactionService payService;
private PayTransactionService payTransactionService;
@GetMapping("/get")
public CommonResult<PayTransactionBO> get(@RequestParam("appId") String appId,
@RequestParam("orderId") String orderId) {
return payService.getTransaction(UserSecurityContextHolder.getContext().getUserId(), appId, orderId);
return payTransactionService.getTransaction(UserSecurityContextHolder.getContext().getUserId(), appId, orderId);
}
@PostMapping("/submit") // TODO api 注释
@@ -43,13 +43,13 @@ public class PayTransactionController {
.setAppId(appId).setOrderId(orderId).setPayChannel(payChannel)
.setCreateIp(HttpUtil.getIp(request));
// 提交支付提交
return payService.submitTransaction(payTransactionSubmitDTO);
return payTransactionService.submitTransaction(payTransactionSubmitDTO);
}
@PostMapping(value = "pingxx_pay_success", consumes = MediaType.APPLICATION_JSON_VALUE)
// @GetMapping(value = "pingxx_pay_success")
public String pingxxSuccess(HttpServletRequest request) throws IOException {
logger.info("[pingxxSuccess][被回调]");
public String pingxxPaySuccess(HttpServletRequest request) throws IOException {
logger.info("[pingxxPaySuccess][被回调]");
// 读取 webhook
StringBuilder sb = new StringBuilder();
try (BufferedReader reader = request.getReader()) {
@@ -62,12 +62,12 @@ public class PayTransactionController {
// JSONObject bodyObj = JSON.parseObject(sb.toString());
// bodyObj.put("webhookId", bodyObj.remove("id"));
// String body = bodyObj.toString();
CommonResult<Boolean> result = payService.updateTransactionPaySuccess(PayChannelEnum.PINGXX.getId(), sb.toString());
CommonResult<Boolean> result = payTransactionService.updateTransactionPaySuccess(PayChannelEnum.PINGXX.getId(), sb.toString());
if (result.isError()) {
logger.error("[pingxxSuccess][message({}) result({})]", sb, result);
logger.error("[pingxxPaySuccess][message({}) result({})]", sb, result);
return "failure";
}
return result.isSuccess() ? "success" : "failure";
return "success";
}
}