package com.keao.edu.thirdparty.adapay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huifu.adapay.core.exception.BaseAdaPayException;
import org.springframework.util.DigestUtils;

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

public class Payment extends ConfigInit {


    /**
     * 运行支付类接口
     *
     * @return paymentId
     * @throws Exception 异常
     */
    public static String executePaymentTest(String merchantKey) throws Exception {
        //支付接口
        Map<String, Object> paymentParams = new HashMap<>(10);
        paymentParams.put("order_no", "jsdk_payment_" + System.currentTimeMillis());
        paymentParams.put("pay_channel", "alipay_wap");
        paymentParams.put("pay_amt", "0.01");

        paymentParams.put("goods_title", "your goods title");
        paymentParams.put("goods_desc", "your goods desc");
        paymentParams.put("notify_url", "https://wxwechat.utools.club/studentOrder/adaNotify");


        paymentParams.put("div_members", "");

        Map<String, Object> deviceInfo = new HashMap<>(2);

        deviceInfo.put("device_ip", "127.0.0.1");
        deviceInfo.put("device_mac", "交易设备 MAC");
        deviceInfo.put("device_type", "1");
        deviceInfo.put("device_imei", "交易设备 IMEI");
        deviceInfo.put("device_imsi", "交易设备 IMSI");
        deviceInfo.put("device_iccId", "ICCID");
        deviceInfo.put("device_wifi_mac", "WIFIMAC");

        paymentParams.put("device_info", deviceInfo);

//        Map<String, Object> goodsDetail = new HashMap<>();
//
//        goodsDetail.put("goods_id", "gooods_id_100002");
//        goodsDetail.put("goods_name", "商品名称");
//        goodsDetail.put("quantity", "1");
//        goodsDetail.put("price", "100");
//        goodsDetail.put("goods_category", "商品类目");
//        goodsDetail.put("goods_body", "商品描述信息");
//        goodsDetail.put("show_url", "商品的展示地址");
//
//        Map<String, Object> promotionDetail = new HashMap<>();
//
//        promotionDetail.put("cost_price", "100");
//        promotionDetail.put("receipt_id", "商家小票 id");
//        promotionDetail.put("goods_detail", goodsDetail);

        Map<String, Object> expendParams = new HashMap<>(2);
        String openId = "";//微信授权获取
        expendParams.put("open_id", openId);
        expendParams.put("is_raw", "1");
        expendParams.put("callback_url", "绝对路径");
        expendParams.put("limit_pay", "1");

       // expendParams.put("promotion_detail", promotionDetail);
        paymentParams.put("expend", expendParams);
        Map<String, Object> payment = executePayment(paymentParams);
        System.out.println("钱包支付地址:" + payment.get("pay_url"));
        //支付查询接口
       // queryPayment((String) payment.get("id"));
        // //关单接口
       // closePayment((String) payment.get("id"));

        return (String) payment.get("id");
    }

    /**
     * 执行一个支付交易
     *
     * @return 创建的支付对象
     * @throws Exception 异常
     */
    public static Map<String, Object> executePayment(Map<String, Object> paymentParams) throws Exception {
        paymentParams.put("app_id", appId);

        //调用sdk方法，创建支付，得到支付对象
        Map<String, Object> payment = new HashMap<>();
        try {
            payment = com.huifu.adapay.model.Payment.create(paymentParams);
        } catch (BaseAdaPayException e) {
            e.printStackTrace();
        }

        String error_code = (String) payment.get("error_code");
        if (null != error_code) {
            System.out.println("创建支付返回参数：" + JSON.toJSONString(payment));

            String error_msg = (String) payment.get("error_msg");
            System.out.println("error_code:" + error_code + "............." + error_msg);
        }

        return payment;
    }

    /**
     * 关闭一个支付交易
     *
     * @param paymentId 要关闭的支付id
     * @return 关闭的支付对象
     * @throws Exception 异常
     */
    public static Map<String, Object> closePayment(String paymentId) throws Exception {
        System.out.println("=======close payment begin=======");
        //关闭支付对象的参数，全部参数请参考 https://docs.adapay.tech/api/04-trade.html#id11
        //调用sdk方法，关闭支付，得到支付对象
        Map<String, Object> payment = new HashMap<>();
        Map<String, Object> paymentParams = new HashMap<>(10);
        paymentParams.put("payment_id", paymentId);
        try {
            paymentParams.put("payment_id", paymentId);
            paymentParams.put("reason", "reason");
            paymentParams.put("expend", "expend");
            paymentParams.put("notify_url", "notify_url");
            System.out.println("关单请求参数：" + JSON.toJSONString(paymentId));
            payment = com.huifu.adapay.model.Payment.close(paymentParams);
        } catch (BaseAdaPayException e) {
            e.printStackTrace();
        }


        String error_code = (String) payment.get("error_code");
        if (null != error_code) {
            System.out.println("关单返回参数：" + JSON.toJSONString(payment));
            String error_msg = (String) payment.get("error_msg");
            System.out.println("error_code:" + error_code + "............." + error_msg);
        }


        return payment;
    }

    /**
     * 查询一个支付交易
     *
     * @param paymentId 要查询的支付id
     * @return 查询的支付对象
     * @throws Exception 异常
     */
    public static Map<String, Object> queryPayment(String paymentId) throws Exception {
        System.out.println("=======query payment begin=======");
        //查询支付对象的参数，全部参数请参考 https://docs.adapay.tech/api/04-trade.html#id7
        //调用sdk方法，查询支付交易，得到支付对象
        Map<String, Object> payment = new HashMap<>();
        try {
            System.out.println("支付查询请求参数：" + JSON.toJSONString(paymentId));
            payment = com.huifu.adapay.model.Payment.query(paymentId);
        } catch (BaseAdaPayException e) {
            e.printStackTrace();
        }

        System.out.println("支付查询返回参数：" + JSON.toJSONString(payment));

        String error_code = (String) payment.get("error_code");
        if (null == error_code) {
            String error_msg = (String) payment.get("error_msg");
            System.out.println("error_code:" + error_code + "............." + error_msg);
        }
        System.out.println("=======query payment end=======");
        return payment;
    }


    public static Map<String, Object> getPayMap(BigDecimal amount, String orderNo, String notifyUrl, String returnUrl, String orderSubject, String orderBody) throws Exception {
        Map<String, Object> paymentParams = new HashMap<>(10);
        paymentParams.put("appId", appId);
        paymentParams.put("amount", amount);
        paymentParams.put("orderNo", orderNo);
        paymentParams.put("notifyUrl", notifyUrl);
        paymentParams.put("returnUrl", returnUrl);
        paymentParams.put("orderSubject", orderSubject);
        paymentParams.put("orderBody", orderBody);
        paymentParams.put("wxAppId", wxAppId);

        String originalStr = JSONObject.toJSONString(paymentParams);
        String sign = DigestUtils.md5DigestAsHex(originalStr.getBytes());

        paymentParams.put("sign", sign);
        paymentParams.remove(appId);
        return paymentParams;
    }
}
