Skip to content

Commit

Permalink
added OrderType to templates, moved templates to separate folder, add…
Browse files Browse the repository at this point in the history
…ed shorthand methods, added initial testing
  • Loading branch information
num8er committed Sep 5, 2019
1 parent 9f2dcc3 commit b5c0958
Show file tree
Hide file tree
Showing 21 changed files with 240 additions and 35 deletions.
14 changes: 11 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "num8er/tranzware-payment-gateway",
"version": "1.1.6",
"version": "1.1.7",
"description": "Library for working with TranzWare Payment Gateway",
"type": "library",
"license": "MIT",
Expand All @@ -12,12 +12,20 @@
"role": "Developer"
}
],
"require": {},
"require": {
"ext-curl": "*",
"ext-json": "*",
"ext-simplexml": "*"
},
"require-dev": {
"phpunit/phpunit": "^8"
},
"autoload": {
"psr-4": {
"num8er\\TranzWarePaymentGateway\\": "src/",
"num8er\\TranzWarePaymentGateway\\Requests\\": "src/requests/",
"num8er\\TranzWarePaymentGateway\\Handlers\\": "src/handlers/"
"num8er\\TranzWarePaymentGateway\\Handlers\\": "src/handlers/",
"tests\\": "tests/"
}
}
}
2 changes: 1 addition & 1 deletion samples/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"num8er/tranzware-payment-gateway": "^1.1.5"
"num8er/tranzware-payment-gateway": "^1.1.7"
}
}
13 changes: 9 additions & 4 deletions samples/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

require_once('vendor/autoload.php');

use num8er\TranzWarePaymentGateway\TranzWarePaymentGatewayRequestFactory;
use num8er\TranzWarePaymentGateway\CurrencyCodes;
use \num8er\TranzWarePaymentGateway\TranzWarePaymentGatewayRequestFactory;
use \num8er\TranzWarePaymentGateway\CurrencyCodes;
use \num8er\TranzWarePaymentGateway\OrderTypes;

