HEX
Server: Apache
System: Linux webm019.cluster128.gra.hosting.ovh.net 6.18.42-ovh-vps-grsec-zfs+ #1 SMP PREEMPT_DYNAMIC Wed Aug 5 15:59:48 CEST 2026 x86_64
User: petsclubcc (68997)
PHP: 5.4.45
Disabled: _dyuweyrj4,_dyuweyrj4r,dl
Upload Files
File: /home/petsclubcc/shop/modules/amazonpay/src/classes/Helper/AmazonPayAdminActionsHelper.php
<?php
/**
 * 2007-2021 patworx.de
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade AmazonPay to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 *  @author    patworx multimedia GmbH <service@patworx.de>
 *  @copyright 2007-2021 patworx multimedia GmbH
 *  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

class AmazonPayAdminActionsHelper
{
    private $transactions = [];
    private $summary = [
        'max_refund' => 0,
        'charge_allowed' => false,
        'refund_allowed' => false,
        'cancel_allowed' => false,
        'close_allowed' => false,
    ];

    /**
     * AmazonPayAdminActionsHelper constructor.
     * @param $transactions
     */
    public function __construct($transactions)
    {
        $this->transactions = $transactions;
    }

    public function getSummary()
    {
        $this->summary['max_refund'] = $this->calcMaxRefund();
        $this->summary['charge_allowed'] = $this->isChargeAllowed();
        $this->summary['refund_allowed'] = $this->isRefundAllowed();
        $this->summary['cancel_allowed'] = $this->isCancelAllowed();
        $this->summary['close_allowed'] = $this->isCloseAllowed();
        return $this->summary;
    }

    private function calcMaxRefund()
    {
        $sum = 0;
        foreach ($this->transactions as $tx) {
            if ($tx['transaction_type'] != 'chargePermission' && $tx['transaction_type'] != 'refund_declined') {
                $sum+= $tx['transaction_amount'];
            }
        }
        return $sum;
    }

    public function isChargeAllowed()
    {
        foreach ($this->transactions as $tx) {
            if (in_array($tx['transaction_type'], ['refund', 'cancel', 'close', 'capture'])) {
                return false;
            }
        }
        return true;
    }

    public function isRefundAllowed()
    {
        if ($this->summary['max_refund'] <= 0) {
            return false;
        } else {
            foreach ($this->transactions as $tx) {
                if ($tx['transaction_type'] == 'cancel' || $tx['transaction_type'] == 'close') {
                    return false;
                }
            }
        }
        foreach ($this->transactions as $tx) {
            if ($tx['transaction_type'] == 'capture') {
                return true;
            }
        }
        return false;
    }

    public function isCancelAllowed()
    {
        foreach ($this->transactions as $tx) {
            if (in_array($tx['transaction_type'], ['cancel', 'close', 'capture'])) {
                return false;
            }
        }
        return true;
    }

    public function isCloseAllowed()
    {
        foreach ($this->transactions as $tx) {
            if (in_array($tx['transaction_type'], ['close'])) {
                return false;
            }
        }
        return true;
    }
}