package com.slodon.b2b2c.starter.pay;

import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.starter.client.*;
import com.slodon.b2b2c.starter.entity.*;
import lombok.extern.slf4j.Slf4j;

import static com.slodon.b2b2c.core.constant.PayConst.*;

/**
 * 三方支付统一入口类
 *
 * @author lxk
 */
@Slf4j
public class SlodonPay {

    /**
     * 统一支付入口
     *
     * @param slodonPayRequest 统一支付封装参数
     * @return
     */
    public SlodonPayResponse unitePay(SlodonPayRequest slodonPayRequest) {
        SlodonPayClient client;//支付客户端
        if (METHOD_WX.equalsIgnoreCase(slodonPayRequest.getPayMethod())) {
            //微信支付
            client = new SlodonWxPayClient(slodonPayRequest);
        } else if (METHOD_ALIPAY.equalsIgnoreCase(slodonPayRequest.getPayMethod())) {
            //支付宝支付
            client = new SlodonAliPayClient(slodonPayRequest);
        } else if (METHOD_PAYPAL.equalsIgnoreCase(slodonPayRequest.getPayMethod())) {
            //PayPal支付
            client = new SlodonPayPalClient(slodonPayRequest);
        } else if (METHOD_ZJ_WX.equalsIgnoreCase(slodonPayRequest.getPayMethod())
                || METHOD_ZJ.equalsIgnoreCase(slodonPayRequest.getPayMethod())
                || METHOD_ZJ_ALI.equalsIgnoreCase(slodonPayRequest.getPayMethod())
                || METHOD_ZJ_BANK.equalsIgnoreCase(slodonPayRequest.getPayMethod())){
            //中金支付
            client = new SlodonZjPayClient(slodonPayRequest);
        } else {
            throw new MallException("支付方式未开启");
        }
        //执行支付
        SlodonPayResponse response;
        try {
            response = client.doPay();
        } catch (Exception e) {
            log.error("第三方支付异常:", e);
            throw new MallException("支付功能维护中");
        }
        return response;
    }

    /**
     * 统一退款入口
     *
     * @param refundRequest 退款参数
     * @param paymentCode   支付编码 {@link com.slodon.b2b2c.core.constant.OrderPaymentConst#PAYMENT_CODE_PCWXPAY}
     * @return
     * @throws Exception
     */
    public SlodonRefundResponse refund(SlodonRefundRequest refundRequest, String paymentCode) {
        SlodonPayClient client;//支付客户端
        if (paymentCode.toLowerCase().contains(METHOD_ALIPAY.toLowerCase())) {
            //支付宝退款
            client = new SlodonAliPayClient(refundRequest);
        } else if (paymentCode.toLowerCase().contains(METHOD_WX.toLowerCase())) {
            //微信退款
            client = new SlodonWxPayClient(refundRequest);
        } else if (paymentCode.toLowerCase().contains(METHOD_PAYPAL.toLowerCase())) {
            //PayPal退款
            client = new SlodonPayPalClient(refundRequest);
        } else {
            throw new MallException("支付方式未开启");
        }
        //执行退款
        SlodonRefundResponse response;
        try {
            response = client.doRefund();
        } catch (Exception e) {
            log.error("第三方退款异常:", e);
            throw new MallException("退款功能维护中");
        }
        return response;
    }

    /**
     * 统一转账入口
     *
     * @param slodonTransfersRequest 统一转账封装参数
     * @return
     */
    public SlodonTransfersResponse transfers(SlodonTransfersRequest slodonTransfersRequest) throws Exception {
        SlodonPayClient client;//支付客户端
        if (METHOD_WX.equalsIgnoreCase(slodonTransfersRequest.getTransfersMethod())) {
            //微信转账
            client = new SlodonWxPayClient(slodonTransfersRequest);
        } else if (METHOD_ALIPAY.equalsIgnoreCase(slodonTransfersRequest.getTransfersMethod())) {
            //支付宝转账
            client = new SlodonAliPayClient(slodonTransfersRequest);
        } else {
            throw new MallException("转账方式未开启");
        }
        //执行转账
        SlodonTransfersResponse response;
        try {
            response = client.doTransfers();
        } catch (Exception e) {
            log.error("第三方转账异常:", e);
            throw new MallException("转账功能维护中");
        }
        return response;
    }
}
