Skip to content

Commit 1f61f02

Browse files
committed
Abstracted common functions from RapidPurchaseRequest for future extensions, added extra eWAY specific fields and incorporated more existing Omnnipay fields
1 parent 7343493 commit 1f61f02

File tree

4 files changed

+251
-29
lines changed

4 files changed

+251
-29
lines changed

src/Message/AbstractRequest.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Omnipay\Eway\Message;
4+
5+
/**
6+
* eWAY Abstract Request
7+
*
8+
*/
9+
abstract class AbstractRequest extends \Omnipay\Common\Message\AbstractRequest
10+
{
11+
protected $liveEndpoint = 'https://api.ewaypayments.com';
12+
protected $testEndpoint = 'https://api.sandbox.ewaypayments.com';
13+
14+
public function getApiKey()
15+
{
16+
return $this->getParameter('apiKey');
17+
}
18+
19+
public function setApiKey($value)
20+
{
21+
return $this->setParameter('apiKey', $value);
22+
}
23+
24+
public function getPassword()
25+
{
26+
return $this->getParameter('password');
27+
}
28+
29+
public function setPassword($value)
30+
{
31+
return $this->setParameter('password', $value);
32+
}
33+
34+
public function getPartnerId()
35+
{
36+
return $this->getParameter('partnerId');
37+
}
38+
39+
public function setPartnerId($value)
40+
{
41+
return $this->setParameter('partnerId', $value);
42+
}
43+
44+
public function getTransactionType()
45+
{
46+
return $this->getParameter('transactionType');
47+
}
48+
49+
/**
50+
* Sets the transaction type
51+
* One of "Purchase" (default), "MOTO" or "Recurring"
52+
*/
53+
public function setTransactionType($value)
54+
{
55+
return $this->setParameter('transactionType', $value);
56+
}
57+
58+
public function getShippingMethod()
59+
{
60+
return $this->getParameter('shippingMethod');
61+
}
62+
63+
public function setShippingMethod($value)
64+
{
65+
return $this->setParameter('shippingMethod', $value);
66+
}
67+
68+
public function getInvoiceReference()
69+
{
70+
return $this->getParameter('invoiceReference');
71+
}
72+
73+
public function setInvoiceReference($value)
74+
{
75+
return $this->setParameter('invoiceReference', $value);
76+
}
77+
78+
protected function getItemData()
79+
{
80+
$itemArray = array();
81+
$items = $this->getItems();
82+
if ($items) {
83+
foreach ($items as $item) {
84+
$data = array();
85+
$data['SKU'] = strval($item->getName());
86+
$data['Description'] = strval($item->getDescription());
87+
$data['Quantity'] = strval($item->getQuantity());
88+
$cost = $this->formatCurrency($item->getPrice());
89+
$data['UnitCost'] = strval($this->getCostInteger($cost));
90+
$itemArray[] = $data;
91+
}
92+
}
93+
94+
return $itemArray;
95+
}
96+
97+
protected function getCostInteger($amount)
98+
{
99+
return (int) round($amount * pow(10, $this->getCurrencyDecimalPlaces()));
100+
}
101+
102+
public function getEndpointBase()
103+
{
104+
return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
105+
}
106+
}

src/Message/RapidPurchaseRequest.php

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,14 @@
22

33
namespace Omnipay\Eway\Message;
44

