<?php


namespace app\traits;
use AdapayMerchantSdk\MerchantApp;
use AdapayMerchantSdk\MerchantConf;
use AdapayMerchantSdk\MerchantUser;
use AdapayMerchantSdk\MerchantProfile;
use AdaPaySdk\AdapayTools;
use AdaPaySdk\CorpMember;
use AdaPaySdk\Drawcash;
use AdaPaySdk\Member;
use AdaPaySdk\Payment;
use AdaPaySdk\PaymentConfirm;
use AdaPaySdk\PaymentReverse;
use AdaPaySdk\Refund;
use AdaPaySdk\SettleAccount;
use app\BaseController;
use think\facade\Db;
use think\facade\Log;
use think\model\relation\HasMany;


trait HuiPay
{
    /**
     * @param $uid
     * 配置文件
     */
    public function config($name)
    {

        ini_set('date.timezone','Asia/Shanghai');
        if(!defined('SDK_BASE')){
            define("SDK_BASE", dirname(__FILE__));
            define("SDK_VERSION", "v1.2.1");
            define("GATE_WAY_URL", "https://%s.adapay.tech");
            define("DEBUG", true);
            define("LOG", dirname(SDK_BASE)."/../runtime/huifuLog/prod");
            define("ENV", "prod");
            \AdaPay\AdaPay::init(dirname(__FILE__). '/../../extend/AdaPay/config/'.$name.'.json', "live", false);
        }

    }
    /**
     * @param $uid
     * 配置文件
     */
    public function configs($name)
    {

        ini_set('date.timezone','Asia/Shanghai');
        if(!defined('SDK_BASE')){
            define("SDK_BASE", dirname(__FILE__));
            define("SDK_VERSION", "v1.2.1");
            define("GATE_WAY_URL", "https://%s.adapay.tech");
            define("DEBUG", true);
            define("LOG", dirname(SDK_BASE)."/../runtime/huifuLog/prod");
            define("ENV", "prod");
            \AdaPay\AdaPay::init(dirname(__FILE__). '/../../extend/AdaPay/config/'.$name.'.json', "live", false);
        }

    }

    /**
     * @param $merchant_params
     * @return array
     * 商户进件
     */
    public function incoming_parts($merchant_params,$name)
    {
        $this->config($name);
        $merchant=new MerchantUser();
        $merchant->create($merchant_params);
        # 返回进件结果
        $merchant->isError();
        return $merchant->result;
    }

    /**
     * @param $merchant_params
     * @return array
     * 进件查询
     */
    public function incoming_parts_select($merchant_params,$name)
    {
        $this->config($name);
        $merchant = new MerchantUser();
        $merchant->query($merchant_params);
        $merchant->isError();
        return $merchant->result;
    }

    /**
     * @param $merchant_params
     * @return array
     * 商户入驻查询
     */
    public function Merchant_settlement_select($merchant_params,$name)
    {
        $this->config($name);
        $merchant = new MerchantConf();
        $merchant->query($merchant_params);
        # 返回入驻查询结果
        $merchant->isError();

        return $merchant->result;
    }
    /**
     * @param $merchant_params
     * @return array
     * 商户入驻查询
     */
    public function Merchant_settlement($merchant_params,$name)
    {
        $this->config($name);
        $merchant = new MerchantConf();
        $merchant->create($merchant_params);
        # 返回入驻结果
        $merchant->isError();

        return $merchant->result;
    }
    /**
     * @param $merchant_params
     * @return array
     * 商户入驻修改
     */
    public function modify($merchant_params)
    {
        $this->config('config');
        $merchant = new MerchantConf();
        $merchant->modify($merchant_params);
        # 返回修改结果
        $merchant->isError();
        return $merchant->result;
    }

    /**
     * @param $mer_upload_params
     * @return array
     * 提交照片
     */
    public function merProfilePicture($mer_upload_params,$name)
    {
        $this->config($name);
        $merchant = new MerchantProfile();
        $merchant->merProfilePicture($mer_upload_params);
        # 返回上传结果
        $merchant->isError();
        return $merchant->result;
    }

