package com.izouma.nineth;

import com.alibaba.fastjson15.JSON;
import com.alibaba.fastjson15.JSONObject;
import com.alibaba.fastjson15.parser.Feature;
import com.izouma.nineth.utils.SnowflakeIdWorker;
import com.upay.sdk.CipherWrapper;
import com.upay.sdk.ConfigurationUtils;
import com.upay.sdk.HttpClientUtils;
import com.upay.sdk.cashier.bindcard.builder.*;
import com.upay.sdk.cashier.order.builder.ReceiptPaymentBuilder;
import com.upay.sdk.entity.Payer;
import com.upay.sdk.entity.ProductDetail;
import com.upay.sdk.onlinepay.builder.OrderBuilder;
import com.upay.sdk.onlinepay.builder.QueryBuilder;
import com.upay.sdk.onlinepay.builder.RefundBuilder;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class PayEaseTest {
    String merchantId = "896593123";

    public JSONObject request(String url, JSONObject requestData) {
        log.info("requestData: {}", JSON.toJSONString(requestData, true));
        String responseStr = HttpClientUtils.post3(url, requestData);
        log.info("responseStr: \n{}", responseStr);
        JSONObject responseData = JSONObject.parseObject(responseStr, Feature.SortFeidFastMatch);
        responseData = CipherWrapper.bothDecryptWrap(responseData);
        log.info("responseStr: {}", JSON.toJSONString(responseData, true));
        return responseData;
    }

    @Test
    public void bindCard() {
        //商户会员id
        String merchantUserId = "1";
        //	银⾏卡号
        String bankCardNumber = "6222024301070380165";
        //预留⼿机号
        String phoneNumber = "15077886171";
        //	持卡⼈姓名
        String name = "熊竹";
        //	身份证号
        String idCardNum = "321002199408304614";


        BindCardBuilder builder = new BindCardBuilder(merchantId);
        builder.setMerchantUserId(merchantUserId)
                .setBankCardNumber(bankCardNumber)
                .setPhoneNumber(phoneNumber)
                .setName(name)
                .setIdCardNum(idCardNum);
        JSONObject requestData = builder.bothEncryptBuild();
        log.info("bindCard requestData: {}", JSON.toJSONString(requestData, true));
        String responseStr = HttpClientUtils.post3(ConfigurationUtils.getCashierBindCardUrl(), requestData);
        log.info("bindCard responseStr: \n{}", responseStr);
        JSONObject responseData = JSONObject.parseObject(responseStr, Feature.SortFeidFastMatch);
        responseData = CipherWrapper.bothDecryptWrap(responseData);
        log.info("bindCard responseStr: {}", JSON.toJSONString(responseData, true));
    }

    @Test
    public void bindCardConfirm() {
        String bindCardId = "20220602315804717062699846352896";
        String kaptchaCode = "321838";
        String merchantUserId = "1";

        BindCardConfirmBuilder builder = new BindCardConfirmBuilder(merchantId);
        builder.setBindCardId(bindCardId)
                .setMerchantUserId(merchantUserId)
                .setKaptchaCode(kaptchaCode);

        JSONObject requestData = builder.bothEncryptBuild();
        log.info("bindCard requestData: {}", JSON.toJSONString(requestData, true));
        String responseStr = HttpClientUtils.post3(ConfigurationUtils.getCashierBindCardConfirmUrl(), requestData);
        log.info("bindCard responseStr: \n{}", responseStr);
        JSONObject responseData = JSONObject.parseObject(responseStr, Feature.SortFeidFastMatch);
        responseData = CipherWrapper.bothDecryptWrap(responseData);
        log.info("bindCard responseStr: {}", JSON.toJSONString(responseData, true));
    }

    @Test
    public void bindCardCaptcha() {
        String bindCardId = "20220519435026712047423086018560";

        BindCardKaptchaBuilder builder = new BindCardKaptchaBuilder(merchantId);
        builder.setBindCardId(bindCardId);
        JSONObject response = request(ConfigurationUtils.getCashierBindCardKaptchaUrl(), builder.bothEncryptBuild());
    }

    @Test
    public void unbind() {
        String bindCardId = "20220524516958713695754308698112";
        String merchantUserId = "1";

        UnBindCardBuilder builder = new UnBindCardBuilder(merchantId);
        builder.setBindCardId(bindCardId)
                .setMerchantUserId(merchantUserId);
        JSONObject response = request(ConfigurationUtils.getCashierUnBindCardUrl(), builder.bothEncryptBuild());
    }

    @Test
    public void queryBind() {
        String bindCardId = "20220520544899712342676254900224";
        BindCardQueryBuilder builder = new BindCardQueryBuilder(merchantId);
        builder.setBindCardId(bindCardId)
                .setMerchantUserId("1787318");
        JSONObject response = request(ConfigurationUtils.getCashierBindCardQueryUrl(), builder.bothEncryptBuild());
    }

    @Test
    public void pay() {
        String subject = "充值";
        //订单号
        String requestId = new SnowflakeIdWorker(0, 0).nextId() + "";
        //订单金额
        String orderAmount = "1";
        //通知地址
        String notifyUrl = "http://xiongzhu.frp.izouma.com/payease/notify/test/" + requestId;
        //回调地址
        String callbackUrl = "https://www.raex.vip/9th/home";
        //备注
        String remark = "111";

        //商户会员Id
        String merchantUserId = "1";
        //绑卡Id
        String bindCardId = "20220524516958713695754308698112";
        //超时时间
        String timeout = "3";

        OrderBuilder builder = new OrderBuilder(merchantId);
        builder.setRequestId(requestId)
                .setOrderAmount(orderAmount)
                .setOrderCurrency("CNY")
                .setNotifyUrl(notifyUrl)
                .setCallbackUrl(callbackUrl)
                .setRemark(remark)
                .setPaymentModeCode("BANK_CARD-AGREEMENT-P2P")
                .setNotifyUrl(notifyUrl)
                .setMerchantUserId(merchantUserId)
                .setBindCardId(bindCardId)
                .setClientIp("192.168.22.55");
        builder.setTimeout(timeout);


        ProductDetail productDetail = new ProductDetail();
        productDetail.setName(subject);
        productDetail.setQuantity(1L);
        productDetail.setAmount(Long.parseLong(orderAmount));
        builder.addProductDetail(productDetail);


        Payer payer = new Payer();

        builder.setPayer(payer);
        JSONObject response = request(ConfigurationUtils.getOnlinePayOrderUrl(), builder.bothEncryptBuild());
    }

    @Test
    public void payConfirm() {
        String requestId = "978683558270861312";
        String paymentOrderId = "f03fc9a74ac3450292cdfc18f6723b9c";
        String kaptchaCode = "478513";
        ReceiptPaymentBuilder builder = new ReceiptPaymentBuilder(merchantId);
        builder.setRequestId(requestId)
                .setPaymentOrderId(paymentOrderId)
                .setKaptchaCode(kaptchaCode);

        JSONObject response = request(ConfigurationUtils.getCashierReceiptPaymentUrl(), builder.bothEncryptBuild());
    }

    @Test
    public void refund() {
        String orderId = "7f159751086347cbb728e7fd0de8ad1c";
        String amount = "1";
        String notifyUrl = "https://www.raex.vip/payease/notify/order";
        RefundBuilder builder = new RefundBuilder(merchantId);
        builder.setRequestId("978601560772706304")
                .setAmount(amount)
                .setOrderId(orderId)
                .setNotifyUrl(notifyUrl);
        JSONObject response = request(ConfigurationUtils.getOnlinePayRefundUrl(), builder.bothEncryptBuild());
    }

    @Test
    public void query() {
        String requestId = "978601560772706304";
        QueryBuilder builder = new QueryBuilder(merchantId);
        builder.setRequestId(requestId);
        JSONObject response = request(ConfigurationUtils.getOnlinePayQueryUrl(), builder.bothEncryptBuild());
    }

    @Test
    public void queryRefund() {
        String requestId = "978601560772706304";
        RefundBuilder builder = new RefundBuilder(merchantId);
        builder.setRequestId(requestId);
        JSONObject response = request(ConfigurationUtils.getOnlinePayRefundQueryUrl(), builder.bothEncryptBuild());
    }
}
