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

Eway Improvements to support transparent redirect & shared gateway recurring #23

Merged
merged 10 commits into from
Feb 21, 2018
Merged
15 changes: 15 additions & 0 deletions .editorConfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
Expand Down
17 changes: 17 additions & 0 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
{
protected $liveEndpoint = 'https://api.ewaypayments.com';
protected $testEndpoint = 'https://api.sandbox.ewaypayments.com';
protected $action;

public function getApiKey()
{
Expand Down Expand Up @@ -84,6 +85,22 @@ public function setInvoiceReference($value)
return $this->setParameter('invoiceReference', $value);
}

/**
* @return string|NULL
*/
public function getAction()
{
return $this->action;
}

/**
* @param string $action
*/
public function setAction($action)
{
$this->action = $action;
}

protected function getBaseData()
{
$data = array();
Expand Down
45 changes: 45 additions & 0 deletions src/Message/RapidCreateCardRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* eWAY Rapid Shared Page Create Card Request
*/

namespace Omnipay\Eway\Message;

/**
* eWAY Rapid Shared Page Create Card Request
*
* Creates a payment URL using eWAY's Responsive Shared Page
*
* @link https://eway.io/api-v3/#responsive-shared-page
*/
class RapidCreateCardRequest extends RapidPurchaseRequest
{
public function getData()
{
$this->validate('returnUrl');

$data = $this->getBaseData();

$data['TransactionType'] = 'Purchase';
$data['RedirectUrl'] = $this->getReturnUrl();

// Shared page parameters (optional)
$data['CancelUrl'] = $this->getCancelUrl();

$data['Payment'] = array();

if ($this->getAction() === 'Purchase') {
$data['Payment']['TotalAmount'] = (int) $this->getAmountInteger();
$data['Payment']['InvoiceNumber'] = $this->getTransactionId();
$data['Payment']['InvoiceDescription'] = $this->getDescription();
$data['Payment']['CurrencyCode'] = $this->getCurrency();
$data['Payment']['InvoiceReference'] = $this->getInvoiceReference();
$data['Method'] = 'TokenPayment';
} else {
$data['Method'] = 'CreateTokenCustomer';
$data['Payment']['TotalAmount'] = 0;
}

return $data;
}
}
8 changes: 6 additions & 2 deletions src/Message/RapidDirectAbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ protected function getBaseData()
if ($this->getCard()) {
$data['Customer']['CardDetails'] = array();
$data['Customer']['CardDetails']['Name'] = $this->getCard()->getName();
$data['Customer']['CardDetails']['ExpiryMonth'] = $this->getCard()->getExpiryDate('m');
$data['Customer']['CardDetails']['ExpiryYear'] = $this->getCard()->getExpiryDate('y');

if ($this->getCard()->getExpiryYear() && $this->getCard()->getExpiryMonth()) {
// Expiry date not required if token present
$data['Customer']['CardDetails']['ExpiryMonth'] = $this->getCard()->getExpiryDate('m');
$data['Customer']['CardDetails']['ExpiryYear'] = $this->getCard()->getExpiryDate('y');
}
$data['Customer']['CardDetails']['CVN'] = $this->getCard()->getCvv();

if ($this->getEncryptedCardNumber()) {
Expand Down
24 changes: 23 additions & 1 deletion src/Message/RapidDirectCreateCardRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace Omnipay\Eway\Message;

use Omnipay\Eway\RapidDirectGateway;
use Omnipay\Omnipay;

/**
* eWAY Rapid Direct Create Card Request
*
Expand Down Expand Up @@ -78,6 +81,25 @@ public function sendData($data)
->setAuth($this->getApiKey(), $this->getPassword())
->send();

return $this->response = new RapidDirectCreateCardResponse($this, $httpResponse->json());
$this->response = new RapidDirectCreateCardResponse($this, $httpResponse->json());

if ($this->getAction() === 'Purchase' && $this->response->isSuccessful()) {
/** @var RapidDirectGateway $purchaseGateway */
$purchaseGateway = Omnipay::create('Eway_RapidDirect');
$purchaseGateway->setApiKey($this->getApiKey());
$purchaseGateway->setPassword($this->getPassword());
$purchaseGateway->setTestMode($this->getTestMode());
$purchaseResponse = $purchaseGateway->purchase(array(
'amount' => $this->getAmount(),
'currency' => $this->getCurrency(),
'description' => $this->getDescription(),
'transactionId' => $this->getTransactionId(),
'card' => $this->getCard(),
'cardReference' => $this->response->getCardReference(),
))->send();
$this->response->setPurchaseResponse($purchaseResponse);
}

return $this->response;
}
}
35 changes: 31 additions & 4 deletions src/Message/RapidDirectCreateCardResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,47 @@
/**
* eWAY Rapid Direct Create Card Response
*/

namespace Omnipay\Eway\Message;

use Omnipay\SecurePay\Message\DirectPostCompletePurchaseResponse;

/**
* eWAY Rapid Direct Create Card Response
*
* This is the response class for Rapid Direct when creating
*
* This is the response class for Rapid Direct when creating
* or updating a card
*
*/
class RapidDirectCreateCardResponse extends RapidResponse
{
/**
* @var DirectPostCompletePurchaseResponse
*/
protected $purchaseResponse;

/**
* @return DirectPostCompletePurchaseResponse
*/
public function getPurchaseResponse()
{
return $this->purchaseResponse;
}

/**
* @param DirectPostCompletePurchaseResponse $purchaseResponse
*/
public function setPurchaseResponse($purchaseResponse)
{
$this->purchaseResponse = $purchaseResponse;
}

public function isSuccessful()
{
return $this->data['ResponseMessage'] == 'A2000';
if (!$this->getPurchaseResponse()) {
return $this->data['ResponseMessage'] == 'A2000';
} else {
return ($this->data['ResponseMessage'] == 'A2000' && $this->purchaseResponse->isSuccessful());
}
}
}
5 changes: 5 additions & 0 deletions src/Message/RapidDirectPurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public function getData()
$data['Payment']['CurrencyCode'] = $this->getCurrency();
$data['Payment']['InvoiceReference'] = $this->getInvoiceReference();

if (empty($data['Customer']['CardDetails']['CVN']) && $this->getCardReference()) {
// We have a token and card is not present so treat as MOTO.
$data['TransactionType'] = 'MOTO';
}

if ($this->getCardReference()) {
$data['Method'] = 'TokenPayment';
} else {
Expand Down
30 changes: 30 additions & 0 deletions src/Message/RapidResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public function getRedirectData()
}
}