$requestFactory = new TranzWarePaymentGatewayRequestFactory(
'https://tranz-ware-payment-gateway/url',
Expand All @@ -22,12 +23,16 @@
$requestFactory->setCertificate($certFile, $keyFile, $keyPass);
$requestFactory->setDebugFile(__DIR__.'/debug.log');

$orderRequest = $requestFactory->createOrderRequest(1, CurrencyCodes::USD, 'TEST PAYMENT $0.01');
$orderRequest = $requestFactory->createOrderRequest(1, CurrencyCodes::USD, 'TEST PAYMENT $0.01', OrderTypes::PURCHASE);
/**
* or shorthand:
* $orderRequest = $requestFactory->createPurchaseOrderRequest(1, CurrencyCodes::USD, 'TEST PAYMENT $0.01');
*/
$orderRequestResult = $orderRequest->execute();
if ($orderRequestResult->success()) {
$orderData = $orderRequestResult->getData();
echo '<a href="'.$orderData['PaymentUrl'].'">LINK TO PAYMENT</a>';
exit(0);
}

echo 'FAILED';
echo 'FAILED';
3 changes: 1 addition & 2 deletions samples/get_status.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require_once('vendor/autoload.php');

use num8er\TranzWarePaymentGateway\TranzWarePaymentGatewayRequestFactory;
use \num8er\TranzWarePaymentGateway\TranzWarePaymentGatewayRequestFactory;

$requestFactory = new TranzWarePaymentGatewayRequestFactory(
'https://tranz-ware-payment-gateway/url',
Expand All @@ -24,4 +24,3 @@
$orderStatusData = $orderStatusRequestResult->getData();

var_dump($orderStatusData);

3 changes: 1 addition & 2 deletions samples/order_approved.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php
require_once('vendor/autoload.php');

use num8er\TranzWarePaymentGateway\TranzWarePaymentGatewayHandlerFactory;
use \num8er\TranzWarePaymentGateway\TranzWarePaymentGatewayHandlerFactory;

$handlerFactory = new TranzWarePaymentGatewayHandlerFactory();
$orderCallbackHandler = $handlerFactory->createOrderCallbackHandler();

$orderStatusData = $orderCallbackHandler->handle();

var_dump($orderStatusData);

3 changes: 1 addition & 2 deletions samples/order_canceled.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php
require_once('vendor/autoload.php');

use num8er\TranzWarePaymentGateway\TranzWarePaymentGatewayHandlerFactory;
use \num8er\TranzWarePaymentGateway\TranzWarePaymentGatewayHandlerFactory;

$handlerFactory = new TranzWarePaymentGatewayHandlerFactory();
$orderCallbackHandler = $handlerFactory->createOrderCallbackHandler();

$orderStatusData = $orderCallbackHandler->handle();

var_dump($orderStatusData);

3 changes: 1 addition & 2 deletions samples/order_declined.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php
require_once('vendor/autoload.php');

use num8er\TranzWarePaymentGateway\TranzWarePaymentGatewayHandlerFactory;
use \num8er\TranzWarePaymentGateway\TranzWarePaymentGatewayHandlerFactory;

$handlerFactory = new TranzWarePaymentGatewayHandlerFactory();
$orderCallbackHandler = $handlerFactory->createOrderCallbackHandler();

$orderStatusData = $orderCallbackHandler->handle();

var_dump($orderStatusData);

43 changes: 43 additions & 0 deletions src/OrderTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace num8er\TranzWarePaymentGateway;

class OrderTypes
{
const PURCHASE = 'Purchase';
const PRE_AUTH = 'PreAuth';

public static function sanitizeValue($value)
{
$value = preg_replace("/[^A-Za-z_]/", '', $value);
$value = trim($value, '_');
if (!$value) return '';

$isSnakeCase = strpos($value, '_') > 0;
if ($isSnakeCase) {
return
implode('',
array_map('ucwords',
array_map('strtolower',
explode('_', $value)
)
)
);
}

$words = implode('_', preg_split('/(?=[A-Z])/', $value));
return self::sanitizeValue($words);
}

public static function isValid($orderType)
{
$allowedTypes = [self::PURCHASE, self::PRE_AUTH];
return in_array($orderType, $allowedTypes);
}

public static function fromString($orderType)
{
$orderType = self::sanitizeValue($orderType);
return self::isValid($orderType) ? $orderType : self::PURCHASE;
}
}
2 changes: 1 addition & 1 deletion src/TranzWarePaymentGatewayHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace num8er\TranzWarePaymentGateway;

use num8er\TranzWarePaymentGateway\Handlers\TranzWarePaymentGatewayOrderCallbackHandler;
use \num8er\TranzWarePaymentGateway\Handlers\TranzWarePaymentGatewayOrderCallbackHandler;

/**
* Class TranzWarePaymentGatewayHandlerFactory
Expand Down
3 changes: 2 additions & 1 deletion src/TranzWarePaymentGatewayHandlerFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace num8er\TranzWarePaymentGateway;
use num8er\TranzWarePaymentGateway\Handlers\TranzWarePaymentGatewayHandlerInterface;

use \num8er\TranzWarePaymentGateway\Handlers\TranzWarePaymentGatewayHandlerInterface;

/**
* Interface TranzWarePaymentGatewayHandlerFactoryInterface
Expand Down
48 changes: 39 additions & 9 deletions src/TranzWarePaymentGatewayRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace num8er\TranzWarePaymentGateway;

use num8er\TranzWarePaymentGateway\Requests\TranzWarePaymentGatewayOrderRequest;
use num8er\TranzWarePaymentGateway\Requests\TranzWarePaymentGatewayOrderStatusRequest;
use \num8er\TranzWarePaymentGateway\Requests\TranzWarePaymentGatewayOrderRequest;
use \num8er\TranzWarePaymentGateway\Requests\TranzWarePaymentGatewayOrderStatusRequest;

/**
* Factory class for creation of request objects
Expand Down Expand Up @@ -39,7 +39,11 @@ class TranzWarePaymentGatewayRequestFactory implements TranzWarePaymentGatewayRe
* @param string $ON_ORDER_CANCELED_URL
* @param string $LANG
*/
public function __construct($GATEWAY_URL, $MERCHANT_ID, $ON_ORDER_APPROVED_URL, $ON_ORDER_DECLINED_URL, $ON_ORDER_CANCELED_URL, $LANG = 'EN')
public function __construct(
$GATEWAY_URL, $MERCHANT_ID,
$ON_ORDER_APPROVED_URL, $ON_ORDER_DECLINED_URL, $ON_ORDER_CANCELED_URL,
$LANG = 'EN'
)
{
$this->MERCHANT_ID = $MERCHANT_ID;
$this->LANG = $LANG;
Expand Down Expand Up @@ -94,31 +98,33 @@ final private function getUrlProvider()
}

protected $debug = false, $debugFile;

/**
* @param string $path_to_file
* @param string $pathToFile
*/
final public function setDebugFile($path_to_file)
final public function setDebugFile($pathToFile)
{
$this->debug = true;
$this->debugFile = $path_to_file;
$this->debugFile = $pathToFile;
}


/**
* @param float $amount
* @param string $currency
* @param string $description
* @param string $orderType
* @param string{OrderTypes::PURCHASE, OrderTypes::PRE_AUTH} $orderType
*
* @return TranzWarePaymentGatewayOrderRequest
*/
final public function createOrderRequest($amount, $currency, $description = '', $orderType = '')
final public function createOrderRequest($amount, $currency, $description = '', $orderType = OrderTypes::PURCHASE)
{
$request = new TranzWarePaymentGatewayOrderRequest(
$this->getUrlProvider()->getGatewayUrl(),
$this->getUrlProvider()->getOnOrderApprovedUrl(),
$this->getUrlProvider()->getOnOrderDeclinedUrl(),
$this->getUrlProvider()->getOnOrderCanceledUrl(),
$orderType,
OrderTypes::fromString($orderType),
$this->MERCHANT_ID,
$amount,
$currency,
Expand All @@ -130,6 +136,30 @@ final public function createOrderRequest($amount, $currency, $description = '',
return $request;
}

/**
* @param float $amount
* @param string $currency
* @param string $description
*
* @return TranzWarePaymentGatewayOrderRequest
*/
final public function createOrderPreAuthRequest($amount, $currency, $description = '')
{
return $this->createOrderRequest($amount, $currency, $description, OrderTypes::PRE_AUTH);
}

/**
* @param float $amount
* @param string $currency
* @param string $description
*
* @return TranzWarePaymentGatewayOrderRequest
*/
final public function createOrderPurchaseRequest($amount, $currency, $description = '')
{
return $this->createOrderRequest($amount, $currency, $description, OrderTypes::PURCHASE);
}

/**
* @param string $orderId
* @param string $sessionId
Expand Down
12 changes: 9 additions & 3 deletions src/TranzWarePaymentGatewayRequestFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace num8er\TranzWarePaymentGateway;
use num8er\TranzWarePaymentGateway\Requests\TranzWarePaymentGatewayRequestInterface;

use \num8er\TranzWarePaymentGateway\Requests\TranzWarePaymentGatewayRequestInterface;

/**
* Interface TranzWarePaymentGatewayRequestFactoryInterface
Expand All @@ -19,7 +20,11 @@ interface TranzWarePaymentGatewayRequestFactoryInterface
* @param string $ON_ORDER_CANCELED_URL
* @param string $LANG
*/
public function __construct($GATEWAY_URL, $MERCHANT_ID, $ON_ORDER_APPROVED_URL, $ON_ORDER_DECLINED_URL, $ON_ORDER_CANCELED_URL, $LANG = 'EN');
public function __construct(
$GATEWAY_URL, $MERCHANT_ID,
$ON_ORDER_APPROVED_URL, $ON_ORDER_DECLINED_URL, $ON_ORDER_CANCELED_URL,
$LANG = 'EN'
);

/**
* Sets verbose mode in requests and file to output
Expand All @@ -33,10 +38,11 @@ public function setDebugFile($path_to_file);
* @param float $amount
* @param string $currency
* @param string $description
* @param string{OrderTypes::PURCHASE, OrderTypes::PRE_AUTH} $orderType
*
* @return TranzWarePaymentGatewayRequestInterface
*/
public function createOrderRequest($amount, $currency, $description = '');
public function createOrderRequest($amount, $currency, $description = '', $orderType = OrderTypes::PURCHASE);

/**
* @param string $orderId
Expand Down
14 changes: 11 additions & 3 deletions src/requests/TranzWarePaymentGatewayOrderRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace num8er\TranzWarePaymentGateway\Requests;

use \num8er\TranzWarePaymentGateway\OrderTypes;

/**
* Class TranzWarePaymentGatewayOrderRequest
* @package num8er\TranzWarePaymentGateway\Requests
Expand All @@ -15,18 +17,22 @@ class TranzWarePaymentGatewayOrderRequest implements TranzWarePaymentGatewayRequ
* TranzWarePaymentGatewayOrderRequest constructor.
*
* @param string $requestUrl
* @param string $orderType
* @param string $approvalUrl
* @param string $declineUrl
* @param string $cancelUrl
* @param string{OrderTypes::PURCHASE, OrderTypes::PRE_AUTH} $orderType
* @param string $merchantId
* @param float $amount
* @param string $currency
* @param string $description
* @param string $lang
* @param string $debugToFile
*/
public function __construct($requestUrl, $approvalUrl, $declineUrl, $cancelUrl, $orderType, $merchantId, $amount, $currency, $description = '', $lang = 'EN', $debugToFile = null)
public function __construct(
$requestUrl, $approvalUrl, $declineUrl, $cancelUrl,
$orderType, $merchantId, $amount, $currency,
$description = '', $lang = 'EN', $debugToFile = null
)
{
$this->requestAttributes =
compact('requestUrl', 'approvalUrl', 'declineUrl', 'cancelUrl', 'orderType', 'merchantId', 'amount', 'currency', 'description', 'lang');
Expand All @@ -50,7 +56,9 @@ public function execute()

final private function getRequestBody()
{
$body = file_get_contents(__DIR__ . '/OrderRequestBodyTemplate.xml');
$orderType = OrderTypes::fromString($this->requestAttributes['orderType']);
$templateFile = __DIR__ . '/templates/'.$orderType.'OrderRequestBodyTemplate.xml';
$body = file_get_contents($templateFile);
foreach ($this->requestAttributes AS $key => $value) {
$body = str_replace('{{' . $key . '}}', $value, $body);
}
Expand Down
5 changes: 5 additions & 0 deletions src/requests/TranzWarePaymentGatewayOrderRequestResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class TranzWarePaymentGatewayOrderRequestResult implements TranzWarePaymentGatew
private $status;
private $data;

/**
* TranzWarePaymentGatewayOrderRequestResult constructor.
*
* @param TranzWarePaymentGatewayHTTPClientResultInterface $HTTPClientResult
*/
public function __construct(TranzWarePaymentGatewayHTTPClientResultInterface $HTTPClientResult)
{
$this->responseBody = $HTTPClientResult->getOutput();
Expand Down
3 changes: 2 additions & 1 deletion src/requests/TranzWarePaymentGatewayOrderStatusRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public function execute()

final private function getRequestBody()
{
$body = file_get_contents(__DIR__ . '/OrderStatusRequestBodyTemplate.xml');
$templateFile = __DIR__ . '/templates/OrderStatusRequestBodyTemplate.xml';
$body = file_get_contents($templateFile);
foreach ($this->requestAttributes AS $key => $value) {
$body = str_replace('{{' . $key . '}}', $value, $body);
}
Expand Down
Loading

0 comments on commit b5c0958

Please sign in to comment.