|
@@ -11,6 +11,8 @@ import com.miaxis.common.core.domain.entity.UserInfo;
|
|
|
import com.miaxis.common.enums.OrderStatusEnum;
|
|
|
import com.miaxis.common.exception.CustomException;
|
|
|
import com.miaxis.common.utils.SecurityUtils;
|
|
|
+import com.miaxis.common.utils.ServletUtils;
|
|
|
+import com.miaxis.common.utils.ip.IpUtils;
|
|
|
import com.miaxis.common.utils.uuid.CommonUtils;
|
|
|
import com.miaxis.exam.domain.ExamInfo;
|
|
|
import com.miaxis.exam.service.IExamInfoService;
|
|
@@ -84,6 +86,15 @@ public class WxController extends BaseController {
|
|
|
@Value("${app.appId}")
|
|
|
private String appId;
|
|
|
|
|
|
+ @Value("${app.bundleId}")
|
|
|
+ private String bundleId;
|
|
|
+
|
|
|
+ @Value("${app.packageName}")
|
|
|
+ private String packageName;
|
|
|
+
|
|
|
+ @Value("${gzh.appId}")
|
|
|
+ private String gzhAppId;
|
|
|
+
|
|
|
@Value("${wxpay.notifyUrl}")
|
|
|
private String notifyUrl ;
|
|
|
|
|
@@ -326,6 +337,94 @@ public class WxController extends BaseController {
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 微信小程序支付获取预订单id
|
|
|
+ */
|
|
|
+ @PostMapping(value = "/h5PrepareOrder")
|
|
|
+ @ApiOperation("微信H5支付下单")
|
|
|
+ public Response<JSONObject> h5PrepareOrder(@RequestBody WxOrderDTO wxOrderDTO) throws Exception{
|
|
|
+ UserInfo userInfo = SecurityUtils.getLoginUser().getStudent();
|
|
|
+ if(wxOrderDTO.getGoodsType()==1) {
|
|
|
+ int count = vipUserExamService.getUserExamByUnionIdAndGoodsId(userInfo.getUnionId(), wxOrderDTO.getGoodsId());
|
|
|
+ if (count > 0) {
|
|
|
+ throw new CustomException("您已购买过该考场,退回上个页面,重新进入。");
|
|
|
+ }
|
|
|
+ ExamInfo examInfo = examInfoService.getById(wxOrderDTO.getGoodsId());
|
|
|
+ //创建本地微信订单
|
|
|
+ WxOrder order = new WxOrder();
|
|
|
+ String orderCode = getOrderCode(userInfo.getId());
|
|
|
+ order.setGoodsId(wxOrderDTO.getGoodsId());
|
|
|
+ order.setGoodsName(examInfo.getName());
|
|
|
+ order.setGoodsType(1);
|
|
|
+ order.setOutTradeNo(orderCode);
|
|
|
+ order.setUserId(userInfo.getId());
|
|
|
+ order.setXcxOpenid(userInfo.getXcxOpenid());
|
|
|
+ order.setAppOpenid(userInfo.getAppOpenid());
|
|
|
+ order.setGzhOpenid(userInfo.getGzhOpenid());
|
|
|
+ order.setUnionId(userInfo.getUnionId());
|
|
|
+ order.setPhoneType(wxOrderDTO.getPhoneType()); //手机类型 1:苹果 2:安卓
|
|
|
+ order.setForeType(wxOrderDTO.getForeType()); //前端类型 1:app 2:小程序 3:公众号
|
|
|
+ order.setTotal(examInfo.getPrice());
|
|
|
+ order.setOrderStatus(OrderStatusEnum.PROCESSING.getCode());
|
|
|
+ wxOrderService.save(order);
|
|
|
+ return Response.success(placeH5WxOrder(order, examInfo.getName()));
|
|
|
+ } else if (wxOrderDTO.getGoodsType()==2) {
|
|
|
+ throw new CustomException("请正确传入GoodsType,GoodsType=2未开发");
|
|
|
+ } else {
|
|
|
+ throw new CustomException("请正确传入GoodsType。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信H5下单
|
|
|
+ * @param order
|
|
|
+ * @param goodsName
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private JSONObject placeH5WxOrder(WxOrder order, String goodsName) throws Exception {
|
|
|
+ HttpPost httpPost = initHttpPost(wxpayConfig.getV3JsUrl());
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ ObjectNode rootNode = objectMapper.createObjectNode();
|
|
|
+ ObjectNode sceneInfoNode = objectMapper.createObjectNode();
|
|
|
+ ObjectNode h5InfoNode = objectMapper.createObjectNode();
|
|
|
+ rootNode.put("mchid",wxpayConfig.getMerchantId())
|
|
|
+ .put("appid", gzhAppId)
|
|
|
+ .put("description", goodsName)
|
|
|
+ .put("notify_url", notifyUrl)
|
|
|
+ .put("out_trade_no", order.getOutTradeNo());
|
|
|
+ rootNode.putObject("amount").put("total", order.getTotal()).put("currency","CNY");
|
|
|
+
|
|
|
+ //前端类型 1:app 2:小程序 3:公众号
|
|
|
+ if(order.getPhoneType()==1 && order.getForeType()==1){ //1苹果 又是APP中支付
|
|
|
+ h5InfoNode.put("type", "iOS");
|
|
|
+ h5InfoNode.put("bundle_id",bundleId);
|
|
|
+ } else if (order.getPhoneType()==2 && order.getForeType()==1){ //2安卓 又是APP中支付
|
|
|
+ h5InfoNode.put("type", "Android");
|
|
|
+ h5InfoNode.put("package_name",packageName);
|
|
|
+ } else {
|
|
|
+ h5InfoNode.put("type", "Wap");
|
|
|
+ }
|
|
|
+ String ipAddr = IpUtils.getIpAddr(ServletUtils.getRequest()); //获取请求IP
|
|
|
+ sceneInfoNode.put("payer_client_ip", ipAddr)
|
|
|
+ .set("h5_info", h5InfoNode);
|
|
|
+ rootNode.set("scene_info",sceneInfoNode);
|
|
|
+
|
|
|
+ objectMapper.writeValue(bos, rootNode);
|
|
|
+ httpPost.setEntity(new StringEntity(rootNode.toString(), "utf-8"));
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
+ String bodyAsString = EntityUtils.toString(response.getEntity());
|
|
|
+ if (JSONObject.parseObject(bodyAsString).get("prepay_id") == null){
|
|
|
+ throw new CustomException(JSONObject.parseObject(bodyAsString).get("message").toString());
|
|
|
+ }
|
|
|
+ return getWxParamJson(bodyAsString);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
|
|
|
|