Skip to content

Commit

Permalink
Add update token customer function
Browse files Browse the repository at this point in the history
  • Loading branch information
incarnate committed Oct 28, 2015
1 parent 6b9dcc1 commit f4f6dd1
Show file tree
Hide file tree
Showing 8 changed files with 508 additions and 19 deletions.
56 changes: 56 additions & 0 deletions src/Rapid/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ public function createCustomer($apiMethod, $customer)
return $this->_invoke(CreateCustomerResponse::getClass());
}

/**
* @inheritdoc
*/
public function updateCustomer($apiMethod, $customer)
{
return $this->_invoke(CreateCustomerResponse::getClass());
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -394,6 +402,54 @@ private function _createCustomer($apiMethod, $customer)
}
}

/**
*
* @param $apiMethod
* @param type $customer
* @return ResponseInterface
* @throws MethodNotImplementedException
*/
private function _updateCustomer($apiMethod, $customer)
{
/** @var Customer $customer */
$customer = ClassValidator::getInstance('Eway\Rapid\Model\Customer', $customer);

$apiMethod = EnumValidator::validate('Eway\Rapid\Enum\ApiMethod', 'ApiMethod', $apiMethod);

$transaction = [
'Customer' => $customer->toArray(),
'Payment' => ['TotalAmount' => 0],
'Method' => PaymentMethod::UPDATE_TOKEN_CUSTOMER,
'TransactionType' => TransactionType::PURCHASE,
];

if (isset($customer->RedirectUrl)) {
$transaction['RedirectUrl'] = $customer->RedirectUrl;
}
if (isset($customer->CancelUrl)) {
$transaction['CancelUrl'] = $customer->CancelUrl;
}

/** @var Transaction $transaction */
$transaction = ClassValidator::getInstance('Eway\Rapid\Model\Transaction', $transaction);

switch ($apiMethod) {
case ApiMethod::DIRECT:
return $this->getHttpService()->postTransaction($transaction->toArray());

case ApiMethod::RESPONSIVE_SHARED:
return $this->getHttpService()->postAccessCodeShared($transaction->toArray());

case ApiMethod::TRANSPARENT_REDIRECT:
return $this->getHttpService()->postAccessCode($transaction->toArray());

default:
// Although right now this code is not reachable, protect against incomplete
// changes to ApiMethod
throw new MethodNotImplementedException();
}
}

/**
* @param $tokenCustomerId
*
Expand Down
21 changes: 20 additions & 1 deletion src/Rapid/Contract/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface Client
/**
* Rapid SDK Version.
*/
const VERSION = '1.0.0';
const VERSION = '1.1.1';

/**
* Sandbox mode.
Expand Down Expand Up @@ -205,6 +205,25 @@ public function queryInvoiceReference($invoiceReference);
*/
public function createCustomer($apiMethod, $customer);

/**
* This Method is used to update a existing token customer for the merchant in their eWAY account. Card, email, and
* address changes can be made.
*
* Like the CreateCustomer, a PaymentMethod is specified which determines what method will be used to capture the
* card that will be saved with the customer. Depending on the PaymentMethod the customer may be updated
* immediately, or it may be pending (waiting for Card Details to be supplied by the Responsive Shared Page, or
* Transparent Redirect).
*
* The SDK will use the PaymentMethod parameter to determine what type of transaction to create as per the
* transaction type mapping table in the Create (Transaction) API Method spec (above).
*
* @param string $apiMethod
* @param Customer|array $customer
*
* @return CreateCustomerResponse
*/
public function updateCustomer($apiMethod, $customer);

