Skip to content

Commit f8e1f5e

Browse files
authored
Merge pull request #1 from povils/develop
Big initial request
2 parents 4dda2e1 + f026982 commit f8e1f5e

18 files changed

+1530
-2
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
/vendor
22
composer.lock
3-
composer.phar
4-
phpunit.xml
53
.idea

phpunit.xml.dist

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
bootstrap="vendor/autoload.php"
6+
colors="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false"
12+
syntaxCheck="false">
13+
14+
<testsuites>
15+
<testsuite name="Omnipay Paysera Test Suite">
16+
<directory>./tests</directory>
17+
</testsuite>
18+
</testsuites>
19+
20+
<filter>
21+
<whitelist>
22+
<directory>./src</directory>
23+
<exclude>
24+
<directory>./vendor</directory>
25+
</exclude>
26+
</whitelist>
27+
</filter>
28+
29+
</phpunit>

src/Common/Encoder.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* This is the part of Povils open-source library.
5+
*
6+
* @author Povilas Susinskas
7+
*/
8+
9+
namespace Omnipay\Paysera\Common;
10+
11+
/**
12+
* Class Encoder
13+
*
14+
* @package Omnipay\Paysera\Common
15+
*/
16+
class Encoder
17+
{
18+
/**
19+
* @param string $input
20+
*
21+
* @return string
22+
*/
23+
public static function encode($input)
24+
{
25+
return strtr(base64_encode($input), ['+' => '-', '/' => '_']);
26+
}
27+
28+
/**
29+
* @param $input
30+
*
31+
* @return string
32+
*/
33+
public static function decode($input)
34+
{
35+
return base64_decode(strtr($input, ['-' => '+', '_' => '/']));
36+
}
37+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
/**
4+
* This is the part of Povils open-source library.
5+
*
6+
* @author Povilas Susinskas
7+
*/
8+
9+
namespace Omnipay\Paysera\Common;
10+
11+
use Omnipay\Paysera\Message\PurchaseRequest;
12+
13+
/**
14+
* Class PurchaseDataGenerator
15+
*
16+
* @package Omnipay\Paysera\Common
17+
*/
18+
class PurchaseDataGenerator
19+
{
20+
/**
21+
* @param PurchaseRequest $request
22+
*
23+
* @return string
24+
*/
25+
public static function generate(PurchaseRequest $request)
26+
{
27+
$parameters = [
28+
'projectid' => $request->getProjectId(),
29+
'orderid' => $request->getTransactionId(),
30+
'accepturl' => $request->getReturnUrl(),
31+
'cancelurl' => $request->getCancelUrl(),
32+
'callbackurl' => $request->getNotifyUrl(),
33+
'version' => $request->getVersion(),
34+
'payment' => $request->getPaymentMethod(),
35+
'lang' => $request->getLanguage(),
36+
'amount' => $request->getAmountInteger(),
37+
'currency' => $request->getCurrency(),
38+
'test' => $request->getTestMode() ? '1' : '0',
39+
];
40+
41+
if(null !== $customer = $request->getCustomer()){
42+
$customerData = [
43+
'p_firstname' => $customer->getFirstName(),
44+
'p_lastname' => $customer->getLastName(),
45+
'p_email' => $customer->getEmail(),
46+
'p_street' => $customer->getStreet(),
47+
'p_city' => $customer->getCity(),
48+
'p_state' => $customer->getState(),
49+
'p_zip' => $customer->getPostcode(),
50+
'p_countrycode' => $customer->getCountryCode(),
51+
'country' => $customer->getCountry(),
52+
];
53+
54+
$parameters = array_merge($parameters, $customerData);
55+
}
56+
57+
$filteredParameters = self::filterParameters($parameters);
58+
59+
PurchaseParameterValidator::validate($filteredParameters);
60+
61+
return Encoder::encode(http_build_query($filteredParameters, '', '&'));
62+
}
63+
64+
/**
65+
* @param array $parameters
66+
*
67+
* @return array
68+
*/
69+
private static function filterParameters(array $parameters)
70+
{
71+
return array_filter($parameters, function ($value) {
72+
return $value !== '' && $value !== null;
73+
});
74+
}
75+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/**
4+
* This is the part of Povils open-source library.
5+
*
6+
* @author Povilas Susinskas
7+
*/
8+
9+
namespace Omnipay\Paysera\Common;
10+
11+
use Omnipay\Common\Exception\InvalidRequestException;
12+
13+
/**
14+
* Class PurchaseParameterValidator
15+
*
16+
* @package Omnipay\Paysera\Common
17+
*/
18+
class PurchaseParameterValidator
19+
{
20+
/**
21+
* @param array $data
22+
*
23+
* @throws InvalidRequestException
24+
*/
25+
public static function validate(array $data)
26+
{
27+
foreach (self::getRequestSpecifications() as $specification) {
28+
list($name, $maxLength, $required, $regexp) = $specification;
29+
if ($required && false === isset($data[$name])) {
30+
throw new InvalidRequestException(sprintf("'%s' is required but missing.", $name));
31+
}
32+
33+
if (false === empty($data[$name])) {
34+
if ($maxLength && strlen($data[$name]) > $maxLength) {
35+
throw new InvalidRequestException(sprintf(
36+
"'%s' value is too long (%d), %d characters allowed.",
37+
$name,
38+
strlen($data[$name]),
39+
$maxLength
40+
));
41+
}
42+
43+
if ($regexp !== '' && !preg_match($regexp, $data[$name])) {
44+
throw new InvalidRequestException(sprintf("'%s' value '%s' is invalid.", $name, $data[$name]));
45+
}
46+
}
47+
}
48+
}
49+
50+
/**
51+
* Array structure:
52+
* name – request parameter name
53+
* maxLength – max allowed length for parameter
54+
* required – is this parameter required
55+
* regexp – regexp to test parameter value
56+
*
57+
* @return array
58+
*/
59+
protected static function getRequestSpecifications()
60+
{
61+
return [
62+
['orderid', 40, true, ''],
63+
['accepturl', 255, true, ''],
64+
['cancelurl', 255, true, ''],
65+
['callbackurl', 255, true, ''],
66+
['lang', 3, false, '/^[a-z]{3}$/i'],
67+
['amount', 11, false, '/^\d+$/'],
68+
['currency', 3, false, '/^[a-z]{3}$/i'],
69+
['payment', 20, false, ''],
70+
['country', 2, false, '/^[a-z_]{2}$/i'],
71+
['p_firstname', 255, false, ''],
72+
['p_lastname', 255, false, ''],
73+
['p_email', 255, false, ''],
74+
['p_street', 255, false, ''],
75+
['p_city', 255, false, ''],
76+
['p_state', 20, false, ''],
77+
['p_zip', 20, false, ''],
78+
['p_countrycode', 2, false, '/^[a-z]{2}$/i'],
79+
['test', 1, false, '/^[01]$/'],
80+
];
81+
}
82+
}

src/Common/SignatureGenerator.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* This is the part of Povils open-source library.
5+
*
6+
* @author Povilas Susinskas
7+
*/
8+
9+
namespace Omnipay\Paysera\Common;
10+
11+
/**
12+
* Class SignatureGenerator
13+
*
14+
* @package Omnipay\Paysera\Common
15+
*/
16+
class SignatureGenerator
17+
{
18+
/**
19+
* @param string $data
20+
* @param string $password
21+
*
22+
* @return string
23+
*/
24+
public static function generate($data, $password)
25+
{
26+
return md5($data . $password);
27+
}
28+
}

src/Common/SignatureValidator.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/**
4+
* This is the part of Povils open-source library.
5+
*
6+
* @author Povilas Susinskas
7+
*/
8+
9+
namespace Omnipay\Paysera\Common;
10+
11+
use Guzzle\Http\ClientInterface;
12+
13+
/**
14+
* Class SignatureValidator
15+
*
16+
* @package Omnipay\Paysera\Common
17+
*/
18+
class SignatureValidator
19+
{
20+
/**
21+
* @var string
22+
*/
23+
private static $endpoint = 'http://www.paysera.com/download/public.key';
24+
25+
/**
26+
* @param array $data
27+
* @param string $password
28+
* @param ClientInterface $client
29+
*
30+
* @return bool
31+
*/
32+
public static function isValid(array $data, $password, ClientInterface $client)
33+
{
34+
return self::isValidSS1($data, $password) && self::isValidSS2($data, $client);
35+
}
36+
37+
/**
38+
* @param array $data
39+
* @param string $password
40+
*
41+
* @return bool
42+
*/
43+
private static function isValidSS1(array $data, $password)
44+
{
45+
return SignatureGenerator::generate($data['data'], $password) === $data['ss1'];
46+
}
47+
48+
/**
49+
* @param array $data
50+
* @param ClientInterface $client
51+
*
52+
* @return bool
53+
*/
54+
private static function isValidSS2(array $data, ClientInterface $client)
55+
{
56+
$response = $client->get(self::$endpoint)->send();
57+
if (200 === $response->getStatusCode() && false !== $publicKey = openssl_get_publickey($response->getBody())) {
58+
return openssl_verify($data['data'], Encoder::decode($data['ss2']), $publicKey) === 1;
59+
}
60+
61+
return false;
62+
}
63+
}

0 commit comments

Comments
 (0)