    /**
     * @param $mer_upload_params
     * @return array
     * 提交审核
     */
    public function merProfileForAudit($mer_upload_params,$name)
    {
        $this->config($name);
        $merchant = new MerchantProfile();
        $merchant->merProfileForAudit($mer_upload_params);
        # 返回修改结果
        $merchant->isError();
        return $merchant->result;
    }
    /**
     * @param $mer_upload_params
     * @return array
     * 审核状态
     */
    public function merProfileAuditStatus($mer_upload_params)
    {
        $this->config('config');
        $merchant = new MerchantProfile();
        $merchant->merProfileAuditStatus($mer_upload_params);
        # 返回修改结果
        $merchant->isError();
        return $merchant->result;
    }

    /**
     * @param $payment
     * @return array
     * 创建app
     */
    public function create_app_traits($payment_params)
    {
        $payment=new MerchantApp();

        $payment->create($payment_params);
        # 返回创建结果
        $payment->isError();
        return $payment->result;

    }
    /**
     * @param $payment
     * @return array
     * 查询app
     */
    public function select_app_traits($payment_params)
    {
        $payment=new MerchantApp();

        $payment->query($payment_params);
        # 返回查询结果
        $payment->isError();
        return $payment->result;

    }
    /**
     * @param $payment
     * @return array
     * 创建支付对象
     */
    public function Payment_create_traits($payment_params,$type,$key)
    {
        log::info($key);
        if ($key == 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871'){
            $this->configs('tdyl');
        }else{
            $this->configs('merchantConfig');
        }
        $payment=new Payment();
        $payment->create($payment_params,$key);
        # 返回创建结果
        $payment->isError();
        return $payment->result;
    }
    public function Payment_create_traits_a($payment_params,$type,$key)
    {
        log::info($key);
        $this->configs('merchantConfig');
        $payment=new Payment();
        $payment->create($payment_params,$key);
        # 返回创建结果
        $payment->isError();
        return $payment->result;
    }
    public function Payment_create_traits_b($payment_params,$type,$key)
    {
        log::info($key);
        $this->configs('tdyl');
        $payment=new Payment();
        $payment->create_b($payment_params,$key);
        # 返回创建结果
        $payment->isError();
        return $payment->result;
    }

    /**
     * @param $paymnet_params
     * @return array
     * 查询支付对象
     */
    public function Payment_query_traits($paymnet_params)
    {
        $payment=new Payment();
        $payment->query($paymnet_params);
        # 返回查询结果
        $payment->isError();
        return $payment->result;
    }

    /**
     * @param $paymnet_params
     * @return array
     * 关闭支付对象
     */
    public function Payment_close_traits($paymnet_params)
    {
        $payment=new Payment();
        $payment->close($paymnet_params);
        # 返回查询结果
        $payment->isError();
        return $payment->result;
    }

    /**
     * @param $paymnet_params
     * @return array
     * 支付撤销
     */
    public function payment_Reverse($payment_params,$type,$pay_wx,$key)
    {
        if($type==1){//支付宝
            $this->config('merchantConfig');
        }else{//微信
            if ($pay_wx == 2){
                $this->config('merchantConfigtdba');
            }elseif ($pay_wx == 3){
                $this->config('tdyl');
            }
        }
        $payment = new PaymentReverse();
        # 发起支付撤销
        $payment->create($payment_params,$key);
        # 返回查询结果
        $payment->isError();
        return $payment->result;
    }

    /**
     * @param $paymnet_params
     * @return array
     * 退款
     */
    public function Payment_refund_traits($paymnet_params,$key)
    {
        $this->config('merchantConfig');
        $Refund=new Refund();
        $Refund->create($paymnet_params,$key);
        # 返回查询结果
        $Refund->isError();
        return $Refund->result;
    }

