|
@@ -5,32 +5,36 @@ import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.szwl.common.AccessTokenCommon;
|
|
|
+import com.szwl.constant.AirwallexConstant;
|
|
|
import com.szwl.model.bean.PaymentIntentRequestBody;
|
|
|
import com.szwl.model.bean.RefundRequestBody;
|
|
|
import com.szwl.model.bo.R;
|
|
|
import com.szwl.model.bo.ResponseModel;
|
|
|
import com.szwl.model.entity.TOrderAbroad;
|
|
|
+import com.szwl.model.utils.AccessTokenThreadUtil;
|
|
|
import com.szwl.service.AirwallexService;
|
|
|
import com.szwl.service.TAdminService;
|
|
|
import com.szwl.service.TOrderAbroadService;
|
|
|
import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiModelProperty;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
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 java.util.*;
|
|
|
|
|
|
import static com.szwl.constant.ResponseCodesEnum.L0006;
|
|
|
|
|
@@ -49,14 +53,18 @@ public class AirwallexPayController {
|
|
|
@Resource
|
|
|
TOrderAbroadService tOrderAbroadService;
|
|
|
|
|
|
+// @GetMapping("/getAccessToken")
|
|
|
+// public ResponseModel<?> getAccessToken() {
|
|
|
+// String accessToken = airwallexService.getAccessToken();
|
|
|
+// //获取到的accessToken可以放入内存或者存到数据库中,不用每次都获取
|
|
|
+// return R.ok(accessToken);
|
|
|
+// }
|
|
|
|
|
|
@ApiOperation(value = "获取token")
|
|
|
@GetMapping("/getAccessToken")
|
|
|
- @Scheduled(cron = "0 0/20 * * * ?") // 每20分钟执行一次,我删了入参String id
|
|
|
- public ResponseModel<?> getAccessToken() {
|
|
|
- String accessToken = airwallexService.getAccessToken();
|
|
|
- //获取到的accessToken可以放入内存或者存到数据库中,不用每次都获取
|
|
|
- return R.ok(accessToken);
|
|
|
+ @Scheduled(cron = "0 0/20 * * * ?")
|
|
|
+ public void getAccessToken() {
|
|
|
+ AccessTokenCommon.ACCESS_TOKEN = AccessTokenThreadUtil.getAccessToken();
|
|
|
}
|
|
|
|
|
|
@ApiOperation(value = "Create a PaymentIntent")
|
|
@@ -64,11 +72,14 @@ public class AirwallexPayController {
|
|
|
public R createAPaymentIntent(@RequestBody PaymentIntentRequestBody paymentIntentRequestBody) {
|
|
|
log.info("发起支付请求");
|
|
|
// 先获取token
|
|
|
- String accessToken = airwallexService.getAccessToken();
|
|
|
- // TODO: 创建订单
|
|
|
- if(accessToken == null || "".equals(accessToken)) {
|
|
|
- return R.fail(L0006);
|
|
|
- }
|
|
|
+// String accessToken = airwallexService.getAccessToken();
|
|
|
+// new AccessTokenThreadUtil().run();
|
|
|
+// String accessToken = AccessTokenCommon.ACCESS_TOKEN;
|
|
|
+//
|
|
|
+// if(accessToken == null || "".equals(accessToken)) {
|
|
|
+// accessToken = new AccessTokenThreadUtil().getAccessToken();
|
|
|
+// return R.fail(L0006);
|
|
|
+// }
|
|
|
|
|
|
BigDecimal amount = paymentIntentRequestBody.getAmount();
|
|
|
String currency = paymentIntentRequestBody.getCurrency();
|
|
@@ -78,7 +89,7 @@ public class AirwallexPayController {
|
|
|
String productName = paymentIntentRequestBody.getProductName();
|
|
|
String clientId = paymentIntentRequestBody.getClientId();
|
|
|
Long equipmentId = paymentIntentRequestBody.getEquipmentId();
|
|
|
-
|
|
|
+ // 创建订单
|
|
|
TOrderAbroad tOrderAbroad = new TOrderAbroad();
|
|
|
tOrderAbroad.setAmount(amount);
|
|
|
tOrderAbroad.setCurrency(currency);
|
|
@@ -90,12 +101,26 @@ public class AirwallexPayController {
|
|
|
tOrderAbroad.setEquipmentId(equipmentId);
|
|
|
// 设置订单支付状态,0未支付,1已支付,3已退款。
|
|
|
tOrderAbroad.setPayStatus(0);
|
|
|
+
|
|
|
+ // 返回payment intent id和client secret
|
|
|
+ Map<String, Object> responseMap = airwallexService.caPaymentIntent(amount, currency, requestId, merchantOrderId);
|
|
|
+
|
|
|
+ String paymentIntentId = (String) responseMap.get("id");
|
|
|
+ String clientSecret = (String) responseMap.get("client_secret");
|
|
|
+
|
|
|
+ // 把 paymentIntentId 保存到数据库
|
|
|
+ tOrderAbroad.setPaymentIntentId(paymentIntentId);
|
|
|
// 将订单存入数据库
|
|
|
boolean save = tOrderAbroadService.save(tOrderAbroad);
|
|
|
System.out.println("将订单存入数据库:" + save);
|
|
|
|
|
|
- // 返回payment intent id和client secret,拼接二维码链接给前端
|
|
|
- String QR = airwallexService.caPaymentIntent(amount, currency, requestId, merchantOrderId);
|
|
|
+ // 拼接二维码链接给前端
|
|
|
+// String env = AirwallexConstant.ENV;
|
|
|
+// String mode = AirwallexConstant.MODE;
|
|
|
+// String locale = "it";
|
|
|
+ String qrUrl = AirwallexConstant.QR_URL;
|
|
|
+
|
|
|
+ String QR = qrUrl + "?intent_id=" + paymentIntentId + "&client_secret=" + clientSecret + "¤cy=" + currency;
|
|
|
|
|
|
// TODO: 设置分销
|
|
|
|
|
@@ -103,18 +128,44 @@ public class AirwallexPayController {
|
|
|
}
|
|
|
|
|
|
@ApiOperation(value = "获取支付订单的信息,订单状态")
|
|
|
- @GetMapping("/retrieveAPaymentIntent/{intId}")
|
|
|
- public String retrieveAPaymentIntent(@PathVariable Long intId) {
|
|
|
+ @GetMapping("/retrieveAPaymentIntent")
|
|
|
+ public JSON retrieveAPaymentIntent(@RequestParam String id) {
|
|
|
log.info("查询支付订单信息");
|
|
|
-
|
|
|
- String retrieve = airwallexService.retrieveAPaymentIntent(intId);
|
|
|
- JSONObject jsonObject = JSON.parseObject(retrieve);
|
|
|
+ // 这里的intId指的是paymentIntendId,也是id
|
|
|
+ String intentResp = airwallexService.retrieveAPaymentIntent(id);
|
|
|
+ JSONObject jsonObject = JSON.parseObject(intentResp);
|
|
|
// 取消订单的原因
|
|
|
// String cancellationReason = jsonObject.getString("cancellation_reason");
|
|
|
- // 订单状态
|
|
|
+ // 支付信息
|
|
|
+ String amount = jsonObject.getString("amount");
|
|
|
+ String currency = jsonObject.getString("currency");
|
|
|
+ String createdAt = jsonObject.getString("created_at");
|
|
|
+ String updatedAt = jsonObject.getString("updated_at");
|
|
|
String status = jsonObject.getString("status");
|
|
|
+// String customer = jsonObject.getString("customer");
|
|
|
+// JSONObject customerObj = JSON.parseObject(customer);
|
|
|
+// String address = customerObj.getString("address");
|
|
|
|
|
|
- return status;
|
|
|
+ JSONObject state = Optional.ofNullable(jsonObject)
|
|
|
+ .flatMap(jsonObject1 -> Optional.ofNullable(jsonObject.getJSONObject("customer")))
|
|
|
+ .flatMap(jsonObject1 -> Optional.ofNullable(jsonObject.getJSONObject("address")))
|
|
|
+ .flatMap(jsonObject1 -> Optional.ofNullable(jsonObject.getJSONObject("state")))
|
|
|
+ .orElse(null);
|
|
|
+ JSONObject city = Optional.ofNullable(jsonObject)
|
|
|
+ .flatMap(jsonObject1 -> Optional.ofNullable(jsonObject.getJSONObject("customer")))
|
|
|
+ .flatMap(jsonObject1 -> Optional.ofNullable(jsonObject.getJSONObject("address")))
|
|
|
+ .flatMap(jsonObject1 -> Optional.ofNullable(jsonObject.getJSONObject("city")))
|
|
|
+ .orElse(null);
|
|
|
+
|
|
|
+ Map<String, Object> intentDetails = new HashMap<>();
|
|
|
+ intentDetails.put("amount",amount);
|
|
|
+ intentDetails.put("currency",currency);
|
|
|
+ intentDetails.put("state",state);
|
|
|
+ intentDetails.put("city",city);
|
|
|
+ intentDetails.put("createdAt",createdAt);
|
|
|
+ intentDetails.put("status",status);
|
|
|
+
|
|
|
+ return new JSONObject(intentDetails);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -122,19 +173,26 @@ public class AirwallexPayController {
|
|
|
@PostMapping("/createARefund")
|
|
|
ResponseModel<String> createARefund(@RequestBody RefundRequestBody refundRequestBody) {
|
|
|
log.info("发起退款");
|
|
|
- String requestId = refundRequestBody.getRequestId();
|
|
|
+ String requestId = UUID.randomUUID().toString().trim().replaceAll("-", "");
|
|
|
+
|
|
|
+// String requestId = refundRequestBody.getRequestId();
|
|
|
String paymentIntentId = refundRequestBody.getPaymentIntentId();
|
|
|
+// String reason = refundRequestBody.getReason();
|
|
|
+
|
|
|
// 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);
|
|
|
+ lambdaQueryWrapper.eq(TOrderAbroad::getPaymentIntentId, paymentIntentId);
|
|
|
TOrderAbroad orderAbroad = tOrderAbroadService.getOne(lambdaQueryWrapper);
|
|
|
|
|
|
if(orderAbroad == null) {
|
|
|
return R.fail("订单为空/error");
|
|
|
}
|
|
|
+
|
|
|
+ // 支付状态:退款中
|
|
|
+ orderAbroad.setPayStatus(2);
|
|
|
// 修改订单表
|
|
|
// SimpleDateFormat sdf = new SimpleDateFormat();
|
|
|
// sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
|
|
@@ -145,19 +203,80 @@ public class AirwallexPayController {
|
|
|
// String format = localDateTime.format(dtf);
|
|
|
// 退款发起时间
|
|
|
orderAbroad.setRefundDate(new Date());
|
|
|
+ tOrderAbroadService.updateById(orderAbroad);
|
|
|
|
|
|
|
|
|
// 发起退款
|
|
|
+// String refund = airwallexService.createARefund(requestId, paymentIntentId, reason);
|
|
|
String refund = airwallexService.createARefund(requestId, paymentIntentId);
|
|
|
|
|
|
+ System.out.println(refund);
|
|
|
+
|
|
|
JSONObject jsonObject = JSON.parseObject(refund);
|
|
|
String status = jsonObject.getString("status");
|
|
|
+// orderAbroad.set;
|
|
|
+// tOrderAbroadService.updateById(orderAbroad);
|
|
|
+// switch (status) {
|
|
|
+// case "RECEIVED" :
|
|
|
+// System.out.println("退款被接收");
|
|
|
+// break;
|
|
|
+// case "ACCEPTED" :
|
|
|
+// System.out.println("退款被接受");
|
|
|
+// break;
|
|
|
+// case "SUCCEEDED" :
|
|
|
+// System.out.println("退款成功");
|
|
|
+// break;
|
|
|
+// case "FAILED" :
|
|
|
+// System.out.println("退款失败");
|
|
|
+// break;
|
|
|
+// }
|
|
|
+
|
|
|
+// if (!status.equals("FAILED")) {
|
|
|
+ if (status.equals("RECEIVED") || status.equals("ACCEPTED") || status.equals("SUCCEEDED")) {
|
|
|
+ String refundId = jsonObject.getString("id");
|
|
|
+ orderAbroad.setRefundId(refundId);
|
|
|
+ tOrderAbroadService.updateById(orderAbroad);
|
|
|
+ return R.ok("退款处理中,请稍候查询");
|
|
|
+ }
|
|
|
+ return R.fail("退款失败");
|
|
|
+// if (Objects.equals(status, "SUCCEEDED")) {
|
|
|
+// String refundId = jsonObject.getString("id");
|
|
|
+// orderAbroad.setRefundId(refundId);
|
|
|
+//// orderAbroad.setPayStatus(3);
|
|
|
+// tOrderAbroadService.updateById(orderAbroad);
|
|
|
+// return R.ok("退款申请中,请稍候查询");
|
|
|
+// } else {
|
|
|
+// return R.ok("退款失败");
|
|
|
+// }
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation(value = "查询某笔退款详情")
|
|
|
+ @GetMapping("/retrieveARefund/{id}")
|
|
|
+ public String retrieveARefund(@PathVariable("id") String refundId) {
|
|
|
+ log.info("根据rfd_查询某笔退款详情");
|
|
|
+
|
|
|
+ String refundResp = airwallexService.retrieveARefund(refundId);
|
|
|
+ JSONObject jsonObject = JSON.parseObject(refundResp);
|
|
|
+ String status = jsonObject.getString("status");
|
|
|
+
|
|
|
+ if (status.equals("SUCCEEDED")) {
|
|
|
|
|
|
- if (Objects.equals(status, "SUCCEEDED")) {
|
|
|
- return R.ok("退款申请中,请稍候查询");
|
|
|
- } else {
|
|
|
- return R.ok("退款失败");
|
|
|
+ LambdaQueryWrapper<TOrderAbroad> lambdaQueryWrapper = Wrappers.lambdaQuery();
|
|
|
+ lambdaQueryWrapper.eq(TOrderAbroad::getRefundId, refundId);
|
|
|
+ TOrderAbroad orderAbroad = tOrderAbroadService.getOne(lambdaQueryWrapper);
|
|
|
+
|
|
|
+// if (orderAbroad == null ) {
|
|
|
+// return "订单为空/error";
|
|
|
+// }
|
|
|
+ if (Objects.isNull(orderAbroad)) {
|
|
|
+ return "订单为空/error";
|
|
|
+ }
|
|
|
+ // 设置支付状态为 3已退款
|
|
|
+ orderAbroad.setPayStatus(3);
|
|
|
+ tOrderAbroadService.updateById(orderAbroad);
|
|
|
}
|
|
|
+ return status;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|