gui-gui/omnipay-payu-brazil

View on GitHub
src/Message/RefundResponse.php

Summary

Maintainability
B
4 hrs
Test Coverage
<?php

namespace Omnipay\PayUBrazil\Message;

use Omnipay\Common\Message\AbstractResponse;

class RefundResponse extends AbstractResponse
{
    
    protected $refundState = null;

    /**
     * Is the transaction successful?
     *
     * @return bool
     */
    public function isSuccessful()
    {
        if($this->getRefundState() === 'SUCCESS')
        {
            return true;
        }
        return false;
    }

    /**
     * Is the response successful?
     *
     * @return boolean
     */
    public function isPending()
    {
        if($this->getRefundState() === 'PENDING')
        {
            return true;
        }
        return false;
    }

     /**
     * Get the transaction reference generated by the gateway.
     *
     * @return string|null
     */
    public function getTransactionReference()
    {
        $state = $this->getRefundState();

        if ($state === 'SUCCESS' || $state == 'NONRESOLVED') 
        {
            return $this->data['result']['payload']['transactions'][0]['id'];
        }

        if($state === 'FAILED')
        {
            return $this->data['reportingResponse']['result']['payload']['transactions']['transaction']['id'];
        }
        
        return null;
    }

     /**
     * Get the order reference generated by the gateway.
     *
     * @return string|null
     */
    public function getOrderReference()
    {
        $state = $this->getRefundState();

        if ($state === 'SUCCESS' || $state == 'NONRESOLVED') 
        {
            return $this->data['result']['payload']['id'];
        }

        if($state === 'FAILED')
        {
            return $this->data['reportingResponse']['result']['payload']['id'];
        }

        if($state === 'PENDING')
        {
            return $this->data['transactionResponse']['orderId'];
        }

        return null;
    }
    /**
     * Response Message
     *
     * @return null|string A response message from the payment gateway
     */
    public function getMessage()
    {
        return isset($this->data['error']) ? $this->data['error'] : null;
    }

    /**
     * Response code
     *
     * @return null|string A response code from the payment gateway
     */
    public function getCode()
    {
        $state = $this->getRefundState();

        if ($state === 'SUCCESS' || $state == 'NONRESOLVED') 
        {
            return $this->data['result']['payload']['status'];
        }

        if($state === 'FAILED')
        {
            return $this->data['reportingResponse']['result']['payload']['status'];
        }
        
        if($state === 'PENDING')
        {
            return $this->data['transactionResponse']['state'];
        }

        return isset($this->data['code']) ? $this->data['code'] : null;
    }

    public function getRefundState()
    {
        if(!$this->refundState)
        {
            $this->setRefundState();
        }
        
        return $this->refundState;
    }
    /**
    *
    * PayU Refunds are complex. The gateway might return different results with diffrent response structures
    * setRefundState analyses the response and identifies which response type we got and simplifies the implementation
    * for the default response interface
    *
    * @return 'SUCCESS'|'FAILED'|'PENDING'|'NONRESOLVED'|'ERROR'
    */
    protected function setRefundState()
    {
        if(!isset($this->data['error']))
        {   
            // Check if request is pending
            if(isset($this->data['transactionResponse']['state']) && $this->data['transactionResponse']['state'] === 'PENDING')
            {
                $this->refundState = 'PENDING';
                return;
            }
            
            // if $data['result'] is set this response is either NONRESOLVED or SUCCESS
            if(isset($this->data['result']))
            {
                $responseCode = isset($this->data['code']) ? $this->data['code'] : null; // not the same as getCode()
                $responseStatus = isset($this->data['result']['payload']['status']) ? $this->data['result']['payload']['status'] : null;
                $responseParentTransacationReference = isset($this->data['result']['payload']['transactions'][0]['parentTransactionId']) ? $this->data['result']['payload']['transactions'][0]['parentTransactionId'] : null;
                $responseTransactionState = isset($this->data['result']['payload']['transactions'][0]['transactionResponse']['state']) ? $this->data['result']['payload']['transactions'][0]['transactionResponse']['state'] : null;
            
                if ($responseCode === 'SUCCESS' 
                    && $responseStatus === 'CANCELLED' 
                    && $responseParentTransacationReference === $this->request->getTransactionReference() 
                    && $responseTransactionState === 'APPROVED') 
                {
                    $this->refundState = 'SUCCESS';
                    return;
                }

                if ($responseCode === 'SUCCESS' && $responseStatus === 'CAPTURED') 
                {
                    $this->refundState = 'NONRESOLVED';
                    return;
                }
            }

            // If we are here it's probably a failed refund response 
            // We then set variable values in relation to resportingResponse array
            if(isset($this->data['reportingResponse']))
            {
                $responseCode = isset($this->data['reportingResponse']['code']) ? $this->data['reportingResponse']['code'] : null;
                $responseStatus = isset($this->data['reportingResponse']['result']['payload']['status']) ? $this->data['reportingResponse']['result']['payload']['status'] : null;
                $responseParentTransacationReference = isset($this->data['reportingResponse']['result']['payload']['transactions'][0]['parentTransactionId']) ? $this->data['reportingResponse']['result']['payload']['transactions'][0]['parentTransactionId'] : null;
                $responseTransactionState = isset($this->data['reportingResponse']['result']['payload']['transactions']['transaction']['transactionResponse']['state']) ? $this->data['reportingResponse']['result']['payload']['transactions']['transaction']['transactionResponse']['state'] : null;

                if( $responseCode === 'SUCCESS' 
                    && $responseStatus === 'CAPTURED'  
                    && $responseTransactionState === 'DECLINED')
                {
                    $this->refundState = 'FAILED';
                    return;
                }
            }

        }

        $this->refundState = 'ERROR';
        return;
    }

}