<?php

namespace Shm\ShmPayment\TBankPayment;

use Shm\ShmPayment\TBankPayment\TBankCollections\TPayments;

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(
            [
                "paymentId" => $this->paymentId
            ],
            [
                '$set' => [
                    'chargeResult' => $response
                ]
            ]
        );
        $this->success = $response['Success'] ?? false;
    }
}