diff --git a/lib/Checkdomain/TeleCash/IPG/API/Model/TransactionDetails.php b/lib/Checkdomain/TeleCash/IPG/API/Model/TransactionDetails.php new file mode 100644 index 0000000..58fa710 --- /dev/null +++ b/lib/Checkdomain/TeleCash/IPG/API/Model/TransactionDetails.php @@ -0,0 +1,59 @@ +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; + } +} diff --git a/lib/Checkdomain/TeleCash/IPG/API/Request/Action/Validate.php b/lib/Checkdomain/TeleCash/IPG/API/Request/Action/Validate.php index 9fb57d9..9a3554a 100644 --- a/lib/Checkdomain/TeleCash/IPG/API/Request/Action/Validate.php +++ b/lib/Checkdomain/TeleCash/IPG/API/Request/Action/Validate.php @@ -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; @@ -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); @@ -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); } diff --git a/lib/Checkdomain/TeleCash/IPG/API/Request/Transaction.php b/lib/Checkdomain/TeleCash/IPG/API/Request/Transaction.php index b1de52d..a239b09 100644 --- a/lib/Checkdomain/TeleCash/IPG/API/Request/Transaction.php +++ b/lib/Checkdomain/TeleCash/IPG/API/Request/Transaction.php @@ -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); + } + } diff --git a/lib/Checkdomain/TeleCash/IPG/API/Request/Transaction/SellHostedData.php b/lib/Checkdomain/TeleCash/IPG/API/Request/Transaction/SellHostedData.php index d9251f8..ed91c01 100644 --- a/lib/Checkdomain/TeleCash/IPG/API/Request/Transaction/SellHostedData.php +++ b/lib/Checkdomain/TeleCash/IPG/API/Request/Transaction/SellHostedData.php @@ -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; @@ -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); @@ -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); + } } /** diff --git a/lib/Checkdomain/TeleCash/TeleCash.php b/lib/Checkdomain/TeleCash/TeleCash.php index fa10be7..f552b75 100644 --- a/lib/Checkdomain/TeleCash/TeleCash.php +++ b/lib/Checkdomain/TeleCash/TeleCash.php @@ -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(); } @@ -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(); } diff --git a/tests/Checkdomain/TeleCash/IPG/API/Model/PaymentTest.php b/tests/Checkdomain/TeleCash/IPG/API/Model/PaymentTest.php index 35e3909..96d9a6b 100644 --- a/tests/Checkdomain/TeleCash/IPG/API/Model/PaymentTest.php +++ b/tests/Checkdomain/TeleCash/IPG/API/Model/PaymentTest.php @@ -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'); } } @@ -52,7 +59,8 @@ public function dataProvider() { return [ ['abc-def', null], - ['abc-def', 1.23] + ['abc-def', 1.23], + [null, 1.23] ]; } } diff --git a/tests/Checkdomain/TeleCash/IPG/API/Model/TransactionDetailsTest.php b/tests/Checkdomain/TeleCash/IPG/API/Model/TransactionDetailsTest.php new file mode 100644 index 0000000..e43b9f7 --- /dev/null +++ b/tests/Checkdomain/TeleCash/IPG/API/Model/TransactionDetailsTest.php @@ -0,0 +1,58 @@ +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'] + ]; + } +} diff --git a/tests/Checkdomain/TeleCash/IPG/API/Request/Action/ValidateTest.php b/tests/Checkdomain/TeleCash/IPG/API/Request/Action/ValidateTest.php index 6eb8ab0..95cdd23 100644 --- a/tests/Checkdomain/TeleCash/IPG/API/Request/Action/ValidateTest.php +++ b/tests/Checkdomain/TeleCash/IPG/API/Request/Action/ValidateTest.php @@ -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()); @@ -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'); + } } /** @@ -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'], ]; } } diff --git a/tests/Checkdomain/TeleCash/IPG/API/Request/Transaction/SellHostedDataTest.php b/tests/Checkdomain/TeleCash/IPG/API/Request/Transaction/SellHostedDataTest.php index ec76f1e..869396e 100644 --- a/tests/Checkdomain/TeleCash/IPG/API/Request/Transaction/SellHostedDataTest.php +++ b/tests/Checkdomain/TeleCash/IPG/API/Request/Transaction/SellHostedDataTest.php @@ -2,6 +2,7 @@ namespace Checkdomain\TeleCash\IPG\API\Request\Action; use Checkdomain\TeleCash\IPG\API\Model\Payment; +use Checkdomain\TeleCash\IPG\API\Model\TransactionDetails; use Checkdomain\TeleCash\IPG\API\Request\Transaction\SellHostedData; use Prophecy\Prophet; @@ -12,16 +13,17 @@ class SellHostedDataTest extends \PHPUnit_Framework_TestCase { /** - * @param Payment $payment + * @param Payment $payment + * @param TransactionDetails $transactionDetails * * @dataProvider dataProvider */ - public function testXMLGeneration($payment) + public function testXMLGeneration($payment, $transactionDetails) { $prophet = new Prophet(); $orderService = $prophet->prophesize('Checkdomain\TeleCash\IPG\API\Service\OrderService'); - $sellHosted = new SellHostedData($orderService->reveal(), $payment); + $sellHosted = new SellHostedData($orderService->reveal(), $payment, $transactionDetails); $document = $sellHosted->getDocument(); $document->appendChild($sellHosted->getElement()); @@ -39,7 +41,14 @@ public function testXMLGeneration($payment) $elementPayment = $document->getElementsByTagName('ns1:Payment'); $this->assertEquals(1, $elementPayment->length, 'Expected element Payment not found'); - //no need to further test Payment, as this is already covered in PaymentTest + + if ($transactionDetails !== null) { + $elementDetails = $document->getElementsByTagName('ns2:TransactionDetails'); + $this->assertEquals(1, $elementDetails->length, 'Expected element TransactionDetails not found'); + } else { + $elementDetails = $document->getElementsByTagName('ns2:TransactionDetails'); + $this->assertEquals(0, $elementDetails->length, 'Unexpected element TransactionDetails was found'); + } } /** @@ -50,7 +59,8 @@ public function testXMLGeneration($payment) public function dataProvider() { return [ - [new Payment('abc-def')] + [new Payment('abc-def'), null], + [new Payment('abc-def'), new TransactionDetails('Testkommentar', '1234-TestTestTest')], ]; } }