/**
* Is the response a transparent redirect?
*
* @return boolean
*/
public function isTransparentRedirect()
{
return true;
}

/**
* Get a card reference (eWAY Token), for createCard requests.
*
Expand All @@ -49,12 +59,32 @@ public function getCardReference()
if (isset($this->data['Customer']['TokenCustomerID'])) {
return $this->data['Customer']['TokenCustomerID'];
}
if (isset($this->data['TokenCustomerID'])) {
// This format appears when creating a card and making a concurrent
// payment using Shared or Transparent redirect methods.
return $this->data['TokenCustomerID'];
}

return null;
}

/**
* Get the transaction ID as generated by the merchant website.
*
* @return string
*/
public function getTransactionId()
{
return $this->data['InvoiceNumber'];
}

/**
* Get InvoiceNumber - merchant reference for a transaction
*
* @deprecated Omnipay standard interface is to return this when getTransactionId
* is called
*
* @see https://github.com/thephpleague/omnipay#successful-response
*/
public function getInvoiceNumber()
{
Expand Down
33 changes: 31 additions & 2 deletions src/Message/RapidSharedCreateCardRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,30 @@
*/
class RapidSharedCreateCardRequest extends RapidSharedPurchaseRequest
{
protected $action;

/**
* @return string|NULL
*/
public function getAction()
{
return $this->action;
}

/**
* @param string $action
*/
public function setAction($action)
{
$this->action = $action;
}

public function getData()
{
$this->validate('returnUrl');

$data = $this->getBaseData();
$data['Method'] = 'CreateTokenCustomer';

$data['TransactionType'] = 'Purchase';
$data['RedirectUrl'] = $this->getReturnUrl();

Expand All @@ -32,7 +50,18 @@ public function getData()
$data['CustomView'] = $this->getCustomView();

$data['Payment'] = array();
$data['Payment']['TotalAmount'] = 0;

if ($this->getAction() === 'Purchase') {
$data['Payment']['TotalAmount'] = (int) $this->getAmountInteger();
$data['Payment']['InvoiceNumber'] = $this->getTransactionId();
$data['Payment']['InvoiceDescription'] = $this->getDescription();
$data['Payment']['CurrencyCode'] = $this->getCurrency();
$data['Payment']['InvoiceReference'] = $this->getInvoiceReference();
$data['Method'] = 'TokenPayment';
} else {
$data['Method'] = 'CreateTokenCustomer';
$data['Payment']['TotalAmount'] = 0;
}

return $data;
}
Expand Down
13 changes: 13 additions & 0 deletions src/RapidGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Omnipay\Eway;

use Omnipay\Common\AbstractGateway;
use Omnipay\Omnipay;

/**
* eWAY Rapid Transparent Redirect Gateway
Expand Down Expand Up @@ -64,6 +65,13 @@ public function setPassword($value)

public function purchase(array $parameters = array())
{
if (!empty($parameters['cardTransactionType']) && $parameters['cardTransactionType'] === 'continuous') {
$gateway = Omnipay::create('Eway_RapidDirect');
$gateway->setApiKey($this->getApiKey());
$gateway->setPassword($this->getPassword());
$gateway->setTestMode($this->getTestMode());
return $gateway->createRequest('\Omnipay\Eway\Message\RapidDirectPurchaseRequest', $parameters);
}
return $this->createRequest('\Omnipay\Eway\Message\RapidPurchaseRequest', $parameters);
}

Expand All @@ -76,4 +84,9 @@ public function refund(array $parameters = array())
{
return $this->createRequest('\Omnipay\Eway\Message\RefundRequest', $parameters);
}

public function createCard(array $parameters = array())
{
return $this->createRequest('\Omnipay\Eway\Message\RapidCreateCardRequest', $parameters);
}
}
8 changes: 8 additions & 0 deletions src/RapidSharedGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Omnipay\Eway;

use Omnipay\Common\AbstractGateway;
use Omnipay\Omnipay;

/**
* eWAY Rapid Responsive Shared Page Gateway
Expand Down Expand Up @@ -61,6 +62,13 @@ public function setPassword($value)

public function purchase(array $parameters = array())
{
if (!empty($parameters['cardTransactionType']) && $parameters['cardTransactionType'] === 'continuous') {
$gateway = Omnipay::create('Eway_RapidDirect');
$gateway->setApiKey($this->getApiKey());
$gateway->setPassword($this->getPassword());
$gateway->setTestMode($this->getTestMode());
return $gateway->createRequest('\Omnipay\Eway\Message\RapidDirectPurchaseRequest', $parameters);
}
return $this->createRequest('\Omnipay\Eway\Message\RapidSharedPurchaseRequest', $parameters);
}

Expand Down
Loading