Skip to content

Commit cb0baef

Browse files
committed
Add: Transaction details for Validate and Sell
1 parent dda16e3 commit cb0baef

File tree

9 files changed

+221
-25
lines changed

9 files changed

+221
-25
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Checkdomain\TeleCash\IPG\API\Model;
4+
5+
class TransactionDetails implements ElementInterface
6+
{
7+
/**
8+
* @var string $namespace
9+
*/
10+
private $namespace;
11+
12+
/**
13+
* @var string $comments
14+
*/
15+
private $comments;
16+
17+
/**
18+
* @var string $invoiceNumber
19+
*/
20+
private $invoiceNumber;
21+
22+
/**
23+
* TransactionDetails constructor.
24+
*
25+
* @param string $namespace
26+
* @param string $comments
27+
* @param string|null $invoiceNumber
28+
*/
29+
public function __construct($namespace, $comments, $invoiceNumber = null)
30+
{
31+
$this->namespace = $namespace;
32+
$this->comments = $comments;
33+
$this->invoiceNumber = $invoiceNumber;
34+
}
35+
36+
/**
37+
* @param \DOMDocument $document
38+
*
39+
* @return \DOMElement
40+
*/
41+
public function getXML(\DOMDocument $document)
42+
{
43+
$xml = $document->createElement(sprintf('%s:TransactionDetails', $this->namespace));
44+
45+
$comments = $document->createElement('ns1:Comments');
46+
$comments->textContent = $this->comments;
47+
48+
$xml->appendChild($comments);
49+
50+
if (!empty($this->invoiceNumber)) {
51+
$invoiceNumber = $document->createElement('ns1:InvoiceNumber');
52+
$invoiceNumber->textContent = $this->invoiceNumber;
53+
54+
$xml->appendChild($invoiceNumber);
55+
}
56+
57+
return $xml;
58+
}
59+
}

lib/Checkdomain/TeleCash/IPG/API/Request/Action/Validate.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Checkdomain\TeleCash\IPG\API\Model\CreditCardData;
66
use Checkdomain\TeleCash\IPG\API\Model\Payment;
7+
use Checkdomain\TeleCash\IPG\API\Model\TransactionDetails;
78
use Checkdomain\TeleCash\IPG\API\Request\Action;
89
use Checkdomain\TeleCash\IPG\API\Response\Error;
910
use Checkdomain\TeleCash\IPG\API\Response\Action\Validation;
@@ -19,8 +20,9 @@ class Validate extends Action
1920
* @param OrderService $service
2021
* @param CreditCardData $creditCardData
2122
* @param float $amount
23+
* @param string $text
2224
*/
23-
public function __construct(OrderService $service, CreditCardData $creditCardData, $amount = 1.0)
25+
public function __construct(OrderService $service, CreditCardData $creditCardData, $amount = 1.0, $text = null)
2426
{
2527
parent::__construct($service);
2628

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

39+
if (!empty($text)) {
40+
$transactionDetails = new TransactionDetails('ns2', $text);
41+
$transactionDetailsData = $transactionDetails->getXML($this->document);
42+
$xml->appendChild($transactionDetailsData);
43+
}
44+
3745
$this->element->getElementsByTagName('ns2:Action')->item(0)->appendChild($xml);
3846
}
3947

lib/Checkdomain/TeleCash/IPG/API/Request/Transaction.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ public function __construct(OrderService $service)
2020
$this->element->appendChild($this->document->createElement('ns1:Transaction'));
2121
}
2222

23+
protected function getTransactionElement()
24+
{
25+
return $this->element->getElementsByTagName('ns1:Transaction')->item(0);
26+
}
27+
2328
}

lib/Checkdomain/TeleCash/IPG/API/Request/Transaction/SellHostedData.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Checkdomain\TeleCash\IPG\API\Request\Transaction;
44