5-
use Omnipay\Common\Message\AbstractRequest;
6-
75
/**
86
* eWAY Rapid 3.0 Purchase Request
7+
*
8+
* Creates a payment URL using eWAY's Transparent Redirect
9+
* http://api-portal.anypoint.mulesoft.com/eway/api/eway-rapid-31-api/docs/reference/transparent-redirect
910
*/
1011
class RapidPurchaseRequest extends AbstractRequest
1112
{
12-
protected $liveEndpoint = 'https://api.ewaypayments.com';
13-
protected $testEndpoint = 'https://api.sandbox.ewaypayments.com';
14-
15-
public function getApiKey()
16-
{
17-
return $this->getParameter('apiKey');
18-
}
19-
20-
public function setApiKey($value)
21-
{
22-
return $this->setParameter('apiKey', $value);
23-
}
24-
25-
public function getPassword()
26-
{
27-
return $this->getParameter('password');
28-
}
29-
30-
public function setPassword($value)
31-
{
32-
return $this->setParameter('password', $value);
33-
}
3413

3514
public function getData()
3615
{
@@ -41,12 +20,16 @@ public function getData()
4120
$data['DeviceID'] = 'https://github.com/adrianmacneil/omnipay';
4221
$data['CustomerIP'] = $this->getClientIp();
4322
$data['RedirectUrl'] = $this->getReturnUrl();
23+
$data['PartnerID'] = $this->getPartnerId();
24+
$data['TransactionType'] = $this->getTransactionType();
25+
$data['ShippingMethod'] = $this->getShippingMethod();
4426

4527
$data['Payment'] = array();
4628
$data['Payment']['TotalAmount'] = $this->getAmountInteger();
4729
$data['Payment']['InvoiceNumber'] = $this->getTransactionId();
4830
$data['Payment']['InvoiceDescription'] = $this->getDescription();
4931
$data['Payment']['CurrencyCode'] = $this->getCurrency();
32+
$data['Payment']['InvoiceReference'] = $this->getInvoiceReference();
5033

5134
$data['Customer'] = array();
5235
$card = $this->getCard();
@@ -62,6 +45,20 @@ public function getData()
6245
$data['Customer']['Country'] = strtolower($card->getCountry());
6346
$data['Customer']['Email'] = $card->getEmail();
6447
$data['Customer']['Phone'] = $card->getPhone();
48+
49+
$data['ShippingAddress']['FirstName'] = $card->getShippingFirstName();
50+
$data['ShippingAddress']['LastName'] = $card->getShippingLastName();
51+
$data['ShippingAddress']['Street1'] = $card->getShippingAddress1();
52+
$data['ShippingAddress']['Street2'] = $card->getShippingAddress2();
53+
$data['ShippingAddress']['City'] = $card->getShippingCity();
54+
$data['ShippingAddress']['State'] = $card->getShippingState();
55+
$data['ShippingAddress']['Country'] = strtolower($card->getShippingCountry());
56+
$data['ShippingAddress']['PostalCode'] = $card->getShippingPostcode();
57+
$data['ShippingAddress']['Phone'] = $card->getShippingPhone();
58+
}
59+
60+
if ($this->getItems()) {
61+
$data['Items'] = $this->getItemData();
6562
}
6663

6764
return $data;
@@ -80,9 +77,4 @@ public function getEndpoint()
8077
{
8178
return $this->getEndpointBase().'/CreateAccessCode.json';
8279
}
83-
84-
public function getEndpointBase()
85-
{
86-
return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
87-
}
8880
}

tests/Message/AbstractRequestTest.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Omnipay\Eway\Message;
4+
5+
use Mockery as m;
6+
use Omnipay\Tests\TestCase;
7+
8+
class AbstractRequestTest extends TestCase
9+
{
10+
11+
public function setUp()
12+
{
13+
$this->request = m::mock('\Omnipay\Eway\Message\AbstractRequest')->makePartial();
14+
$this->request->initialize();
15+
}
16+
17+
public function testApiKey()
18+
{
19+
$this->assertSame($this->request, $this->request->setApiKey('API KEY'));
20+
$this->assertSame('API KEY', $this->request->getApiKey());
21+
}
22+
23+
public function testPassword()
24+
{
25+
$this->assertSame($this->request, $this->request->setPassword('secret'));
26+
$this->assertSame('secret', $this->request->getPassword());
27+
}
28+
29+
public function testPartnerId()
30+
{
31+
$this->assertSame($this->request, $this->request->setPartnerId('1234'));
32+
$this->assertSame('1234', $this->request->getPartnerId());
33+
}
34+
35+
public function testTransactionType()
36+
{
37+
$this->assertSame($this->request, $this->request->setTransactionType('Purchase'));
38+
$this->assertSame('Purchase', $this->request->getTransactionType());
39+
}
40+
41+
public function testShippingMethod()
42+
{
43+
$this->assertSame($this->request, $this->request->setShippingMethod('NextDay'));
44+
$this->assertSame('NextDay', $this->request->getShippingMethod());
45+
}
46+
47+
public function testInvoiceReference()
48+
{
49+
$this->assertSame($this->request, $this->request->setInvoiceReference('INV-123'));
50+
$this->assertSame('INV-123', $this->request->getInvoiceReference());
51+
}
52+
53+
public function testGetItemData()
54+
{
55+
$this->request->setItems(array(
56+
array('name' => 'Floppy Disk', 'description' => 'MS-DOS', 'quantity' => 2, 'price' => 10),
57+
array('name' => 'CD-ROM', 'description' => 'Windows 95', 'quantity' => 1, 'price' => 40),
58+
));
59+
60+
$data = $this->request->getItemData();
61+
$this->assertSame('Floppy Disk', $data[0]['SKU']);
62+
$this->assertSame('MS-DOS', $data[0]['Description']);
63+
$this->assertSame('2', $data[0]['Quantity']);
64+
$this->assertSame('1000', $data[0]['UnitCost']);
65+
66+
$this->assertSame('CD-ROM', $data[1]['SKU']);
67+
$this->assertSame('Windows 95', $data[1]['Description']);
68+
$this->assertSame('1', $data[1]['Quantity']);
69+
$this->assertSame('4000', $data[1]['UnitCost']);
70+
}
71+
}

