|
@@ -3,6 +3,12 @@ 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.request.*;
|
|
|
+import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryV3Result;
|
|
|
+import com.github.binarywang.wxpay.bean.result.WxPayRefundV3Result;
|
|
|
+import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
|
|
|
+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.OpenEnum;
|
|
@@ -14,7 +20,10 @@ import com.yonge.toolset.payment.base.model.RefundBill;
|
|
|
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 org.apache.commons.collections.map.HashedMap;
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
@@ -42,14 +51,18 @@ public class OriginalWxAppTemplate implements PaymentTemplate {
|
|
|
@Autowired
|
|
|
private SysConfigPaymentService configPaymentService;
|
|
|
|
|
|
+ /*@Autowired
|
|
|
+ private CloseableHttpClient httpClient;*/
|
|
|
@Autowired
|
|
|
- private CloseableHttpClient httpClient;
|
|
|
+ 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();
|
|
@@ -252,4 +265,142 @@ public class OriginalWxAppTemplate implements PaymentTemplate {
|
|
|
refundBill.setId(refund_id);
|
|
|
return BaseResult.succeed(refundBill);
|
|
|
}
|
|
|
+ **/
|
|
|
+
|
|
|
+ @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();
|
|
|
+
|
|
|
+ WxPayUnifiedOrderV3Request request = new WxPayUnifiedOrderV3Request();
|
|
|
+ request.setAppid(APP_ID);
|
|
|
+ request.setMchid(MERCHANT_ID);
|
|
|
+ request.setDescription(payment.getOrderDesc());
|
|
|
+ request.setOutTradeNo(payment.getPaymentNo());
|
|
|
+ request.setNotifyUrl(paymentProperties.getNotifyUrl()
|
|
|
+ + "/" + payment.getOpenType().getCode()
|
|
|
+ + "/" + payment.getPayChannel().getCode()
|
|
|
+ + "/executePayment");
|
|
|
+
|
|
|
+ if (StringUtil.isEmpty(payment.getPayAmt())) {
|
|
|
+ return BaseResult.failed("微信APP支付金额异常");
|
|
|
+ }
|
|
|
+
|
|
|
+ BigDecimal payAmt = new BigDecimal(payment.getPayAmt()).multiply(new BigDecimal("100"));
|
|
|
+
|
|
|
+ WxPayUnifiedOrderV3Request.Amount amount = new WxPayUnifiedOrderV3Request.Amount();
|
|
|
+ amount.setTotal(payAmt.abs().intValue());
|
|
|
+ request.setAmount(amount);
|
|
|
+
|
|
|
+ try {
|
|
|
+ Object orderV3 = wxPayService.createOrderV3(TradeTypeEnum.APP, request);
|
|
|
+ String prepayid = ObjectUtil.getFieldValueByFieldName("prepayid", orderV3);
|
|
|
+ payment.setPayInfo(StringUtil.get(prepayid));
|
|
|
+
|
|
|
+ BaseResult<Payment> paymentBaseResult = queryPayment(payment);
|
|
|
+ if (paymentBaseResult.getStatus()) {
|
|
|
+ payment.setId(paymentBaseResult.getData().getId());
|
|
|
+ }
|
|
|
+ return BaseResult.succeed(payment);
|
|
|
+ } catch (WxPayException e) {
|
|
|
+ log.error("微信APP支付请求失败:{}", e.toString());
|
|
|
+ return BaseResult.failed(e.getReturnMsg());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return BaseResult.failed("微信APP支付请求失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public BaseResult<Payment> queryPayment(Payment payment) {
|
|
|
+ String MERCHANT_ID = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_MERCHANT_ID).getParamValue();
|
|
|
+
|
|
|
+ WxPayOrderQueryV3Request param = new WxPayOrderQueryV3Request();
|
|
|
+ param.setMchid(MERCHANT_ID);
|
|
|
+ if(!StringUtil.isEmpty(payment.getId())){
|
|
|
+ param.setTransactionId(payment.getId());
|
|
|
+ }
|
|
|
+ param.setOutTradeNo(payment.getPaymentNo());
|
|
|
+
|
|
|
+ try {
|
|
|
+ WxPayOrderQueryV3Result res = wxPayService.queryOrderV3(param);
|
|
|
+
|
|
|
+ payment.setId(res.getTransactionId());
|
|
|
+ String tradeState = res.getTradeState();
|
|
|
+ if ("SUCCESS".equals(tradeState)) {
|
|
|
+ payment.setStatus(TradeStatusEnum.succeeded);
|
|
|
+ } else if ("REFUND".equals(tradeState)) {
|
|
|
+ payment.setStatus(TradeStatusEnum.close);
|
|
|
+ } else if ("NOTPAY".equals(tradeState)) {
|
|
|
+ payment.setStatus(TradeStatusEnum.pending);
|
|
|
+ } else if ("CLOSED".equals(tradeState)) {
|
|
|
+ payment.setStatus(TradeStatusEnum.close);
|
|
|
+ }
|
|
|
+ return BaseResult.succeed(payment);
|
|
|
+ } catch (WxPayException e) {
|
|
|
+ log.error("微信APP支付查询请求失败:{}", e.toString());
|
|
|
+ return BaseResult.failed(e.getReturnMsg());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return BaseResult.failed("微信APP支付查询请求失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public BaseResult<ClosePayment> closePayment(ClosePayment closePayment) {
|
|
|
+ String MERCHANT_ID = configPaymentService.getPaymentConfig(OpenEnum.ORIGINAL, WxpayConstant.WX_MERCHANT_ID).getParamValue();
|
|
|
+
|
|
|
+ WxPayOrderCloseV3Request param = new WxPayOrderCloseV3Request();
|
|
|
+ param.setMchid(MERCHANT_ID);
|
|
|
+ param.setOutTradeNo(closePayment.getPaymentNo());
|
|
|
+
|
|
|
+ try{
|
|
|
+ wxPayService.closeOrderV3(param);
|
|
|
+ return BaseResult.succeed(closePayment);
|
|
|
+ } catch (WxPayException e) {
|
|
|
+ log.error("微信APP支付关单请求失败:{}", e.toString());
|
|
|
+ return BaseResult.failed(e.getReturnMsg());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return BaseResult.failed("微信APP支付关单请求失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public BaseResult<RefundBill> refundPayment(RefundBill refundBill) {
|
|
|
+ WxPayRefundV3Request param = new WxPayRefundV3Request();
|
|
|
+ param.setTransactionId(refundBill.getTradeNo());
|
|
|
+ param.setOutTradeNo(refundBill.getPaymentNo());
|
|
|
+ param.setOutRefundNo(refundBill.getRefundNo());
|
|
|
+ param.setReason(refundBill.getReason());
|
|
|
+
|
|
|
+ param.setNotifyUrl(paymentProperties.getNotifyUrl()
|
|
|
+ + "/" + refundBill.getOpenType().getCode()
|
|
|
+ + "/" + refundBill.getPayChannel().getCode()
|
|
|
+ + "/refundPayment");
|
|
|
+
|
|
|
+ WxPayRefundV3Request.Amount amount = new WxPayRefundV3Request.Amount();
|
|
|
+ BigDecimal refundAmt = refundBill.getRefundAmt().multiply(new BigDecimal("100"));
|
|
|
+ amount.setRefund(refundAmt.intValue());
|
|
|
+
|
|
|
+ BigDecimal orderAmt = refundBill.getOrderAmt().multiply(new BigDecimal("100"));
|
|
|
+ amount.setTotal(orderAmt.intValue());
|
|
|
+
|
|
|
+ amount.setCurrency("CNY");
|
|
|
+ param.setAmount(amount);
|
|
|
+
|
|
|
+ try {
|
|
|
+ WxPayRefundV3Result res = wxPayService.refundV3(param);
|
|
|
+
|
|
|
+ String refund_id = res.getRefundId();
|
|
|
+ refundBill.setId(refund_id);
|
|
|
+ return BaseResult.succeed(refundBill);
|
|
|
+ } catch (WxPayException e) {
|
|
|
+ log.error("微信APP支付请求失败:{}", e.toString());
|
|
|
+ return BaseResult.failed(e.getReturnMsg());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return BaseResult.failed("微信APP支付关单请求失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|