-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Povilas
committed
Oct 16, 2016
1 parent
4dda2e1
commit f026982
Showing
18 changed files
with
1,530 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
/vendor | ||
composer.lock | ||
composer.phar | ||
phpunit.xml | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false"> | ||
|
||
<testsuites> | ||
<testsuite name="Omnipay Paysera Test Suite"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist> | ||
<directory>./src</directory> | ||
<exclude> | ||
<directory>./vendor</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
|
||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/** | ||
* This is the part of Povils open-source library. | ||
* | ||
* @author Povilas Susinskas | ||
*/ | ||
|
||
namespace Omnipay\Paysera\Common; | ||
|
||
/** | ||
* Class Encoder | ||
* | ||
* @package Omnipay\Paysera\Common | ||
*/ | ||
class Encoder | ||
{ | ||
/** | ||
* @param string $input | ||
* | ||
* @return string | ||
*/ | ||
public static function encode($input) | ||
{ | ||
return strtr(base64_encode($input), ['+' => '-', '/' => '_']); | ||
} | ||
|
||
/** | ||
* @param $input | ||
* | ||
* @return string | ||
*/ | ||
public static function decode($input) | ||
{ | ||
return base64_decode(strtr($input, ['-' => '+', '_' => '/'])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
/** | ||
* This is the part of Povils open-source library. | ||
* | ||
* @author Povilas Susinskas | ||
*/ | ||
|
||
namespace Omnipay\Paysera\Common; | ||
|
||
use Omnipay\Paysera\Message\PurchaseRequest; | ||
|
||
/** | ||
* Class PurchaseDataGenerator | ||
* | ||
* @package Omnipay\Paysera\Common | ||
*/ | ||
class PurchaseDataGenerator | ||
{ | ||
/** | ||
* @param PurchaseRequest $request | ||
* | ||
* @return string | ||
*/ | ||
public static function generate(PurchaseRequest $request) | ||
{ | ||
$parameters = [ | ||
'projectid' => $request->getProjectId(), | ||
'orderid' => $request->getTransactionId(), | ||
'accepturl' => $request->getReturnUrl(), | ||
'cancelurl' => $request->getCancelUrl(), | ||
'callbackurl' => $request->getNotifyUrl(), | ||
'version' => $request->getVersion(), | ||
'payment' => $request->getPaymentMethod(), | ||
'lang' => $request->getLanguage(), | ||
'amount' => $request->getAmountInteger(), | ||
'currency' => $request->getCurrency(), | ||
'test' => $request->getTestMode() ? '1' : '0', | ||
]; | ||
|
||
if(null !== $customer = $request->getCustomer()){ | ||
$customerData = [ | ||
'p_firstname' => $customer->getFirstName(), | ||
'p_lastname' => $customer->getLastName(), | ||
'p_email' => $customer->getEmail(), | ||
'p_street' => $customer->getStreet(), | ||
'p_city' => $customer->getCity(), | ||
'p_state' => $customer->getState(), | ||
'p_zip' => $customer->getPostcode(), | ||
'p_countrycode' => $customer->getCountryCode(), | ||
'country' => $customer->getCountry(), | ||
]; | ||
|
||
$parameters = array_merge($parameters, $customerData); | ||
} | ||
|
||
$filteredParameters = self::filterParameters($parameters); | ||
|
||
PurchaseParameterValidator::validate($filteredParameters); | ||
|
||
return Encoder::encode(http_build_query($filteredParameters, '', '&')); | ||
} | ||
|
||
/** | ||
* @param array $parameters | ||
* | ||
* @return array | ||
*/ | ||
private static function filterParameters(array $parameters) | ||
{ | ||
return array_filter($parameters, function ($value) { | ||
return $value !== '' && $value !== null; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/** | ||
* This is the part of Povils open-source library. | ||
* | ||
* @author Povilas Susinskas | ||
*/ | ||
|
||
namespace Omnipay\Paysera\Common; | ||
|
||
use Omnipay\Common\Exception\InvalidRequestException; | ||
|
||
/** | ||
* Class PurchaseParameterValidator | ||
* | ||
* @package Omnipay\Paysera\Common | ||
*/ | ||
class PurchaseParameterValidator | ||
{ | ||
/** | ||
* @param array $data | ||
* | ||
* @throws InvalidRequestException | ||
*/ | ||
public static function validate(array $data) | ||
{ | ||
foreach (self::getRequestSpecifications() as $specification) { | ||
list($name, $maxLength, $required, $regexp) = $specification; | ||
if ($required && false === isset($data[$name])) { | ||
throw new InvalidRequestException(sprintf("'%s' is required but missing.", $name)); | ||
} | ||
|
||
if (false === empty($data[$name])) { | ||
if ($maxLength && strlen($data[$name]) > $maxLength) { | ||
throw new InvalidRequestException(sprintf( | ||
"'%s' value is too long (%d), %d characters allowed.", | ||
$name, | ||
strlen($data[$name]), | ||
$maxLength | ||
)); | ||
} | ||
|
||
if ($regexp !== '' && !preg_match($regexp, $data[$name])) { | ||
throw new InvalidRequestException(sprintf("'%s' value '%s' is invalid.", $name, $data[$name])); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Array structure: | ||
* name – request parameter name | ||
* maxLength – max allowed length for parameter | ||
* required – is this parameter required | ||
* regexp – regexp to test parameter value | ||
* | ||
* @return array | ||
*/ | ||
protected static function getRequestSpecifications() | ||
{ | ||
return [ | ||
['orderid', 40, true, ''], | ||
['accepturl', 255, true, ''], | ||
['cancelurl', 255, true, ''], | ||
['callbackurl', 255, true, ''], | ||
['lang', 3, false, '/^[a-z]{3}$/i'], | ||
['amount', 11, false, '/^\d+$/'], | ||
['currency', 3, false, '/^[a-z]{3}$/i'], | ||
['payment', 20, false, ''], | ||
['country', 2, false, '/^[a-z_]{2}$/i'], | ||
['p_firstname', 255, false, ''], | ||
['p_lastname', 255, false, ''], | ||
['p_email', 255, false, ''], | ||
['p_street', 255, false, ''], | ||
['p_city', 255, false, ''], | ||
['p_state', 20, false, ''], | ||
['p_zip', 20, false, ''], | ||
['p_countrycode', 2, false, '/^[a-z]{2}$/i'], | ||
['test', 1, false, '/^[01]$/'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/** | ||
* This is the part of Povils open-source library. | ||
* | ||
* @author Povilas Susinskas | ||
*/ | ||
|
||
namespace Omnipay\Paysera\Common; | ||
|
||
/** | ||
* Class SignatureGenerator | ||
* | ||
* @package Omnipay\Paysera\Common | ||
*/ | ||
class SignatureGenerator | ||
{ | ||
/** | ||
* @param string $data | ||
* @param string $password | ||
* | ||
* @return string | ||
*/ | ||
public static function generate($data, $password) | ||
{ | ||
return md5($data . $password); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/** | ||
* This is the part of Povils open-source library. | ||
* | ||
* @author Povilas Susinskas | ||
*/ | ||
|
||
namespace Omnipay\Paysera\Common; | ||
|
||
use Guzzle\Http\ClientInterface; | ||
|
||
/** | ||
* Class SignatureValidator | ||
* | ||
* @package Omnipay\Paysera\Common | ||
*/ | ||
class SignatureValidator | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private static $endpoint = 'http://www.paysera.com/download/public.key'; | ||
|
||
/** | ||
* @param array $data | ||
* @param string $password | ||
* @param ClientInterface $client | ||
* | ||
* @return bool | ||
*/ | ||
public static function isValid(array $data, $password, ClientInterface $client) | ||
{ | ||
return self::isValidSS1($data, $password) && self::isValidSS2($data, $client); | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* @param string $password | ||
* | ||
* @return bool | ||
*/ | ||
private static function isValidSS1(array $data, $password) | ||
{ | ||
return SignatureGenerator::generate($data['data'], $password) === $data['ss1']; | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* @param ClientInterface $client | ||
* | ||
* @return bool | ||
*/ | ||
private static function isValidSS2(array $data, ClientInterface $client) | ||
{ | ||
$response = $client->get(self::$endpoint)->send(); | ||
if (200 === $response->getStatusCode() && false !== $publicKey = openssl_get_publickey($response->getBody())) { | ||
return openssl_verify($data['data'], Encoder::decode($data['ss2']), $publicKey) === 1; | ||
} | ||
|
||
return false; | ||
} | ||
} |
Oops, something went wrong.