Skip to content

Commit 220997f

Browse files
committed
Functionality improvements
1 parent 1e6d676 commit 220997f

File tree

6 files changed

+127
-31
lines changed

6 files changed

+127
-31
lines changed

src/Omnipay/Paysafecard/Message/AbstractRequest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ public function sendData($data)
140140
);
141141

142142
$httpResponse = $this->httpClient->post($endpoint, $headers, $data)->send();
143-
//var_dump($httpResponse->getRawHeaders());
144-
//var_dump((string)$httpResponse->getBody());
143+
145144
return $this->createResponse($httpResponse->xml());
146145
}
147146

src/Omnipay/Paysafecard/Message/CompletePurchaseRequest.php

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
class CompletePurchaseRequest extends AbstractRequest
1818
{
19+
protected $fetchTransaction;
20+
1921
/**
2022
* {@inheritdoc}
2123
*/
@@ -24,6 +26,13 @@ protected function getMethod()
2426
return 'executeDebit';
2527
}
2628

29+
public function setFetchTransactionRequest(FetchTransactionRequest $request)
30+
{
31+
$this->fetchTransaction = $request;
32+
33+
return $this;
34+
}
35+
2736
/**
2837
* Get the data for this request.
2938
*
@@ -33,18 +42,32 @@ protected function getMethod()
3342
*/
3443
public function getData()
3544
{
45+
if ($transactionId = $this->httpRequest->query->get('mtid')) {
46+
$this->setTransactionId($transactionId);
47+
}
48+
49+
if ($currency = $this->httpRequest->query->get('currency')) {
50+
$this->setCurrency($currency);
51+
}
52+
53+
if ($amount = $this->httpRequest->query->get('amount')) {
54+
$this->setAmount($amount);
55+
}
56+
57+
if ($subId = $this->httpRequest->query->get('subid')) {
58+
$this->setSubId($subId);
59+
}
60+
3661
$this->validate(
3762
'username',
38-
'password'
63+
'password',
64+
'transactionId',
65+
'currency',
66+
'amount'
3967
);
4068

41-
$transactionId = $this->httpRequest->query->get('mtid');
42-
$currency = $this->httpRequest->query->get('currency');
43-
$amount = $this->httpRequest->query->get('amount');
44-
$subId = $this->httpRequest->query->get('subid', '');
45-
46-
if (!$transactionId || !$currency || !$amount) {
47-
throw new InvalidRequestException('Missing query parameter');
69+
if ($this->getDispositionState() !== 'S') {
70+
throw new InvalidRequestException('Transaction state must be "Disposed"');
4871
}
4972

5073
$document = new \DOMDocument('1.0', 'utf-8');
@@ -73,19 +96,19 @@ public function getData()
7396
);
7497

7598
$debit->appendChild(
76-
$document->createElement('urn:mtid', $transactionId)
99+
$document->createElement('urn:mtid', $this->getTransactionId())
77100
);
78101

79102
$debit->appendChild(
80-
$document->createElement('urn:subId', $subId)
103+
$document->createElement('urn:subId', $this->getSubId())
81104
);
82105

83106
$debit->appendChild(
84-
$document->createElement('urn:amount', $amount)
107+
$document->createElement('urn:amount', $this->getAmount())
85108
);
86109

87110
$debit->appendChild(
88-
$document->createElement('urn:currency', $currency)
111+
$document->createElement('urn:currency', $this->getCurrency())
89112
);
90113

91114
$debit->appendChild(
@@ -95,6 +118,25 @@ public function getData()
95118
return $document->saveXML();
96119
}
97120

121+
protected function getDispositionState()
122+
{
123+
if (!$this->fetchTransaction) {
124+
$this->fetchTransaction = new FetchTransactionRequest($this->httpClient, $this->httpRequest);
125+
}
126+
127+
$response = $this->fetchTransaction->initialize(array(
128+
'testMode' => $this->getTestMode(),
129+
'username' => $this->getUsername(),
130+
'password' => $this->getPassword(),
131+
'subId' => $this->getSubId(),
132+
'transactionId' => $this->getTransactionId(),
133+
'currency' => $this->getCurrency(),
134+
'amount' => $this->getAmount(),
135+
))->send();
136+
137+
return $response->getDispositionState();
138+
}
139+
98140
/**
99141
* Create a proper response based on the request.
100142
*

src/Omnipay/Paysafecard/Message/PurchaseRequest.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ protected function getMethod()
2727
/**
2828
* {@inheritdoc}
2929
*/
30-
public function getNotifyUrl()
30+
public function getReturnUrl()
3131
{
32-
$url = Url::createFromUrl($this->getParameter('notifyUrl'));
33-
$url->getQuery()->modify(array(
34-
'mtid' => $this->getTransactionId(),
35-
'subid' => $this->getSubId(),
36-
'amount' => $this->getAmount(),
37-
'currency' => $this->getCurrency(),
38-
));
32+
return $this->modifyUrl($this->getParameter('returnUrl'));
33+
}
3934

40-
return (string) $url;
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function getNotifyUrl()
39+
{
40+
return $this->modifyUrl($this->getParameter('notifyUrl'));
4141
}
4242

4343
/**
@@ -492,6 +492,22 @@ public function getData()
492492
return $document->saveXML();
493493
}
494494

495+
/**
496+
* {@inheritdoc}
497+
*/
498+
protected function modifyUrl($url)
499+
{
500+
$url = Url::createFromUrl($url);
501+
$url->getQuery()->modify(array(
502+
'mtid' => $this->getTransactionId(),
503+
'subid' => $this->getSubId(),
504+
'amount' => $this->getAmount(),
505+
'currency' => $this->getCurrency(),
506+
));
507+
508+
return (string) $url;
509+
}
510+
495511
/**
496512
* Create a proper response based on the request.
497513
*

tests/Omnipay/Paysafecard/Message/CompletePurchaseRequestTest.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Omnipay\Paysafecard\Message;
33

44
use Omnipay\Tests\TestCase;
5+
use Guzzle\Http\Client as HttpClient;
56
use Symfony\Component\HttpFoundation\Request as HttpRequest;
67

78
class CompletePurchaseRequestTest extends TestCase
@@ -12,13 +13,20 @@ public function setUp()
1213
{
1314
parent::setUp();
1415

15-
$httpResponse = $this->getMockHttpResponse('CompletePurchaseSuccess.txt');
16+
$httpCompletePurchaseResponse = $this->getMockHttpResponse('CompletePurchaseSuccess.txt');
17+
$httpFetchTransactionResponse = $this->getMockHttpResponse('FetchTransactionPending.txt');
1618

17-
$mockPlugin = new \Guzzle\Plugin\Mock\MockPlugin();
18-
$mockPlugin->addResponse($httpResponse);
19+
$mockCompletePurchasePlugin = new \Guzzle\Plugin\Mock\MockPlugin();
20+
$mockCompletePurchasePlugin->addResponse($httpCompletePurchaseResponse);
1921

20-
$httpClient = $this->getHttpClient();
21-
$httpClient->addSubscriber($mockPlugin);
22+
$mockFetchTransactionPlugin = new \Guzzle\Plugin\Mock\MockPlugin();
23+
$mockFetchTransactionPlugin->addResponse($httpFetchTransactionResponse);
24+
25+
$httpCompletePurchaseClient = new HttpClient();
26+
$httpCompletePurchaseClient->addSubscriber($mockCompletePurchasePlugin);
27+
28+
$httpFetchTransactionClient = new HttpClient();
29+
$httpFetchTransactionClient->addSubscriber($mockFetchTransactionPlugin);
2230

2331
$httpRequest = new HttpRequest(array(
2432
'mtid' => 'TX9997888',
@@ -27,24 +35,49 @@ public function setUp()
2735
'currency' => 'EUR'
2836
));
2937

30-
$this->request = new CompletePurchaseRequest($httpClient, $httpRequest);
38+
$this->request = new CompletePurchaseRequest($httpCompletePurchaseClient, $httpRequest);
3139
$this->request->initialize(array(
3240
'username' => 'SOAP_USERNAME',
3341
'password' => 'oJ2rHLBVSbD5iGfT'
3442
));
43+
44+
$fetchTransaction = new FetchTransactionRequest($httpFetchTransactionClient, new HttpRequest());
45+
$this->request->setFetchTransactionRequest($fetchTransaction);
3546
}
3647

3748
public function testExceptions()
3849
{
3950
try {
40-
$request = new CompletePurchaseRequest($this->getHttpClient(), new HttpRequest());
51+
$request = new CompletePurchaseRequest(new HttpClient(), new HttpRequest());
4152
$request->initialize(array(
4253
'username' => 'SOAP_USERNAME',
4354
'password' => 'oJ2rHLBVSbD5iGfT'
4455
))->getData();
4556
} catch (\Exception $e) {
4657
$this->assertEquals('Omnipay\Common\Exception\InvalidRequestException', get_class($e));
4758
}
59+
60+
try {
61+
$httpResponse = $this->getMockHttpResponse('FetchTransactionSuccess.txt');
62+
63+
$mockPlugin = new \Guzzle\Plugin\Mock\MockPlugin();
64+
$mockPlugin->addResponse($httpResponse);
65+
66+
$httpClient = new HttpClient();
67+
$httpClient->addSubscriber($mockPlugin);
68+
69+
$request = new CompletePurchaseRequest($httpClient, new HttpRequest());
70+
$request->initialize(array(
71+
'username' => 'SOAP_USERNAME',
72+
'password' => 'oJ2rHLBVSbD5iGfT',
73+
'transactionId' => 'TX9997888',
74+
'SubId' => 'shop1',
75+
'amount' => '1.00',
76+
'currency' => 'EUR'
77+
))->getData();
78+
} catch (\Exception $e) {
79+
$this->assertEquals('Omnipay\Common\Exception\InvalidRequestException', get_class($e));
80+
}
4881
}
4982

5083
public function testGetData()

tests/Omnipay/Paysafecard/Message/PurchaseRequestTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function testGetData()
5858
$this->assertSame('shop1', (string) $request->subId);
5959
$this->assertSame('14.65', (string) $request->amount);
6060
$this->assertSame('EUR', (string) $request->currency);
61-
$this->assertSame('https%3A%2F%2Fwww.foodstore.com%2Fsuccess', (string) $request->okUrl);
61+
$this->assertSame('https%3A%2F%2Fwww.foodstore.com%2Fsuccess%3Fmtid%3DTX9997888%26subid%3Dshop1%26amount%3D14.65%26currency%3DEUR', (string) $request->okUrl);
6262
$this->assertSame('https%3A%2F%2Fwww.foodstore.com%2Ffailure', (string) $request->nokUrl);
6363
$this->assertSame('https%3A%2F%2Fwww.foodstore.com%2Fnotify%3Fmtid%3DTX9997888%26subid%3Dshop1%26amount%3D14.65%26currency%3DEUR', (string) $request->pnUrl);
6464
$this->assertSame('2568-B415rh_785', (string) $request->shopId);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
HTTP/1.1 200 OK
2+
Date: Wed, 12 Aug 2015 12:00:00 GMT
3+
Content-Type: text/xml;charset=UTF-8
4+
Set-Cookie: TS036f01c9=012c3040cf2c0e94b44b9c98fb5cc371a856b67e8318446febba39668e5327f8c7de429b75; Path=/
5+
6+
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:pscservice"><soap:Body><ns1:getSerialNumbersResponse><ns1:getSerialNumbersReturn><ns1:mtid>TX9997889</ns1:mtid><ns1:subId>shop1</ns1:subId><ns1:resultCode>0</ns1:resultCode><ns1:errorCode>0</ns1:errorCode><ns1:amount>1.0</ns1:amount><ns1:currency>EUR</ns1:currency><ns1:dispositionState>S</ns1:dispositionState><ns1:serialNumbers>9922921184073520;1.00</ns1:serialNumbers></ns1:getSerialNumbersReturn></ns1:getSerialNumbersResponse></soap:Body></soap:Envelope>

0 commit comments

Comments
 (0)