/**
* This method is used to return the details of a Token Customer. This includes masked Card information for displaying in a UI to a user.
*
Expand Down
1 change: 1 addition & 0 deletions src/Rapid/Model/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* @property string $Method
* @property string $InvoiceNumber
* @property string $InvoiceDescription
* @property string $TokenCustomerID
*/
class Transaction extends AbstractModel
{
Expand Down
3 changes: 3 additions & 0 deletions tests/AbstractClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ protected function setup()
protected function assertCustomer($customer, $response, $cardWithinCustomer = false)
{
foreach ($customer as $key => $value) {
if (in_array($key, ['RedirectUrl', 'CancelUrl'])) {
continue;
}
if ('CardDetails' === $key) {
$this->assertCardDetails($value, $response, $cardWithinCustomer);
} else {
Expand Down
20 changes: 3 additions & 17 deletions tests/Integration/CreateCustomerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,8 @@ public function testCreateCustomerResponsiveShared()
'FirstName' => 'John',
'LastName' => 'Smith',
'Country' => 'au',
'CardDetails' => [
'Name' => 'John Smith',
'Number' => '4444333322221111',
'ExpiryMonth' => '12',
'ExpiryYear' => '25',
'CVN' => '123',
],
"RedirectUrl" => "http://www.eway.com.au",
"CancelUrl" => "http://www.eway.com.au",
'RedirectUrl' => 'http://www.eway.com.au',
'CancelUrl' => 'http://www.eway.com.au',
];

$response = $this->client->createCustomer(ApiMethod::RESPONSIVE_SHARED, $customer);
Expand All @@ -75,14 +68,7 @@ public function testCreateCustomerTransparentRedirect()
'FirstName' => 'John',
'LastName' => 'Smith',
'Country' => 'au',
'CardDetails' => [
'Name' => 'John Smith',
'Number' => '4444333322221111',
'ExpiryMonth' => '12',
'ExpiryYear' => '25',
'CVN' => '123',
],
"RedirectUrl" => "http://www.eway.com.au",
'RedirectUrl' => 'http://www.eway.com.au',
];

$response = $this->client->createCustomer(ApiMethod::TRANSPARENT_REDIRECT, $customer);
Expand Down
121 changes: 121 additions & 0 deletions tests/Integration/UpdateCustomerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace Eway\Test\Integration;

use Eway\Rapid\Enum\ApiMethod;
use Eway\Rapid\Model\Response\CreateCustomerResponse;
use Eway\Test\AbstractClientTest;

/**
* Class UpdateCustomerTest.
*/
class UpdateCustomerTest extends AbstractClientTest
{

private $tokenCustomerId;

protected function setup()
{
parent::setUp();

$customer = [
'Title' => 'Mr.',
'FirstName' => 'John',
'LastName' => 'Smith',
'Country' => 'au',
'CardDetails' => [
'Name' => 'John Smith',
'Number' => '4444333322221111',
'ExpiryMonth' => '12',
'ExpiryYear' => '25',
'CVN' => '123',
],
];

$response = $this->client->createCustomer(ApiMethod::DIRECT, $customer);

$this->tokenCustomerId = $response->Customer->TokenCustomerID;
}

/**
* Update Customer Token for Direct Connection Transaction
*/
public function testUpdateCustomerDirectConnection()
{
$newCustomer = [
'TokenCustomerID' => $this->tokenCustomerId,
'Title' => 'Mrs.',
'FirstName' => 'Jane',
'LastName' => 'Doe',
'Country' => 'vn',
'CardDetails' => [
'Name' => 'Jane Doe',
'Number' => '4444333322221111',
'ExpiryMonth' => '12',
'ExpiryYear' => '24',
'CVN' => '321',
],
];

$newResponse = $this->client->updateCustomer(ApiMethod::DIRECT, $newCustomer);

$this->assertInstanceOf(CreateCustomerResponse::getClass(), $newResponse);
$this->assertTrue(is_array($newResponse->getErrors()));
$this->assertEmpty($newResponse->getErrors());
$this->assertCustomer($newCustomer, $newResponse);
}

/**
* Update Customer Token for Responsive Shared Transaction
*/
public function testUpdateCustomerResponsiveShared()
{
/*
* Update Token
*/
$newCustomer = [
'TokenCustomerID' => $this->tokenCustomerId,
'Title' => 'Mrs.',
'FirstName' => 'Jane',
'LastName' => 'Doe',
'Country' => 'vn',
'RedirectUrl' => 'http://www.eway.com.au',
'CancelUrl' => "http://www.eway.com.au",
];

$updateCustomerResponse = $this->client->updateCustomer(ApiMethod::RESPONSIVE_SHARED, $newCustomer);

/*
* Assert
*/
$this->assertInstanceOf(CreateCustomerResponse::getClass(), $updateCustomerResponse);
$this->assertTrue(is_array($updateCustomerResponse->getErrors()));
$this->assertEmpty($updateCustomerResponse->getErrors());
$this->assertCustomer($newCustomer, $updateCustomerResponse);
}

/**
* Update Customer Token for Transparent Redirect Transaction
*/
public function testUpdateCustomerTransparentRedirect()
{
$newCustomer = [
'TokenCustomerID' => $this->tokenCustomerId,
'Title' => 'Mrs.',
'FirstName' => 'Jane',
'LastName' => 'Doe',
'Country' => 'vn',
'RedirectUrl' => 'http://www.eway.com.au',
];

$updateCustomerResponse = $this->client->updateCustomer(ApiMethod::TRANSPARENT_REDIRECT, $newCustomer);

/*
* Assert
*/
$this->assertInstanceOf(CreateCustomerResponse::getClass(), $updateCustomerResponse);
$this->assertTrue(is_array($updateCustomerResponse->getErrors()));
$this->assertEmpty($updateCustomerResponse->getErrors());
$this->assertCustomer($newCustomer, $updateCustomerResponse);
}
}
Loading

0 comments on commit f4f6dd1

Please sign in to comment.