Skip to content

Commit

Permalink
Added void and refund functions for credit card payments.
Browse files Browse the repository at this point in the history
  • Loading branch information
judgej committed May 20, 2018
1 parent a20e9c7 commit 582f76d
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 11 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,29 @@ $response = $gateway->capture([
'transactionReference' => $transactionReference,
])->send();
```

An authorized transaction can be voided:

```php
// Captured from the authorization response.
$transactionReference = $response->getTransactionReference();

$response = $gateway->void([
'transactionReference' => $transactionReference,
])->send();
```

A cleared credit card payment can be refunded, given the original
transaction reference, the original amount, and the last four digits
of the credit card:

```php
$response = $gateway->refund([
'amount' => '7.99',
'currency' => 'USD',
'transactionReference' => $transactionReference,
'numberLastFour' => '1234',
])->send();
```


24 changes: 24 additions & 0 deletions src/ApiGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

use Omnipay\AuthorizeNetApi\Message\AuthorizeRequest;
use Omnipay\AuthorizeNetApi\Message\PurchaseRequest;
use Omnipay\AuthorizeNetApi\Message\VoidRequest;
use Omnipay\AuthorizeNetApi\Message\RefundRequest;

class ApiGateway extends AbstractGateway
{
Expand Down Expand Up @@ -42,4 +44,26 @@ public function purchase(array $parameters = array())
$parameters
);
}

/**
* Void an authorized transaction.
*/
public function void(array $parameters = array())
{
return $this->createRequest(
VoidRequest::class,
$parameters
);
}

/**
* Refund a captured transaction (before it is cleared).
*/
public function refund(array $parameters = array())
{
return $this->createRequest(
RefundRequest::class,
$parameters
);
}
}
7 changes: 0 additions & 7 deletions src/Message/AuthorizeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ class AuthorizeRequest extends AbstractRequest
*/
public function getData()
{
/*$amount = new MoneyPhp(
new Money(
$this->getAmountInteger(),
new Currency($this->getCurrency())
)
);*/

$amount = new Amount($this->getCurrency(), $this->getAmountInteger());

$transaction = $this->createTransaction($amount);
Expand Down
2 changes: 1 addition & 1 deletion src/Message/CaptureRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ public function sendData($data)
{
$response_data = $this->sendTransaction($data);

return new CaptureResponse($this, $response_data);
return new Response($this, $response_data);
}
}
67 changes: 67 additions & 0 deletions src/Message/RefundRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Omnipay\AuthorizeNetApi\Message;

use Academe\AuthorizeNet\Amount\MoneyPhp;
use Academe\AuthorizeNet\Amount\Amount;
use Academe\AuthorizeNet\AmountInterface;
use Academe\AuthorizeNet\Request\Transaction\Refund;
use Academe\AuthorizeNet\Request\Model\Order;
use Academe\AuthorizeNet\Payment\CreditCard;

class RefundRequest extends CaptureRequest
{
public function getData()
{
$transaction = parent::getData();

$card = $this->getCard();

if ($card) {
// A credit card has been supplied.

if ($card->getNumber()) {
$creditCard = new CreditCard(
$card->getNumber(),
// Either MMYY or MMYYYY will work.
// (This will be overwritten with 'XXXX' for now)
$card->getExpiryMonth() . $card->getExpiryYear()
);

$transaction = $transaction->withPayment($creditCard);
}
}

// Instead of supplying the full credit card dtails, just
// provide the lasy four digits of the card number.

if ($this->getNumberLastFour()) {
$creditCard = new CreditCard(
$this->getNumberLastFour(),
'XXXX'
);

$transaction = $transaction->withPayment($creditCard);
}

return $transaction;
}

protected function createTransaction(AmountInterface $amount, $refTransId)
{
return new Refund($amount, $refTransId);
}

/**
* The last four digits of the origonal credit card.
*/
public function getNumberLastFour()
{
return $this->getParameter('numberLastFour');
}

public function setNumberLastFour($value)
{
return $this->setParameter('numberLastFour', $value);
}
}
4 changes: 1 addition & 3 deletions src/Message/CaptureResponse.php → src/Message/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
use Academe\AuthorizeNet\Response\Collections\TransactionMessages;
use Academe\AuthorizeNet\Response\Collections\Errors;
use Omnipay\Common\Message\RequestInterface;
use Academe\AuthorizeNet\Response\Response;
use Academe\AuthorizeNet\Response\Model\TransactionResponse as TransactionResponseModel;

class CaptureResponse extends AbstractResponse
class Response extends AbstractResponse
{
public function __construct(RequestInterface $request, $data)
{
Expand Down
38 changes: 38 additions & 0 deletions src/Message/VoidRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Omnipay\AuthorizeNetApi\Message;

use Academe\AuthorizeNet\Amount\MoneyPhp;
use Academe\AuthorizeNet\Amount\Amount;
use Academe\AuthorizeNet\AmountInterface;
use Academe\AuthorizeNet\Request\Transaction\VoidTransaction;
use Academe\AuthorizeNet\Request\Model\Order;

class VoidRequest extends AbstractRequest
{
/**
* Return the complete message object.
*/
public function getData()
{
// Identify the original transaction being voided.
$refTransId = $this->getTransactionReference();

$transaction = new VoidTransaction($refTransId);

return $transaction;
}

/**
* Accept a transaction and sends it as a request.
*
* @param $data TransactionRequestInterface
* @returns CaptureResponse
*/
public function sendData($data)
{
$response_data = $this->sendTransaction($data);

return new Response($this, $response_data);
}
}

0 comments on commit 582f76d

Please sign in to comment.