|  | @@ -11,12 +11,16 @@ import com.ym.mec.biz.service.StudentPaymentOrderService;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.service.StudentRegistrationService;
 | 
	
		
			
				|  |  |  import com.ym.mec.biz.service.VipGroupService;
 | 
	
		
			
				|  |  |  import com.ym.mec.common.controller.BaseController;
 | 
	
		
			
				|  |  | +import com.ym.mec.thirdparty.adapay.NotifyEvent;
 | 
	
		
			
				|  |  |  import com.ym.mec.thirdparty.adapay.Pay;
 | 
	
		
			
				|  |  |  import com.ym.mec.thirdparty.yqpay.Msg;
 | 
	
		
			
				|  |  |  import com.ym.mec.thirdparty.yqpay.YqPayUtil;
 | 
	
		
			
				|  |  |  import com.ym.mec.thirdparty.yqpay.YqPayFeignService;
 | 
	
		
			
				|  |  |  import com.ym.mec.thirdparty.yqpay.RsqMsg;
 | 
	
		
			
				|  |  | +import com.ym.mec.util.http.HttpUtil;
 | 
	
		
			
				|  |  |  import io.swagger.annotations.Api;
 | 
	
		
			
				|  |  | +import io.swagger.annotations.ApiImplicitParam;
 | 
	
		
			
				|  |  | +import io.swagger.annotations.ApiImplicitParams;
 | 
	
		
			
				|  |  |  import io.swagger.annotations.ApiOperation;
 | 
	
		
			
				|  |  |  import org.apache.commons.lang.StringUtils;
 | 
	
		
			
				|  |  |  import org.slf4j.Logger;
 | 
	
	
		
			
				|  | @@ -27,8 +31,11 @@ import org.springframework.scheduling.annotation.Scheduled;
 | 
	
		
			
				|  |  |  import org.springframework.web.bind.annotation.*;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import java.math.BigDecimal;
 | 
	
		
			
				|  |  | +import java.net.URLEncoder;
 | 
	
		
			
				|  |  |  import java.util.*;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import static com.netflix.config.DeploymentContext.ContextKey.appId;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  @RequestMapping("studentOrder")
 | 
	
		
			
				|  |  |  @Api(tags = "订单回调")
 | 
	
		
			
				|  |  |  @RestController
 | 
	
	
		
			
				|  | @@ -106,10 +113,12 @@ public class StudentOrderController extends BaseController {
 | 
	
		
			
				|  |  |          //支付中订单存在,更新状态
 | 
	
		
			
				|  |  |          if (msg.getResponseType().equals("1") && notifyMap.size() > 0) {
 | 
	
		
			
				|  |  |              String tradeState = msg.getCode().equals("88") ? "1" : "0";
 | 
	
		
			
				|  |  | +            String channelType = notifyMap.get("channelType").equals("1") ? "WXPay" : (notifyMap.get("channelType").equals("2") ? "Alipay" : "quickPay");
 | 
	
		
			
				|  |  |              notifyMap.put("tradeState", tradeState);
 | 
	
		
			
				|  |  |              notifyMap.put("totalMoney", notifyMap.get("payAmount"));
 | 
	
		
			
				|  |  |              notifyMap.put("merOrderNo", notifyMap.get("merMerOrderNo"));
 | 
	
		
			
				|  |  | -            this.updateOrder(notifyMap);
 | 
	
		
			
				|  |  | +            notifyMap.put("channelType", channelType);
 | 
	
		
			
				|  |  | +            updateOrder(notifyMap);
 | 
	
		
			
				|  |  |              msg.setCode("000000");
 | 
	
		
			
				|  |  |              msg.setMsg("success");
 | 
	
		
			
				|  |  |          }
 | 
	
	
		
			
				|  | @@ -132,13 +141,39 @@ public class StudentOrderController extends BaseController {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @ApiOperation(value = "台牌支付")
 | 
	
		
			
				|  |  |      @PostMapping("/executePayment")
 | 
	
		
			
				|  |  | -    public Object executePayment(BigDecimal amount, String orderNo, String notifyUrl, String returnUrl, String orderSubject, String orderBody, String payChannel, String sign) throws Exception {
 | 
	
		
			
				|  |  | +    @ApiImplicitParams({
 | 
	
		
			
				|  |  | +            @ApiImplicitParam(name = "amount", value = "支付金额", required = true, dataType = "BigDecimal"),
 | 
	
		
			
				|  |  | +            @ApiImplicitParam(name = "orderNo", value = "订单号", required = true, dataType = "String"),
 | 
	
		
			
				|  |  | +            @ApiImplicitParam(name = "payChannel", value = "支付方式(alipay-支付宝app支付)", required = true, dataType = "String"),
 | 
	
		
			
				|  |  | +    })
 | 
	
		
			
				|  |  | +    public Object executePayment(BigDecimal amount, String orderNo, String payChannel, String notifyUrl, String returnUrl, String orderSubject, String orderBody, String sign, String code) throws Exception {
 | 
	
		
			
				|  |  | +        String appId = "wxcf8e8b33a9477845";
 | 
	
		
			
				|  |  | +        String appSecret = "1286452b9c68b13325dece7cdf892645";
 | 
	
		
			
				|  |  | +        String wxMpOAuth2AccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
 | 
	
		
			
				|  |  |          payChannel = PayChannelEnum.ALIPAY_QR.getCode();
 | 
	
		
			
				|  |  |  //        if (!new Pay().verifySign(amount, orderNo, notifyUrl, orderSubject, orderBody,sign)) {
 | 
	
		
			
				|  |  |  //            return failed("签名验证失败");
 | 
	
		
			
				|  |  |  //        }
 | 
	
		
			
				|  |  | -        Payment payment = new Pay().executePayment(amount, orderNo, payChannel, orderSubject, orderBody);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        String openid = "";
 | 
	
		
			
				|  |  | +        if (payChannel == "wx_pub") {
 | 
	
		
			
				|  |  | +            if (code == null || code.isEmpty()) {
 | 
	
		
			
				|  |  | +                return failed("微信支付请先授权");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            wxMpOAuth2AccessTokenUrl = String.format(wxMpOAuth2AccessTokenUrl, appId, appSecret, code);
 | 
	
		
			
				|  |  | +            Map<String, String> weChatRes = JSON.parseObject(HttpUtil.get(wxMpOAuth2AccessTokenUrl, new HashMap<>()), Map.class);
 | 
	
		
			
				|  |  | +            if (!weChatRes.containsKey("openid")) {
 | 
	
		
			
				|  |  | +                return failed("授权失败,请重新授权");
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            openid = weChatRes.get("openid");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          StudentPaymentOrder order = studentPaymentOrderService.findOrderByOrderNo(orderNo);
 | 
	
		
			
				|  |  | +        if (order == null) {
 | 
	
		
			
				|  |  | +            return failed("订单不存在");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Payment payment = new Pay().executePayment(amount, orderNo, payChannel, orderSubject, orderBody, code);
 | 
	
		
			
				|  |  |          order.setTransNo(payment.getId());
 | 
	
		
			
				|  |  |          studentPaymentOrderService.update(order);
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -180,6 +215,9 @@ public class StudentOrderController extends BaseController {
 | 
	
		
			
				|  |  |              List<Map<String, String>> responseList = JSON.parseObject(responseParameters, List.class);
 | 
	
		
			
				|  |  |              for (Map<String, String> response : responseList) {
 | 
	
		
			
				|  |  |                  Map<String, String> rpMap = response;
 | 
	
		
			
				|  |  | +                String channelType = rpMap.get("channelType").equals("1") ? "WXPay" : (rpMap.get("channelType").equals("2") ? "Alipay" : "quickPay");
 | 
	
		
			
				|  |  | +                rpMap.put("channelType", channelType);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |                  if (Arrays.asList(statusArr).contains(rpMap.get("tradeState"))) {
 | 
	
		
			
				|  |  |                      updateOrder(rpMap); //更新订单
 | 
	
		
			
				|  |  |                  }
 | 
	
	
		
			
				|  | @@ -216,7 +254,6 @@ public class StudentOrderController extends BaseController {
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |      public void closeOrders(List<String> orderNoList) throws Exception {
 | 
	
		
			
				|  |  |          if (orderNoList.size() == 0) {
 | 
	
		
			
				|  |  |              return;
 | 
	
	
		
			
				|  | @@ -241,7 +278,7 @@ public class StudentOrderController extends BaseController {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    @Scheduled(cron = "0/5 * * * * ?")
 | 
	
		
			
				|  |  | +    // @Scheduled(cron = "0/5 * * * * ?")
 | 
	
		
			
				|  |  |      public void adaPayQuery() throws Exception {
 | 
	
		
			
				|  |  |          List<StudentPaymentOrder> payingOrders = studentPaymentOrderService.findOrdersByStatus(DealStatusEnum.ING, "ADAPAY");
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -272,4 +309,78 @@ public class StudentOrderController extends BaseController {
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    @PostMapping("/adaNotify")
 | 
	
		
			
				|  |  | +    public void adaNotify(@ModelAttribute NotifyEvent notifyEvent) throws Exception {
 | 
	
		
			
				|  |  | +        logger.info(notifyEvent.toString());
 | 
	
		
			
				|  |  | +        if (notifyEvent.getType().equals("payment.success") && notifyEvent.getType().equals("payment.failed")) {
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Map<String, String> notifyMap = JSON.parseObject(notifyEvent.getData(), Map.class);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //支付中订单存在,更新状态
 | 
	
		
			
				|  |  | +        if (notifyMap.size() > 0) {
 | 
	
		
			
				|  |  | +            String tradeState = notifyEvent.getType().equals("payment.success") ? "1" : "0";
 | 
	
		
			
				|  |  | +            notifyMap.put("tradeState", tradeState);
 | 
	
		
			
				|  |  | +            notifyMap.put("totalMoney", notifyMap.get("pay_amt"));
 | 
	
		
			
				|  |  | +            notifyMap.put("merOrderNo", notifyMap.get("order_no"));
 | 
	
		
			
				|  |  | +            notifyMap.put("merOrderNo", notifyMap.get("order_no"));
 | 
	
		
			
				|  |  | +            notifyMap.put("remarks", notifyMap.get("description"));
 | 
	
		
			
				|  |  | +            updateOrder(notifyMap);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @GetMapping("/authorize")
 | 
	
		
			
				|  |  | +    public String authorize(@RequestParam("returnUrl") String returnUrl) {
 | 
	
		
			
				|  |  | +        String appId = "wxcf8e8b33a9477845";
 | 
	
		
			
				|  |  | +        String url = URLEncoder.encode("http://wxwechat.utools.club/studentOrder/userInfo");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        String redirectURL = String.format("https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=1&connect_redirect=1#wechat_redirect",
 | 
	
		
			
				|  |  | +                appId, url);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return "redirect:" + redirectURL;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @GetMapping("/userInfo")
 | 
	
		
			
				|  |  | +    public Object userInfo(@RequestParam("code") String code,
 | 
	
		
			
				|  |  | +                           @RequestParam("state") String returnUrl) throws Exception {
 | 
	
		
			
				|  |  | +        String appId = "wxcf8e8b33a9477845";
 | 
	
		
			
				|  |  | +        String appSecret = "1286452b9c68b13325dece7cdf892645";
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        String wxMpOAuth2AccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        wxMpOAuth2AccessTokenUrl = String.format(wxMpOAuth2AccessTokenUrl, appId, appSecret, code);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Map<String, String> map = JSON.parseObject(HttpUtil.get(wxMpOAuth2AccessTokenUrl, new HashMap<>()), Map.class);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (!map.containsKey("openid")) {
 | 
	
		
			
				|  |  | +            return failed("授权失败,请重新授权");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return map.get("openid");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Scheduled(cron = "0/5 * * * * ?")
 | 
	
		
			
				|  |  | +    public void setSuccessStatus() throws Exception {
 | 
	
		
			
				|  |  | +        List<StudentPaymentOrder> payingOrders = studentPaymentOrderService.findOrdersByStatus(DealStatusEnum.ING, "YQPAY");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        String[] statusArr = {"0", "1", "7"};
 | 
	
		
			
				|  |  | +        for (StudentPaymentOrder payingOrder : payingOrders) {
 | 
	
		
			
				|  |  | +            Map<String, String> rpMap = new HashMap<>();
 | 
	
		
			
				|  |  | +            rpMap.put("tradeState", "1");
 | 
	
		
			
				|  |  | +            rpMap.put("remarks", "模拟支付成功");
 | 
	
		
			
				|  |  | +            rpMap.put("merOrderNo", payingOrder.getOrderNo());
 | 
	
		
			
				|  |  | +            rpMap.put("orderNo", payingOrder.getOrderNo());
 | 
	
		
			
				|  |  | +            rpMap.put("channelType", "1");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            String channelType = rpMap.get("channelType").equals("1") ? "WXPay" : (rpMap.get("channelType").equals("2") ? "Alipay" : "quickPay");
 | 
	
		
			
				|  |  | +            rpMap.put("channelType", channelType);
 | 
	
		
			
				|  |  | +            if (Arrays.asList(statusArr).contains(rpMap.get("tradeState"))) {
 | 
	
		
			
				|  |  | +                updateOrder(rpMap); //更新订单
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  }
 |