|
@@ -1,46 +1,162 @@
|
|
package com.szwl.controller;
|
|
package com.szwl.controller;
|
|
|
|
|
|
-
|
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
-import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
-import com.szwl.constant.ResponseCodesEnum;
|
|
|
|
|
|
+import com.szwl.model.bean.PaymentIntentRequestBody;
|
|
|
|
+import com.szwl.model.bean.RefundRequestBody;
|
|
import com.szwl.model.bo.R;
|
|
import com.szwl.model.bo.R;
|
|
import com.szwl.model.bo.ResponseModel;
|
|
import com.szwl.model.bo.ResponseModel;
|
|
-import com.szwl.model.entity.TAdmin;
|
|
|
|
-import com.szwl.model.query.TAdminParam;
|
|
|
|
|
|
+import com.szwl.model.entity.TOrderAbroad;
|
|
|
|
+import com.szwl.service.AirwallexService;
|
|
import com.szwl.service.TAdminService;
|
|
import com.szwl.service.TAdminService;
|
|
-import com.szwl.service.TAirwallexService;
|
|
|
|
|
|
+import com.szwl.service.TOrderAbroadService;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
|
|
+import static com.szwl.constant.ResponseCodesEnum.L0006;
|
|
|
|
|
|
-@Api(value = "/api/airwallexPay", tags = {"空中云汇"})
|
|
|
|
|
|
+//@CrossOrigin
|
|
|
|
+@Api(value = "/api/airwallexPay", tags = {"空中云汇" })
|
|
@RestController
|
|
@RestController
|
|
@RequestMapping("/api/airwallexPay")
|
|
@RequestMapping("/api/airwallexPay")
|
|
|
|
+@Slf4j
|
|
public class AirwallexPayController {
|
|
public class AirwallexPayController {
|
|
@Value("${test.value:bbb}")
|
|
@Value("${test.value:bbb}")
|
|
private String testValue;
|
|
private String testValue;
|
|
@Autowired
|
|
@Autowired
|
|
TAdminService tAdminService;
|
|
TAdminService tAdminService;
|
|
@Autowired
|
|
@Autowired
|
|
- TAirwallexService airwallexService;
|
|
|
|
- @ApiOperation(value = "请求支付")
|
|
|
|
|
|
+ AirwallexService airwallexService;
|
|
|
|
+ @Resource
|
|
|
|
+ TOrderAbroadService tOrderAbroadService;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @ApiOperation(value = "获取token")
|
|
@GetMapping("/getAccessToken")
|
|
@GetMapping("/getAccessToken")
|
|
- public ResponseModel<?> getAccessToken(String id) {
|
|
|
|
|
|
+ @Scheduled(cron = "0 0/20 * * * ?") // 每20分钟执行一次,我删了入参String id
|
|
|
|
+ public ResponseModel<?> getAccessToken() {
|
|
String accessToken = airwallexService.getAccessToken();
|
|
String accessToken = airwallexService.getAccessToken();
|
|
return R.ok(accessToken);
|
|
return R.ok(accessToken);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @ApiOperation(value = "Create a PaymentIntent")
|
|
|
|
+ @PostMapping("/createAPaymentIntent")
|
|
|
|
+ public R createAPaymentIntent(@RequestBody PaymentIntentRequestBody paymentIntentRequestBody) {
|
|
|
|
+ log.info("发起支付请求");
|
|
|
|
+ // 先获取token
|
|
|
|
+ String accessToken = airwallexService.getAccessToken();
|
|
|
|
+ // TODO: 创建订单
|
|
|
|
+ if(accessToken == null || "".equals(accessToken)) {
|
|
|
|
+ return R.fail(L0006);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ BigDecimal amount = paymentIntentRequestBody.getAmount();
|
|
|
|
+ String currency = paymentIntentRequestBody.getCurrency();
|
|
|
|
+ String merchantOrderId = paymentIntentRequestBody.getMerchantOrderId();
|
|
|
|
+ String requestId = paymentIntentRequestBody.getRequestId();
|
|
|
|
+ Long productId = paymentIntentRequestBody.getProductId();
|
|
|
|
+ String productName = paymentIntentRequestBody.getProductName();
|
|
|
|
+ String clientId = paymentIntentRequestBody.getClientId();
|
|
|
|
+ Long equipmentId = paymentIntentRequestBody.getEquipmentId();
|
|
|
|
+
|
|
|
|
+ TOrderAbroad tOrderAbroad = new TOrderAbroad();
|
|
|
|
+ tOrderAbroad.setAmount(amount);
|
|
|
|
+ tOrderAbroad.setCurrency(currency);
|
|
|
|
+ tOrderAbroad.setMerchantOrderId(merchantOrderId);
|
|
|
|
+ tOrderAbroad.setRequestId(requestId);
|
|
|
|
+ tOrderAbroad.setProductId(productId);
|
|
|
|
+ tOrderAbroad.setProductName(productName);
|
|
|
|
+ tOrderAbroad.setClientId(clientId);
|
|
|
|
+ tOrderAbroad.setEquipmentId(equipmentId);
|
|
|
|
+ // TODO: 设置订单支付状态,0未支付,1已支付。
|
|
|
|
+ tOrderAbroad.setPayStatus(0);
|
|
|
|
+ // 将订单存入数据库
|
|
|
|
+ boolean save = tOrderAbroadService.save(tOrderAbroad);
|
|
|
|
+ System.out.println("将订单存入数据库:" + save);
|
|
|
|
+
|
|
|
|
+ // 返回payment intent id和client secret,拼接二维码链接给前端
|
|
|
|
+ String QR = airwallexService.caPaymentIntent(amount, currency, requestId, merchantOrderId);
|
|
|
|
+
|
|
|
|
+ // TODO: 设置分销
|
|
|
|
+
|
|
|
|
+ return R.ok().setData(QR);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @ApiOperation(value = "获取支付订单的信息,订单状态")
|
|
|
|
+ @GetMapping("/retrieveAPaymentIntent/{intId}")
|
|
|
|
+ public String retrieveAPaymentIntent(@PathVariable Long intId) {
|
|
|
|
+ log.info("查询支付订单信息");
|
|
|
|
+
|
|
|
|
+ String retrieve = airwallexService.retrieveAPaymentIntent(intId);
|
|
|
|
+ JSONObject jsonObject = JSON.parseObject(retrieve);
|
|
|
|
+ // 取消订单的原因
|
|
|
|
+// String cancellationReason = jsonObject.getString("cancellation_reason");
|
|
|
|
+ // 订单状态
|
|
|
|
+ String status = jsonObject.getString("status");
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ @ApiOperation(value = "发起退款")
|
|
|
|
+ @PostMapping("/createARefund")
|
|
|
|
+ ResponseModel<String> createARefund(@RequestBody RefundRequestBody refundRequestBody) {
|
|
|
|
+ log.info("发起退款");
|
|
|
|
+ String requestId = refundRequestBody.getRequestId();
|
|
|
|
+// QueryWrapper<TOrderAbroad> tOrderAbroadQueryWrapper = new QueryWrapper<>();
|
|
|
|
+// tOrderAbroadQueryWrapper.eq("request_id", requestId);
|
|
|
|
+// TOrderAbroad orderAbroad = tOrderAbroadService.getOne(tOrderAbroadQueryWrapper);
|
|
|
|
+
|
|
|
|
+ LambdaQueryWrapper<TOrderAbroad> lambdaQueryWrapper = Wrappers.lambdaQuery();
|
|
|
|
+ lambdaQueryWrapper.eq(TOrderAbroad::getRequestId, requestId);
|
|
|
|
+ TOrderAbroad orderAbroad = tOrderAbroadService.getOne(lambdaQueryWrapper);
|
|
|
|
+
|
|
|
|
+ if(orderAbroad == null) {
|
|
|
|
+ return R.fail("订单为空/error");
|
|
|
|
+ }
|
|
|
|
+ // 修改订单表
|
|
|
|
+// SimpleDateFormat sdf = new SimpleDateFormat();
|
|
|
|
+// sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
+// orderAbroad.setRefundDate(sdf.format(date));
|
|
|
|
+
|
|
|
|
+// LocalDateTime localDateTime = LocalDateTime.now();
|
|
|
|
+// DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
+// String format = localDateTime.format(dtf);
|
|
|
|
+ // 退款发起时间
|
|
|
|
+ orderAbroad.setRefundDate(new Date());
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ // 发起退款
|
|
|
|
+ String refund = airwallexService.createARefund(requestId);
|
|
|
|
+
|
|
|
|
+ JSONObject jsonObject = JSON.parseObject(refund);
|
|
|
|
+ String status = jsonObject.getString("status");
|
|
|
|
+
|
|
|
|
+ if (Objects.equals(status, "SUCCEEDED")) {
|
|
|
|
+ return R.ok("退款申请中,请稍候查询");
|
|
|
|
+ } else {
|
|
|
|
+ return R.ok("退款失败");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|