55
use Checkdomain\TeleCash\IPG\API\Model\Payment;
6+
use Checkdomain\TeleCash\IPG\API\Model\TransactionDetails;
67
use Checkdomain\TeleCash\IPG\API\Request\Transaction;
78
use Checkdomain\TeleCash\IPG\API\Response\Error;
89
use Checkdomain\TeleCash\IPG\API\Response\Order\Sell;
@@ -15,10 +16,11 @@ class SellHostedData extends Transaction
1516
{
1617

1718
/**
18-
* @param OrderService $service
19-
* @param Payment $payment
19+
* @param OrderService $service
20+
* @param Payment $payment
21+
* @param TransactionDetails|null $transactionDetails
2022
*/
21-
public function __construct(OrderService $service, Payment $payment)
23+
public function __construct(OrderService $service, Payment $payment, TransactionDetails $transactionDetails = null)
2224
{
2325
parent::__construct($service);
2426

@@ -27,8 +29,13 @@ public function __construct(OrderService $service, Payment $payment)
2729
$ccType->nodeValue = 'sale';
2830
$ccTxType->appendChild($ccType);
2931
$paymentData = $payment->getXML($this->document);
30-
$this->element->getElementsByTagName('ns1:Transaction')->item(0)->appendChild($ccTxType);
31-
$this->element->getElementsByTagName('ns1:Transaction')->item(0)->appendChild($paymentData);
32+
$this->getTransactionElement()->appendChild($ccTxType);
33+
$this->getTransactionElement()->appendChild($paymentData);
34+
35+
if (null !== $transactionDetails) {
36+
$transactionDetailsData = $transactionDetails->getXML($this->document);
37+
$this->getTransactionElement()->appendChild($transactionDetailsData);
38+
}
3239
}
3340

3441
/**

lib/Checkdomain/TeleCash/TeleCash.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,19 @@ public function setDebugMode($debug)
6666
* @param string $ccNumber
6767
* @param string $ccValid
6868
* @param float $amount
69+
* @param string $text
6970
*
7071
* @return Response\Action\Validation|Response\Error
7172
* @throws \Exception
7273
*/
73-
public function validate($ccNumber, $ccValid, $amount = 1.0)
74+
public function validate($ccNumber, $ccValid, $amount = 1.0, $text = null)
7475
{
7576
$service = $this->getService();
7677

7778
$validMonth = substr($ccValid, 0, 2);
7879
$validYear = substr($ccValid, 3, 4);
7980
$ccData = new Model\CreditCardData($ccNumber, $validMonth, $validYear);
80-
$validateAction = new Request\Action\Validate($service, $ccData, $amount);
81+
$validateAction = new Request\Action\Validate($service, $ccData, $amount, $text);
8182

8283
return $validateAction->validate();
8384
}
@@ -162,18 +163,25 @@ public function deleteHostedData($hostedDataId)
162163
/**
163164
* Make a sale using a previously stored credit card information
164165
*
165-
* @param string $hostedDataId
166-
* @param float $amount
166+
* @param string $hostedDataId
167+
* @param float $amount
168+
* @param string|null $comments
169+
* @param string|null $invoiceNumber
167170
*
168171
* @return Response\Order\Sell|Response\Error
169172
* @throws \Exception
170173
*/
171-
public function sellUsingHostedData($hostedDataId, $amount)
174+
public function sellUsingHostedData($hostedDataId, $amount, $comments = null, $invoiceNumber = null)
172175
{
173176
$service = $this->getService();
174177

175-
$payment = new Model\Payment($hostedDataId, $amount);
176-
$sellAction = new Request\Transaction\SellHostedData($service, $payment);
178+
$payment = new Model\Payment($hostedDataId, $amount);
179+
if (!empty($comments) || !empty($invoiceNumber)) {
180+
$transactionDetails = new Model\TransactionDetails('ns1', $comments, $invoiceNumber);
181+
} else {
182+
$transactionDetails = null;
183+
}
184+
$sellAction = new Request\Transaction\SellHostedData($service, $payment, $transactionDetails);
177185

178186
return $sellAction->sell();
179187
}

tests/Checkdomain/TeleCash/IPG/API/Model/PaymentTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,21 @@ public function testXMLGeneration($hostedDataId, $amount)
3232
$children[$child->nodeName] = $child->nodeValue;
3333
}
3434

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

3842
if ($amount !== null) {
3943
$this->assertArrayHasKey('ns1:ChargeTotal', $children, 'Expected element ChargeTotal not found');
4044
$this->assertEquals($amount, $children['ns1:ChargeTotal'], 'Charge total did not match');
4145
$this->assertArrayHasKey('ns1:Currency', $children, 'Expected element Currency not found');
4246
$this->assertEquals('978', $children['ns1:Currency'], 'Currency did not match');
47+
} else {
48+
$this->assertArrayNotHasKey('ns1:ChargeTotal', $children, 'Unexpected element ChargeTotal was found');
49+
$this->assertArrayNotHasKey('ns1:Currency', $children, 'Unexpected element Currency was found');
4350
}
4451
}
4552

