Skip to content

Commit

Permalink
Add: Transaction details for Validate and Sell
Browse files Browse the repository at this point in the history
  • Loading branch information
TPete committed Jan 28, 2016
1 parent dda16e3 commit cb0baef
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 25 deletions.
59 changes: 59 additions & 0 deletions lib/Checkdomain/TeleCash/IPG/API/Model/TransactionDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Checkdomain\TeleCash\IPG\API\Model;

class TransactionDetails implements ElementInterface
{
/**
* @var string $namespace
*/
private $namespace;

/**
* @var string $comments
*/
private $comments;

/**
* @var string $invoiceNumber
*/
private $invoiceNumber;

/**
* TransactionDetails constructor.
*
* @param string $namespace
* @param string $comments
* @param string|null $invoiceNumber
*/
public function __construct($namespace, $comments, $invoiceNumber = null)
{
$this->namespace = $namespace;
$this->comments = $comments;
$this->invoiceNumber = $invoiceNumber;
}

/**
* @param \DOMDocument $document
*
* @return \DOMElement
*/
public function getXML(\DOMDocument $document)
{
$xml = $document->createElement(sprintf('%s:TransactionDetails', $this->namespace));

$comments = $document->createElement('ns1:Comments');
$comments->textContent = $this->comments;

$xml->appendChild($comments);

if (!empty($this->invoiceNumber)) {
$invoiceNumber = $document->createElement('ns1:InvoiceNumber');
$invoiceNumber->textContent = $this->invoiceNumber;

$xml->appendChild($invoiceNumber);
}

return $xml;
}
}
10 changes: 9 additions & 1 deletion lib/Checkdomain/TeleCash/IPG/API/Request/Action/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Checkdomain\TeleCash\IPG\API\Model\CreditCardData;
use Checkdomain\TeleCash\IPG\API\Model\Payment;
use Checkdomain\TeleCash\IPG\API\Model\TransactionDetails;
use Checkdomain\TeleCash\IPG\API\Request\Action;
use Checkdomain\TeleCash\IPG\API\Response\Error;
use Checkdomain\TeleCash\IPG\API\Response\Action\Validation;
Expand All @@ -19,8 +20,9 @@ class Validate extends Action
* @param OrderService $service
* @param CreditCardData $creditCardData
* @param float $amount
* @param string $text
*/
public function __construct(OrderService $service, CreditCardData $creditCardData, $amount = 1.0)
public function __construct(OrderService $service, CreditCardData $creditCardData, $amount = 1.0, $text = null)
{
parent::__construct($service);

Expand All @@ -34,6 +36,12 @@ public function __construct(OrderService $service, CreditCardData $creditCardDat
$xml->appendChild($paymentData);
}

if (!empty($text)) {
$transactionDetails = new TransactionDetails('ns2', $text);
$transactionDetailsData = $transactionDetails->getXML($this->document);
$xml->appendChild($transactionDetailsData);
}

$this->element->getElementsByTagName('ns2:Action')->item(0)->appendChild($xml);
}

Expand Down
5 changes: 5 additions & 0 deletions lib/Checkdomain/TeleCash/IPG/API/Request/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public function __construct(OrderService $service)
$this->element->appendChild($this->document->createElement('ns1:Transaction'));
}

