Refund Class

The Refund class is used to process a refund transaction through the IPC API using the IPCRefund method. You can issue full or partial refunds by referencing the original transaction.

Ideal for scenarios where a purchase must be reversed or adjusted after completion.

Class Details

  • Namespace: Mypos\IPC
  • Located at: Refund.php
  • Extends: Mypos\IPC\Base
  • Purpose: Initiate a refund based on a previous successful payment

Method Summary

MethodReturn TypeDescription
__construct(Config $cnf)-Initializes the refund request with a configuration object.
setAmount(float $amount)RefundSets the refund amount.
getAmount()floatReturns the set refund amount.
setCurrency(string $currency)RefundSets the ISO 4217 currency code (e.g., EUR, USD).
getCurrency()stringReturns the set currency code.
setTrnref(string $trnref)RefundSets the original transaction reference (from the purchase).
getTrnref()stringReturns the transaction reference.
setOrderID(string $orderID)RefundSets a unique order ID for this refund operation.
getOrderID()stringReturns the refund order ID.
validate()boolValidates that all required fields are provided.
process()boolInitiates the refund API request and returns the result.

Inherited from Base

Includes all core infrastructure methods:

  • _addPostParam()
  • _processPost(), _processHtmlPost()
  • getCnf(), setCnf()
  • getOutputFormat(), setOutputFormat()
  • isValidSignature()

Example Usage

<?php
namespace Mypos\IPC;
/**
 * Process IPC method: IPCRefund.
 * Collect, validate and send API params
 */
class Refund extends Base
{
    private $currency = 'EUR', $amount, $trnref, $orderID;
    /**
     * Return Refund object
     *
     * @param Config $cnf
     */
    public function __construct(Config $cnf)
    {
        $this->setCnf($cnf);
    }
    /**
     * Refund amount
     *
     * @param float $amount
     */
    public function setAmount($amount)
    {
        $this->amount = $amount;
    }
    /**
     * Transaction reference - transaction unique identifier
     *
     * @param string $trnref
     *
     * @return Refund
     */
    public function setTrnref($trnref)
    {
        $this->trnref = $trnref;
        return $this;
    }
    /**
     * Request identifier - must be unique
     *
     * @param string $orderID
     *
     * @return Refund
     */
    public function setOrderID($orderID)
    {
        $this->orderID = $orderID;
        return $this;
    }
    /**
     * Initiate API request
     *
     * @return boolean
     */
    public function process()
    {
        $this->validate();
        $this->_addPostParam('IPCmethod', 'IPCRefund');
        $this->_addPostParam('IPCVersion', $this->getCnf()->getVersion());
        $this->_addPostParam('IPCLanguage', $this->getCnf()->getLang());
        $this->_addPostParam('SID', $this->getCnf()->getSid());
        $this->_addPostParam('WalletNumber', $this->getCnf()->getWallet());
        $this->_addPostParam('KeyIndex', $this->getCnf()->getKeyIndex());
        $this->_addPostParam('Source', $this->getCnf()->getSource());
        $this->_addPostParam('Currency', $this->getCurrency());
        $this->_addPostParam('Amount', $this->getAmount());
        $this->_addPostParam('OrderID', $this->getOrderID());
        $this->_addPostParam('IPC_Trnref', $this->getTrnref());
        $this->_addPostParam('OutputFormat', $this->getOutputFormat());
        $response = $this->_processPost()->getData(CASE_LOWER);
        if (empty($response['ipc_trnref']) || (empty($response['amount']) || $response['amount'] != $this->getAmount()) || (empty($response['currency']) || $response['currency'] != $this->getCurrency()) || $response['status'] != Defines::STATUS_SUCCESS) {
            return false;
        }
        return true;
    }
    /**
     * Validate all set refund details
     *
     * @return boolean
     * @throws IPC_Exception
     */
    public function validate()
    {
        try {
            $this->getCnf()->validate();
        } catch (\Exception $ex) {
            throw new IPC_Exception('Invalid Config details: '.$ex->getMessage());
        }
        if ($this->getAmount() == null || !Helper::isValidAmount($this->getAmount())) {
            throw new IPC_Exception('Invalid Amount');
        }
        if ($this->getCurrency() == null || !Helper::isValidCurrency($this->getCurrency())) {
            throw new IPC_Exception('Invalid Currency');
        }
        if ($this->getTrnref() == null || !Helper::isValidTrnRef($this->getTrnref())) {
            throw new IPC_Exception('Invalid TrnRef');
        }
        if ($this->getOrderID() == null || !Helper::isValidOrderId($this->getOrderID())) {
            throw new IPC_Exception('Invalid OrderId');
        }
        if ($this->getOutputFormat() == null || !Helper::isValidOutputFormat($this->getOutputFormat())) {
            throw new IPC_Exception('Invalid Output format');
        }
        return true;
    }
    /**
     * Refund amount
     *
     * @return float
     */
    public function getAmount()
    {
        return $this->amount;
    }
    /**
     * ISO-4217 Three letter currency code
     *
     * @return string
     */
    public function getCurrency()
    {
        return $this->currency;
    }
    /**
     * ISO-4217 Three letter currency code
     *
     * @param string $currency
     *
     * @return Refund
     */
    public function setCurrency($currency)
    {
        $this->currency = $currency;
        return $this;
    }
    /**
     * Transaction reference - transaction unique identifier
     *
     * @return string
     */
    public function getTrnref()
    {
        return $this->trnref;
    }
    /**
     * Request identifier - must be unique
     *
     * @return string
     */
    public function getOrderID()
    {
        return $this->orderID;
    }
}

Use cases:

Use the Refund class when:

  • You need to partially or fully reverse a previous purchase
  • A customer canceled an order or requested a return
  • You want to automate refund workflows post-payment
  • You need to maintain compliance with refund policies and payment regulations