omnipay-authorizenet icon indicating copy to clipboard operation
omnipay-authorizenet copied to clipboard

Error on refund

Open jsgv opened this issue 6 years ago • 6 comments

Using: omnipay/authorizenet:2.6.0 omnipay/common:2.5.2 laravel/framework:5.4.36

I am attempting to issue a refund and I am getting error The card parameter is required

$gateway = Omnipay::create('AuthorizeNet_AIM');
$gateway->setApiLoginId('api_login_id');
$gateway->setTransactionKey('transaction_key');
$response = $gateway->refund([
    'amount' => $amount,
    'transactionReference' => '{"approvalCode":"DXNR77","transId":"60107279743"}'
])->send();

I understand I need the card details when issuing refunds. How can I get the card details? I do not store credit card information to DB.

jsgv avatar Aug 20 '18 01:08 jsgv

Did you solve this problem? Is it an erroneous validation check that should perhaps be removed?

judgej avatar Dec 30 '18 23:12 judgej

The Authorize.net api has a GetTransactionDetails method that takes a transId and returns all the details about the transaction including the last four digits of the cards. The transId and the last 4 of the card should be all you need. But I don't think this package supports GetTransactionDetails api call (unless i'm mistaken).

ammonkc avatar Feb 11 '19 23:02 ammonkc

From what I remember, the GetTransactionDetails API was a completely different API system that would need some major writing, additional credentials etc. Since then, the Authorize.Net REST API has changed substantially, so it may be something that can be revisited if it is now more closely integrated.

judgej avatar Aug 14 '19 21:08 judgej

/**
     * Get details by transaction id.
     *
     * @param $transactionId
     * @return \Omnipay\Common\Message\ResponseInterface
     */
    public function getTransactionDetails($transactionId)
    {
        $refId = 'ref' . time();

        $params = [
            'refId' => $refId,
            'transactionReference' => $transactionId,
        ];

        $request = $this->gateway_auth->queryDetail($params);


        return $request->send();
    }

getTransactionDetails works for AIM

Nks avatar Sep 04 '19 01:09 Nks

@Nks Two quick questions:

  • What is $this->gateway_auth?
  • Is it always necessary to provide a unique refId even for transaction queries?

judgej avatar Sep 05 '19 08:09 judgej

@Nks Two quick questions:

  • What is $this->gateway_auth?
  • Is it always necessary to provide a unique refId even for transaction queries?

1 - This is just the instance to omnipay.

        $this->gateway_auth = Omnipay::create('AuthorizeNet_AIM');
        $this->gateway_auth->setDeveloperMode(config('app.env') !== 'production');
        $this->gateway_auth->setTestMode(config('services.authorize_net.test_mode', false));
        $this->gateway_auth->setApiLoginId(config('services.authorize_net.login_id'));
        $this->gateway_auth->setTransactionKey(config('services.authorize_net.transaction_key'));

2 - Feel free to check. I don't think so. https://developer.authorize.net/api/reference/#transaction-reporting-get-transaction-details

After this you can make partial refunds. Make sure that you are storing the refund transaction which can be voided in this case. If you are going refund full amount just send the same amount on refund operation.

Here how I making the refund:

 /**
     * Making the refund for the payment.
     *
     * @param array $transactionReference
     * @param float $amount
     * @return \Omnipay\AuthorizeNet\Message\AIMResponse|\Omnipay\Common\Message\ResponseInterface
     */
    public function makeRefund(array $transactionReference, float $amount)
    {
        $amount = (floor($amount * 100) / 100);

        $params = [
            'amount' => $amount,
            'transactionReference' => json_encode($transactionReference),
        ];

        $request = $this->gateway_auth->refund($params);

        return $request->send();
    }

And here the full example of calling the refund method:

/**
     * @param int $paymentId
     * @param array $refundData
     * @return string
     * @throws \Exception
     */
    public function makeRefund(int $paymentId, array $refundData): string
    {
        /** @var Payment $payment */
        $payment = $this->repository->findEntity($paymentId);

        $amount = 0;

        switch ($refundData['refund_type']) {
            case Payment::REFUND_TYPE_FULL:
                $amount = $payment->total_amount;
                break;
            case Payment::REFUND_OTHER_AMOUNT:
                $amount = $refundData['refund_amount'];
                break;
        }

        if ($amount > $payment->total_amount || $amount <= 0) {
            throw new \Exception('Wrong refund amount');
        }

        //Resolving the transaction detail which requested by refund.
        $transactionResponse = $this->client->getTransactionDetails($payment->transaction_id);

        $transactionReference = [
            'transId' => $payment->transaction_id,
            'card' => [
                'number' => null,
                'expiry' => null,
            ]
        ];

        if ($transactionResponse->isSuccessful()) {
            $transaction = optional($transactionResponse->getData())->transaction;

            $cardNumber = (string)$transaction->payment->creditCard->cardNumber;

            $transactionReference['card'] = [
                'number' => substr($cardNumber, -4, 4) ?: null,
                'expiry' => 'XXXX',
            ];
        } else {
            Log::critical('Unable get transaction details', [
                'payment' => $payment,
                'error' => $transactionResponse->getMessage(),
            ]);

            throw new \Exception(__('Unable get transaction details from Authorize.net: :error', [
                'error' => $transactionResponse->getMessage(),
            ]));
        }

        $response = $this->client
            ->setGateway('AuthorizeNet_AIM')
            ->makeRefund($transactionReference, $amount);

        if ($response->isSuccessful()) {
            $transaction = json_decode($response->getTransactionReference(), true);
            $transId = Arr::get($transaction, 'transId');

            $paymentAttributes = [
                'order_id' => $payment->order_id,
                'customer_id' => $payment->customer_id,
                'total_amount' => $amount,
                'transaction_id' => $transId,
                'payment_method_preview' => __('Refund (:type) for transaction #:trans_id', [
                    'trans_id' => $payment->transaction_id,
                    'type' => Str::title(str_replace('_', ' ', $refundData['refund_type'])),
                ]),
                'payment_status' => $response->getResultCode() ?? Payment::STATUS_REFUNDED,
                'gateway_response' => $response->getMessage(),
            ];

            $this->repository->create($paymentAttributes);

            return $response->getMessage();
        } else {
            Log::critical(__('The payment was not refunded! Payment Gateway response: :error', [
                'error' => $response->getMessage(),
            ]), [
                'response' => $response
            ]);
        }

        throw new \Exception(__('Unable make refund with error: :error', [
            'error' => $response->getMessage()
        ]));
    }

Nks avatar Sep 05 '19 15:09 Nks