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

use Magento\Payment\Gateway\Http\ClientException;

class TransactionRefund extends AbstractTransaction
{
    /**
     * Process http request
     * @param array $data
     * @return
     */
    protected function process(array $data)
    {
        $purchaseUnit = $data;
        
        if ($purchaseUnit &&
            $purchaseUnit['transaction_data']['payments']['captures'][0]['status'] == 'COMPLETED'
        ) {
            $currencyCode = $this->storeManager->getStore()->getCurrentCurrencyCode();
            $requestData = [];
            $requestData['amount']['currency_code'] = $currencyCode;
            $requestData['amount']['value'] = $data['amount'];
            $requestData['invoice_id'] = "REFUND-INV-".
            strtotime($this->_date->gmtDate()).'-'.$purchaseUnit['transaction_id'];
            $requestData['custom'] = "REFUND-CUST-".
            strtotime($this->_date->gmtDate()).'-'.$purchaseUnit['transaction_id'];
            return $this->adapter->refundOrder(
                $requestData,
                $purchaseUnit
            );
        } else {
            throw new ClientException(__("cannot refund transaction currently, please try again later."));
        }
    }
}
