Skip to content

Commit

Permalink
Merge pull request #1 from ravloony/master
Browse files Browse the repository at this point in the history
add refund capability and referenced purchase.
  • Loading branch information
greydnls committed Sep 21, 2014
2 parents 74f4656 + 083c7dc commit 368fb2f
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,18 @@ public function purchase(array $parameters = array())
return $this->createRequest('\Omnipay\CardSave\Message\PurchaseRequest', $parameters);
}

public function referencedPurchase(array $parameters = array())
{
return $this->createRequest('\Omnipay\CardSave\Message\ReferencedPurchaseRequest', $parameters);
}

public function completePurchase(array $parameters = array())
{
return $this->createRequest('\Omnipay\CardSave\Message\CompletePurchaseRequest', $parameters);
}

public function refund(array $parameters = array())
{
return $this->createRequest('\Omnipay\CardSave\Message\RefundRequest', $parameters);
}
}
11 changes: 11 additions & 0 deletions src/Message/ReferencedPurchaseRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Omnipay\CardSave\Message;

/**
* CardSave Purchase Request
*/
class ReferencedPurchaseRequest extends RefundRequest
{
public $transactionType = 'SALE';
}
38 changes: 38 additions & 0 deletions src/Message/RefundRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Omnipay\CardSave\Message;

use DOMDocument;
use SimpleXMLElement;

/**
* CardSave Purchase Request
*/
class RefundRequest extends PurchaseRequest
{
public $transactionType = 'REFUND';

public function getData()
{
$this->validate('transactionReference', 'amount', 'currency');

$data = new SimpleXMLElement('<CrossReferenceTransaction/>');
$data->addAttribute('xmlns', $this->namespace);

$data->PaymentMessage->MerchantAuthentication['MerchantID'] = $this->getMerchantId();
$data->PaymentMessage->MerchantAuthentication['Password'] = $this->getPassword();
$data->PaymentMessage->TransactionDetails['Amount'] = $this->getAmountInteger();
$data->PaymentMessage->TransactionDetails['CurrencyCode'] = $this->getCurrencyNumeric();
$data->PaymentMessage->TransactionDetails->OrderID = $this->getTransactionId();
$data->PaymentMessage->TransactionDetails->OrderDescription = $this->getDescription();
$data->PaymentMessage->TransactionDetails->MessageDetails['TransactionType'] = $this->transactionType;
$data->PaymentMessage->TransactionDetails->MessageDetails['NewTransaction'] = false;
$data->PaymentMessage->TransactionDetails->MessageDetails['CrossReference'] = $this->getTransactionReference();

// requires numeric country code
// $data->PaymentMessage->CustomerDetails->BillingAddress->CountryCode = $this->getCard()->getCountryNumeric;
$data->PaymentMessage->CustomerDetails->CustomerIPAddress = $this->getClientIp();

return $data;
}
}
37 changes: 37 additions & 0 deletions tests/Message/ReferencedPurchaseRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Omnipay\CardSave\Message;

use Omnipay\Tests\TestCase;

class ReferencedPurchaseRequestTest extends TestCase
{
public function setUp()
{
parent::setUp();

$this->request = new ReferencedPurchaseRequest($this->getHttpClient(), $this->getHttpRequest());
$this->request->initialize(
array(
'amount' => '12.00',
'transactionReference' => '0987654345678900987654',
'currency' => 'GBP',
'testMode' => true,
)
);
}

public function testGetData()
{
$data = $this->request->getData();

/*
* See https://bugs.php.net/bug.php?id=29500 for why this is cast to string
*/
$this->assertSame('SALE', (string)$data->PaymentMessage->TransactionDetails->MessageDetails['TransactionType']);
$this->assertSame('1200', (string)$data->PaymentMessage->TransactionDetails['Amount']);
$this->assertSame('826', (string)$data->PaymentMessage->TransactionDetails['CurrencyCode']);
$this->assertSame('0987654345678900987654', (string)$data->PaymentMessage->TransactionDetails->MessageDetails['CrossReference']);
}

}
37 changes: 37 additions & 0 deletions tests/Message/RefundRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Omnipay\CardSave\Message;

use Omnipay\Tests\TestCase;

class RefundRequestTest extends TestCase
{
public function setUp()
{
parent::setUp();

$this->request = new RefundRequest($this->getHttpClient(), $this->getHttpRequest());
$this->request->initialize(
array(
'amount' => '12.00',
'transactionReference' => '0987654345678900987654',
'currency' => 'GBP',
'testMode' => true,
)
);
}

public function testGetData()
{
$data = $this->request->getData();

/*
* See https://bugs.php.net/bug.php?id=29500 for why this is cast to string
*/
$this->assertSame('REFUND', (string)$data->PaymentMessage->TransactionDetails->MessageDetails['TransactionType']);
$this->assertSame('1200', (string)$data->PaymentMessage->TransactionDetails['Amount']);
$this->assertSame('826', (string)$data->PaymentMessage->TransactionDetails['CurrencyCode']);
$this->assertSame('0987654345678900987654', (string)$data->PaymentMessage->TransactionDetails->MessageDetails['CrossReference']);
}

}

0 comments on commit 368fb2f

Please sign in to comment.