|
@@ -1,8 +1,8 @@
|
|
|
package com.yonge.toolset.payment.original.wx;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
-import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
+import com.github.binarywang.wxpay.bean.notify.SignatureHeader;
|
|
|
+import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyV3Result;
|
|
|
+import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyV3Result;
|
|
|
import com.github.binarywang.wxpay.bean.request.*;
|
|
|
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryV3Result;
|
|
|
import com.github.binarywang.wxpay.bean.result.WxPayRefundV3Result;
|
|
@@ -11,18 +11,22 @@ import com.github.binarywang.wxpay.exception.WxPayException;
|
|
|
import com.github.binarywang.wxpay.service.WxPayService;
|
|
|
import com.yonge.toolset.base.result.BaseResult;
|
|
|
import com.yonge.toolset.payment.base.PaymentTemplate;
|
|
|
+import com.yonge.toolset.payment.base.enums.MethodNameEnum;
|
|
|
import com.yonge.toolset.payment.base.enums.OpenEnum;
|
|
|
+import com.yonge.toolset.payment.base.enums.PayChannelEnum;
|
|
|
import com.yonge.toolset.payment.base.enums.TradeStatusEnum;
|
|
|
import com.yonge.toolset.payment.base.model.ClosePayment;
|
|
|
import com.yonge.toolset.payment.base.model.OpenAuth;
|
|
|
import com.yonge.toolset.payment.base.model.Payment;
|
|
|
import com.yonge.toolset.payment.base.model.RefundBill;
|
|
|
+import com.yonge.toolset.payment.base.model.callback.PaymentCallBack;
|
|
|
import com.yonge.toolset.payment.core.props.PaymentProperties;
|
|
|
import com.yonge.toolset.payment.core.service.SysConfigPaymentService;
|
|
|
import com.yonge.toolset.payment.original.wx.constant.WxpayConstant;
|
|
|
import com.yonge.toolset.utils.collection.MapUtil;
|
|
|
import com.yonge.toolset.utils.obj.ObjectUtil;
|
|
|
import com.yonge.toolset.utils.string.StringUtil;
|
|
|
+import com.yonge.toolset.utils.web.WebUtil;
|
|
|
import org.apache.commons.collections.map.HashedMap;
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
@@ -34,10 +38,13 @@ import org.apache.http.util.EntityUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Component
|
|
@@ -51,221 +58,13 @@ public class OriginalWxAppTemplate implements PaymentTemplate {
|
|
|
@Autowired
|
|
|
private SysConfigPaymentService configPaymentService;
|
|
|
|
|
|
- /*@Autowired
|
|
|
- private CloseableHttpClient httpClient;*/
|
|
|
@Autowired
|
|
|
private WxPayService wxPayService;
|
|
|
- private String tradeState;
|
|
|
-
|
|
|
|
|
|
@Override
|
|
|
public BaseResult<Map<String, Object>> getOpenAuthMsg(OpenAuth openAuth) {
|
|
|
return null;
|
|
|
}
|
|
|
- /**
|
|
|
- @Override
|
|
|
- public BaseResult<Payment> executePayment(Payment payment) {
|
|
|
- String APP_ID = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_APPID).getParamValue();
|
|
|
- String MERCHANT_ID = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_MERCHANT_ID).getParamValue();
|
|
|
-
|
|
|
- HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/app");
|
|
|
- httpPost.addHeader("Accept", "application/json");
|
|
|
- httpPost.addHeader("Content-type", "application/json; charset=utf-8");
|
|
|
-
|
|
|
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
-
|
|
|
- ObjectNode rootNode = objectMapper.createObjectNode();
|
|
|
- rootNode.put("mchid", MERCHANT_ID)
|
|
|
- .put("appid", APP_ID)
|
|
|
- .put("description", payment.getOrderDesc())
|
|
|
- .put("out_trade_no", payment.getPaymentNo())
|
|
|
- .put("notify_url", paymentProperties.getNotifyUrl()
|
|
|
- + "/" + payment.getOpenType().getCode()
|
|
|
- + "/" + payment.getPayChannel().getCode()
|
|
|
- + "/executePayment");
|
|
|
-
|
|
|
- if (StringUtil.isEmpty(payment.getPayAmt())) {
|
|
|
- return BaseResult.failed("微信APP支付金额异常");
|
|
|
- }
|
|
|
-
|
|
|
- BigDecimal amnout = new BigDecimal(payment.getPayAmt()).multiply(new BigDecimal("100"));
|
|
|
-
|
|
|
- rootNode.putObject("amount")
|
|
|
- .put("total", amnout.abs().intValue());
|
|
|
-
|
|
|
- try {
|
|
|
- objectMapper.writeValue(bos, rootNode);
|
|
|
- httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return BaseResult.failed("微信APP支付参数转换失败");
|
|
|
- }
|
|
|
-
|
|
|
- CloseableHttpResponse response;
|
|
|
- try {
|
|
|
- response = httpClient.execute(httpPost);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return BaseResult.failed("微信APP支付请求失败");
|
|
|
- }
|
|
|
- String bodyAsString;
|
|
|
- try {
|
|
|
- bodyAsString = EntityUtils.toString(response.getEntity());
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return BaseResult.failed("微信APP支付返回结果转换失败");
|
|
|
- }
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(bodyAsString);
|
|
|
- String prepay_id = jsonObject.getString("prepay_id");
|
|
|
- payment.setPayInfo(prepay_id);
|
|
|
-
|
|
|
- BaseResult<Payment> paymentBaseResult = queryPayment(payment);
|
|
|
- if (paymentBaseResult.getStatus()) {
|
|
|
- payment.setId(paymentBaseResult.getData().getId());
|
|
|
- }
|
|
|
- return BaseResult.succeed(payment);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public BaseResult<Payment> queryPayment(Payment payment) {
|
|
|
- String MERCHANT_ID = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_MERCHANT_ID).getParamValue();
|
|
|
-
|
|
|
- String url = "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + payment.getPaymentNo() + "?mchid=" + MERCHANT_ID;
|
|
|
-
|
|
|
- HttpGet httpGet;
|
|
|
- try {
|
|
|
- URIBuilder uriBuilder = new URIBuilder(url);
|
|
|
- httpGet = new HttpGet(uriBuilder.build());
|
|
|
- httpGet.addHeader("Accept", "application/json");
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return BaseResult.failed("微信APP支付参数转换失败");
|
|
|
- }
|
|
|
-
|
|
|
- CloseableHttpResponse response;
|
|
|
- try {
|
|
|
- response = httpClient.execute(httpGet);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return BaseResult.failed("微信APP支付请求失败");
|
|
|
- }
|
|
|
- String bodyAsString;
|
|
|
- try {
|
|
|
- bodyAsString = EntityUtils.toString(response.getEntity());
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return BaseResult.failed("微信APP支付返回结果转换失败");
|
|
|
- }
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(bodyAsString);
|
|
|
- payment.setId(jsonObject.getString("transaction_id"));
|
|
|
-
|
|
|
- String trade_state = jsonObject.getString("trade_state");
|
|
|
- if ("SUCCESS".equals(trade_state)) {
|
|
|
- payment.setStatus(TradeStatusEnum.succeeded);
|
|
|
- } else if ("REFUND".equals(trade_state)) {
|
|
|
- payment.setStatus(TradeStatusEnum.close);
|
|
|
- } else if ("NOTPAY".equals(trade_state)) {
|
|
|
- payment.setStatus(TradeStatusEnum.pending);
|
|
|
- } else if ("CLOSED".equals(trade_state)) {
|
|
|
- payment.setStatus(TradeStatusEnum.close);
|
|
|
- }
|
|
|
- return BaseResult.succeed(payment);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public BaseResult<ClosePayment> closePayment(ClosePayment closePayment) {
|
|
|
- String MERCHANT_ID = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_MERCHANT_ID).getParamValue();
|
|
|
-
|
|
|
- HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + closePayment.getPaymentNo() + "/close");
|
|
|
- httpPost.addHeader("Accept", "application/json");
|
|
|
- httpPost.addHeader("Content-type", "application/json; charset=utf-8");
|
|
|
-
|
|
|
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
-
|
|
|
- ObjectNode rootNode = objectMapper.createObjectNode();
|
|
|
- rootNode.put("mchid", MERCHANT_ID)
|
|
|
- .put("notify_url", paymentProperties.getNotifyUrl()
|
|
|
- + "/" + closePayment.getOpenType().getCode()
|
|
|
- + "/" + closePayment.getPayChannel().getCode()
|
|
|
- + "/closePayment");
|
|
|
- try {
|
|
|
- objectMapper.writeValue(bos, rootNode);
|
|
|
- httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return BaseResult.failed("微信APP支付参数转换失败");
|
|
|
- }
|
|
|
-
|
|
|
- CloseableHttpResponse response;
|
|
|
- try {
|
|
|
- response = httpClient.execute(httpPost);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return BaseResult.failed("微信APP支付请求失败");
|
|
|
- }
|
|
|
- return BaseResult.succeed(closePayment);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public BaseResult<RefundBill> refundPayment(RefundBill refundBill) {
|
|
|
- String APP_ID = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_APPID).getParamValue();
|
|
|
- String MERCHANT_ID = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_MERCHANT_ID).getParamValue();
|
|
|
-
|
|
|
- HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/refund/domestic/refunds");
|
|
|
- httpPost.addHeader("Accept", "application/json");
|
|
|
- httpPost.addHeader("Content-type", "application/json; charset=utf-8");
|
|
|
-
|
|
|
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
- ObjectMapper objectMapper = new ObjectMapper();
|
|
|
-
|
|
|
- ObjectNode rootNode = objectMapper.createObjectNode();
|
|
|
- rootNode.put("mchid", MERCHANT_ID)
|
|
|
- .put("appid", APP_ID)
|
|
|
- .put("transaction_id", refundBill.getTradeNo())
|
|
|
- .put("out_trade_no", refundBill.getPaymentNo())
|
|
|
- .put("out_refund_no", refundBill.getRefundNo())
|
|
|
- .put("reason", refundBill.getReason())
|
|
|
- .put("notify_url", paymentProperties.getNotifyUrl()
|
|
|
- + "/" + refundBill.getOpenType().getCode()
|
|
|
- + "/" + refundBill.getPayChannel().getCode()
|
|
|
- + "/refundPayment");
|
|
|
-
|
|
|
- rootNode.putObject("amount")
|
|
|
- .put("refund", refundBill.getRefundAmt())
|
|
|
- .put("total", refundBill.getOrderAmt())
|
|
|
- .put("currency", "CNY");
|
|
|
-
|
|
|
- try {
|
|
|
- objectMapper.writeValue(bos, rootNode);
|
|
|
-
|
|
|
- httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return BaseResult.failed("微信APP支付参数转换失败");
|
|
|
- }
|
|
|
-
|
|
|
- CloseableHttpResponse response;
|
|
|
- try {
|
|
|
- response = httpClient.execute(httpPost);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return BaseResult.failed("微信APP支付请求失败");
|
|
|
- }
|
|
|
- String bodyAsString;
|
|
|
- try {
|
|
|
- bodyAsString = EntityUtils.toString(response.getEntity());
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return BaseResult.failed("微信APP支付返回结果转换失败");
|
|
|
- }
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(bodyAsString);
|
|
|
- String refund_id = jsonObject.getString("refund_id");
|
|
|
- refundBill.setId(refund_id);
|
|
|
- return BaseResult.succeed(refundBill);
|
|
|
- }
|
|
|
- **/
|
|
|
|
|
|
@Override
|
|
|
public BaseResult<Payment> executePayment(Payment payment) {
|
|
@@ -317,7 +116,7 @@ public class OriginalWxAppTemplate implements PaymentTemplate {
|
|
|
|
|
|
WxPayOrderQueryV3Request param = new WxPayOrderQueryV3Request();
|
|
|
param.setMchid(MERCHANT_ID);
|
|
|
- if(!StringUtil.isEmpty(payment.getId())){
|
|
|
+ if (!StringUtil.isEmpty(payment.getId())) {
|
|
|
param.setTransactionId(payment.getId());
|
|
|
}
|
|
|
param.setOutTradeNo(payment.getPaymentNo());
|
|
@@ -354,7 +153,7 @@ public class OriginalWxAppTemplate implements PaymentTemplate {
|
|
|
param.setMchid(MERCHANT_ID);
|
|
|
param.setOutTradeNo(closePayment.getPaymentNo());
|
|
|
|
|
|
- try{
|
|
|
+ try {
|
|
|
wxPayService.closeOrderV3(param);
|
|
|
return BaseResult.succeed(closePayment);
|
|
|
} catch (WxPayException e) {
|
|
@@ -403,4 +202,43 @@ public class OriginalWxAppTemplate implements PaymentTemplate {
|
|
|
return BaseResult.failed("微信APP支付关单请求失败");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public BaseResult<PaymentCallBack> analysisNotice(OpenEnum openType, PayChannelEnum payChannel, MethodNameEnum methodName, HttpServletRequest request) {
|
|
|
+ //支付回调
|
|
|
+ SignatureHeader header = new SignatureHeader();
|
|
|
+ header.setTimeStamp(request.getHeader("Wechatpay-Timestamp"));
|
|
|
+ header.setNonce(request.getHeader("Wechatpay-Nonce"));
|
|
|
+ header.setSerial(request.getHeader("Wechatpay-Serial"));
|
|
|
+ header.setSignature(request.getHeader("Wechatpay-Signature"));
|
|
|
+
|
|
|
+ String params = WebUtil.getBodyData(request);
|
|
|
+ PaymentCallBack paymentCallBack = new PaymentCallBack();
|
|
|
+
|
|
|
+ Map<String,Object> resMsg = new HashMap<>();
|
|
|
+ try {
|
|
|
+ if (MethodNameEnum.executePayment.equals(methodName)) {
|
|
|
+ WxPayOrderNotifyV3Result wxPayOrderNotifyV3Result = wxPayService.parseOrderNotifyV3Result(params, header);
|
|
|
+ //封装支付回调对象
|
|
|
+ } else if (MethodNameEnum.refundPayment.equals(methodName)) {
|
|
|
+ WxPayRefundNotifyV3Result wxPayRefundNotifyV3Result = wxPayService.parseRefundNotifyV3Result(params, header);
|
|
|
+ //封装退款回调对象
|
|
|
+ }
|
|
|
+ resMsg.put("code","SUCCESS");
|
|
|
+ paymentCallBack.setResMsg(resMsg);
|
|
|
+ return BaseResult.succeed(paymentCallBack);
|
|
|
+ } catch (WxPayException e) {
|
|
|
+ resMsg.put("code","FAIL");
|
|
|
+ resMsg.put("message","失败");
|
|
|
+ paymentCallBack.setResMsg(resMsg);
|
|
|
+ log.error("微信回调对象转换失败, param is {} , err is {}", params, e.toString());
|
|
|
+ return BaseResult.failed(HttpStatus.INTERNAL_SERVER_ERROR, paymentCallBack, "微信回调对象转换失败");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ resMsg.put("code","FAIL");
|
|
|
+ resMsg.put("message","失败");
|
|
|
+ paymentCallBack.setResMsg(resMsg);
|
|
|
+ return BaseResult.failed(HttpStatus.INTERNAL_SERVER_ERROR, paymentCallBack, "解析异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|