    /**
     * @param $paymnet_params
     * @return array
     * 退款查询
     */
    public function Payment_refund_query_traits($paymnet_params)
    {
//        dump($paymnet_params);die;
        $Refund=new Refund();
        $Refund->query($paymnet_params);
        # 返回查询结果
        $Refund->isError();
        return $Refund->result;
    }
    /**
     * @param $paymnet_params
     * @return array
     * 创建个人用户
     */
    public function crate_merber_personal_traits($paymnet_params,$type,$key)
    {
        if ($type == 3){
            $this->config('tdyl');
        }else{
            $this->config('merchantConfig');
        }
        $merber=new Member() ;
        $merber->create($paymnet_params,$key);
        # 返回查询结果
        $merber->isError();
        return $merber->result;
    }
    /**
     * @param $paymnet_params
     * @return array
     * 创建结算账户
     */
    public function create_settleAccounttraits($paymnet_params,$type,$key)
    {
        if ($type == 3){
            $this->config('tdyl');
        }else{
            $this->config('merchantConfig');
        }
        $merber=new SettleAccount() ;
        $merber->create($paymnet_params,$key);
        # 返回查询结果
        $merber->isError();
        return $merber->result;
    }
    /**
     * @param $paymnet_params
     * @return array
     * 查询结算账户
     */
    public function query_config_settleAccount_traits($paymnet_params)
    {
        $merber=new SettleAccount() ;
        $merber->query($paymnet_params);
        # 返回查询结果
        $merber->isError();
        return $merber->result;
    }
    /**
     * @param $paymnet_params
     * @return array
     * 修改结算账户
     */
    public function update_config_settleAccount_traits($paymnet_params)
    {

        $merber=new SettleAccount() ;
        $merber->update($paymnet_params);
        # 返回查询结果
        $merber->isError();
        return $merber->result;
    }
    /**
     * @param $paymnet_params
     * @return array
     * 删除结算账户
     */
    public function delete_config_settleAccount_traits($paymnet_params)
    {
        $merber=new SettleAccount() ;
        $merber->delete($paymnet_params);
        # 返回查询结果
        $merber->isError();
        return $merber->result;
    }
    /**
     * @param $paymnet_params
     * @return array
     * 查询个人用户
     */
    public function query_merber_personal_traits($paymnet_params)
    {
        $merber=new Member() ;
        $merber->query($paymnet_params);
        # 返回查询结果
        $merber->isError();
        return $merber->result;
    }
    /**
     * @param $paymnet_params
     * @return array
     * 创建企业用户
     */
    public function crate_merber_enterprise_traits($paymnet_params,$type,$key)
    {
        if($type==1){//支付宝
            $this->config('merchantConfig');
        }elseif ($type==2){//微信
            $this->config('merchantConfigtdba');
        }elseif ($type==3){//天地易联
            $this->config('tdyl');
        }

        $merber=new CorpMember() ;
        $merber->create($paymnet_params,$key);
        # 返回查询结果
        $merber->isError();
        return $merber->result;
    }
    /**
     * @param $paymnet_params
     * @return array
     * 查询企业用户
     */
    public function query_merber_enterprise_traits($paymnet_params)
    {
        $merber=new CorpMember() ;
        $merber->query($paymnet_params);
        # 返回查询结果
        $merber->isError();
        return $merber->result;
    }
    public function download($date){
        $bill = new AdapayTools();

# 对账单下载
        $bill->download($date);
        # 返回查询结果
        $bill->isError();
        return $bill->result;
    }
    /**
     * @param $paymnet_params
     * @return array
     * 钱包提现
     */
    public function cash_withdrawal_traits($paymnet_params)
    {
        $merber=new Drawcash() ;
        $merber->create($paymnet_params);
        # 返回查询结果
        $merber->isError();
        return $merber->result;
    }
    /**
     * @param $paymnet_params
     * @return array
     * 创建支付确认对象
     */
    public function payment_confirm_create_traits($paymnet_params)
    {
        $merber=new PaymentConfirm() ;
        $merber->create($paymnet_params);
        # 返回查询结果
        $merber->isError();
        return $merber->result;
    }
    /**
     * @param $paymnet_params
     * @return array
     * 查询支付确认对象
     */
    public function payment_confirm_query_traits($paymnet_params)
    {
        $merber=new PaymentConfirm() ;
        $merber->query($paymnet_params);
        # 返回查询结果
        $merber->isError();
        return $merber->result;
    }


}