Skip to content

Commit

Permalink
Merge pull request #1 from povils/develop
Browse files Browse the repository at this point in the history
Big initial request
  • Loading branch information
povils authored Oct 16, 2016
2 parents 4dda2e1 + f026982 commit f8e1f5e
Show file tree
Hide file tree
Showing 18 changed files with 1,530 additions and 2 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/vendor
composer.lock
composer.phar
phpunit.xml
.idea
29 changes: 29 additions & 0 deletions phpunit.xml.dist
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>
37 changes: 37 additions & 0 deletions src/Common/Encoder.php
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, ['-' => '+', '_' => '/']));
}
}
75 changes: 75 additions & 0 deletions src/Common/PurchaseDataGenerator.php
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;
});
}
}
82 changes: 82 additions & 0 deletions src/Common/PurchaseParameterValidator.php
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]$/'],
];
}
}
28 changes: 28 additions & 0 deletions src/Common/SignatureGenerator.php
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);
}
}
63 changes: 63 additions & 0 deletions src/Common/SignatureValidator.php
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;
}
}
Loading

0 comments on commit f8e1f5e

Please sign in to comment.