|
@@ -0,0 +1,191 @@
|
|
|
+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.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.exception.CustomException;
|
|
|
+import com.miaxis.common.utils.SecurityUtils;
|
|
|
+import com.miaxis.common.utils.uuid.CommonUtils;
|
|
|
+import com.miaxis.system.service.ISysDictDataService;
|
|
|
+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;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang.RandomStringUtils;
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.security.PrivateKey;
|
|
|
+import java.security.Signature;
|
|
|
+import java.util.Base64;
|
|
|
+
|
|
|
+import static com.miaxis.common.utils.OrderCodeFactory.getOrderCode;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 【小程序-微信支付】Controller
|
|
|
+ *
|
|
|
+ * @author miaxis
|
|
|
+ * @date 2021-03-10
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequiredArgsConstructor
|
|
|
+@RequestMapping(Constants.STUDENT_PREFIX+"/xcx/wx")
|
|
|
+@Api(tags = {"【小程序-微信支付】"})
|
|
|
+@Slf4j
|
|
|
+public class XcxWxController extends BaseController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private HttpClient httpClient;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WxpayConfig wxpayConfig;
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ISysDictDataService dictDataService;
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IWxOrderService wxOrderService;
|
|
|
+
|
|
|
+
|
|
|
+ @Value("${xcx.appId}")
|
|
|
+ private String appId;
|
|
|
+ @Value("${wxpay.xcxNotifyUrl}")
|
|
|
+ private String xcxNotifyUrl ;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信支付获取预订单id
|
|
|
+ */
|
|
|
+ @PostMapping(value = "/prepareOrder")
|
|
|
+ @ApiOperation("微信支付下单")
|
|
|
+ public Response<JSONObject> getXcxPrepareOrder(@RequestBody WxOrderDTO wxOrderDTO) throws Exception{
|
|
|
+
|
|
|
+
|
|
|
+ SysDictData sysDictData = dictDataService.selectDictDataById(wxOrderDTO.getDictCode());
|
|
|
+ String[] values = sysDictData.getDictValue().split(",");
|
|
|
+ //创建本地微信订单
|
|
|
+ WxOrder order = createOrder(values);
|
|
|
+ wxOrderService.save(order);
|
|
|
+ return Response.success(placeWxOrder(order, sysDictData.getDictLabel(),xcxNotifyUrl));
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private WxOrder createOrder(String[] values) {
|
|
|
+ WxOrder order = new WxOrder();
|
|
|
+ UserInfo student = SecurityUtils.getLoginUser().getStudent();
|
|
|
+ String orderCode = getOrderCode(student.getId());
|
|
|
+ order.setOutTradeNo(orderCode);
|
|
|
+ order.setXcxOpenid(student.getXcxOpenid());
|
|
|
+ order.setUnionId(student.getUnionId());
|
|
|
+ Double dprice = Double.valueOf(values[0]);
|
|
|
+ Double v =dprice* 100;
|
|
|
+ order.setTotal(v.intValue());
|
|
|
+ //order.setOrderDataJson(values[2]);
|
|
|
+ return order;
|
|
|
+ }
|
|
|
+
|
|
|
+ //下单
|
|
|
+ private JSONObject placeWxOrder(WxOrder order, String goodsName, String notifyUrl) throws Exception {
|
|
|
+ HttpPost httpPost = initHttpPost();
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ ObjectNode rootNode = objectMapper.createObjectNode();
|
|
|
+ rootNode.put("mchid",wxpayConfig.getMerchantId())
|
|
|
+ .put("appid", appId)
|
|
|
+ .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,order.getOutTradeNo());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成调用调起微信支付所需参数
|
|
|
+ private JSONObject getWxParamJson(String bodyAsString, String orderCode) 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("outTradeNo",orderCode);
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|