Current File : /var/www/vinorea/modules/ipexportimport/classes/import/order/EIAPayment.php
<?php
/**
 *
 * NOTICE OF LICENSE
 *
 *  @author    SmartPresta <tehran.alishov@gmail.com>
 *  @copyright 2024 SmartPresta
 *  @license   Commercial License
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

class EIAPayment
{

    private $id_order;
    private $order_reference;
    private $id_currency;
    private $amount;
    private $payment_method;
    private $transaction_id;
    private $date_add;
    private $id_order_payment;
    private $conversion_rate;
    private $card_number;
    private $card_brand;
    private $card_expiration;
    private $card_holder;
    private $row;
    private $id_process;
    public $validateOnly;
    public $id_map = [];

    public function __construct($id_order, $order_reference, $paymentData = null, $row = null, $id_process = null, $validateOnly = false)
    {
        $this->row = $row;
        $this->id_process = $id_process;
        $this->validateOnly = $validateOnly;
        $this->id_order = $id_order;
        $this->order_reference = $order_reference;
        $this->id_order_payment = isset($paymentData['id_order_payment']) ? $paymentData['id_order_payment'] : array();
        $this->id_currency = isset($paymentData['id_currency']) ? $paymentData['id_currency'] : array();
        $this->amount = isset($paymentData['amount']) ? $paymentData['amount'] : array();
        $this->payment_method = isset($paymentData['payment_method']) ? $paymentData['payment_method'] : array();
        $this->conversion_rate = isset($paymentData['conversion_rate']) ? $paymentData['conversion_rate'] : array();
        $this->card_number = isset($paymentData['card_number']) ? $paymentData['card_number'] : array();
        $this->card_brand = isset($paymentData['card_brand']) ? $paymentData['card_brand'] : array();
        $this->card_expiration = isset($paymentData['card_expiration']) ? $paymentData['card_expiration'] : array();
        $this->card_holder = isset($paymentData['card_holder']) ? $paymentData['card_holder'] : array();
        $this->transaction_id = isset($paymentData['transaction_id']) ? $paymentData['transaction_id'] : array();
        $this->date_add = isset($paymentData['date_add']) ? $paymentData['date_add'] : array();
    }

    public function save()
    {
        $payments = OrderPayment::getByOrderReference($this->order_reference);
        if (!$payments) {
            foreach ($this->amount as $key => $val) {
                $this->setDataObject($key);
            }
        }

        if ($payments) {
            foreach ($payments as $key => $payment) {
                $this->setDataObject($key, $payment->id);
            }
        }
    }

    private function setDataObject($key, $id_order_payment = false)
    {
        if (!$id_order_payment && !empty($this->id_order_payment[$key])) {
            $id_order_payment = (int) $this->id_order_payment[$key];
        }
        
        $paymentObj = new OrderPayment($id_order_payment);
        $paymentObj->order_reference = $this->order_reference;

        if (isset($this->id_currency[$key]) && $this->id_currency[$key]) {
            $paymentObj->id_currency = $this->getCurrencyId($this->id_currency[$key]);
        }

        if (!$paymentObj->id_currency) {
            $paymentObj->id_currency = Configuration::get('PS_CURRENCY_DEFAULT');
        }

        if (isset($this->amount[$key]) && $this->amount[$key]) {
            $paymentObj->amount = EIATools::preparePrice($this->amount[$key]);
        } elseif (!$paymentObj->amount) {
            $paymentObj->amount = 0;
        }

        if (isset($this->payment_method[$key]) && $this->payment_method[$key]) {
            $paymentObj->payment_method = $this->payment_method[$key];
        }

        if (isset($this->conversion_rate[$key]) && $this->conversion_rate[$key]) {
            $paymentObj->conversion_rate = $this->conversion_rate[$key];
        }

        if (isset($this->card_number[$key]) && $this->card_number[$key]) {
            $paymentObj->card_number = $this->card_number[$key];
        }

        if (isset($this->card_brand[$key]) && $this->card_brand[$key]) {
            $paymentObj->card_brand = $this->card_brand[$key];
        }

        if (isset($this->card_expiration[$key]) && $this->card_expiration[$key]) {
            $paymentObj->card_expiration = $this->card_expiration[$key];
        }

        if (isset($this->card_holder[$key]) && $this->card_holder[$key]) {
            $paymentObj->card_holder = $this->card_holder[$key];
        }

        if (isset($this->transaction_id[$key]) && $this->transaction_id[$key]) {
            $paymentObj->transaction_id = $this->transaction_id[$key];
        }

        if (isset($this->date_add[$key]) && $this->date_add[$key]) {
            $paymentObj->date_add = $this->date_add[$key];
        }
        if (($error = $paymentObj->validateFields(false, true)) !== true) {
            throw new PrestaShopException("Error: $error. ID: $this->id_order. Row in file: $this->row.");
        }

        $this->validateOnly || $paymentObj->save();

        if (!empty($this->id_order_payment[$key]) && (int) $this->id_order_payment[$key]) {
            $this->id_map[(int) $this->id_order_payment[$key]] = $paymentObj->id;
        }
    }

    private function getCurrencyId($iso_code)
    {
        $sql = "
                SELECT *
                FROM " . _DB_PREFIX_ . "currency as c
                WHERE '" . pSQL($iso_code) . "'
               ";
        $currency = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
        if (isset($currency['id_currency']) && $currency['id_currency']) {
            return $currency['id_currency'];
        }
        return Configuration::get('PS_CURRENCY_DEFAULT');
    }

}