import dayjs from "dayjs";
import md5 from 'js-md5';
const handleParams = (obj) => Object.entries(obj).reduce((total,curr) => {
    if(!total){
        total += `${curr[0]}=${curr[1]}`
    }else{
        total += `|${curr[0]}=${curr[1]}`
    }
    return total
},'')


export default function usePay(){
    const payFunc = (args) => {
        const {order_no,notify_url,TranAmt,MerName,sign,plain_text} = args;
        const MercCode = import.meta.env.VITE_YCNC_MERCH_CODE
        const tranAmt = Number(TranAmt).toFixed(2)
        // const plainText = handleParams({MercCode,TranAmt:tranAmt,TermSsn:order_no})
        const Plain = {
            MercUrl:notify_url,
            TranAmt:tranAmt,
            TermSsn:order_no,
            BackLink:encodeURIComponent(`${window.location.origin}/#/pages/ycnc/orderDetail?order_no=${order_no}&isPayBack=true`),
            psbcmcc:'LSXD',
            TxnDt:dayjs(Date.now()).format('YYYY-MM-DD'),
            MercCode:MercCode,
            IsIntegral:'1',
            MerName:MerName,
        }
        const params = {
            Plain:handleParams(Plain),
            plainText:plain_text,
            sign:sign,
            Signature: '',
        }
        console.log('handCodePay-plain',Plain);
        console.log('handCodePay-params',params);
        Fw.device.api.handCodePay(params);
    }
    return {payFunc}
}