Skip to content

Commit

Permalink
Created processor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ibrahim Gunduz committed May 3, 2018
1 parent 97e29fb commit 578f2b4
Show file tree
Hide file tree
Showing 34 changed files with 1,329 additions and 134 deletions.
99 changes: 0 additions & 99 deletions src/Event/TransactionEvent.php

This file was deleted.

46 changes: 18 additions & 28 deletions src/Pos/AbstractPos.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Paranoia\Exception\CommunicationError;
use Paranoia\Request;
use Paranoia\TransactionType;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

abstract class AbstractPos
{
Expand All @@ -15,6 +16,8 @@ abstract class AbstractPos
*/
protected $configuration;

/** @var EventDispatcherInterface */
private $dispatcher;

public function __construct(AbstractConfiguration $configuration)
{
Expand Down Expand Up @@ -68,17 +71,22 @@ protected function sendRequest($url, $data, $options = null)
}
}

private function performTransaction(Request $request, $transactionType)
{
$rawRequest = $this->buildRequest($request, $transactionType);
$rawResponse = $this->sendRequest($this->configuration->getApiUrl(), $rawRequest);
$response = $this->parseResponse($rawResponse, $transactionType);
return $response;
}

/**
* @param \Paranoia\Request $request
*
* @return \Paranoia\Response\PaymentResponse
*/
public function preAuthorization(Request $request)
{
$rawRequest = $this->buildRequest($request, TransactionType::PRE_AUTHORIZATION);
$rawResponse = $this->sendRequest($this->configuration->getApiUrl(), $rawRequest);
$response = $this->parseResponse($rawResponse, TransactionType::PRE_AUTHORIZATION);
return $response;
return $this->performTransaction($request, TransactionType::PRE_AUTHORIZATION);
}

/**
Expand All @@ -88,10 +96,7 @@ public function preAuthorization(Request $request)
*/
public function postAuthorization(Request $request)
{
$rawRequest = $this->buildRequest($request, TransactionType::POST_AUTHORIZATION);
$rawResponse = $this->sendRequest($this->configuration->getApiUrl(), $rawRequest);
$response = $this->parseResponse($rawResponse, TransactionType::POST_AUTHORIZATION);
return $response;
return $this->performTransaction($request, TransactionType::POST_AUTHORIZATION);
}

/**
Expand All @@ -101,10 +106,7 @@ public function postAuthorization(Request $request)
*/
public function sale(Request $request)
{
$rawRequest = $this->buildRequest($request, TransactionType::SALE);
$rawResponse = $this->sendRequest($this->configuration->getApiUrl(), $rawRequest);
$response = $this->parseResponse($rawResponse, TransactionType::SALE);
return $response;
return $this->performTransaction($request, TransactionType::SALE);
}

/**
Expand All @@ -114,10 +116,7 @@ public function sale(Request $request)
*/
public function refund(Request $request)
{
$rawRequest = $this->buildRequest($request, TransactionType::REFUND);
$rawResponse = $this->sendRequest($this->configuration->getApiUrl(), $rawRequest);
$response = $this->parseResponse($rawResponse, TransactionType::REFUND);
return $response;
return $this->performTransaction($request, TransactionType::REFUND);
}

/**
Expand All @@ -127,10 +126,7 @@ public function refund(Request $request)
*/
public function cancel(Request $request)
{
$rawRequest = $this->buildRequest($request, TransactionType::CANCEL);
$rawResponse = $this->sendRequest($this->configuration->getApiUrl(), $rawRequest);
$response = $this->parseResponse($rawResponse, TransactionType::CANCEL);
return $response;
return $this->performTransaction($request, TransactionType::CANCEL);
}

/**
Expand All @@ -140,10 +136,7 @@ public function cancel(Request $request)
*/
public function pointQuery(Request $request)
{
$rawRequest = $this->buildRequest($request, TransactionType::POINT_INQUIRY);
$rawResponse = $this->sendRequest($this->configuration->getApiUrl(), $rawRequest);
$response = $this->parseResponse($rawResponse, TransactionType::POINT_INQUIRY);
return $response;
return $this->performTransaction($request, TransactionType::POINT_INQUIRY);
}

