<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/25
 * Time: 14:24
 */

use think\facade\Db;

class AliPayConfig
{

    private $appId = '';
    private $pId = '';
    private $publicKey = '';
    private $privateKey = '';
    private $signType = '';

    function __construct()
    {
        $mch_id = $GLOBALS['mch_id'];
        $pay_type = $GLOBALS['pay_type'];
        $platform = $GLOBALS['platform'];
        $config = Db::name('miniapp_config')->where(['mch_id' => $mch_id, 'type' => $platform])->find();
        $agent_id = isset($GLOBALS['agent_id']) ? $GLOBALS['agent_id'] : 0;
        $config_base = json_decode($config['base'], true);
        if($agent_id>0){
            $condition['mch_id'] = $mch_id;
            $condition['config_key'] = 'pay_setting'.$agent_id;
            $info = Db::name('sys_config')
                ->where($condition)
                ->find();
            if (empty($info)) {
                $config_pay = ['AliAppId' => '', 'AliPublicKey' => '', 'AliPrivateKey' => ''];
            }else{
                $pay = json_decode($info['config_value'], true);
                $config_pay = isset($pay['pay']) ? $pay['pay'] : ['AliAppId' => '', 'AliPublicKey' => '', 'AliPrivateKey' => ''];
            }
        }else{
            $config_pay = json_decode($config['pay'], true);
        }
        if ($pay_type == 1) {
            //支付宝小程序支付
            $this->appId = $config_base['APP_ID'];
            $this->publicKey = $config_base['ALI_PUB'];
            $this->privateKey = $config_base['PRIVATE_KEY'];
        } elseif ($pay_type == 2) {
            //支付宝APP支付
            $this->appId = $config_pay['AliAppId'];
            $this->publicKey = $config_pay['AliPublicKey'];
            $this->privateKey = $config_pay['AliPrivateKey'];
        } elseif ($pay_type == 3) {
            //支付宝Wap支付
            $this->appId = $config_pay['AliAppId'];
            $this->publicKey = $config_pay['AliPublicKey'];
            $this->privateKey = $config_pay['AliPrivateKey'];
        } elseif ($pay_type == 6) {
            //支付宝当面付
            $shop_config = Db::name('sys_config')->where(['mch_id' => $mch_id, 'config_key'=> 'MICRO_PAY'])->find();
            $value = json_decode($shop_config['config_value'], true);
            $this->appId = $value['AliAppId'];
            $this->publicKey = $value['AliPublicKey'];
            $this->privateKey = $value['AliPrivateKey'];
        }
        if ($pay_type == 0) {
            //应用支付宝第三方开放平台
            $shop_config = Db::name('sys_config')
                ->field('value')
                ->where(['config_key'=> 'ali_scanorder_setting', 'mch_id' => $mch_id])
                ->find();
            $flag = true;
            if (!empty($shop_config)) {
                //$flag = false;
                //判断是否使用平台参数
                $config_base = json_decode($shop_config['config_value'], true);
                foreach ($config_base as $key => $value) {
                    if ($key == 'IS_ORDER_SYNC' || !$flag) {
                        continue;
                    }
                    //应用支付宝配置信息有一个参数值不为空时不使用平台参数
                    if (!empty($value) && $flag) {
                        $flag = false;
                    }
                }
            }
            if ($flag) {
                //平台支付宝第三方开放平台
                $shop_config = Db::name('sys_config')
                    ->field('value')
                    ->where(['config_key'=> 'ali_scanorder_setting', 'mch_id' => 0])
                    ->find();
            }
            $config_base = json_decode($shop_config['config_value'], true);
            $this->appId = $config_base['APP_ID'];
            $this->pId = $config_base['PID'];
            $this->publicKey = $config_base['ALI_PUB'];
            $this->privateKey = $config_base['PRIVATE_KEY'];
        }
        $this->signType = 'RSA2';
    }

    public function GetAppId()
    {
        return $this->appId;
    }

    public function GetPId()
    {
        return $this->pId;
    }

    public function GetPublicKey()
    {
        return $this->publicKey;
    }

    public function GetPrivateKey()
    {
        return $this->privateKey;
    }

    public function GetNotifyUrl()
    {
        return get_localhost_domain(false, true, true) . '/extend/AliSDK/notify_ali.php';
    }

    public function GetSignType()
    {
        return $this->signType;
    }
}