protected function getTransactionElement()
{
return $this->element->getElementsByTagName('ns1:Transaction')->item(0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Checkdomain\TeleCash\IPG\API\Request\Transaction;

use Checkdomain\TeleCash\IPG\API\Model\Payment;
use Checkdomain\TeleCash\IPG\API\Model\TransactionDetails;
use Checkdomain\TeleCash\IPG\API\Request\Transaction;
use Checkdomain\TeleCash\IPG\API\Response\Error;
use Checkdomain\TeleCash\IPG\API\Response\Order\Sell;
Expand All @@ -15,10 +16,11 @@ class SellHostedData extends Transaction
{

/**
* @param OrderService $service
* @param Payment $payment
* @param OrderService $service
* @param Payment $payment
* @param TransactionDetails|null $transactionDetails
*/
public function __construct(OrderService $service, Payment $payment)
public function __construct(OrderService $service, Payment $payment, TransactionDetails $transactionDetails = null)
{
parent::__construct($service);

Expand All @@ -27,8 +29,13 @@ public function __construct(OrderService $service, Payment $payment)
$ccType->nodeValue = 'sale';
$ccTxType->appendChild($ccType);
$paymentData = $payment->getXML($this->document);
$this->element->getElementsByTagName('ns1:Transaction')->item(0)->appendChild($ccTxType);
$this->element->getElementsByTagName('ns1:Transaction')->item(0)->appendChild($paymentData);
$this->getTransactionElement()->appendChild($ccTxType);
$this->getTransactionElement()->appendChild($paymentData);

if (null !== $transactionDetails) {
$transactionDetailsData = $transactionDetails->getXML($this->document);
$this->getTransactionElement()->appendChild($transactionDetailsData);
}
}

/**
Expand Down
22 changes: 15 additions & 7 deletions lib/Checkdomain/TeleCash/TeleCash.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,19 @@ public function setDebugMode($debug)
* @param string $ccNumber
* @param string $ccValid
* @param float $amount
* @param string $text
*
* @return Response\Action\Validation|Response\Error
* @throws \Exception
*/
public function validate($ccNumber, $ccValid, $amount = 1.0)
public function validate($ccNumber, $ccValid, $amount = 1.0, $text = null)
{
$service = $this->getService();

$validMonth = substr($ccValid, 0, 2);
$validYear = substr($ccValid, 3, 4);
$ccData = new Model\CreditCardData($ccNumber, $validMonth, $validYear);
$validateAction = new Request\Action\Validate($service, $ccData, $amount);
$validateAction = new Request\Action\Validate($service, $ccData, $amount, $text);

return $validateAction->validate();
}
Expand Down Expand Up @@ -162,18 +163,25 @@ public function deleteHostedData($hostedDataId)
/**
* Make a sale using a previously stored credit card information
*
* @param string $hostedDataId
* @param float $amount
* @param string $hostedDataId
* @param float $amount
* @param string|null $comments
* @param string|null $invoiceNumber
*
* @return Response\Order\Sell|Response\Error
* @throws \Exception
*/
public function sellUsingHostedData($hostedDataId, $amount)
public function sellUsingHostedData($hostedDataId, $amount, $comments = null, $invoiceNumber = null)
{
$service = $this->getService();

$payment = new Model\Payment($hostedDataId, $amount);
$sellAction = new Request\Transaction\SellHostedData($service, $payment);
$payment = new Model\Payment($hostedDataId, $amount);
if (!empty($comments) || !empty($invoiceNumber)) {
$transactionDetails = new Model\TransactionDetails('ns1', $comments, $invoiceNumber);
} else {
$transactionDetails = null;
}
$sellAction = new Request\Transaction\SellHostedData($service, $payment, $transactionDetails);

return $sellAction->sell();
}
Expand Down
14 changes: 11 additions & 3 deletions tests/Checkdomain/TeleCash/IPG/API/Model/PaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@ public function testXMLGeneration($hostedDataId, $amount)
$children[$child->nodeName] = $child->nodeValue;
}

$this->assertArrayHasKey('ns1:HostedDataID', $children, 'Expected element HostedDataID not found');
$this->assertEquals($hostedDataId, $children['ns1:HostedDataID'], 'Hosted data id did not match');
if ($hostedDataId !== null) {
$this->assertArrayHasKey('ns1:HostedDataID', $children, 'Expected element HostedDataID not found');
$this->assertEquals($hostedDataId, $children['ns1:HostedDataID'], 'Hosted data id did not match');
} else {
$this->assertArrayNotHasKey('ns1:HostedDataID', $children, 'Unexpected element HostedDataID was found');
}

if ($amount !== null) {
$this->assertArrayHasKey('ns1:ChargeTotal', $children, 'Expected element ChargeTotal not found');
$this->assertEquals($amount, $children['ns1:ChargeTotal'], 'Charge total did not match');
$this->assertArrayHasKey('ns1:Currency', $children, 'Expected element Currency not found');
$this->assertEquals('978', $children['ns1:Currency'], 'Currency did not match');
} else {
$this->assertArrayNotHasKey('ns1:ChargeTotal', $children, 'Unexpected element ChargeTotal was found');
$this->assertArrayNotHasKey('ns1:Currency', $children, 'Unexpected element Currency was found');
}
}

Expand All @@ -52,7 +59,8 @@ public function dataProvider()
{
return [
['abc-def', null],
['abc-def', 1.23]
['abc-def', 1.23],
[null, 1.23]
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Checkdomain\TeleCash\IPG\API\Model;

/**
* Test case for Payment
*
* @package Checkdomain\TeleCash\IPG\API\Model
*/
class TransactionDetailsTest extends \PHPUnit_Framework_TestCase
{

/**
* @param string $comments
* @param string $invoiceNumber
*
* @dataProvider dataProvider
*/
public function testXMLGeneration($comments, $invoiceNumber)
{
$ccData = new TransactionDetails($comments, $invoiceNumber);
$document = new \DOMDocument('1.0', 'UTF-8');
$xml = $ccData->getXML($document);
$document->appendChild($xml);

$elementPayment = $document->getElementsByTagName('ns2:TransactionDetails');
$this->assertEquals(1, $elementPayment->length, 'Expected element TransactionDetails not found');

$children = [];
/** @var \DOMNode $child */
foreach ($elementPayment->item(0)->childNodes as $child) {
$children[$child->nodeName] = $child->nodeValue;
}

$this->assertArrayHasKey('ns1:Comments', $children, 'Expected element Comments not found');
$this->assertEquals($comments, $children['ns1:Comments'], 'Comments data id did not match');

if ($invoiceNumber !== null) {
$this->assertArrayHasKey('ns1:InvoiceNumber', $children, 'Expected element InvoiceNumber not found');
$this->assertEquals($invoiceNumber, $children['ns1:InvoiceNumber'], 'InvoiceNumber did not match');
} else {
$this->assertArrayNotHasKey('ns1:InvoiceNumber', $children, 'Unexpected element InvoiceNumber was found');
}
}

/**
* Provides some test values
*
* @return array
*/
public function dataProvider()
{
return [
['Testkommentar', null],
['Testkommentar', '1234-TestTestTest']
];
}
}
41 changes: 37 additions & 4 deletions tests/Checkdomain/TeleCash/IPG/API/Request/Action/ValidateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ class ValidateTest extends \PHPUnit_Framework_TestCase

/**
* @param CreditCardData $ccData
* @param float $amount
* @param string $text
*
* @dataProvider dataProvider
*/
public function testXMLGeneration($ccData)
public function testXMLGeneration($ccData, $amount, $text)
{
$prophet = new Prophet();
$orderService = $prophet->prophesize('Checkdomain\TeleCash\IPG\API\Service\OrderService');

$validate = new Validate($orderService->reveal(), $ccData);
$validate = new Validate($orderService->reveal(), $ccData, $amount, $text);
$document = $validate->getDocument();
$document->appendChild($validate->getElement());

Expand All @@ -34,7 +36,35 @@ public function testXMLGeneration($ccData)
}

$this->assertArrayHasKey('ns2:CreditCardData', $children, 'Expected element CreditCardData not found');
//no need to further test CreditCardData, as this is already covered in CreditCardDataTest

if ($amount !== 1.0) {
$this->assertArrayHasKey('ns1:Payment', $children, 'Expected element Payment not found');

$elementPayment = $document->getElementsByTagName('ns1:Payment');
$paymentChildren = [];
/** @var \DOMNode $child */
foreach ($elementPayment->item(0)->childNodes as $child) {
$paymentChildren[$child->nodeName] = $child->nodeValue;
}

$this->assertEquals($amount, $paymentChildren['ns1:ChargeTotal'], 'Amount did not match');
} else {
$this->assertArrayNotHasKey('ns1:Payment', $children, 'Unexpected element Payment was found');
}

if ($text !== null) {
$this->assertArrayHasKey('ns2:TransactionDetails', $children, 'Expected element TransactionDetails not found');

$elementDetails = $document->getElementsByTagName('ns2:TransactionDetails');
$detailsChildren = [];
/** @var \DOMNode $child */
foreach ($elementDetails->item(0)->childNodes as $child) {
$detailsChildren[$child->nodeName] = $child->nodeValue;
}
$this->assertEquals($text, $detailsChildren['ns1:Comments'], 'Comments did not match');
} else {
$this->assertArrayNotHasKey('ns2:TransactionDetails', $children, 'Unexpected element TransactionDetails was found');
}
}

/**
Expand All @@ -45,7 +75,10 @@ public function testXMLGeneration($ccData)
public function dataProvider()
{
return [
[new CreditCardData('12345678901234', '01', '00')]
[new CreditCardData('12345678901234', '01', '00'), 1.0, null],
[new CreditCardData('12345678901234', '01', '00'), 3.0, null],
[new CreditCardData('12345678901234', '01', '00'), 1.0, 'Testkommentar'],
[new CreditCardData('12345678901234', '01', '00'), 3.0, 'Testkommentar'],
];
}
}
Loading

0 comments on commit cb0baef

Please sign in to comment.