/**
Expand All @@ -153,9 +146,6 @@ public function pointQuery(Request $request)
*/
public function pointUsage(Request $request)
{
$rawRequest = $this->buildRequest($request, TransactionType::POINT_USAGE);
$rawResponse = $this->sendRequest($this->configuration->getApiUrl(), $rawRequest);
$response = $this->parseResponse($rawResponse, TransactionType::POINT_USAGE);
return $response;
return $this->performTransaction($request, TransactionType::POINT_USAGE);
}
}
17 changes: 14 additions & 3 deletions src/Processor/Gvp/BaseResponseProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,19 @@ private function prepareErrorDetails(\SimpleXMLElement $xml, PaymentResponse $re
*/
private function prepareTransactionDetails(\SimpleXMLElement $xml, PaymentResponse $response)
{
$response->setOrderId((string)$xml->Order->OrderID);
$response->setTransactionId((string)$xml->Transaction->RetrefNum);
if (property_exists($xml, 'Order') && property_exists($xml->Order, 'OrderID')) {
$response->setOrderId((string)$xml->Order->OrderID);
}

if (property_exists($xml, 'Transaction')) {
if (property_exists($xml->Transaction, 'RetrefNum')) {
$response->setTransactionId((string)$xml->Transaction->RetrefNum);
}

if (property_exists($xml->Transaction, 'AuthCode')) {
$response->setAuthCode((string)$xml->Transaction->AuthCode);
}
}
}

/**
Expand All @@ -57,7 +68,7 @@ protected function processCommonResponse($rawResponse)
}
$this->validateResponse($xml);
$response->setIsSuccess('00' == (string)$xml->Transaction->Response->Code);
$response->setResponseCode((string)$xml->Transaction->ReasonCode);
$response->setResponseCode((string)$xml->Transaction->Response->ReasonCode);
if (!$response->isSuccess()) {
$this->prepareErrorDetails($xml, $response);
} else {
Expand Down
7 changes: 3 additions & 4 deletions src/Processor/Posnet/BaseResponseProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ abstract class BaseResponseProcessor extends AbstractResponseProcessor
*/
private function prepareErrorDetails(\SimpleXMLElement $xml, PaymentResponse $response)
{
$response->setResponseCode((string)$xml->respCode);
$errorMessages = array();
if (property_exists($xml, 'respCode')) {
$errorMessages[] = sprintf('Error: %s', (string)$xml->respCode);
$response->setResponseCode((string)$xml->respCode);
}
if (property_exists($xml, 'respText')) {
$errorMessages[] = sprintf('Error Message: %s ', (string)$xml->respText);
$errorMessages[] = sprintf('%s ', (string)$xml->respText);
}
$errorMessage = implode(' ', $errorMessages);
$response->setResponseMessage($errorMessage);
Expand All @@ -36,7 +35,7 @@ private function prepareTransactionDetails(\SimpleXMLElement $xml, PaymentRespon
}
$response->setTransactionId((string)$xml->hostlogkey);
if (property_exists($xml, 'authCode')) {
$response->setOrderId((string)$xml->authCode);
$response->setAuthCode((string)$xml->authCode);
}
}

Expand Down
71 changes: 71 additions & 0 deletions tests/Processor/Gvp/CancelResponseProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
namespace Paranoia\Test\Processor\Gvp;

use Paranoia\Configuration\AbstractConfiguration;
use Paranoia\Exception\BadResponseException;
use Paranoia\Processor\Gvp\CancelResponseProcessor;
use Paranoia\Response\PaymentResponse;
use PHPUnit\Framework\TestCase;

class CancelResponseProcessorTest extends TestCase
{
public function test_success_response()
{
$rawResponse = file_get_contents(
__DIR__ . '/../../samples/response/gvp/cancel_successful.xml'
);

/** @var AbstractConfiguration $configuration */
$configuration = $this->getMockBuilder(AbstractConfiguration::class)->getMock();
$processor = new CancelResponseProcessor($configuration);
$response = $processor->process($rawResponse);
$this->assertInstanceOf(PaymentResponse::class, $response);
$this->assertEquals(true, $response->isSuccess());
$this->assertEquals('311616674771', $response->getTransactionId());
$this->assertEquals('489787', $response->getAuthCode());
$this->assertEquals('1476', $response->getOrderId());
}

public function test_failed_response()
{
$rawResponse = file_get_contents(
__DIR__ . '/../../samples/response/gvp/cancel_failed.xml'
);

/** @var AbstractConfiguration $configuration */
$configuration = $this->getMockBuilder(AbstractConfiguration::class)->getMock();
$processor = new CancelResponseProcessor($configuration);
$response = $processor->process($rawResponse);
$this->assertInstanceOf(PaymentResponse::class, $response);
$this->assertEquals(false, $response->isSuccess());
$this->assertEquals(null, $response->getTransactionId());
$this->assertEquals(null, $response->getOrderId());
$this->assertEquals('0202', $response->getResponseCode());
$this->assertEquals('Error Message: ›ptal edebileceiniz birden fazla i˛lem var, RRN bilgisi gonderi System Error Message: ErrorId: 0202', $response->getResponseMessage());
}

/**
* @dataProvider badResponses
* @param string $rawResponse
*/
public function test_bad_response($rawResponse)
{
/** @var AbstractConfiguration $configuration */
$configuration = $this->getMockBuilder(AbstractConfiguration::class)->getMock();
$processor = new CancelResponseProcessor($configuration);

$this->expectException(BadResponseException::class);
$processor->process($rawResponse);
}

public function badResponses()
{
return [
[null],
[''],
['DUMMY'],
[1],
];
}

}
Loading

0 comments on commit 578f2b4

Please sign in to comment.