Skip to content

Commit

Permalink
Functionality improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dercoder committed Sep 2, 2015
1 parent 1e6d676 commit 220997f
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 31 deletions.
3 changes: 1 addition & 2 deletions src/Omnipay/Paysafecard/Message/AbstractRequest.php
Expand Up @@ -140,8 +140,7 @@ public function sendData($data)
);

$httpResponse = $this->httpClient->post($endpoint, $headers, $data)->send();
//var_dump($httpResponse->getRawHeaders());
//var_dump((string)$httpResponse->getBody());

return $this->createResponse($httpResponse->xml());
}

Expand Down
66 changes: 54 additions & 12 deletions src/Omnipay/Paysafecard/Message/CompletePurchaseRequest.php
Expand Up @@ -16,6 +16,8 @@
*/
class CompletePurchaseRequest extends AbstractRequest
{
protected $fetchTransaction;

/**
* {@inheritdoc}
*/
Expand All @@ -24,6 +26,13 @@ protected function getMethod()
return 'executeDebit';
}

public function setFetchTransactionRequest(FetchTransactionRequest $request)
{
$this->fetchTransaction = $request;

return $this;
}

/**
* Get the data for this request.
*
Expand All @@ -33,18 +42,32 @@ protected function getMethod()
*/
public function getData()
{
if ($transactionId = $this->httpRequest->query->get('mtid')) {
$this->setTransactionId($transactionId);
}

if ($currency = $this->httpRequest->query->get('currency')) {
$this->setCurrency($currency);
}

if ($amount = $this->httpRequest->query->get('amount')) {
$this->setAmount($amount);
}

if ($subId = $this->httpRequest->query->get('subid')) {
$this->setSubId($subId);
}

$this->validate(
'username',
'password'
'password',
'transactionId',
'currency',
'amount'
);

$transactionId = $this->httpRequest->query->get('mtid');
$currency = $this->httpRequest->query->get('currency');
$amount = $this->httpRequest->query->get('amount');
$subId = $this->httpRequest->query->get('subid', '');

if (!$transactionId || !$currency || !$amount) {
throw new InvalidRequestException('Missing query parameter');
if ($this->getDispositionState() !== 'S') {
throw new InvalidRequestException('Transaction state must be "Disposed"');
}

$document = new \DOMDocument('1.0', 'utf-8');
Expand Down Expand Up @@ -73,19 +96,19 @@ public function getData()
);

$debit->appendChild(
$document->createElement('urn:mtid', $transactionId)
$document->createElement('urn:mtid', $this->getTransactionId())
);

$debit->appendChild(
$document->createElement('urn:subId', $subId)
$document->createElement('urn:subId', $this->getSubId())
);

$debit->appendChild(
$document->createElement('urn:amount', $amount)
$document->createElement('urn:amount', $this->getAmount())
);

$debit->appendChild(
$document->createElement('urn:currency', $currency)
$document->createElement('urn:currency', $this->getCurrency())
);

$debit->appendChild(
Expand All @@ -95,6 +118,25 @@ public function getData()
return $document->saveXML();
}

protected function getDispositionState()
{
if (!$this->fetchTransaction) {
$this->fetchTransaction = new FetchTransactionRequest($this->httpClient, $this->httpRequest);
}

$response = $this->fetchTransaction->initialize(array(
'testMode' => $this->getTestMode(),
'username' => $this->getUsername(),
'password' => $this->getPassword(),
'subId' => $this->getSubId(),
'transactionId' => $this->getTransactionId(),
'currency' => $this->getCurrency(),
'amount' => $this->getAmount(),
))->send();

return $response->getDispositionState();
}

/**
* Create a proper response based on the request.
*
Expand Down
34 changes: 25 additions & 9 deletions src/Omnipay/Paysafecard/Message/PurchaseRequest.php
Expand Up @@ -27,17 +27,17 @@ protected function getMethod()
/**
* {@inheritdoc}
*/
public function getNotifyUrl()
public function getReturnUrl()
{
$url = Url::createFromUrl($this->getParameter('notifyUrl'));
$url->getQuery()->modify(array(
'mtid' => $this->getTransactionId(),
'subid' => $this->getSubId(),
'amount' => $this->getAmount(),
'currency' => $this->getCurrency(),
));
return $this->modifyUrl($this->getParameter('returnUrl'));
}

return (string) $url;
/**
* {@inheritdoc}
*/
public function getNotifyUrl()
{
return $this->modifyUrl($this->getParameter('notifyUrl'));
}

/**
Expand Down Expand Up @@ -492,6 +492,22 @@ public function getData()
return $document->saveXML();
}

/**
* {@inheritdoc}
*/
protected function modifyUrl($url)
{
$url = Url::createFromUrl($url);
$url->getQuery()->modify(array(
'mtid' => $this->getTransactionId(),
'subid' => $this->getSubId(),
'amount' => $this->getAmount(),
'currency' => $this->getCurrency(),
));

return (string) $url;
}

/**
* Create a proper response based on the request.
*
Expand Down
47 changes: 40 additions & 7 deletions tests/Omnipay/Paysafecard/Message/CompletePurchaseRequestTest.php
Expand Up @@ -2,6 +2,7 @@
namespace Omnipay\Paysafecard\Message;

use Omnipay\Tests\TestCase;
use Guzzle\Http\Client as HttpClient;
use Symfony\Component\HttpFoundation\Request as HttpRequest;

class CompletePurchaseRequestTest extends TestCase
Expand All @@ -12,13 +13,20 @@ public function setUp()
{
parent::setUp();

$httpResponse = $this->getMockHttpResponse('CompletePurchaseSuccess.txt');
$httpCompletePurchaseResponse = $this->getMockHttpResponse('CompletePurchaseSuccess.txt');
$httpFetchTransactionResponse = $this->getMockHttpResponse('FetchTransactionPending.txt');

$mockPlugin = new \Guzzle\Plugin\Mock\MockPlugin();
$mockPlugin->addResponse($httpResponse);
$mockCompletePurchasePlugin = new \Guzzle\Plugin\Mock\MockPlugin();
$mockCompletePurchasePlugin->addResponse($httpCompletePurchaseResponse);

$httpClient = $this->getHttpClient();
$httpClient->addSubscriber($mockPlugin);
$mockFetchTransactionPlugin = new \Guzzle\Plugin\Mock\MockPlugin();
$mockFetchTransactionPlugin->addResponse($httpFetchTransactionResponse);

$httpCompletePurchaseClient = new HttpClient();
$httpCompletePurchaseClient->addSubscriber($mockCompletePurchasePlugin);

$httpFetchTransactionClient = new HttpClient();
$httpFetchTransactionClient->addSubscriber($mockFetchTransactionPlugin);

$httpRequest = new HttpRequest(array(
'mtid' => 'TX9997888',
Expand All @@ -27,24 +35,49 @@ public function setUp()
'currency' => 'EUR'
));

$this->request = new CompletePurchaseRequest($httpClient, $httpRequest);
$this->request = new CompletePurchaseRequest($httpCompletePurchaseClient, $httpRequest);
$this->request->initialize(array(
'username' => 'SOAP_USERNAME',
'password' => 'oJ2rHLBVSbD5iGfT'
));

$fetchTransaction = new FetchTransactionRequest($httpFetchTransactionClient, new HttpRequest());
$this->request->setFetchTransactionRequest($fetchTransaction);
}

public function testExceptions()
{
try {
$request = new CompletePurchaseRequest($this->getHttpClient(), new HttpRequest());
$request = new CompletePurchaseRequest(new HttpClient(), new HttpRequest());
$request->initialize(array(
'username' => 'SOAP_USERNAME',
'password' => 'oJ2rHLBVSbD5iGfT'
))->getData();
} catch (\Exception $e) {
$this->assertEquals('Omnipay\Common\Exception\InvalidRequestException', get_class($e));
}

try {
$httpResponse = $this->getMockHttpResponse('FetchTransactionSuccess.txt');

$mockPlugin = new \Guzzle\Plugin\Mock\MockPlugin();
$mockPlugin->addResponse($httpResponse);

$httpClient = new HttpClient();
$httpClient->addSubscriber($mockPlugin);

$request = new CompletePurchaseRequest($httpClient, new HttpRequest());
$request->initialize(array(
'username' => 'SOAP_USERNAME',
'password' => 'oJ2rHLBVSbD5iGfT',
'transactionId' => 'TX9997888',
'SubId' => 'shop1',
'amount' => '1.00',
'currency' => 'EUR'
))->getData();
} catch (\Exception $e) {
$this->assertEquals('Omnipay\Common\Exception\InvalidRequestException', get_class($e));
}
}

public function testGetData()
Expand Down
2 changes: 1 addition & 1 deletion tests/Omnipay/Paysafecard/Message/PurchaseRequestTest.php
Expand Up @@ -58,7 +58,7 @@ public function testGetData()
$this->assertSame('shop1', (string) $request->subId);
$this->assertSame('14.65', (string) $request->amount);
$this->assertSame('EUR', (string) $request->currency);
$this->assertSame('https%3A%2F%2Fwww.foodstore.com%2Fsuccess', (string) $request->okUrl);
$this->assertSame('https%3A%2F%2Fwww.foodstore.com%2Fsuccess%3Fmtid%3DTX9997888%26subid%3Dshop1%26amount%3D14.65%26currency%3DEUR', (string) $request->okUrl);
$this->assertSame('https%3A%2F%2Fwww.foodstore.com%2Ffailure', (string) $request->nokUrl);
$this->assertSame('https%3A%2F%2Fwww.foodstore.com%2Fnotify%3Fmtid%3DTX9997888%26subid%3Dshop1%26amount%3D14.65%26currency%3DEUR', (string) $request->pnUrl);
$this->assertSame('2568-B415rh_785', (string) $request->shopId);
Expand Down
6 changes: 6 additions & 0 deletions tests/Omnipay/Paysafecard/Mock/FetchTransactionPending.txt
@@ -0,0 +1,6 @@
HTTP/1.1 200 OK
Date: Wed, 12 Aug 2015 12:00:00 GMT
Content-Type: text/xml;charset=UTF-8
Set-Cookie: TS036f01c9=012c3040cf2c0e94b44b9c98fb5cc371a856b67e8318446febba39668e5327f8c7de429b75; Path=/

<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 comments on commit 220997f

Please sign in to comment.