|
@@ -9,6 +9,7 @@ import com.miaxis.common.core.controller.BaseController;
|
|
|
import com.miaxis.common.core.domain.Response;
|
|
|
import com.miaxis.common.core.domain.entity.SysDictData;
|
|
|
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.uuid.CommonUtils;
|
|
@@ -18,6 +19,7 @@ import com.miaxis.system.service.ISysDictDataService;
|
|
|
import com.miaxis.system.service.ISysDictTypeService;
|
|
|
import com.miaxis.system.service.ISysUserService;
|
|
|
import com.miaxis.wx.domain.WxOrder;
|
|
|
+import com.miaxis.wx.dto.WxOrderDTO;
|
|
|
import com.miaxis.wx.service.IWxOrderService;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
@@ -69,50 +71,29 @@ public class WxController extends BaseController {
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
- private ISysUserService sysUserService;
|
|
|
+ private ISysDictDataService dictDataService;
|
|
|
|
|
|
- @Autowired
|
|
|
- private ISysConfigService sysConfigService;
|
|
|
|
|
|
- @Autowired
|
|
|
- private ISysDictDataService sysDictDataService;
|
|
|
+ @Value("${xcx.appid}")
|
|
|
+ private String xcxAppid;
|
|
|
+
|
|
|
|
|
|
@Value("${app.appid}")
|
|
|
private String appid;
|
|
|
- @Value("${wxpay.notifyUrl}")
|
|
|
- private String notifyUrl ;
|
|
|
-
|
|
|
|
|
|
|
|
|
+ @Value("${wxpay.notifyUrl}")
|
|
|
+ private String notifyUrl ;
|
|
|
|
|
|
|
|
|
|
|
|
-// @GetMapping("/order/list")
|
|
|
-// @ApiOperation("查询订单列表")
|
|
|
-// @ApiImplicitParams({
|
|
|
-// @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
|
|
|
-// @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
|
|
|
-// })
|
|
|
-// public ResponsePageInfo<WxOrder> list(@ModelAttribute WxOrderListDTO wxOrderListDTO){
|
|
|
-//
|
|
|
-// String openid = SecurityUtils.getLoginUser().getStudent().getAppOpenid()();
|
|
|
-// startPage();
|
|
|
-// wxOrderListDTO.setOpenid(openid);
|
|
|
-// List<WxOrder> list = wxOrderService.selectOrderList(wxOrderListDTO);
|
|
|
-// return toResponsePageInfo(list);
|
|
|
-// }
|
|
|
|
|
|
/**
|
|
|
- * 微信支付获取预订单id
|
|
|
+ * APP微信支付获取预订单id
|
|
|
*/
|
|
|
- @PostMapping(value = "/prepareOrder")
|
|
|
+ @PostMapping(value = "/appPrepareOrder")
|
|
|
@ApiOperation("app端微信支付下单")
|
|
|
- public Response<WxParamEntity> getPrepareOrder(@RequestParam @ApiParam(name="dictCode",value="字典编码")Long dictCode) throws Exception{
|
|
|
-
|
|
|
- //兼容线上,app上线后删除
|
|
|
-// if (type == null){
|
|
|
-// type = 1;
|
|
|
-// }
|
|
|
+ public Response<WxParamEntity> appPrepareOrder(@RequestParam @ApiParam(name="dictCode",value="字典编码")Long dictCode) throws Exception{
|
|
|
//创建本地微信订单
|
|
|
WxOrder order = new WxOrder();
|
|
|
UserInfo student = SecurityUtils.getLoginUser().getStudent();
|
|
@@ -121,49 +102,28 @@ public class WxController extends BaseController {
|
|
|
order.setAppOpenid(student.getAppOpenid());
|
|
|
order.setUnionId(student.getUnionId());
|
|
|
|
|
|
- SysDictData sysDictData = sysDictDataService.selectDictDataById(dictCode);
|
|
|
- wxOrderService.createVipOrder(order,sysDictData);
|
|
|
- wxOrderService.save(order);
|
|
|
-
|
|
|
- return Response.success(placeWxOrder(order, order.getGoodsName()));
|
|
|
-
|
|
|
+ SysDictData sysDictData = dictDataService.selectDictDataById(dictCode);
|
|
|
+ String[] values = sysDictData.getDictValue().split(",");
|
|
|
+ Double price = Double.parseDouble(values[0])*100; //订单总额
|
|
|
+ int b = new Double(price).intValue();
|
|
|
+ order.setTotal(b);
|
|
|
+ order.setOrderStatus(OrderStatusEnum.PROCESSING.getCode());
|
|
|
|
|
|
- }
|
|
|
|
|
|
- private String getKeyByType(Integer type) {
|
|
|
- if(type == null){
|
|
|
- throw new CustomException("类型不能为空");
|
|
|
- }
|
|
|
- if (type == 1){
|
|
|
- return "vip_price";
|
|
|
- }
|
|
|
- if (type == 2){
|
|
|
- return "vip_price_recommend";
|
|
|
- }
|
|
|
- if (type == 3){
|
|
|
- return "vip_price_upgrade";
|
|
|
- }
|
|
|
- throw new CustomException("类型值有误");
|
|
|
+ wxOrderService.save(order);
|
|
|
+ return Response.success(placeAppWxOrder(order, order.getGoodsName()));
|
|
|
}
|
|
|
|
|
|
|
|
|
-// /**
|
|
|
-// * 查询是否存在推荐码
|
|
|
-// */
|
|
|
-// @GetMapping(value = "/{referralCode}")
|
|
|
-// @ApiOperation("查询是否存在推荐码")
|
|
|
-// public Response<Boolean> getInfo(
|
|
|
-// @ApiParam(name = "referralCode", value = "推荐码", required = true)
|
|
|
-// @PathVariable("referralCode") String referralCode
|
|
|
-// ){
|
|
|
-//
|
|
|
-// return Response.success(sysUserService.selectUserByReferralCode(referralCode)!= null ? true :false);
|
|
|
-// }
|
|
|
-
|
|
|
-
|
|
|
- //下单
|
|
|
- private WxParamEntity placeWxOrder(WxOrder order, String goodsName) throws Exception {
|
|
|
- HttpPost httpPost = initHttpPost();
|
|
|
+ /**
|
|
|
+ * APP下单
|
|
|
+ * @param order
|
|
|
+ * @param goodsName
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private WxParamEntity placeAppWxOrder(WxOrder order, String goodsName) throws Exception {
|
|
|
+ HttpPost httpPost = initHttpPost(wxpayConfig.getV3url());
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
ObjectNode rootNode = objectMapper.createObjectNode();
|
|
@@ -181,12 +141,17 @@ public class WxController extends BaseController {
|
|
|
if (JSONObject.parseObject(bodyAsString).get("prepay_id") == null){
|
|
|
throw new CustomException(JSONObject.parseObject(bodyAsString).get("message").toString());
|
|
|
}
|
|
|
- return getWxParamJson(bodyAsString,order.getOutTradeNo());
|
|
|
-
|
|
|
+ return getWxAppParamJson(bodyAsString);
|
|
|
}
|
|
|
|
|
|
- // 生成调用调起微信支付所需参数
|
|
|
- private WxParamEntity getWxParamJson(String bodyAsString, String orderCode) throws Exception {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * APP支付生成调用调起微信支付所需参数
|
|
|
+ * @param bodyAsString
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private WxParamEntity getWxAppParamJson(String bodyAsString) throws Exception {
|
|
|
WxParamEntity wxParamEntity = new WxParamEntity();
|
|
|
wxParamEntity.setAppId(appid);
|
|
|
wxParamEntity.setPartnerId(wxpayConfig.getMerchantId());
|
|
@@ -204,7 +169,6 @@ public class WxController extends BaseController {
|
|
|
sb.append(nonce_str + "\n");
|
|
|
sb.append(prepayId+ "\n");
|
|
|
|
|
|
-
|
|
|
File file = new ClassPathResource("wechatpay/apiclient_key.pem").getFile();
|
|
|
String realPath =file.getAbsolutePath();
|
|
|
PrivateKey privateKey = CommonUtils.getPrivateKey(realPath);
|
|
@@ -219,13 +183,9 @@ public class WxController extends BaseController {
|
|
|
return wxParamEntity;
|
|
|
}
|
|
|
|
|
|
- private HttpPost initHttpPost() {
|
|
|
- HttpPost httpPost = new HttpPost(wxpayConfig.getV3url());
|
|
|
- httpPost.addHeader("Accept", "application/json");
|
|
|
- httpPost.addHeader("Content-type","application/json; charset=utf-8");
|
|
|
- return httpPost;
|
|
|
- }
|
|
|
-
|
|
|
+ /**
|
|
|
+ * APP支付返回参数
|
|
|
+ */
|
|
|
@Data
|
|
|
class WxParamEntity{
|
|
|
String appId;
|
|
@@ -240,11 +200,122 @@ public class WxController extends BaseController {
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 微信小程序支付获取预订单id
|
|
|
+ */
|
|
|
+ @PostMapping(value = "/xcxPrepareOrder")
|
|
|
+ @ApiOperation("微信小程序支付下单")
|
|
|
+ public Response<JSONObject> xcxPrepareOrder(@RequestBody WxOrderDTO wxOrderDTO) throws Exception{
|
|
|
+
|
|
|
+ SysDictData sysDictData = dictDataService.selectDictDataById(wxOrderDTO.getDictCode());
|
|
|
+ String[] values = sysDictData.getDictValue().split(",");
|
|
|
+ //创建本地微信订单
|
|
|
+ WxOrder order = new WxOrder();
|
|
|
+ UserInfo userInfo = SecurityUtils.getLoginUser().getStudent();
|
|
|
+ String orderCode = getOrderCode(userInfo.getId());
|
|
|
+ order.setOutTradeNo(orderCode);
|
|
|
+ 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:公众号
|
|
|
+
|
|
|
+ Double price = Double.parseDouble(values[0])*100; //订单总额
|
|
|
+ int b = new Double(price).intValue();
|
|
|
+ order.setTotal(b);
|
|
|
+ order.setOrderStatus(OrderStatusEnum.PROCESSING.getCode());
|
|
|
+ wxOrderService.save(order);
|
|
|
+ return Response.success(placeXcxWxOrder(order, sysDictData.getDictLabel()));
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信小程序或公众号内 JSAPI下单
|
|
|
+ * @param order
|
|
|
+ * @param goodsName
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private JSONObject placeXcxWxOrder(WxOrder order, String goodsName) throws Exception {
|
|
|
+ HttpPost httpPost = initHttpPost(wxpayConfig.getV3JsUrl());
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ ObjectNode rootNode = objectMapper.createObjectNode();
|
|
|
+ rootNode.put("mchid",wxpayConfig.getMerchantId())
|
|
|
+ .put("appid", xcxAppid)
|
|
|
+ .put("description", goodsName)
|
|
|
+ .put("notify_url", notifyUrl)
|
|
|
+ .put("out_trade_no", order.getOutTradeNo());
|
|
|
+ rootNode.putObject("amount").put("total", order.getTotal());
|
|
|
+ rootNode.putObject("payer").put("openid", order.getXcxOpenid());
|
|
|
+ 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);
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 微信小程序或公众号内 JSAPI下单
|
|
|
+ * 生成调用调起微信支付所需参数
|
|
|
+ * @param bodyAsString
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private JSONObject getWxParamJson(String bodyAsString) throws Exception {
|
|
|
+ String packageStr = "prepay_id="+JSONObject.parseObject(bodyAsString).get("prepay_id");
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("package",packageStr);
|
|
|
+ String nonce_str = RandomStringUtils.randomAlphanumeric(32);
|
|
|
+ jsonObject.put("nonceStr",nonce_str);
|
|
|
+ long timestamp = System.currentTimeMillis()/1000;
|
|
|
+ jsonObject.put("timeStamp",String.valueOf(timestamp));
|
|
|
+ jsonObject.put("signType","RSA");
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ sb.append(appid + "\n");
|
|
|
+ sb.append(timestamp + "\n");
|
|
|
+ sb.append(nonce_str + "\n");
|
|
|
+ sb.append(packageStr+ "\n");
|
|
|
+ System.out.println(sb);
|
|
|
+
|
|
|
+ File file = new ClassPathResource("wechatpay/apiclient_key.pem").getFile();
|
|
|
+ String realPath =file.getAbsolutePath();
|
|
|
+ PrivateKey privateKey = CommonUtils.getPrivateKey(realPath);
|
|
|
+ // 进行签名服务
|
|
|
+ Signature signature = Signature.getInstance("SHA256withRSA");
|
|
|
+ signature.initSign(privateKey);
|
|
|
+ signature.update(sb.toString().getBytes("UTF-8"));
|
|
|
+ byte[] signedData = signature.sign();
|
|
|
+ String base64Str = Base64.getEncoder().encodeToString(signedData);
|
|
|
+ jsonObject.put("paySign",base64Str);
|
|
|
+ jsonObject.put("appId",appid);
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private HttpPost initHttpPost(String url) {
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ httpPost.addHeader("Accept", "application/json");
|
|
|
+ httpPost.addHeader("Content-type","application/json; charset=utf-8");
|
|
|
+ return httpPost;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
}
|