Skip to content

Commit 2716e50

Browse files
authored
Merge pull request #4 from CaswellWC/master
Implemented Refund, Void, and Capture methods plus added testing
2 parents 71526db + 5ade4ce commit 2716e50

16 files changed

+732
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
composer.lock
33
composer.phar
44
phpunit.xml
5+
/tests/Mock/myCredentials.json

src/Gateway.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,36 @@ public function purchase(array $parameters = array())
8282
return $this->createRequest('\Omnipay\Beanstream\Message\PurchaseRequest', $parameters);
8383
}
8484

85+
/**
86+
* @param array $parameters
87+
*
88+
* @return \Omnipay\Beanstream\Message\RefundRequest
89+
*/
90+
public function refund(array $parameters = array())
91+
{
92+
return $this->createRequest('\Omnipay\Beanstream\Message\RefundRequest', $parameters);
93+
}
94+
95+
/**
96+
* @param array $parameters
97+
*
98+
* @return \Omnipay\Beanstream\Message\VoidReqeust
99+
*/
100+
public function void(array $parameters = array())
101+
{
102+
return $this->createRequest('\Omnipay\Beanstream\Message\VoidRequest', $parameters);
103+
}
104+
105+
/**
106+
* @param array $parameters
107+
*
108+
* @return \Omnipay\Beanstream\Message\CaptureRequest
109+
*/
110+
public function capture(array $parameters = array())
111+
{
112+
return $this->createRequest('\Omnipay\Beanstream\Message\CaptureRequest', $parameters);
113+
}
114+
85115
/**
86116
* @param array $parameters
87117
* @return \Omnipay\Beanstream\Message\CreateProfileRequest

src/Message/CaptureRequest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Omnipay\Beanstream\Message;
4+
5+
/**
6+
* Class CaptureRequest
7+
*
8+
* The data for this is the same as an authorization except the endpoint references the original transaction id and
9+
* complete is set to true on the card.
10+
*
11+
* @package Omnipay\Beanstream\Message
12+
*/
13+
class CaptureRequest extends AuthorizeRequest
14+
{
15+
/** @var bool Overwrites that same value in the AuthorizeRequest */
16+
protected $complete = true;
17+
18+
/**
19+
* Create the endpoint for a capture AKA a completion in Beanstream
20+
*
21+
* @return string
22+
*/
23+
public function getEndpoint()
24+
{
25+
return $this->endpoint . '/payments/' . $this->getTransactionReference() . '/completions';
26+
}
27+
}

src/Message/RefundRequest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Omnipay\Beanstream\Message;
4+
5+
/**
6+
* Class RefundRequest
7+
*
8+
* @package Omnipay\Beanstream\Message
9+
*/
10+
class RefundRequest extends AbstractRequest
11+
{
12+
13+
/**
14+
* Get the data for a refund
15+
*/
16+
public function getData()
17+
{
18+
$this->validate('amount', 'transactionReference');
19+
20+
return array(
21+
'amount'=>$this->getAmount(),
22+
'order_number'=>$this->getOrderNumber()
23+
);
24+
}
25+
26+
/**
27+
* Get the endpoint for a Refund. This is overwriting the method so we can add the transaction reference dynamically
28+
*
29+
* @return string
30+
*/
31+
public function getEndpoint()
32+
{
33+
return $this->endpoint . '/payments/' . $this->getTransactionReference() . '/returns';
34+
}
35+
}

src/Message/VoidRequest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Omnipay\Beanstream\Message;
4+
5+
/**
6+
* Class VoidRequest
7+
*
8+
* @package Omnipay\Beanstream\Message
9+
*/
10+
class VoidRequest extends AbstractRequest
11+
{
12+
13+
/**
14+
* Get the data necessary for a Void
15+
*
16+
* @return array
17+
*/
18+
public function getData()
19+
{
20+
$this->validate('amount', 'transactionReference');
21+
22+
return array(
23+
'amount'=>$this->getAmount()
24+
);
25+
}
26+
27+
/**
28+
* Get the endpoint for a Void. This is overwriting the method so we can add the transaction reference dynamically
29+
*
30+
* @return string
31+
*/
32+
public function getEndpoint()
33+
{
34+
return $this->endpoint . '/payments/' . $this->getTransactionReference() . '/void';
35+
}
36+
}

