Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error on refund #114

Open
jsgv opened this issue Aug 20, 2018 · 6 comments
Open

Error on refund #114

jsgv opened this issue Aug 20, 2018 · 6 comments

Comments

@jsgv
Copy link

jsgv commented Aug 20, 2018

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.

@judgej
Copy link
Member

judgej commented Dec 30, 2018

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

@ammonkc
Copy link

ammonkc commented Feb 11, 2019

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).

@judgej
Copy link
Member

judgej commented Aug 14, 2019

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.

@Nks
Copy link

Nks commented Sep 4, 2019

/**
     * 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

@judgej
Copy link
Member

judgej commented Sep 5, 2019

@Nks Two quick questions:

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

@Nks
Copy link

Nks commented Sep 5, 2019

@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()
        ]));
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
@ammonkc @Nks @judgej @jsgv and others