|  | @@ -0,0 +1,133 @@
 | 
	
		
			
				|  |  | +package com.yonge.toolset.payment.adapay;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | +import com.alipay.api.AlipayClient;
 | 
	
		
			
				|  |  | +import com.huifu.adapay.core.exception.BaseAdaPayException;
 | 
	
		
			
				|  |  | +import com.huifu.adapay.core.util.StringUtil;
 | 
	
		
			
				|  |  | +import com.huifu.adapay.model.Refund;
 | 
	
		
			
				|  |  | +import com.yonge.toolset.base.result.BaseResult;
 | 
	
		
			
				|  |  | +import com.yonge.toolset.payment.adapay.config.HuifuConfiguration;
 | 
	
		
			
				|  |  | +import com.yonge.toolset.payment.adapay.model.DeviceInfo;
 | 
	
		
			
				|  |  | +import com.yonge.toolset.payment.core.PaymentTemplate;
 | 
	
		
			
				|  |  | +import com.yonge.toolset.payment.core.enums.TradeStatusEnum;
 | 
	
		
			
				|  |  | +import com.yonge.toolset.payment.core.model.ClosePayment;
 | 
	
		
			
				|  |  | +import com.yonge.toolset.payment.core.model.Payment;
 | 
	
		
			
				|  |  | +import com.yonge.toolset.payment.core.model.RefundBill;
 | 
	
		
			
				|  |  | +import com.yonge.toolset.payment.core.model.res.ClosePaymentRes;
 | 
	
		
			
				|  |  | +import com.yonge.toolset.payment.core.model.res.PaymentRes;
 | 
	
		
			
				|  |  | +import com.yonge.toolset.payment.core.model.res.RefundBillRes;
 | 
	
		
			
				|  |  | +import org.slf4j.Logger;
 | 
	
		
			
				|  |  | +import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | +import org.springframework.beans.BeanUtils;
 | 
	
		
			
				|  |  | +import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | +import org.springframework.stereotype.Component;
 | 
	
		
			
				|  |  | +import org.springframework.util.CollectionUtils;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import java.util.HashMap;
 | 
	
		
			
				|  |  | +import java.util.Map;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +@Component
 | 
	
		
			
				|  |  | +public class AdapayTemplate implements PaymentTemplate {
 | 
	
		
			
				|  |  | +    private final static Logger log = LoggerFactory.getLogger(AdapayTemplate.class);
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private AlipayClient alipayClient;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public BaseResult<PaymentRes> executePayment(Payment payment) throws Exception {
 | 
	
		
			
				|  |  | +        Map<String, Object> paymentMap = new HashMap<>();
 | 
	
		
			
				|  |  | +        paymentMap.put("order_no", payment.getOrderNo());
 | 
	
		
			
				|  |  | +        paymentMap.put("app_id", HuifuConfiguration.getHuifuProperties().getAppId());
 | 
	
		
			
				|  |  | +        paymentMap.put("pay_channel", payment.getPayChannel().getCode());
 | 
	
		
			
				|  |  | +        paymentMap.put("pay_amt", payment.getPayAmt());
 | 
	
		
			
				|  |  | +        paymentMap.put("goods_title", payment.getOrderTitle());
 | 
	
		
			
				|  |  | +        paymentMap.put("goods_desc", payment.getOrderDesc());
 | 
	
		
			
				|  |  | +        paymentMap.put("div_members", JSONObject.toJSONString(payment.getDivMembers()));
 | 
	
		
			
				|  |  | +        paymentMap.put("description", payment.getDescription());
 | 
	
		
			
				|  |  | +        paymentMap.put("device_info", payment.getDeviceInfo());
 | 
	
		
			
				|  |  | +        paymentMap.put("expend", payment.getExpend());
 | 
	
		
			
				|  |  | +        paymentMap.put("notify_url", StringUtil.isEmpty(payment.getNotifyUrl()) ? HuifuConfiguration.getHuifuProperties().getNotifyUrl() : payment.getNotifyUrl());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        log.info("汇付[创建支付对象] Req:{}", JSONObject.toJSONString(paymentMap));
 | 
	
		
			
				|  |  | +        //调用sdk方法,创建支付,得到支付对象
 | 
	
		
			
				|  |  | +        Map<String, Object> res;
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            res = com.huifu.adapay.model.Payment.create(paymentMap);
 | 
	
		
			
				|  |  | +        } catch (BaseAdaPayException e) {
 | 
	
		
			
				|  |  | +            return BaseResult.failed(e.getMessage());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (CollectionUtils.isEmpty(res)) {
 | 
	
		
			
				|  |  | +            return BaseResult.failed("请求失败");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        log.info("汇付[创建支付对象] Resp:{}", res);
 | 
	
		
			
				|  |  | +        String errorCode = (String) res.get("error_code");
 | 
	
		
			
				|  |  | +        if (null != errorCode) {
 | 
	
		
			
				|  |  | +            String errorMsg = (String) res.get("error_msg");
 | 
	
		
			
				|  |  | +            return BaseResult.failed(errorMsg);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        PaymentRes paymentRes = new PaymentRes();
 | 
	
		
			
				|  |  | +        BeanUtils.copyProperties(payment, paymentRes);
 | 
	
		
			
				|  |  | +        paymentRes.setId(res.get("id").toString());
 | 
	
		
			
				|  |  | +        paymentRes.setStatus(TradeStatusEnum.getByName(res.get("status").toString()));
 | 
	
		
			
				|  |  | +        return BaseResult.succeed(paymentRes);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public BaseResult<ClosePaymentRes> closePayment(ClosePayment closePayment) throws Exception {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Map<String, Object> paymentParams = new HashMap<>(10);
 | 
	
		
			
				|  |  | +        paymentParams.put("payment_id", closePayment.getPaymentId());
 | 
	
		
			
				|  |  | +        paymentParams.put("reason", closePayment.getReason());
 | 
	
		
			
				|  |  | +        paymentParams.put("expend", closePayment.getExpend());
 | 
	
		
			
				|  |  | +        paymentParams.put("notify_url", StringUtil.isEmpty(closePayment.getNotifyUrl()) ? HuifuConfiguration.getHuifuProperties().getNotifyUrl() : closePayment.getNotifyUrl());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Map<String, Object> res;
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            res = com.huifu.adapay.model.Payment.close(paymentParams);
 | 
	
		
			
				|  |  | +        } catch (BaseAdaPayException e) {
 | 
	
		
			
				|  |  | +            return BaseResult.failed(e.getMessage());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (CollectionUtils.isEmpty(res)) {
 | 
	
		
			
				|  |  | +            return BaseResult.failed("请求失败");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        log.info("汇付[支付关单] Resp:{}", res);
 | 
	
		
			
				|  |  | +        String errorCode = (String) res.get("error_code");
 | 
	
		
			
				|  |  | +        if (null != errorCode) {
 | 
	
		
			
				|  |  | +            String errorMsg = (String) res.get("error_msg");
 | 
	
		
			
				|  |  | +            return BaseResult.failed(errorMsg);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        ClosePaymentRes paymentRes = new ClosePaymentRes();
 | 
	
		
			
				|  |  | +        BeanUtils.copyProperties(closePayment, paymentRes);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        paymentRes.setStatus(TradeStatusEnum.getByName(res.get("status").toString()));
 | 
	
		
			
				|  |  | +        return BaseResult.succeed(paymentRes);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public BaseResult<RefundBillRes> refundPayment(RefundBill refundBill) throws Exception {
 | 
	
		
			
				|  |  | +        Map<String, Object> refundParams = new HashMap<>(10);
 | 
	
		
			
				|  |  | +        refundParams.put("refund_amt", refundBill.getRefundAmt());
 | 
	
		
			
				|  |  | +        refundParams.put("refund_order_no", refundBill.getRefundNo());
 | 
	
		
			
				|  |  | +        refundParams.put("notify_url", StringUtil.isEmpty(refundBill.getNotifyUrl()) ? HuifuConfiguration.getHuifuProperties().getNotifyUrl() : refundBill.getNotifyUrl());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        Map<String, Object> res;
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            res = Refund.create(refundBill.getPaymentId(), refundParams);
 | 
	
		
			
				|  |  | +        } catch (BaseAdaPayException e) {
 | 
	
		
			
				|  |  | +            return BaseResult.failed(e.getMessage());
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (CollectionUtils.isEmpty(res)) {
 | 
	
		
			
				|  |  | +            return BaseResult.failed("请求失败");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        log.info("汇付[支付退款] Resp:{}", res);
 | 
	
		
			
				|  |  | +        String errorCode = (String) res.get("error_code");
 | 
	
		
			
				|  |  | +        if (null != errorCode) {
 | 
	
		
			
				|  |  | +            String errorMsg = (String) res.get("error_msg");
 | 
	
		
			
				|  |  | +            return BaseResult.failed(errorMsg);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        RefundBillRes billRes = new RefundBillRes();
 | 
	
		
			
				|  |  | +        BeanUtils.copyProperties(refundBill, billRes);
 | 
	
		
			
				|  |  | +        billRes.setStatus(TradeStatusEnum.getByName(res.get("status").toString()));
 | 
	
		
			
				|  |  | +        return BaseResult.succeed(billRes);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |