package com.slodon.b2b2c.starter.pay;

import com.slodon.b2b2c.core.config.DomainUrlUtil;
import com.slodon.b2b2c.core.constant.PayConst;
import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.starter.client.SlodonAliPayClient;
import com.slodon.b2b2c.starter.client.SlodonPayClient;
import com.slodon.b2b2c.starter.client.SlodonWxPayClient;
import com.slodon.b2b2c.starter.entity.*;

import java.math.BigDecimal;

import static com.slodon.b2b2c.core.constant.PayConst.METHOD_ALIPAY;
import static com.slodon.b2b2c.core.constant.PayConst.METHOD_WX;

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

    /**
     * 统一支付入口
     *
     * @param slodonPayRequest 统一支付封装参数
     * @return
     */
    public SlodonPayResponse unitePay(SlodonPayRequest slodonPayRequest) throws Exception {
        SlodonPayClient client;//支付客户端
        if (METHOD_WX.equalsIgnoreCase(slodonPayRequest.getPayMethod())) {
            //微信支付
            client = new SlodonWxPayClient(slodonPayRequest);
        } else if (METHOD_ALIPAY.equalsIgnoreCase(slodonPayRequest.getPayMethod())) {
            //支付宝支付
            client = new SlodonAliPayClient(slodonPayRequest);
        } else {
            throw new MallException("支付方式未开启");
        }
        //执行支付
        return client.doPay();
    }

    /**
     * 统一退款入口
     *
     * @param refundRequest 退款参数
     * @param paymentCode 支付编码 {@link com.slodon.b2b2c.core.constant.OrderPaymentConst#PAYMENT_CODE_PCWXPAY}
     * @return
     * @throws Exception
     */
    public SlodonRefundResponse refund(SlodonRefundRequest refundRequest,String paymentCode) throws Exception {
        SlodonPayClient client;//支付客户端
        if (paymentCode.toLowerCase().contains(METHOD_ALIPAY.toLowerCase())){
            //支付宝退款
            client = new SlodonAliPayClient(refundRequest);
        }else if (paymentCode.toLowerCase().contains(METHOD_WX.toLowerCase())){
            //微信支付时，支付单号格式为 paySn + '_' + 支付方式
            refundRequest.setPaySn(refundRequest.getPaySn() + "_" + paymentCode.replace(PayConst.METHOD_WX.toUpperCase(), ""));
            client = new SlodonWxPayClient(refundRequest);
        }else {
            throw new MallException("支付方式未开启");
        }
        return client.doRefund();
    }


    /**
     * 支付测试
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        WxPayProperties wxPayProperties = new WxPayProperties("wx43aba958092afe0c","1489871402",
                "acAcsnklknskfnslalcn134643sljlda","classpath:wx_cert/apiclient_cert.p12");
        SlodonPay slodonPay = new SlodonPay();
        SlodonPayRequest payRequest = new SlodonPayRequest();
        payRequest.setPayMethod(PayConst.METHOD_WX);
        payRequest.setPayType(PayConst.WX_TYPE_JSAPI);
        payRequest.setWxPayProperties(wxPayProperties);
        payRequest.setPaySn("400003100004_JSAPI");
        payRequest.setPayAmount(new BigDecimal(100));
        payRequest.setNotifyUrl(DomainUrlUtil.SLD_API_URL + "/v3/business/front/orderPayCallback/wx");
        payRequest.setSubject("测试数据");
        payRequest.setBody("QQ公仔");
        /*ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();*/
        payRequest.setIp("110.178.52.91");
        payRequest.setOpenId("oErsf0ZrpmLkLscDslQICS1QIEfs");
        SlodonPayResponse slodonPayResponse = slodonPay.unitePay(payRequest);
        System.out.println(slodonPayResponse);
    }
    private void main_test_01(){
        /*WxPayProperties wxPayProperties = new WxPayProperties("wx9019a5f5fb0a0844","1484872062",
                "15556f6f736f74bb8fe6ca10ae491c4c","classpath:wx_cert/apiclient_cert.p12");
        AliPayProperties aliPayProperties = new AliPayProperties("2088721779427806",
                "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC2gb6hgNzXJBKeIPlSoPObGrjYTI+WxVsn4e0yFwDYrPYWi2cbNyexG0nRbMIHPjANI/BbqqAkZoep+DZKIiQgvdTlnnkvOGUhbbAHgO82PW9pnq9hIxPE1o+NZZt8sAa0ybiAVxINEmqKVkLMu4ET0hSJ3qweQkFs9PKhYEP/volB9IPUxzJ1a/zSylgS8Ek7oKgcU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ZBP6QvBkyaMDqt2ZlpjhJDtI9RW/bxkC1zcsTmJwS2hG/PvuYDYGXRBCVD6WuMhBlJGvehKZJH/0tYQluVsEjsvW+Z4El6KtcElMJpPISMYo3o09pM6aupAoGBAJ6LDMouPOn+Lbucft35nEfqE1pDBztR/PhSjUA8S7RS/M7XB7iRQITo9haPmdMKwGG/JlB1zOIBLb3RMM2+lk+tBlrCJ0PUs36EVS0CoGP3BAsHTIJ01T3EIfCr9vfMGT5bic0AkSHmpVck1CUQCJ1wLUqd/8WeZhydntBCX2wP",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsSah6GBhd7JM7zaot3OlaGm2W38xTsywQpyh3XjijOnjlbxyoSvXRMix4/VEKqhBlRRRh7L+f1Csqn2oZB6H19juie0ER93l/zjpEs+bMcfB3QE5qeV62RTCswo7LEGY8XQZx1DysN1U/O5nvgG5yzIuVGk+08LTYaISqV1s5pD3/kFk/rrbIRmUVpr3leb/sRMpyGIxM+tslV/oRvsjW59RR4nkkPVWHVXddQ15VgRSmBPDPPF2ALDFQt7KTDaE9nKJ6IlnVhDenKANT1FTj7KRBJLRFZyEN8YkuQssu4C4naUQrdCFFUEIkyTeHeUbtWhlY9Depp9BX1LzxYzykQIDAQAB",
                "2017112900241969");
        SlodonRefundRequest refundRequest = new SlodonRefundRequest();
        refundRequest.setAlipayProperties(aliPayProperties);
        refundRequest.setWxPayProperties(wxPayProperties);
        refundRequest.setPaySn("400000470002");
        refundRequest.setRefundAmount(new BigDecimal("0.01"));
        refundRequest.setRefundSn("22222");
        refundRequest.setTotalAmount(new BigDecimal("0.01"));

        SlodonPay slodonPay = new SlodonPay();
        SlodonRefundResponse slodonPayResponse = slodonPay.refund(refundRequest, "MWEBALIPAY");
        System.out.println(slodonPayResponse);*/
    }

}
