123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- package com.caimei.controller;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.caimei.model.ResponseJson;
- import com.caimei.model.dto.PaymentDto;
- import com.caimei.service.PayOrderService;
- import com.caimei.util.HttpRequest;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiOperation;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.http.HttpHeaders;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.reactive.function.server.ServerRequest;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * Description
- *
- * @author : plf
- * @date : 2020/5/6
- */
- @Api(tags = "订单支付")
- @Slf4j
- @RestController
- @RequiredArgsConstructor
- @RequestMapping("/PayOrder")
- public class PayOrderApi {
- private final PayOrderService payOrderService;
- @Value("${caimei.notifyUrl}")
- private String notifyUrl;
- @Value("${caimei.redirectLink}")
- private String redirectLink;
- @Value("${wx.AppId}")
- private String appId;
- @Value("${wx.AppSecret}")
- private String appSecret;
- /**
- * 微信线上支付
- */
- @ApiOperation("微信线上支付")
- @PostMapping("/miniWxPay")
- public ResponseJson<JSONObject> miniWxPay(@RequestBody PaymentDto payment, @RequestHeader HttpHeaders headers) {
- if (!"WEIXIN".equals(payment.getPayWay()) || payment.getPayAmount() == null || payment.getPayAmount() < 2) {
- return ResponseJson.error("参数异常", null);
- }
- String clientIp = headers.getFirst("X-CLIENT-IP");
- payment.setClientIp(clientIp);
- //小程序微信快捷支付
- payment.setPayType("MINIAPP_WEIXIN");
- String infos;
- try {
- // 获取当前微信小程序的环境
- String referer = headers.getFirst("Referer");
- log.info("referer-is----:" + referer);
- String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
- Map<String, String> requestUrlParam = new HashMap<String, String>(4);
- // 小程序appId
- requestUrlParam.put("appid", appId);
- // 小程序appSecret
- requestUrlParam.put("secret", appSecret);
- // 小程序端返回的code
- requestUrlParam.put("js_code", payment.getCode());
- // 默认参数
- requestUrlParam.put("grant_type", "authorization_code");
- // 发送post请求读取调用微信接口获取openid用户唯一标识
- infos = HttpRequest.sendPost(requestUrl, requestUrlParam);
- } catch (Exception e) {
- log.error("错误信息", e);
- return ResponseJson.error("wx小程序获取openid失败", null);
- }
- // 解析相应内容(转换成json对象)
- JSONObject jsonObject = JSON.parseObject(infos);
- String openId = jsonObject.getString("openid");
- String errCode = jsonObject.getString("errcode");
- String errMsg = jsonObject.getString("errmsg");
- boolean errFlag = StringUtils.isNotEmpty(errCode) && ("-1".equals(errCode) || "40029".equals(errCode) || "45011".equals(errCode));
- if (errFlag) {
- return ResponseJson.error(-1, errMsg, null);
- }
- if (openId == null) {
- return ResponseJson.error("wx获取openid失败", null);
- }
- payment.setOpenid(openId);
- payment.setNotifyUrl(notifyUrl);
- log.info("wx支付openid>>>>>" + openId);
- return payOrderService.pay(payment);
- }
- /**
- * 支付异步通知回调
- */
- @GetMapping("/paymentCallback")
- public String paymentCallback(ServerRequest request) throws Exception {
- log.info("异步回调通知>>>>>>>start");
- String data = request.pathVariable("data");
- if (StringUtils.isBlank(data)) {
- return "回调参数失败";
- }
- return payOrderService.paymentCallback(data);
- }
- /**
- * 判断此次支付是否完成
- */
- @ApiOperation("判断此次支付是否完成")
- @GetMapping("/payWhetherSuccess")
- public ResponseJson<String> payWhetherSuccess(Integer orderId, Integer paySuccessCounter) {
- if (null == orderId || null == paySuccessCounter) {
- return ResponseJson.error("参数异常", null);
- }
- return payOrderService.payWhetherSuccess(orderId, paySuccessCounter);
- }
- /**
- * 查询本次支付订单是否完成
- */
- @GetMapping("/findOrderStatus")
- public ResponseJson<JSONObject> findOrderStatus(String mbOrderId) {
- if (null == mbOrderId) {
- return ResponseJson.error("参数异常", null);
- }
- return payOrderService.findOrderStatus(mbOrderId);
- }
- }
|