<?php

namespace App\Classes\Modules\Vouchers\Services\Voucherify;

use Illuminate\Support\Facades\Log;
use App\Classes\Modules\Vouchers\DataTransferObjects\UpdateVoucherifyOrderObject;

class UpdatesVoucherifyOrder
{

    /** @var VoucherifyClient */
    private $voucherifyClient;


    /**
     * UpdatesVoucherifyOrder constructor.
     *
     */
    public function __construct()
    {
        $this->voucherifyClient = createVoucherifyClient();
    }


    /**
     * @param UpdateVoucherifyOrderObject $obj
     * @return null|object
     * @throws \Voucherify\ClientException
     */
    public function execute(UpdateVoucherifyOrderObject $obj)
    {
        try {
            $updateOrderObject = [
                "id" => $obj->getVoucherifyOrderId(),
                "status" => $obj->getStatus(),
            ];

            $updateOrderObject['metadata']['transaction_id'] = $obj->getTransactionId();

            $result = $this->voucherifyClient->orders->update($updateOrderObject);
            return $result;
        } catch (\Voucherify\ClientException $e) {
            Log::error('UpdatesVoucherifyOrder '.$e);
            return null;
        }
    }
}
