<?php

namespace Shm\ShmPayment\TBankPayment;

use Shm\ShmPayment\TBankPayment\TBankCollections\TPayments;
use Shm\ShmPayment\TBankPayment\TBankCollections\TPaymentsField;

class TBankCharge
{
    public function __construct() {}

    public $paymentId;
    public $rebillId;

    public static function create($paymentId, $rebillId)
    {
        $TBankCharge = new TBankCharge();
        $TBankCharge->paymentId($paymentId);
        $TBankCharge->rebillId($rebillId);
        return $TBankCharge;
    }

    public function paymentId($paymentId)
    {
        $this->paymentId = $paymentId;
        return $this;
    }

    public function rebillId($rebillId)
    {
        $this->rebillId = $rebillId;
        return $this;
    }

    public function readyToRequest()
    {

        if (!$this->paymentId) {
            return false;
        }

        if (!$this->rebillId) {
            return false;
        }

        return true;
    }

    public $success = false;

    public function request()
    {
        $response = TBankPayment::sendRequest('Charge', [
            "PaymentId" => $this->paymentId,
            "RebillId" => $this->rebillId,
        ]);

        TPayments::updateOne(
            [
                TPaymentsField::PAYMENT_ID => $this->paymentId
            ],
            [
                '$set' => [
                    TPaymentsField::CHARGE_RESULT => $response
                ]
            ]
        );
        $this->success = $response['Success'] ?? false;
    }
}