|
@@ -3,13 +3,18 @@ package com.miaxis.app.controller.wx;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
+import com.miaxis.common.config.AppConfig;
|
|
|
import com.miaxis.common.config.WxpayConfig;
|
|
|
import com.miaxis.common.constant.Constants;
|
|
|
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.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;
|
|
@@ -21,9 +26,15 @@ import com.miaxis.newgzpt.service.IGzptSchPayConfigService;
|
|
|
import com.miaxis.newgzpt.service.IGzptUserInfoService;
|
|
|
import com.miaxis.system.service.ISysDictDataService;
|
|
|
import com.miaxis.wx.domain.WxJsOrder;
|
|
|
+import com.miaxis.wx.domain.WxOrder;
|
|
|
import com.miaxis.wx.dto.WxOrderDTO;
|
|
|
import com.miaxis.wx.dto.WxOrderGoodsDTO;
|
|
|
import com.miaxis.wx.service.IWxJsOrderService;
|
|
|
+import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
|
|
|
+import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
|
|
|
+import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
|
|
|
+import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
|
|
|
+import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.Data;
|
|
@@ -45,6 +56,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
import java.security.PrivateKey;
|
|
|
import java.security.Signature;
|
|
|
import java.util.Base64;
|
|
@@ -60,7 +72,7 @@ import static com.miaxis.common.utils.OrderCodeFactory.getOrderCode;
|
|
|
*/
|
|
|
@RestController
|
|
|
@RequiredArgsConstructor
|
|
|
-@RequestMapping(Constants.OPEN_PREFIX+"/wxjs")
|
|
|
+@RequestMapping(Constants.OPEN_PREFIX + "/wxjs")
|
|
|
@Api(tags = {"【APP-微信支付】"})
|
|
|
@Slf4j
|
|
|
public class WxJsjpController extends BaseController {
|
|
@@ -77,10 +89,9 @@ public class WxJsjpController extends BaseController {
|
|
|
@Autowired
|
|
|
private ISysDictDataService dictDataService;
|
|
|
|
|
|
- @Value("${app.appid}")
|
|
|
- private String appid;
|
|
|
- @Value("${wxpay.notifyUrl}")
|
|
|
- private String notifyUrl ;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AppConfig appConfig;
|
|
|
|
|
|
@Autowired
|
|
|
private IGzptUserInfoService userInfoService;
|
|
@@ -103,7 +114,7 @@ public class WxJsjpController extends BaseController {
|
|
|
*/
|
|
|
@PostMapping(value = "/prepareOrder")
|
|
|
@ApiOperation("极速驾培微信下单")
|
|
|
- public Response<WxParamEntity> getPrepareOrder(@RequestBody WxOrderDTO wxOrderDTO) throws Exception{
|
|
|
+ public Response<WxParamEntity> getPrepareOrder(@RequestBody WxOrderDTO wxOrderDTO) throws Exception {
|
|
|
SysDictData sysDictData = dictDataService.selectDictDataById(wxOrderDTO.getDictCode());
|
|
|
String[] values = sysDictData.getDictValue().split(",");
|
|
|
|
|
@@ -130,28 +141,43 @@ public class WxJsjpController extends BaseController {
|
|
|
order.setIsShare(isShare);
|
|
|
order.setCreateId(gzptUserInfo.getOutCreateId());
|
|
|
|
|
|
- Double price = Double.parseDouble(values[0])*100; //订单总额
|
|
|
- int b = new Double(price).intValue();
|
|
|
+ Double price = Double.parseDouble(values[0]) * 100; //订单总额
|
|
|
+ int b = new Double(price).intValue();
|
|
|
order.setTotal(b);
|
|
|
|
|
|
- if(isShare==1) {
|
|
|
- //如果是合作驾校
|
|
|
- if (wxOrderDTO.getPhoneType()!=null && wxOrderDTO.getPhoneType()==1) { //苹果
|
|
|
- int priceApple = Integer.parseInt(values[3]); //订单总额
|
|
|
- order.setSchoolCommission(priceApple);
|
|
|
- } else if (wxOrderDTO.getPhoneType()!=null && wxOrderDTO.getPhoneType()==2) { //安卓
|
|
|
- int priceAndroid = Integer.parseInt(values[2]); //订单总额
|
|
|
- order.setSchoolCommission(priceAndroid);
|
|
|
+ if (isShare == 1) {
|
|
|
+ //营口学员已绑定教练员
|
|
|
+ if ("2108".equals(gzptUserInfo.getCity()) && gzptUserInfo.getIsBindCoach() == 1) {
|
|
|
+ //如果是合作驾校
|
|
|
+ if (wxOrderDTO.getPhoneType() != null && wxOrderDTO.getPhoneType() == 1) { //苹果
|
|
|
+ int priceApple = Integer.parseInt(values[3]); //订单总额
|
|
|
+ order.setSchoolCommission(priceApple);
|
|
|
+ } else if (wxOrderDTO.getPhoneType() != null && wxOrderDTO.getPhoneType() == 2) { //安卓
|
|
|
+ int priceAndroid = Integer.parseInt(values[2]); //订单总额
|
|
|
+ order.setSchoolCommission(priceAndroid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //非营口只判断有合作
|
|
|
+ if (!"2108".equals(gzptUserInfo.getCity())) {
|
|
|
+ //如果是合作驾校
|
|
|
+ if (wxOrderDTO.getPhoneType() != null && wxOrderDTO.getPhoneType() == 1) { //苹果
|
|
|
+ int priceApple = Integer.parseInt(values[3]); //订单总额
|
|
|
+ order.setSchoolCommission(priceApple);
|
|
|
+ } else if (wxOrderDTO.getPhoneType() != null && wxOrderDTO.getPhoneType() == 2) { //安卓
|
|
|
+ int priceAndroid = Integer.parseInt(values[2]); //订单总额
|
|
|
+ order.setSchoolCommission(priceAndroid);
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
|
- if(sysDictData.getDictLabel().indexOf("计时")!=-1){
|
|
|
- int isActiva = schActivationService.isSchActivation(gzptUserInfo.getOutId());
|
|
|
- if(isActiva==1){
|
|
|
- throw new CustomException("该学员已激活,请退出APP后再重新获取套餐");
|
|
|
- }
|
|
|
- }
|
|
|
+ if (sysDictData.getDictLabel().indexOf("计时") != -1) {
|
|
|
+ int isActiva = schActivationService.isSchActivation(gzptUserInfo.getOutId());
|
|
|
+ if (isActiva == 1) {
|
|
|
+ throw new CustomException("该学员已激活,请退出APP后再重新获取套餐");
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
order.setOrderStatus(OrderStatusEnum.PROCESSING.getCode());
|
|
|
wxJsOrderService.save(order);
|
|
@@ -164,15 +190,15 @@ public class WxJsjpController extends BaseController {
|
|
|
*/
|
|
|
@PostMapping(value = "/getPrepareGoodsOrder")
|
|
|
@ApiOperation("微信商品下单")
|
|
|
- public Response<WxParamEntity> getPrepareGoodsOrder(@RequestBody WxOrderGoodsDTO wxOrderGoodsDTO) throws Exception{
|
|
|
- if(wxOrderGoodsDTO.getGoodsType()==2) {
|
|
|
+ public Response<WxParamEntity> getPrepareGoodsOrder(@RequestBody WxOrderGoodsDTO wxOrderGoodsDTO) throws Exception {
|
|
|
+ if (wxOrderGoodsDTO.getGoodsType() == 2) {
|
|
|
GzptUserInfo gzptUserInfo = userInfoService.getInfoById(wxOrderGoodsDTO.getUserId());
|
|
|
|
|
|
VipUserExam vipUserExam = vipUserExamService.getUserExamByUserIdAndGoodsId(wxOrderGoodsDTO.getUserId(), wxOrderGoodsDTO.getGoodsId());
|
|
|
if (vipUserExam != null) {
|
|
|
Date expirationTime = vipUserExam.getExpirationTime();
|
|
|
Date now = new Date();
|
|
|
- if (expirationTime != null && now.compareTo(expirationTime)<0) {
|
|
|
+ if (expirationTime != null && now.compareTo(expirationTime) < 0) {
|
|
|
throw new CustomException("您已购买过该考场,退回上个页面,重新进入。");
|
|
|
}
|
|
|
}
|
|
@@ -198,7 +224,7 @@ public class WxJsjpController extends BaseController {
|
|
|
order.setOrderStatus(OrderStatusEnum.PROCESSING.getCode());
|
|
|
wxJsOrderService.save(order);
|
|
|
return Response.success(placeWxOrder(order, examInfo.getName()));
|
|
|
- } else if (wxOrderGoodsDTO.getGoodsType()==3) {
|
|
|
+ } else if (wxOrderGoodsDTO.getGoodsType() == 3) {
|
|
|
throw new CustomException("请正确传入GoodsType,GoodsType=2未开发");
|
|
|
} else {
|
|
|
throw new CustomException("请正确传入GoodsType。");
|
|
@@ -209,70 +235,71 @@ public class WxJsjpController extends BaseController {
|
|
|
|
|
|
//下单
|
|
|
private WxParamEntity placeWxOrder(WxJsOrder order, String goodsName) throws Exception {
|
|
|
- HttpPost httpPost = initHttpPost();
|
|
|
+ HttpPost httpPost = initHttpPost(wxpayConfig.getV3url());
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
ObjectNode rootNode = objectMapper.createObjectNode();
|
|
|
- rootNode.put("mchid",wxpayConfig.getMerchantId())
|
|
|
- .put("appid", appid)
|
|
|
+ rootNode.put("mchid", wxpayConfig.getMerchantId())
|
|
|
+ .put("appid", appConfig.getAppid())
|
|
|
.put("description", goodsName)
|
|
|
- .put("notify_url", notifyUrl)
|
|
|
+ .put("notify_url", wxpayConfig.getNotifyUrl())
|
|
|
.put("out_trade_no", order.getOutTradeNo());
|
|
|
rootNode.putObject("amount").put("total", order.getTotal());
|
|
|
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){
|
|
|
+ if (JSONObject.parseObject(bodyAsString).get("prepay_id") == null) {
|
|
|
throw new CustomException(JSONObject.parseObject(bodyAsString).get("message").toString());
|
|
|
}
|
|
|
- return getWxParamJson(bodyAsString,order.getOutTradeNo());
|
|
|
+ return getWxParamJson(bodyAsString, order.getOutTradeNo());
|
|
|
|
|
|
}
|
|
|
|
|
|
+
|
|
|
// 生成调用调起微信支付所需参数
|
|
|
private WxParamEntity getWxParamJson(String bodyAsString, String orderCode) throws Exception {
|
|
|
WxParamEntity wxParamEntity = new WxParamEntity();
|
|
|
- wxParamEntity.setAppId(appid);
|
|
|
+ wxParamEntity.setAppId(appConfig.getAppid());
|
|
|
wxParamEntity.setPartnerId(wxpayConfig.getMerchantId());
|
|
|
- String prepayId = JSONObject.parseObject(bodyAsString).getString("prepay_id");
|
|
|
+ String prepayId = JSONObject.parseObject(bodyAsString).getString("prepay_id");
|
|
|
wxParamEntity.setPrepayId(prepayId);
|
|
|
wxParamEntity.setPackageValue("Sign=WXPay");
|
|
|
String nonce_str = RandomStringUtils.randomAlphanumeric(32);
|
|
|
wxParamEntity.setNonceStr(nonce_str);
|
|
|
- String timestamp = String.valueOf(System.currentTimeMillis()/1000);
|
|
|
+ String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
|
|
|
wxParamEntity.setTimeStamp(timestamp);
|
|
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
- sb.append(appid + "\n");
|
|
|
+ sb.append(appConfig.getAppid() + "\n");
|
|
|
sb.append(timestamp + "\n");
|
|
|
sb.append(nonce_str + "\n");
|
|
|
- sb.append(prepayId+ "\n");
|
|
|
+ sb.append(prepayId + "\n");
|
|
|
|
|
|
|
|
|
File file = new ClassPathResource("wechatpay/apiclient_key.pem").getFile();
|
|
|
- String realPath =file.getAbsolutePath();
|
|
|
+ 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);
|
|
|
+ String base64Str = Base64.getEncoder().encodeToString(signedData);
|
|
|
wxParamEntity.setSign(base64Str);
|
|
|
|
|
|
return wxParamEntity;
|
|
|
}
|
|
|
|
|
|
- private HttpPost initHttpPost() {
|
|
|
- HttpPost httpPost = new HttpPost(wxpayConfig.getV3url());
|
|
|
+ private HttpPost initHttpPost(String url) {
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
httpPost.addHeader("Accept", "application/json");
|
|
|
- httpPost.addHeader("Content-type","application/json; charset=utf-8");
|
|
|
+ httpPost.addHeader("Content-type", "application/json; charset=utf-8");
|
|
|
return httpPost;
|
|
|
}
|
|
|
|
|
|
@Data
|
|
|
- class WxParamEntity{
|
|
|
+ class WxParamEntity {
|
|
|
String appId;
|
|
|
String partnerId;
|
|
|
String prepayId;
|
|
@@ -283,13 +310,205 @@ public class WxJsjpController extends BaseController {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 微信H5商品下单
|
|
|
+ */
|
|
|
+ @PostMapping(value = "/getH5PrepareGoodsOrder")
|
|
|
+ @ApiOperation("微信H5商品下单")
|
|
|
+ public Response<JSONObject> getH5PrepareGoodsOrder(@RequestBody WxOrderGoodsDTO wxOrderGoodsDTO) throws Exception {
|
|
|
+ if (wxOrderGoodsDTO.getGoodsType() == 2) {
|
|
|
+ GzptUserInfo gzptUserInfo = userInfoService.getInfoById(wxOrderGoodsDTO.getUserId());
|
|
|
+
|
|
|
+ VipUserExam vipUserExam = vipUserExamService.getUserExamByUserIdAndGoodsId(wxOrderGoodsDTO.getUserId(), wxOrderGoodsDTO.getGoodsId());
|
|
|
+ if (vipUserExam != null) {
|
|
|
+ Date expirationTime = vipUserExam.getExpirationTime();
|
|
|
+ Date now = new Date();
|
|
|
+ if (expirationTime != null && now.compareTo(expirationTime) < 0) {
|
|
|
+ throw new CustomException("您已购买过该考场,退回上个页面,重新进入。");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ExamInfo examInfo = examInfoService.getById(wxOrderGoodsDTO.getGoodsId());
|
|
|
+ //创建本地微信订单
|
|
|
+ WxJsOrder order = new WxJsOrder();
|
|
|
+ String orderCode = getOrderCode(wxOrderGoodsDTO.getUserId());
|
|
|
+ order.setGoodsId(wxOrderGoodsDTO.getGoodsId());
|
|
|
+ order.setGoodsName(examInfo.getName());
|
|
|
+ order.setGoodsType(wxOrderGoodsDTO.getGoodsType());
|
|
|
+ order.setOutTradeNo(orderCode);
|
|
|
+ order.setUserId(wxOrderGoodsDTO.getUserId());
|
|
|
+ order.setUserName(gzptUserInfo.getUserName());
|
|
|
+ order.setLogincode(gzptUserInfo.getLogincode());
|
|
|
+ order.setPhoneType(wxOrderGoodsDTO.getPhoneType()); //手机类型 1:苹果 2:安卓
|
|
|
+ order.setTotal(examInfo.getPrice());
|
|
|
+ order.setIsShare(0);
|
|
|
+ order.setDqbh(gzptUserInfo.getCity());
|
|
|
+ order.setDqmc(gzptUserInfo.getCityName());
|
|
|
+ order.setSchool(gzptUserInfo.getSchool());
|
|
|
+ order.setSchoolName(gzptUserInfo.getSchoolName());
|
|
|
+ order.setCreateId(gzptUserInfo.getOutCreateId());
|
|
|
+ order.setOrderStatus(OrderStatusEnum.PROCESSING.getCode());
|
|
|
+ wxJsOrderService.save(order);
|
|
|
+ return Response.success(placeH5WxOrder(order, examInfo.getName()));
|
|
|
+ } else if (wxOrderGoodsDTO.getGoodsType() == 3) {
|
|
|
+ throw new CustomException("请正确传入GoodsType,GoodsType=2未开发");
|
|
|
+ } else {
|
|
|
+ throw new CustomException("请正确传入GoodsType。");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信H5支付获取预订单id
|
|
|
+ */
|
|
|
+ @PostMapping(value = "/h5PrepareOrder")
|
|
|
+ @ApiOperation("微信H5支付下单")
|
|
|
+ public Response<JSONObject> h5PrepareOrder(@RequestBody WxOrderDTO wxOrderDTO) throws Exception {
|
|
|
+
|
|
|
+ SysDictData sysDictData = dictDataService.selectDictDataById(wxOrderDTO.getDictCode());
|
|
|
+ String[] values = sysDictData.getDictValue().split(",");
|
|
|
+
|
|
|
+ int isShare = 0;
|
|
|
+
|
|
|
+ //创建本地微信订单
|
|
|
+ WxJsOrder order = new WxJsOrder();
|
|
|
+ GzptUserInfo gzptUserInfo = userInfoService.getInfoById(wxOrderDTO.getUserId());
|
|
|
+ //获取合作驾校配置表
|
|
|
+ isShare = schPayConfigService.isSchPayConfigByUser(gzptUserInfo);
|
|
|
+
|
|
|
+ String orderCode = getOrderCode(gzptUserInfo.getId());
|
|
|
+ order.setOutTradeNo(orderCode);
|
|
|
+ order.setUserId(gzptUserInfo.getId());
|
|
|
+ order.setUserName(gzptUserInfo.getUserName());
|
|
|
+ order.setLogincode(gzptUserInfo.getLogincode());
|
|
|
+ order.setGoodsName(sysDictData.getDictLabel());
|
|
|
+ order.setGoodsType(1);
|
|
|
+ order.setPhoneType(wxOrderDTO.getPhoneType()); //手机类型
|
|
|
+ order.setDqbh(gzptUserInfo.getCity());
|
|
|
+ order.setDqmc(gzptUserInfo.getCityName());
|
|
|
+ order.setSchool(gzptUserInfo.getSchool());
|
|
|
+ order.setSchoolName(gzptUserInfo.getSchoolName());
|
|
|
+ order.setIsShare(isShare);
|
|
|
+ order.setCreateId(gzptUserInfo.getOutCreateId());
|
|
|
+
|
|
|
+ Double price = Double.parseDouble(values[0]) * 100; //订单总额
|
|
|
+ int b = new Double(price).intValue();
|
|
|
+ order.setTotal(b);
|
|
|
+
|
|
|
+ if (isShare == 1) {
|
|
|
+
|
|
|
+ //营口学员已绑定教练员
|
|
|
+ if ("2108".equals(gzptUserInfo.getCity()) && gzptUserInfo.getIsBindCoach() == 1) {
|
|
|
+ //如果是合作驾校
|
|
|
+ if (wxOrderDTO.getPhoneType() != null && wxOrderDTO.getPhoneType() == 1) { //苹果
|
|
|
+ int priceApple = Integer.parseInt(values[3]); //订单总额
|
|
|
+ order.setSchoolCommission(priceApple);
|
|
|
+ } else if (wxOrderDTO.getPhoneType() != null && wxOrderDTO.getPhoneType() == 2) { //安卓
|
|
|
+ int priceAndroid = Integer.parseInt(values[2]); //订单总额
|
|
|
+ order.setSchoolCommission(priceAndroid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //非营口只判断有合作
|
|
|
+ if (!"2108".equals(gzptUserInfo.getCity())) {
|
|
|
+ //如果是合作驾校
|
|
|
+ if (wxOrderDTO.getPhoneType() != null && wxOrderDTO.getPhoneType() == 1) { //苹果
|
|
|
+ int priceApple = Integer.parseInt(values[3]); //订单总额
|
|
|
+ order.setSchoolCommission(priceApple);
|
|
|
+ } else if (wxOrderDTO.getPhoneType() != null && wxOrderDTO.getPhoneType() == 2) { //安卓
|
|
|
+ int priceAndroid = Integer.parseInt(values[2]); //订单总额
|
|
|
+ order.setSchoolCommission(priceAndroid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if (sysDictData.getDictLabel().indexOf("计时") != -1) {
|
|
|
+ int isActiva = schActivationService.isSchActivation(gzptUserInfo.getOutId());
|
|
|
+ if (isActiva == 1) {
|
|
|
+ throw new CustomException("该学员已激活,请退出APP后再重新获取套餐");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ order.setOrderStatus(OrderStatusEnum.PROCESSING.getCode());
|
|
|
+ wxJsOrderService.save(order);
|
|
|
+
|
|
|
+ return Response.success(placeH5WxOrder(order, sysDictData.getDictLabel()));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
+ /**
|
|
|
+ * 微信H5下单
|
|
|
+ *
|
|
|
+ * @param order
|
|
|
+ * @param goodsName
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private JSONObject placeH5WxOrder(WxJsOrder order, String goodsName) throws Exception {
|
|
|
|
|
|
+ HttpPost httpPost = initHttpPost(wxpayConfig.getV3H5Url());
|
|
|
+ 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", appConfig.getAppid())
|
|
|
+ .put("description", goodsName)
|
|
|
+ .put("notify_url", wxpayConfig.getNotifyUrl())
|
|
|
+ .put("out_trade_no", order.getOutTradeNo());
|
|
|
+ rootNode.putObject("amount").put("total", order.getTotal()).put("currency", "CNY");
|
|
|
+
|
|
|
+ //前端类型 1:app 2:小程序 3:公众号
|
|
|
+ if (order.getPhoneType() == 1) { //1苹果 又是APP中支付
|
|
|
+ h5InfoNode.put("type", "iOS");
|
|
|
+ h5InfoNode.put("bundle_id", appConfig.getBundleId());
|
|
|
+ } else if (order.getPhoneType() == 2) { //2安卓 又是APP中支付
|
|
|
+ h5InfoNode.put("type", "Android");
|
|
|
+ h5InfoNode.put("package_name", appConfig.getPackageName());
|
|
|
+ } 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"));
|
|
|
+
|
|
|
+ HttpClient httpClient = initHttpClient();
|
|
|
+ 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 JSONObject.parseObject(bodyAsString);
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
|
|
|
+ private HttpClient initHttpClient() throws IOException {
|
|
|
+ File file = new ClassPathResource("wechatpay/apiclient_key.pem").getFile();
|
|
|
+ String realPath = file.getAbsolutePath();
|
|
|
+ PrivateKey privateKey = CommonUtils.getPrivateKey(realPath);
|
|
|
|
|
|
+ // 加载平台证书(mchId:商户号,mchSerialNo:商户证书序列号,apiV3Key:V3密钥)
|
|
|
+ AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
|
|
|
+ new WechatPay2Credentials(wxpayConfig.getMerchantId(),
|
|
|
+ new PrivateKeySigner(wxpayConfig.getSerialNumber(), privateKey)),
|
|
|
+ wxpayConfig.getV3key().getBytes("utf-8"));
|
|
|
|
|
|
+ // 初始化httpClient
|
|
|
+ HttpClient httpClient = WechatPayHttpClientBuilder.create()
|
|
|
+ .withMerchant(wxpayConfig.getMerchantId(), wxpayConfig.getSerialNumber(), privateKey)
|
|
|
+ .withValidator(new WechatPay2Validator(verifier)).build();
|
|
|
+
|
|
|
+ return httpClient;
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
|
|
|
}
|