tests/GatewayTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
class GatewayTest extends GatewayTestCase
66
{
7+
/** @var Gateway */
8+
protected $gateway;
9+
710
public function setUp()
811
{
912
parent::setUp();
@@ -181,4 +184,58 @@ public function testDeleteProfileCard()
181184
$this->assertSame(2, $request->getCardId());
182185
$this->assertSame('DELETE', $request->getHttpMethod());
183186
}
187+
188+
/**
189+
* Test the creation of a RefundRequest object
190+
*/
191+
public function testRefund()
192+
{
193+
$request = $this->gateway->refund(
194+
array(
195+
'transactionReference'=>100,
196+
'amount'=> 10.00
197+
)
198+
);
199+
$this->assertInstanceOf('Omnipay\Beanstream\Message\RefundRequest', $request);
200+
$this->assertSame(100, $request->getTransactionReference());
201+
$this->assertSame('10.00', $request->getAmount());
202+
$this->assertSame('POST', $request->getHttpMethod());
203+
$this->assertSame('https://www.beanstream.com/api/v1/payments/100/returns', $request->getEndpoint());
204+
}
205+
206+
/**
207+
* Test the creation of a VoidRequest object
208+
*/
209+
public function testVoid()
210+
{
211+
$request = $this->gateway->void(
212+
array(
213+
'transactionReference'=>100,
214+
'amount'=> 10.00
215+
)
216+
);
217+
$this->assertInstanceOf('Omnipay\Beanstream\Message\VoidRequest', $request);
218+
$this->assertSame(100, $request->getTransactionReference());
219+
$this->assertSame('10.00', $request->getAmount());
220+
$this->assertSame('POST', $request->getHttpMethod());
221+
$this->assertSame('https://www.beanstream.com/api/v1/payments/100/void', $request->getEndpoint());
222+
}
223+
224+
/**
225+
* Test the creation of a CaptureRequest object
226+
*/
227+
public function testCapture()
228+
{
229+
$request = $this->gateway->capture(
230+
array(
231+
'transactionReference'=>100,
232+
'amount'=>10.00
233+
)
234+
);
235+
$this->assertInstanceOf('Omnipay\Beanstream\Message\CaptureRequest', $request);
236+
$this->assertSame(100, $request->getTransactionReference());
237+
$this->assertSame('10.00', $request->getAmount());
238+
$this->assertSame('POST', $request->getHttpMethod());
239+
$this->assertSame('https://www.beanstream.com/api/v1/payments/100/completions', $request->getEndpoint());
240+
}
184241
}

