Skip to content

Commit

Permalink
Merge pull request #4 from CaswellWC/master
Browse files Browse the repository at this point in the history
Implemented Refund, Void, and Capture methods plus added testing
  • Loading branch information
aperdomo authored Mar 7, 2017
2 parents 71526db + 5ade4ce commit 2716e50
Show file tree
Hide file tree
Showing 16 changed files with 732 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
composer.lock
composer.phar
phpunit.xml
/tests/Mock/myCredentials.json
30 changes: 30 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,36 @@ public function purchase(array $parameters = array())
return $this->createRequest('\Omnipay\Beanstream\Message\PurchaseRequest', $parameters);
}

/**
* @param array $parameters
*
* @return \Omnipay\Beanstream\Message\RefundRequest
*/
public function refund(array $parameters = array())
{
return $this->createRequest('\Omnipay\Beanstream\Message\RefundRequest', $parameters);
}

/**
* @param array $parameters
*
* @return \Omnipay\Beanstream\Message\VoidReqeust
*/
public function void(array $parameters = array())
{
return $this->createRequest('\Omnipay\Beanstream\Message\VoidRequest', $parameters);
}

/**
* @param array $parameters
*
* @return \Omnipay\Beanstream\Message\CaptureRequest
*/
public function capture(array $parameters = array())
{
return $this->createRequest('\Omnipay\Beanstream\Message\CaptureRequest', $parameters);
}

/**
* @param array $parameters
* @return \Omnipay\Beanstream\Message\CreateProfileRequest
Expand Down
27 changes: 27 additions & 0 deletions src/Message/CaptureRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Omnipay\Beanstream\Message;

/**
* Class CaptureRequest
*
* The data for this is the same as an authorization except the endpoint references the original transaction id and
* complete is set to true on the card.
*
* @package Omnipay\Beanstream\Message
*/
class CaptureRequest extends AuthorizeRequest
{
/** @var bool Overwrites that same value in the AuthorizeRequest */
protected $complete = true;

/**
* Create the endpoint for a capture AKA a completion in Beanstream
*
* @return string
*/
public function getEndpoint()
{
return $this->endpoint . '/payments/' . $this->getTransactionReference() . '/completions';
}
}
35 changes: 35 additions & 0 deletions src/Message/RefundRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Omnipay\Beanstream\Message;

/**
* Class RefundRequest
*
* @package Omnipay\Beanstream\Message
*/
class RefundRequest extends AbstractRequest
{

/**
* Get the data for a refund
*/
public function getData()
{
$this->validate('amount', 'transactionReference');

return array(
'amount'=>$this->getAmount(),
'order_number'=>$this->getOrderNumber()
);
}

/**
* Get the endpoint for a Refund. This is overwriting the method so we can add the transaction reference dynamically
*
* @return string
*/
public function getEndpoint()
{
return $this->endpoint . '/payments/' . $this->getTransactionReference() . '/returns';
}
}
36 changes: 36 additions & 0 deletions src/Message/VoidRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Omnipay\Beanstream\Message;

/**
* Class VoidRequest
*
* @package Omnipay\Beanstream\Message
*/
class VoidRequest extends AbstractRequest
{

/**
* Get the data necessary for a Void
*
* @return array
*/
public function getData()
{
$this->validate('amount', 'transactionReference');

return array(
'amount'=>$this->getAmount()
);
}

/**
* Get the endpoint for a Void. This is overwriting the method so we can add the transaction reference dynamically
*
* @return string
*/
public function getEndpoint()
{
return $this->endpoint . '/payments/' . $this->getTransactionReference() . '/void';
}
}
57 changes: 57 additions & 0 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

class GatewayTest extends GatewayTestCase
{
/** @var Gateway */
protected $gateway;

public function setUp()
{
parent::setUp();
Expand Down Expand Up @@ -181,4 +184,58 @@ public function testDeleteProfileCard()
$this->assertSame(2, $request->getCardId());
$this->assertSame('DELETE', $request->getHttpMethod());
}

/**
* Test the creation of a RefundRequest object
*/
public function testRefund()
{
$request = $this->gateway->refund(
array(
'transactionReference'=>100,
'amount'=> 10.00
)
);
$this->assertInstanceOf('Omnipay\Beanstream\Message\RefundRequest', $request);
$this->assertSame(100, $request->getTransactionReference());
$this->assertSame('10.00', $request->getAmount());
$this->assertSame('POST', $request->getHttpMethod());
$this->assertSame('https://www.beanstream.com/api/v1/payments/100/returns', $request->getEndpoint());
}

/**
* Test the creation of a VoidRequest object
*/
public function testVoid()
{
$request = $this->gateway->void(
array(
'transactionReference'=>100,
'amount'=> 10.00
)
);
$this->assertInstanceOf('Omnipay\Beanstream\Message\VoidRequest', $request);
$this->assertSame(100, $request->getTransactionReference());
$this->assertSame('10.00', $request->getAmount());
$this->assertSame('POST', $request->getHttpMethod());
$this->assertSame('https://www.beanstream.com/api/v1/payments/100/void', $request->getEndpoint());
}

/**
* Test the creation of a CaptureRequest object
*/
public function testCapture()
{
$request = $this->gateway->capture(
array(
'transactionReference'=>100,
'amount'=>10.00
)
);
$this->assertInstanceOf('Omnipay\Beanstream\Message\CaptureRequest', $request);
$this->assertSame(100, $request->getTransactionReference());
$this->assertSame('10.00', $request->getAmount());
$this->assertSame('POST', $request->getHttpMethod());
$this->assertSame('https://www.beanstream.com/api/v1/payments/100/completions', $request->getEndpoint());
}
}
165 changes: 165 additions & 0 deletions tests/IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

