<?php

namespace common\pay\threexmta;

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 $accountId;
    public $partyId;
    public $md5key;

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


    /**
     * 去支付
     * @param array $params
     * @return array
     */
    public function outPay($params)
    {

        $data['orderNo'] =  $params['order_sn']; // 商户订单号
        $data['appType'] = '';
        $data['orderAmount'] = $params['rmb']; //转化为人民币
        $data['goods'] = 'bd'; //商户首码
        $data['bank'] = ''; //
        $data['returnUrl'] = $this->returnUrl;
        $data['checkUrl'] =  $this->notifyUrl;
        $data['cardType'] = 1;
        $data['encodeType'] = 'Md5';
        $data['refCode'] = '00000000';
        $data['partyId'] = $this->partyId;
        $data['accountId'] = $this->accountId;
        $data['signMD5'] = PayUtils::makeSign($data, $this->md5key);
        Yii::warning('支付请求参数，' . VarDumper::dumpAsString($data), __METHOD__);
        $result = static::createHtml($data, $this->payUrl);
        Yii::warning('支付请求结果，' . VarDumper::dumpAsString($result), __METHOD__);
        return $result;
    }


    /**
     * 处理异步回调
     * @param array $params
     * @return array
     */
    public function handleNotify($params)
    {
        Yii::warning('支付异步通知参数，' . VarDumper::dumpAsString($params), __METHOD__);
        if (PayUtils::makeSign($params, $this->md5key) !== $params['signMD5']) {
            Yii::warning('支付异步通知认证签名失败，' . VarDumper::dumpAsString($params), __METHOD__);
            return false;
        }
        if ($params['succ'] == 'Y') {
            $tradeNo = trim($params['orderNo']);
            $reuslt = Deposit::find()->where(['order_sn' => $tradeNo])->asArray()->limit(1)->one();
            if ($reuslt['type'] != 1) {
                $res = Deposit::updateAll(['type' => 1], "order_sn = $tradeNo");
                $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 = $tradeNo;
                    $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 $params
     * @return array
     */
    public function handleReturn($params)
    {
        Yii::warning('支付异步通知参数，' . VarDumper::dumpAsString($params), __METHOD__);
        if (PayUtils::makeSign($params, $this->md5key) !== $params['signMD5']) {
            Yii::warning('支付异步通知认证签名失败，' . VarDumper::dumpAsString($params), __METHOD__);
            return false;
        }

        if ($params['succ'] == 'Y') {
            $tradeNo = trim($params['orderNo']);
            /** @var Deposit $model */
            $reuslt = Deposit::find()->where(['order_sn' => $tradeNo])->asArray()->limit(1)->one();
            if ($reuslt) {
//                    $model->type = 1;
//                    $model->save(false);
                return true;
            }
        }

        return false;
    }

    /**
     * @param bool $success
     */
    public function outReturn($success)
    {
    }
}