<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_MpPayPal
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
namespace Webkul\MpPayPal\Observer;

use Magento\Framework\Event\Observer;
use Magento\Payment\Observer\AbstractDataAssignObserver;
use Magento\Quote\Api\Data\PaymentInterface;

class DataAssignObserver extends AbstractDataAssignObserver
{
    const INTENT = 'intent';
    const ORDER_ID = 'orderID';
    const PAYER_ID = 'payerID';
    const PAYMENT_TOKEN = 'paymentToken';
    const SELLER_EMAIL = 'sellerEmail';
    const QUOTE_ID = 'quoteID';

    /**
     * @var array
     */
    protected $paypalAdditionalInformationList = [
        self::INTENT,
        self::ORDER_ID,
        self::PAYER_ID,
        self::PAYMENT_TOKEN,
        self::SELLER_EMAIL,
        self::QUOTE_ID
    ];

    public function __construct(
        \Webkul\MpPayPal\Helper\Data $helper
    ) {
        $this->helper = $helper;
    }

    /**
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        $data = $this->readDataArgument($observer);

        $additionalData = $data->getData(PaymentInterface::KEY_ADDITIONAL_DATA);
        if (!is_array($additionalData)) {
            return;
        }

        $paymentInfo = $this->readPaymentModelArgument($observer);

        foreach ($this->paypalAdditionalInformationList as $additionalInformationKey) {
            if (isset($additionalData[$additionalInformationKey])) {
                $paymentInfo->setAdditionalInformation(
                    $additionalInformationKey,
                    $additionalData[$additionalInformationKey]
                );
            }
        }
    }
}
