|  | @@ -1,17 +1,32 @@
 | 
	
		
			
				|  |  |  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.yonge.toolset.base.result.BaseResult;
 | 
	
		
			
				|  |  |  import com.yonge.toolset.payment.base.PaymentTemplate;
 | 
	
		
			
				|  |  | +import com.yonge.toolset.payment.base.enums.OpenEnum;
 | 
	
		
			
				|  |  | +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.core.props.PaymentProperties;
 | 
	
		
			
				|  |  |  import com.yonge.toolset.payment.core.service.SysConfigPaymentService;
 | 
	
		
			
				|  |  | +import com.yonge.toolset.payment.original.wx.constant.WxpayConstant;
 | 
	
		
			
				|  |  | +import org.apache.http.client.methods.CloseableHttpResponse;
 | 
	
		
			
				|  |  | +import org.apache.http.client.methods.HttpGet;
 | 
	
		
			
				|  |  | +import org.apache.http.client.methods.HttpPost;
 | 
	
		
			
				|  |  | +import org.apache.http.client.utils.URIBuilder;
 | 
	
		
			
				|  |  | +import org.apache.http.entity.StringEntity;
 | 
	
		
			
				|  |  | +import org.apache.http.impl.client.CloseableHttpClient;
 | 
	
		
			
				|  |  | +import org.apache.http.util.EntityUtils;
 | 
	
		
			
				|  |  |  import org.slf4j.Logger;
 | 
	
		
			
				|  |  |  import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Component;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import java.io.ByteArrayOutputStream;
 | 
	
		
			
				|  |  |  import java.util.Map;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  @Component
 | 
	
	
		
			
				|  | @@ -20,30 +35,214 @@ public class OriginalWxAppTemplate implements PaymentTemplate {
 | 
	
		
			
				|  |  |      private final static Logger log = LoggerFactory.getLogger(OriginalWxAppTemplate.class);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  | +    private PaymentProperties paymentProperties;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  |      private SysConfigPaymentService configPaymentService;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private CloseableHttpClient httpClient;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public BaseResult<Map<String, Object>> getOpenAuthMsg(OpenAuth openAuth) {
 | 
	
		
			
				|  |  |          return null;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  | -    public BaseResult<Payment> executePayment(Payment payment){
 | 
	
		
			
				|  |  | -        return null;
 | 
	
		
			
				|  |  | +    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.getDescription())
 | 
	
		
			
				|  |  | +                .put("out_trade_no", payment.getPaymentNo())
 | 
	
		
			
				|  |  | +                .put("notify_url", paymentProperties.getNotifyUrl()
 | 
	
		
			
				|  |  | +                        + "/" + payment.getOpenType().getCode()
 | 
	
		
			
				|  |  | +                        + "/" + payment.getPayChannel().getCode()
 | 
	
		
			
				|  |  | +                        + "/executePayment");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        rootNode.putObject("amount")
 | 
	
		
			
				|  |  | +                .put("total", payment.getPayAmt());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        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) {
 | 
	
		
			
				|  |  | -        return null;
 | 
	
		
			
				|  |  | +        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) {
 | 
	
		
			
				|  |  | -        return null;
 | 
	
		
			
				|  |  | +        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)  {
 | 
	
		
			
				|  |  | -        return null;
 | 
	
		
			
				|  |  | +    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);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  }
 |