tests/Message/RapidPurchaseRequestTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,81 @@ public function testGetData()
2222
$this->request->initialize(array(
2323
'apiKey' => 'my api key',
2424
'password' => 'secret',
25+
'partnerId' => '1234',
26+
'transactionType' => 'Purchase',
27+
'shippingMethod' => 'NextDay',
2528
'amount' => '10.00',
2629
'transactionId' => '999',
2730
'description' => 'new car',
2831
'currency' => 'AUD',
32+
'invoiceReference' => 'INV-123',
2933
'clientIp' => '127.0.0.1',
3034
'returnUrl' => 'https://www.example.com/return',
3135
'card' => array(
3236
'firstName' => 'Patrick',
3337
'lastName' => 'Collison',
38+
'shippingFirstName' => 'John',
39+
'shippingLastName' => 'Smith',
40+
'shippingAddress1' => 'Level 1',
41+
'shippingAddress2' => '123 Test Lane',
42+
'shippingState' => 'NSW',
43+
'shippingCountry' => 'AU',
3444
),
3545
));
3646

3747
$data = $this->request->getData();
3848

3949
$this->assertSame('127.0.0.1', $data['CustomerIP']);
50+
$this->assertSame('1234', $data['PartnerID']);
51+
$this->assertSame('Purchase', $data['TransactionType']);
52+
$this->assertSame('NextDay', $data['ShippingMethod']);
4053
$this->assertSame('https://www.example.com/return', $data['RedirectUrl']);
4154
$this->assertSame(1000, $data['Payment']['TotalAmount']);
4255
$this->assertSame('999', $data['Payment']['InvoiceNumber']);
4356
$this->assertSame('new car', $data['Payment']['InvoiceDescription']);
57+
$this->assertSame('INV-123', $data['Payment']['InvoiceReference']);
4458
$this->assertSame('AUD', $data['Payment']['CurrencyCode']);
4559
$this->assertSame('Patrick', $data['Customer']['FirstName']);
4660
$this->assertSame('Collison', $data['Customer']['LastName']);
61+
$this->assertSame('John', $data['ShippingAddress']['FirstName']);
62+
$this->assertSame('Smith', $data['ShippingAddress']['LastName']);
63+
$this->assertSame('NSW', $data['ShippingAddress']['State']);
64+
$this->assertSame('au', $data['ShippingAddress']['Country']);
65+
}
66+
67+
public function testGetDataWithItems()
68+
{
69+
$this->request->initialize(array(
70+
'apiKey' => 'my api key',
71+
'password' => 'secret',
72+
'amount' => '10.00',
73+
'transactionId' => '999',
74+
'description' => 'new car',
75+
'currency' => 'AUD',
76+
'clientIp' => '127.0.0.1',
77+
'returnUrl' => 'https://www.example.com/return',
78+
'card' => array(
79+
'firstName' => 'Patrick',
80+
'lastName' => 'Collison',
81+
),
82+
));
83+
84+
$this->request->setItems(array(
85+
array('name' => 'Floppy Disk', 'description' => 'MS-DOS', 'quantity' => 2, 'price' => 10),
86+
array('name' => 'CD-ROM', 'description' => 'Windows 95', 'quantity' => 1, 'price' => 40),
87+
));
88+
89+
$data = $this->request->getData();
90+
91+
$this->assertSame('Floppy Disk', $data['Items'][0]['SKU']);
92+
$this->assertSame('MS-DOS', $data['Items'][0]['Description']);
93+
$this->assertSame('2', $data['Items'][0]['Quantity']);
94+
$this->assertSame('1000', $data['Items'][0]['UnitCost']);
95+
96+
$this->assertSame('CD-ROM', $data['Items'][1]['SKU']);
97+
$this->assertSame('Windows 95', $data['Items'][1]['Description']);
98+
$this->assertSame('1', $data['Items'][1]['Quantity']);
99+
$this->assertSame('4000', $data['Items'][1]['UnitCost']);
47100
}
48101

49102
public function testSendSuccess()

0 commit comments

Comments
 (0)