namespace Omnipay\Beanstream\tests;

use Omnipay\Beanstream\Gateway;
use Omnipay\Tests\GatewayTestCase;

/**
* Class IntegrationTest
*
* This is an integration test class so it actually sends messages to Beanstream. This means you will need to setup a
* test account with them and get your Merchant ID and API Passcode. Once you have those, you can create a new file in
* the Mock folder called myCredentials.json and format it as below:
*
* {
* "merchantId":"<Your Merchant ID here>",
* "apiPasscode":"<Your Passcode here>"
* }
*
* If that file does not exist or is not formatted in this way, all tests in this class will be skipped.
*
* @package Omnipay\Beanstream\tests
*/
class IntegrationTest extends GatewayTestCase
{
/** @var Gateway */
protected $gateway;

/**
* Check for the credentials file. Skips the test if the credentials file is missing or not setup correctly. Otherwise,
* instantiates the gateway and sets up the credentials.
*/
public function setUp()
{
$merchantId = '';
$apiPasscode = '';
$credentialsFilePath = dirname(__FILE__) . '/Mock/myCredentials.json';

if(file_exists($credentialsFilePath)) {
$credentialsJson = file_get_contents($credentialsFilePath);
if($credentialsJson) {
$credentials = json_decode($credentialsJson);
$merchantId = $credentials->merchantId;
$apiPasscode = $credentials->apiPasscode;
}
}

if(empty($merchantId) || empty($apiPasscode)) {
$this->markTestSkipped();
} else {
$this->gateway = new Gateway();
$this->gateway->setMerchantId($merchantId);
$this->gateway->setApiPasscode($apiPasscode);
}
}

/**
* Test an Authorize call followed by a capture call for that transaction
*/
public function testAuthCapture()
{
$card = $this->getValidCard();
$card['number'] = '4030000010001234';
$card['cvv'] = '123';
$authResponse = $this->gateway->authorize(
array(
'amount'=>10.00,
'card'=>$card,
'payment_method'=>'card'
)
)->send();
$this->assertTrue($authResponse->isSuccessful());
$this->assertSame('Approved', $authResponse->getMessage());

$captureResponse = $this->gateway->capture(
array(
'transactionReference'=>$authResponse->getTransactionReference(),
'amount'=>10.00
)
)->send();

$this->assertTrue($captureResponse->isSuccessful());
$this->assertSame('Approved', $captureResponse->getMessage());
}

/**
* Test a failed purchase transaction. The card number used below is a special one for Beanstream that always declines.
*/
public function testFailedPurchase()
{
$card = $this->getValidCard();
$card['number'] = '4003050500040005';
$card['cvv'] = '123';
$purchaseResponse = $this->gateway->purchase(
array(
'amount'=>10.00,
'card'=>$card,
'payment_method'=>'card'
)
)->send();

$this->assertFalse($purchaseResponse->isSuccessful());
$this->assertSame('DECLINE', $purchaseResponse->getMessage());
}

/**
* Test a purchase call followed by a refund call for that purchase
*/
public function testPurchaseRefund()
{
$card = $this->getValidCard();
$card['number'] = '4030000010001234';
$card['cvv'] = '123';
$purchaseResponse = $this->gateway->purchase(
array(
'amount'=>20.00,
'card'=>$card,
'payment_method'=>'card'
)
)->send();

$this->assertTrue($purchaseResponse->isSuccessful());
$this->assertSame('Approved', $purchaseResponse->getMessage());

$refundResponse = $this->gateway->refund(
array(
'amount'=>20.00,
'transactionReference'=>$purchaseResponse->getTransactionReference()
)
)->send();

$this->assertTrue($refundResponse->isSuccessful());
$this->assertSame('Approved', $refundResponse->getMessage());
}

/**
* Test a purchase call followed by a void call for that purchase
*/
public function testPurchaseVoid()
{
$card = $this->getValidCard();
$card['number'] = '4030000010001234';
$card['cvv'] = '123';
$purchaseResponse = $this->gateway->purchase(
array(
'amount'=>20.00,
'card'=>$card,
'payment_method'=>'card'
)
)->send();

$this->assertTrue($purchaseResponse->isSuccessful());
$this->assertSame('Approved', $purchaseResponse->getMessage());

$voidResponse = $this->gateway->void(
array(
'amount'=>20.00,
'transactionReference'=>$purchaseResponse->getTransactionReference()
)
)->send();

$this->assertTrue($voidResponse->isSuccessful());
$this->assertSame('Approved', $voidResponse->getMessage());
}
}
Loading

0 comments on commit 2716e50

Please sign in to comment.