PayOrderApi.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package com.caimei.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.caimei.model.ResponseJson;
  5. import com.caimei.model.dto.PaymentDto;
  6. import com.caimei.service.PayOrderService;
  7. import com.caimei.util.HttpRequest;
  8. import io.swagger.annotations.Api;
  9. import io.swagger.annotations.ApiOperation;
  10. import lombok.RequiredArgsConstructor;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.apache.commons.lang3.StringUtils;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.http.HttpHeaders;
  15. import org.springframework.web.bind.annotation.*;
  16. import org.springframework.web.reactive.function.server.ServerRequest;
  17. import java.util.HashMap;
  18. import java.util.Map;
  19. /**
  20. * Description
  21. *
  22. * @author : plf
  23. * @date : 2020/5/6
  24. */
  25. @Api(tags = "订单支付")
  26. @Slf4j
  27. @RestController
  28. @RequiredArgsConstructor
  29. @RequestMapping("/PayOrder")
  30. public class PayOrderApi {
  31. private final PayOrderService payOrderService;
  32. @Value("${caimei.notifyUrl}")
  33. private String notifyUrl;
  34. @Value("${caimei.redirectLink}")
  35. private String redirectLink;
  36. @Value("${wx.AppId}")
  37. private String appId;
  38. @Value("${wx.AppSecret}")
  39. private String appSecret;
  40. /**
  41. * 微信线上支付
  42. */
  43. @ApiOperation("微信线上支付")
  44. @PostMapping("/miniWxPay")
  45. public ResponseJson<JSONObject> miniWxPay(@RequestBody PaymentDto payment, @RequestHeader HttpHeaders headers) {
  46. if (!"WEIXIN".equals(payment.getPayWay()) || payment.getPayAmount() == null || payment.getPayAmount() < 2) {
  47. return ResponseJson.error("参数异常", null);
  48. }
  49. String clientIp = headers.getFirst("X-CLIENT-IP");
  50. payment.setClientIp(clientIp);
  51. //小程序微信快捷支付
  52. payment.setPayType("MINIAPP_WEIXIN");
  53. String infos;
  54. try {
  55. // 获取当前微信小程序的环境
  56. String referer = headers.getFirst("Referer");
  57. log.info("referer-is----:" + referer);
  58. String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";
  59. Map<String, String> requestUrlParam = new HashMap<String, String>(4);
  60. // 小程序appId
  61. requestUrlParam.put("appid", appId);
  62. // 小程序appSecret
  63. requestUrlParam.put("secret", appSecret);
  64. // 小程序端返回的code
  65. requestUrlParam.put("js_code", payment.getCode());
  66. // 默认参数
  67. requestUrlParam.put("grant_type", "authorization_code");
  68. // 发送post请求读取调用微信接口获取openid用户唯一标识
  69. infos = HttpRequest.sendPost(requestUrl, requestUrlParam);
  70. } catch (Exception e) {
  71. log.error("错误信息", e);
  72. return ResponseJson.error("wx小程序获取openid失败", null);
  73. }
  74. // 解析相应内容(转换成json对象)
  75. JSONObject jsonObject = JSON.parseObject(infos);
  76. String openId = jsonObject.getString("openid");
  77. String errCode = jsonObject.getString("errcode");
  78. String errMsg = jsonObject.getString("errmsg");
  79. boolean errFlag = StringUtils.isNotEmpty(errCode) && ("-1".equals(errCode) || "40029".equals(errCode) || "45011".equals(errCode));
  80. if (errFlag) {
  81. return ResponseJson.error(-1, errMsg, null);
  82. }
  83. if (openId == null) {
  84. return ResponseJson.error("wx获取openid失败", null);
  85. }
  86. payment.setOpenid(openId);
  87. payment.setNotifyUrl(notifyUrl);
  88. log.info("wx支付openid>>>>>" + openId);
  89. return payOrderService.pay(payment);
  90. }
  91. /**
  92. * 支付异步通知回调
  93. */
  94. @GetMapping("/paymentCallback")
  95. public String paymentCallback(ServerRequest request) throws Exception {
  96. log.info("异步回调通知>>>>>>>start");
  97. String data = request.pathVariable("data");
  98. if (StringUtils.isBlank(data)) {
  99. return "回调参数失败";
  100. }
  101. return payOrderService.paymentCallback(data);
  102. }
  103. /**
  104. * 判断此次支付是否完成
  105. */
  106. @ApiOperation("判断此次支付是否完成")
  107. @GetMapping("/payWhetherSuccess")
  108. public ResponseJson<String> payWhetherSuccess(Integer orderId, Integer paySuccessCounter) {
  109. if (null == orderId || null == paySuccessCounter) {
  110. return ResponseJson.error("参数异常", null);
  111. }
  112. return payOrderService.payWhetherSuccess(orderId, paySuccessCounter);
  113. }
  114. /**
  115. * 查询本次支付订单是否完成
  116. */
  117. @GetMapping("/findOrderStatus")
  118. public ResponseJson<JSONObject> findOrderStatus(String mbOrderId) {
  119. if (null == mbOrderId) {
  120. return ResponseJson.error("参数异常", null);
  121. }
  122. return payOrderService.findOrderStatus(mbOrderId);
  123. }
  124. }