|
|
@@ -2,13 +2,17 @@ package com.cyksj.web.controller.advertisement;
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
+import com.cyksj.common.util.Codec;
|
|
|
import com.cyksj.common.util.IoKit;
|
|
|
import com.cyksj.common.util.Xmls;
|
|
|
import com.cyksj.dto.Result;
|
|
|
import com.cyksj.enums.GatewayApiCode;
|
|
|
import com.cyksj.enums.GatewayResponse;
|
|
|
+import com.cyksj.mapper.WxAppMapper;
|
|
|
import com.cyksj.model.dto.H5JsPayParams;
|
|
|
+import com.cyksj.model.entity.AdvertisementOrderDon;
|
|
|
import com.cyksj.model.entity.AdvertisementUser;
|
|
|
import com.cyksj.model.entity.WxApp;
|
|
|
import com.cyksj.model.request.WeChatPayRequest;
|
|
|
@@ -24,12 +28,15 @@ import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import javax.servlet.http.Cookie;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.OutputStream;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.util.Map;
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
|
/**
|
|
|
* @author chan
|
|
|
@@ -41,9 +48,16 @@ import java.util.Map;
|
|
|
@RequestMapping("/applets/advertisement/order")
|
|
|
public class AdvertisementOrderDonController {
|
|
|
|
|
|
+ private static final String OPEN_KEY = "openKey";
|
|
|
+
|
|
|
+ private final HttpServletRequest request;
|
|
|
+
|
|
|
private final AdvertisementUserService advertisementUserService;
|
|
|
|
|
|
private final AdvertisementOrderDonService advertisementOrderDonService;
|
|
|
+
|
|
|
+ private final WxAppMapper wxAppMapper;
|
|
|
+
|
|
|
|
|
|
@PostMapping("/post/pay")
|
|
|
public Result<H5JsPayParams> pay(@RequestBody @Validated WeChatPayRequest weChatPayRequest) throws Exception {
|
|
|
@@ -54,12 +68,40 @@ public class AdvertisementOrderDonController {
|
|
|
if (StringUtils.isBlank(weChatPayRequest.getAppId())) {
|
|
|
throw BusinessRuntimeException.getInstance("缺少必填参数appId");
|
|
|
}
|
|
|
-
|
|
|
- //小程序支付
|
|
|
- AdvertisementUser user = advertisementUserService.getById(StpUserUtil.getLoginIdAsLong());
|
|
|
-
|
|
|
- H5JsPayParams payParams = advertisementOrderDonService.minipay(weChatPayRequest.getPopularizeId()
|
|
|
- , donMoney.setScale(0, RoundingMode.DOWN), weChatPayRequest.getAppId(), user
|
|
|
+ WxApp wxApp = wxAppMapper.selectOne(Wrappers.lambdaQuery(WxApp.class).eq(WxApp::getAppId, weChatPayRequest.getAppId()));
|
|
|
+ if (wxApp == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("app不存在.");
|
|
|
+ }
|
|
|
+ AdvertisementOrderDon.Source source;
|
|
|
+ AdvertisementUser user;
|
|
|
+ if ("小程序".equals(wxApp.getCategory())) {
|
|
|
+
|
|
|
+ //小程序支付
|
|
|
+ user = advertisementUserService.getById(StpUserUtil.getLoginIdAsLong());
|
|
|
+ source = AdvertisementOrderDon.Source.Mini_Program;
|
|
|
+ }else {
|
|
|
+
|
|
|
+ source = AdvertisementOrderDon.Source.WeChat;
|
|
|
+ AtomicReference<String> openKey = new AtomicReference<>();
|
|
|
+ Cookie[] cookies = request.getCookies();
|
|
|
+ Stream.of(cookies).forEach((cookie) -> {
|
|
|
+ if (StringUtils.equals(cookie.getName(), OPEN_KEY)) {
|
|
|
+ openKey.set(cookie.getValue());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if (StringUtils.isBlank(openKey.get())) {
|
|
|
+ throw BusinessRuntimeException.getInstance("您还未授权.");
|
|
|
+ }
|
|
|
+ String payOpenId = Codec.DoBase64.custom().setData(openKey.get()).decodeAsText();
|
|
|
+
|
|
|
+ user = new AdvertisementUser();
|
|
|
+ user.setOpenId(payOpenId);
|
|
|
+ user.setId(0L);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ H5JsPayParams payParams = advertisementOrderDonService.pay(weChatPayRequest.getPopularizeId()
|
|
|
+ , donMoney.setScale(0, RoundingMode.DOWN), weChatPayRequest.getAppId(), user, source
|
|
|
, weChatPayRequest.getCallback(), weChatPayRequest.getCallbackType());
|
|
|
|
|
|
|