<?php

namespace common\pay\ctype;

use backend\models\Config;
use backend\models\Deposit;
use backend\models\SyncDesposit;
use common\pay\BasePayHandler;
use Yii;
use yii\helpers\VarDumper;

class PayHandler extends BasePayHandler
{
    public $payUrl;
    public $partyId;
    public $accountId;
	public $gw_md5key;

    /**
     * @inheritdoc  
     */
    public function init()
    {
        parent::init();
        if ($this->payUrl == null) {
            $this->payUrl = Yii::$app->params['ctype.payUrl'];
        }
        if ($this->partyId == null) {
            $this->partyId = Yii::$app->params['ctype.partyId'];
        }
		 if ($this->accountId == null) {
            $this->accountId = Yii::$app->params['ctype.accountId'];
        }
        if ($this->gw_md5key == null) {
            $this->gw_md5key = Yii::$app->params['ctype.gw_md5key'];
        }
    }

    /**
     * @param array $deposit 
     * @param array $params
     * @return string
     */
    public function outPay($deposit, $params = [])
    {
        $data = [];
        $data['partyId'] = $this->partyId; // 商户号
		$data['accountId'] = $this->accountId; // 商户号
        $data['orderNo'] = $deposit['order_sn']; // 订单号
        $data['orderAmount'] = $deposit['amount']; // 金额
        $data['goods'] = 'ABC';
        $data['checkUrl'] = $this->notifyUrl; // 回调地址
        $data['orderCurrency'] = 'USDT';
        $data['cryptoCurrency'] = 'USDT';
		$data['encodeType'] = 'Sha1';
		
        $data['sign'] = PayUtils::makeSign($data, $this->gw_md5key);
        Yii::warning('支付请求参数，' . VarDumper::dumpAsString($data), __METHOD__);
        $result = static::curlPost($data, $this->payUrl);
        Yii::warning('支付请求结果，' . VarDumper::dumpAsString($result), __METHOD__);
        return $result;
    }

    /**
     * @param array $data 
     * @return bool
     */
    public function handleNotify($data)
    {
        Yii::warning('支付异步通知参数，' . VarDumper::dumpAsString($data), __METHOD__);
        if (isset($data['sign']) && trim($data['sign']) !== '') {
			unset($data['appType']);
            $sign = PayUtils::makeSign($data, $this->gw_md5key);
            if ($sign == $data['sign']) {
                if ($data['succ'] == 'Y') {
                    $merOrderId = trim($data['orderNo']);
                    $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
                    if ($reuslt['type'] != 1) {
                        $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
                        $configData = Config::find()->asArray()->one();
                        if ($configData['auto_deposit'] == 1 && $res) {
                            $syncDespositModel = new SyncDesposit();
                            $syncDespositModel->login = $reuslt['login'];
                            $syncDespositModel->amount = $reuslt['amount'];
                            $syncDespositModel->comment = 'Deposit';
                            $syncDespositModel->memo = $merOrderId;
                            $syncDespositModel->type = 2;
                            $syncDespositModel->in_time = time();
                            $syncDespositModel->save();
                        }
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public function outNotify($success)
    {
        if ($success == true) {
            return "checkok";
        } else {
            return "FAIL";
        }
    }

    /**
     * @param array $data
     * @return bool
     */
    public function handleReturn($data)
    {
       return true;
    }

    public function outReturn($success)
    {
    }
}