tests/IntegrationTest.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
3+
namespace Omnipay\Beanstream\tests;
4+
5+
use Omnipay\Beanstream\Gateway;
6+
use Omnipay\Tests\GatewayTestCase;
7+
8+
/**
9+
* Class IntegrationTest
10+
*
11+
* This is an integration test class so it actually sends messages to Beanstream. This means you will need to setup a
12+
* test account with them and get your Merchant ID and API Passcode. Once you have those, you can create a new file in
13+
* the Mock folder called myCredentials.json and format it as below:
14+
*
15+
* {
16+
* "merchantId":"<Your Merchant ID here>",
17+
* "apiPasscode":"<Your Passcode here>"
18+
* }
19+
*
20+
* If that file does not exist or is not formatted in this way, all tests in this class will be skipped.
21+
*
22+
* @package Omnipay\Beanstream\tests
23+
*/
24+
class IntegrationTest extends GatewayTestCase
25+
{
26+
/** @var Gateway */
27+
protected $gateway;
28+
29+
/**
30+
* Check for the credentials file. Skips the test if the credentials file is missing or not setup correctly. Otherwise,
31+
* instantiates the gateway and sets up the credentials.
32+
*/
33+
public function setUp()
34+
{
35+
$merchantId = '';
36+
$apiPasscode = '';
37+
$credentialsFilePath = dirname(__FILE__) . '/Mock/myCredentials.json';
38+
39+
if(file_exists($credentialsFilePath)) {
40+
$credentialsJson = file_get_contents($credentialsFilePath);
41+
if($credentialsJson) {
42+
$credentials = json_decode($credentialsJson);
43+
$merchantId = $credentials->merchantId;
44+
$apiPasscode = $credentials->apiPasscode;
45+
}
46+
}
47+
48+
if(empty($merchantId) || empty($apiPasscode)) {
49+
$this->markTestSkipped();
50+
} else {
51+
$this->gateway = new Gateway();
52+
$this->gateway->setMerchantId($merchantId);
53+
$this->gateway->setApiPasscode($apiPasscode);
54+
}
55+
}
56+
57+
/**
58+
* Test an Authorize call followed by a capture call for that transaction
59+
*/
60+
public function testAuthCapture()
61+
{
62+
$card = $this->getValidCard();
63+
$card['number'] = '4030000010001234';
64+
$card['cvv'] = '123';
65+
$authResponse = $this->gateway->authorize(
66+
array(
67+
'amount'=>10.00,
68+
'card'=>$card,
69+
'payment_method'=>'card'
70+
)
71+
)->send();
72+
$this->assertTrue($authResponse->isSuccessful());
73+
$this->assertSame('Approved', $authResponse->getMessage());
74+
75+
$captureResponse = $this->gateway->capture(
76+
array(
77+
'transactionReference'=>$authResponse->getTransactionReference(),
78+
'amount'=>10.00
79+
)
80+
)->send();
81+
82+
$this->assertTrue($captureResponse->isSuccessful());
83+
$this->assertSame('Approved', $captureResponse->getMessage());
84+
}
85+
86+
/**
87+
* Test a failed purchase transaction. The card number used below is a special one for Beanstream that always declines.
88+
*/
89+
public function testFailedPurchase()
90+
{
91+
$card = $this->getValidCard();
92+
$card['number'] = '4003050500040005';
93+
$card['cvv'] = '123';
94+
$purchaseResponse = $this->gateway->purchase(
95+
array(
96+
'amount'=>10.00,
97+
'card'=>$card,
98+
'payment_method'=>'card'
99+
)
100+
)->send();
101+
102+
$this->assertFalse($purchaseResponse->isSuccessful());
103+
$this->assertSame('DECLINE', $purchaseResponse->getMessage());
104+
}
105+
106+
/**
107+
* Test a purchase call followed by a refund call for that purchase
108+
*/
109+
public function testPurchaseRefund()
110+
{
111+
$card = $this->getValidCard();
112+
$card['number'] = '4030000010001234';
113+
$card['cvv'] = '123';
114+
$purchaseResponse = $this->gateway->purchase(
115+
array(
116+
'amount'=>20.00,
117+
'card'=>$card,
118+
'payment_method'=>'card'
119+
)
120+
)->send();
121+
122+
$this->assertTrue($purchaseResponse->isSuccessful());
123+
$this->assertSame('Approved', $purchaseResponse->getMessage());
124+
125+
$refundResponse = $this->gateway->refund(
126+
array(
127+
'amount'=>20.00,
128+
'transactionReference'=>$purchaseResponse->getTransactionReference()
129+
)
130+
)->send();
131+
132+
$this->assertTrue($refundResponse->isSuccessful());
133+
$this->assertSame('Approved', $refundResponse->getMessage());
134+
}
135+
136+
/**
137+
* Test a purchase call followed by a void call for that purchase
138+
*/
139+
public function testPurchaseVoid()
140+
{
141+
$card = $this->getValidCard();
142+
$card['number'] = '4030000010001234';
143+
$card['cvv'] = '123';
144+
$purchaseResponse = $this->gateway->purchase(
145+
array(
146+
'amount'=>20.00,
147+
'card'=>$card,
148+
'payment_method'=>'card'
149+
)
150+
)->send();
151+
152+
$this->assertTrue($purchaseResponse->isSuccessful());
153+
$this->assertSame('Approved', $purchaseResponse->getMessage());
154+
155+
$voidResponse = $this->gateway->void(
156+
array(
157+
'amount'=>20.00,
158+
'transactionReference'=>$purchaseResponse->getTransactionReference()
159+
)
160+
)->send();
161+
162+
$this->assertTrue($voidResponse->isSuccessful());
163+
$this->assertSame('Approved', $voidResponse->getMessage());
164+
}
165+
}

0 commit comments

Comments
 (0)