<?php

namespace App\Models\DTO\Toss;
use Illuminate\Contracts\Support\Arrayable;

// https://docs.tosspayments.com/reference#v1paymentspaymentkeycancelpost-cancelreason
class CancelData implements Arrayable
{
    public string $cancelReason; // 결제를 취소하는 이유입니다.
    public int $cancelAmount; // 취소할 금액입니다. 값이 없으면 전액 취소됩니다.
    public ?array $refundReceiveAccount; // 결제 취소 후 금액이 환불될 계좌의 정보입니다.
    public int $taxFreeAmount; // 취소할 금액 중 면세 금액입니다.

    public function __construct()
    {
        $this->cancelReason = "";
        $this->cancelAmount = 0;
        $this->refundReceiveAccount = null;
        $this->taxFreeAmount = 0;
    }

    public function toArray(): array
    {
        $result = [
            'cancelReason' => $this->cancelReason
        ];

        if($this->cancelAmount) {
            $result['cancelAmount'] = $this->cancelAmount;
        }

        if($this->refundReceiveAccount) {
            $result['refundReceiveAccount'] = $this->refundReceiveAccount;
        }

        if($this->taxFreeAmount) {
            $result['taxFreeAmount'] = $this->taxFreeAmount;
        }

        return $result;
    }

    public function toString(): string|false
    {
        return serialize($this->toArray());
    }
}
