<?php


namespace Oney\ThreeByFour\Gateway\Request;


use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
use Magento\Payment\Gateway\Request\BuilderInterface;
use Oney\ThreeByFour\Logger\Logger;

class PaymentDataBuilder implements BuilderInterface
{

    /**
     * @var Logger
     */
    protected $_logger;

    public function __construct(Logger $logger)
    {
        $this->_logger = $logger;
    }

    /**
     * @inheritDoc
     */
    public function build(array $buildSubject)
    {
        if (!isset($buildSubject['payment']) || !$buildSubject['payment'] instanceof PaymentDataObjectInterface) {
            throw new \InvalidArgumentException('Payment data object should be provided');
        }
        
        $payment = $buildSubject['payment'];
        $order = $payment->getOrder();

        $payment = [
            'payment' => [
                "business_transaction_code" => str_replace('threebyfour_', '', $payment->getPayment()->getMethodInstance()->getCode()),
                "payment_amount" => $order->getGrandTotalAmount(),
                "currency_code" => $order->getCurrencyCode()
            ]
        ];

        $this->_logger->info('[SUCCESS] [JSON] Create Payment object (PaymentDataBuilder): ', [
            'payment' => $payment
        ]);
        return $payment;
    }
}
