package com.izouma.nineth.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.huifu.adapay.core.exception.BaseAdaPayException;
import com.huifu.adapay.model.AdapayCommon;
import com.huifu.adapay.model.Payment;
import com.izouma.nineth.ApplicationTests;
import com.izouma.nineth.config.GeneralProperties;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdapayServiceTest extends ApplicationTests {
    @Autowired
    private AdapayService     adapayService;
    @Autowired
    private GeneralProperties generalProperties;

    @Test
    public void testPay() throws BaseAdaPayException {
        Map<String, Object> paymentParams = new HashMap<String, Object>(10);
        List<Map<String, Object>> divMembers = new ArrayList<>();
        divMembers.add(new HashMap<>() {{
            put("member_id", "0");
            put("amount", BigDecimal.valueOf(0.05));
            put("fee_flag", "Y");
        }});
        divMembers.add(new HashMap<>() {{
            put("member_id", "1110");
            put("amount", BigDecimal.valueOf(0.05));
        }});

        paymentParams.put("app_id", "app_f8760acc-f4d8-46f6-8f70-d80e36517075");
        paymentParams.put("order_no", "jsdk_payment" + System.currentTimeMillis());
        paymentParams.put("pay_channel", "alipay_wap");
        paymentParams.put("pay_amt", "0.10");
        paymentParams.put("goods_title", "your goods title");
        paymentParams.put("goods_desc", "your goods desc");
        paymentParams.put("notify_url", "http://9th.frp.izouma.com/notify/adapay/order/1");
        paymentParams.put("description", "orderId=1");

        Map<String, Object> expend = new HashMap<>();
        expend.put("open_id", "oWJG55wLnwdVzXoKka1-DzQKOd_Y");
        paymentParams.put("expend", expend);

        Map<String, Object> payment = Payment.create(paymentParams);
        System.out.println("payment result=" + JSON.toJSONString(payment, SerializerFeature.PrettyFormat));
    }

    @Test
    public void testWxPay() throws BaseAdaPayException {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("order_no", "host_wx_lite_params_sdk_" + System.currentTimeMillis());
        params.put("adapay_func_code", "wxpay.createOrder");
        params.put("pay_amt", "0.02");
        params.put("app_id", "app_f8760acc-f4d8-46f6-8f70-d80e36517075");
        params.put("currency", "cny");
        params.put("goods_title", "Your goods_title");
        params.put("goods_desc", "Your goods_desc");
        params.put("description", "payment Discription");
        params.put("callback_url", generalProperties.getHost() + "/9th/orders");
        Map<String, Object> response = AdapayCommon.requestAdapayUits(params);
        System.out.println("payment result=" + JSON.toJSONString(response, SerializerFeature.PrettyFormat));
    }

    @Test
    public void createMember() throws BaseAdaPayException {
        adapayService.createMember(99999999999999L, "15077886171", "熊竹", "321002199408304614");
    }

    @Test
    public void createSettleAccount() throws BaseAdaPayException {
        adapayService.createSettleAccount("99999999999999", "熊竹", "321002199408304614",
                "15077886171", "6222024301070380163");
    }

    @Test
    public void delSettleAccount() throws BaseAdaPayException {
        adapayService.delSettleAccount("1110", "0288514678171392");
    }

    @Test
    public void queryBalance() throws BaseAdaPayException {
        adapayService.queryBalance("1110", "0288514678171392");
    }
}