@@ -52,7 +59,8 @@ public function dataProvider()
5259
{
5360
return [
5461
['abc-def', null],
55-
['abc-def', 1.23]
62+
['abc-def', 1.23],
63+
[null, 1.23]
5664
];
5765
}
5866
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Checkdomain\TeleCash\IPG\API\Model;
4+
5+
/**
6+
* Test case for Payment
7+
*
8+
* @package Checkdomain\TeleCash\IPG\API\Model
9+
*/
10+
class TransactionDetailsTest extends \PHPUnit_Framework_TestCase
11+
{
12+
13+
/**
14+
* @param string $comments
15+
* @param string $invoiceNumber
16+
*
17+
* @dataProvider dataProvider
18+
*/
19+
public function testXMLGeneration($comments, $invoiceNumber)
20+
{
21+
$ccData = new TransactionDetails($comments, $invoiceNumber);
22+
$document = new \DOMDocument('1.0', 'UTF-8');
23+
$xml = $ccData->getXML($document);
24+
$document->appendChild($xml);
25+
26+
$elementPayment = $document->getElementsByTagName('ns2:TransactionDetails');
27+
$this->assertEquals(1, $elementPayment->length, 'Expected element TransactionDetails not found');
28+
29+
$children = [];
30+
/** @var \DOMNode $child */
31+
foreach ($elementPayment->item(0)->childNodes as $child) {
32+
$children[$child->nodeName] = $child->nodeValue;
33+
}
34+
35+
$this->assertArrayHasKey('ns1:Comments', $children, 'Expected element Comments not found');
36+
$this->assertEquals($comments, $children['ns1:Comments'], 'Comments data id did not match');
37+
38+
if ($invoiceNumber !== null) {
39+
$this->assertArrayHasKey('ns1:InvoiceNumber', $children, 'Expected element InvoiceNumber not found');
40+
$this->assertEquals($invoiceNumber, $children['ns1:InvoiceNumber'], 'InvoiceNumber did not match');
41+
} else {
42+
$this->assertArrayNotHasKey('ns1:InvoiceNumber', $children, 'Unexpected element InvoiceNumber was found');
43+
}
44+
}
45+
46+
/**
47+
* Provides some test values
48+
*
49+
* @return array
50+
*/
51+
public function dataProvider()
52+
{
53+
return [
54+
['Testkommentar', null],
55+
['Testkommentar', '1234-TestTestTest']
56+
];
57+
}
58+
}

tests/Checkdomain/TeleCash/IPG/API/Request/Action/ValidateTest.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ class ValidateTest extends \PHPUnit_Framework_TestCase
1212

1313
/**
1414
* @param CreditCardData $ccData
15+
* @param float $amount
16+
* @param string $text
1517
*
1618
* @dataProvider dataProvider
1719
*/
18-
public function testXMLGeneration($ccData)
20+
public function testXMLGeneration($ccData, $amount, $text)
1921
{
2022
$prophet = new Prophet();
2123
$orderService = $prophet->prophesize('Checkdomain\TeleCash\IPG\API\Service\OrderService');
2224

23-
$validate = new Validate($orderService->reveal(), $ccData);
25+
$validate = new Validate($orderService->reveal(), $ccData, $amount, $text);
2426
$document = $validate->getDocument();
2527
$document->appendChild($validate->getElement());
2628

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

3638
$this->assertArrayHasKey('ns2:CreditCardData', $children, 'Expected element CreditCardData not found');
37-
//no need to further test CreditCardData, as this is already covered in CreditCardDataTest
39+
40+
if ($amount !== 1.0) {
41+
$this->assertArrayHasKey('ns1:Payment', $children, 'Expected element Payment not found');
42+
43+
$elementPayment = $document->getElementsByTagName('ns1:Payment');
44+
$paymentChildren = [];
45+
/** @var \DOMNode $child */
46+
foreach ($elementPayment->item(0)->childNodes as $child) {
47+
$paymentChildren[$child->nodeName] = $child->nodeValue;
48+
}
49+
50+
$this->assertEquals($amount, $paymentChildren['ns1:ChargeTotal'], 'Amount did not match');
51+
} else {
52+
$this->assertArrayNotHasKey('ns1:Payment', $children, 'Unexpected element Payment was found');
53+
}
54+
55+
if ($text !== null) {
56+
$this->assertArrayHasKey('ns2:TransactionDetails', $children, 'Expected element TransactionDetails not found');
57+
58+
$elementDetails = $document->getElementsByTagName('ns2:TransactionDetails');
59+
$detailsChildren = [];
60+
/** @var \DOMNode $child */
61+
foreach ($elementDetails->item(0)->childNodes as $child) {
62+
$detailsChildren[$child->nodeName] = $child->nodeValue;
63+
}
64+
$this->assertEquals($text, $detailsChildren['ns1:Comments'], 'Comments did not match');
65+
} else {
66+
$this->assertArrayNotHasKey('ns2:TransactionDetails', $children, 'Unexpected element TransactionDetails was found');
67+
}
3868
}
3969

4070
/**
@@ -45,7 +75,10 @@ public function testXMLGeneration($ccData)
4575
public function dataProvider()
4676
{
4777
return [
48-
[new CreditCardData('12345678901234', '01', '00')]
78+
[new CreditCardData('12345678901234', '01', '00'), 1.0, null],
79+
[new CreditCardData('12345678901234', '01', '00'), 3.0, null],
80+
[new CreditCardData('12345678901234', '01', '00'), 1.0, 'Testkommentar'],
81+
[new CreditCardData('12345678901234', '01', '00'), 3.0, 'Testkommentar'],
4982
];
5083
}
5184
}

0 commit comments

Comments
 (0)