Skip to content

Commit 88e7934

Browse files
committed
Initial commit of Redsys gateway and unit tests
0 parents  commit 88e7934

17 files changed

+1171
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
composer.lock
3+
composer.phar
4+
phpunit.xml

.travis.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: php
2+
3+
php:
4+
- 5.3
5+
- 5.4
6+
- 5.5
7+
- 5.6
8+
- hhvm
9+
10+
before_script:
11+
- composer install -n --dev --prefer-source
12+
13+
script: vendor/bin/phpcs --standard=PSR2 src && vendor/bin/phpunit --coverage-text

CONTRIBUTING.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Contributing Guidelines
2+
3+
* Fork the project.
4+
* Make your feature addition or bug fix.
5+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
6+
* Commit just the modifications, do not mess with the composer.json or CHANGELOG.md files.
7+
* Ensure your code is nicely formatted in the [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)
8+
style and that all tests pass.
9+
* Send the pull request.
10+
* Check that the Travis CI build passed. If not, rinse and repeat.

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2015 Leith Caldwell
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Omnipay: Redsys
2+
3+
**Redsys driver for the Omnipay PHP payment processing library**
4+
5+
[![Build Status](https://travis-ci.org/PatronBase/omnipay-redsys.png?branch=master)](https://travis-ci.org/PatronBase/omnipay-redsys)
6+
[![Latest Stable Version](https://poser.pugx.org/PatronBase/omnipay-redsys/version.png)](https://packagist.org/packages/patronbase/omnipay-redsys)
7+
[![Total Downloads](https://poser.pugx.org/patronbase/omnipay-redsys/d/total.png)](https://packagist.org/packages/patronbase/omnipay-redsys)
8+
9+
10+
[Omnipay](https://github.com/thephpleague/omnipay) is a framework agnostic, multi-gateway payment
11+
processing library for PHP 5.3+. This package implements Redsys support for Omnipay.
12+
13+
## Installation
14+
15+
Omnipay is installed via [Composer](http://getcomposer.org/). To install, simply add it
16+
to your `composer.json` file:
17+
18+
```json
19+
{
20+
"require": {
21+
"PatronBase/omnipay-redsys": "~2.0"
22+
}
23+
}
24+
```
25+
26+
And run composer to update your dependencies:
27+
28+
$ curl -s http://getcomposer.org/installer | php
29+
$ php composer.phar update
30+
31+
## Basic Usage
32+
33+
The following gateways are provided by this package:
34+
35+
* Redsys
36+
37+
For general usage instructions, please see the main [Omnipay](https://github.com/thephpleague/omnipay)
38+
repository.
39+
40+
## Support
41+
42+
If you are having general issues with Omnipay, we suggest posting on
43+
[Stack Overflow](http://stackoverflow.com/). Be sure to add the
44+
[omnipay tag](http://stackoverflow.com/questions/tagged/omnipay) so it can be easily found.
45+
46+
If you want to keep up to date with release anouncements, discuss ideas for the project,
47+
or ask more detailed questions, there is also a [mailing list](https://groups.google.com/forum/#!forum/omnipay) which
48+
you can subscribe to.
49+
50+
If you believe you have found a bug, please report it using the [GitHub issue tracker](https://github.com/PatronBase/omnipay-redsys/issues),
51+
or better yet, fork the library and submit a pull request.

composer.json

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"name": "patronbase/omnipay-redsys",
3+
"type": "library",
4+
"description": "Redsys driver for the Omnipay payment processing library",
5+
"keywords": [
6+
"gateway",
7+
"merchant",
8+
"omnipay",
9+
"pay",
10+
"payment",
11+
"redsys",
12+
"servired"
13+
],
14+
"homepage": "https://github.com/PatronBase/omnipay-redsys",
15+
"license": "MIT",
16+
"authors": [
17+
{
18+
"name": "Leith Caldwell",
19+
"email": "[email protected]"
20+
}
21+
],
22+
"autoload": {
23+
"psr-4": { "Omnipay\\Redsys\\" : "src/" }
24+
},
25+
"autoload-dev": {
26+
"psr-4": { "Omnipay\\Redsys\\" : "tests/" }
27+
},
28+
"require": {
29+
"omnipay/common": "~2.0"
30+
},
31+
"require-dev": {
32+
"omnipay/tests": "~2.0"
33+
},
34+
"suggest": {
35+
"ext-mcrypt": "Required for hashing functions to check message signatures"
36+
},
37+
"extra": {
38+
"branch-alias": {
39+
"dev-master": "2.0.x-dev"
40+
}
41+
}
42+
}

phpunit.xml.dist

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false">
12+
<testsuites>
13+
<testsuite name="Omnipay Test Suite">
14+
<directory>./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
<listeners>
18+
<listener class="Mockery\Adapter\Phpunit\TestListener" file="vendor/mockery/mockery/library/Mockery/Adapter/Phpunit/TestListener.php" />
19+
</listeners>
20+
<filter>
21+
<whitelist>
22+
<directory>./src</directory>
23+
</whitelist>
24+
</filter>
25+
</phpunit>

src/Gateway.php

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Omnipay\Redsys;
4+
5+
use Omnipay\Common\AbstractGateway;
6+
use Omnipay\Redsys\Message\CompletePurchaseRequest;
7+
use Omnipay\Redsys\Message\PurchaseRequest;
8+
9+
/**
10+
* Redsys Gateway
11+
*
12+
* @link http://www.redsys.es/
13+
*/
14+
class Gateway extends AbstractGateway
15+
{
16+
public function getName()
17+
{
18+
return 'Redsys';
19+
}
20+
21+
public function getDefaultParameters()
22+
{
23+
return array(
24+
'merchantId' => '',
25+
'merchantName' => '',
26+
'terminalId' => '',
27+
'hmacKey' => '',
28+
'testMode' => false,
29+
);
30+
}
31+
32+
public function getMerchantId()
33+
{
34+
return $this->getParameter('merchantId');
35+
}
36+
37+
public function setMerchantId($value)
38+
{
39+
return $this->setParameter('merchantId', $value);
40+
}
41+
42+
public function getMerchantName()
43+
{
44+
return $this->getParameter('merchantName');
45+
}
46+
47+
public function setMerchantName($value)
48+
{
49+
return $this->setParameter('merchantName', $value);
50+
}
51+
52+
public function getTerminalId()
53+
{
54+
return $this->getParameter('terminalId');
55+
}
56+
57+
public function setTerminalId($value)
58+
{
59+
return $this->setParameter('terminalId', $value);
60+
}
61+
62+
public function getHmacKey()
63+
{
64+
return $this->getParameter('hmacKey');
65+
}
66+
67+
public function setHmacKey($value)
68+
{
69+
return $this->setParameter('hmacKey', $value);
70+
}
71+
72+
public function purchase(array $parameters = array())
73+
{
74+
return $this->createRequest('\Omnipay\Redsys\Message\PurchaseRequest', $parameters);
75+
}
76+
77+
public function completePurchase(array $parameters = array())
78+
{
79+
return $this->createRequest('\Omnipay\Redsys\Message\CompletePurchaseRequest', $parameters);
80+
}
81+
}

src/Message/AbstractResponse.php

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Omnipay\Redsys\Message;
4+
5+
use Omnipay\Common\Message\AbstractResponse as BaseAbstractResponse;
6+
use Omnipay\Common\Exception\RuntimeException;
7+
8+
/**
9+
* Abstract Response
10+
*
11+
* This abstract class extends the base Omnipay AbstractResponse in order
12+
* to provide some common encoding and decoding functions.
13+
*/
14+
abstract class AbstractResponse extends BaseAbstractResponse
15+
{
16+
/**
17+
* Encode merchant parameters
18+
*
19+
* @param array $data The parameters to encode
20+
*
21+
* @return string Encoded data
22+
*/
23+
protected function encodeMerchantParameters($data)
24+
{
25+
return base64_encode(json_encode($data));
26+
}
27+
28+
/**
29+
* Decode merchant parameters
30+
*
31+
* @param string $data The encoded string of parameters
32+
*
33+
* @return array Decoded data
34+
*/
35+
protected function decodeMerchantParameters($data)
36+
{
37+
return (array)json_decode(base64_decode(strtr($data, '-_', '+/')));
38+
}
39+
40+
/**
41+
* Encrypt message with given key and default IV
42+
*
43+
* @todo function_exists() vs extension_loaded()?
44+
*
45+
* @param string $message The message to encrypt
46+
* @param string $key The key used to encrypt the message
47+
*
48+
* @return string Encrypted message
49+
*
50+
* @throws RuntimeException
51+
*/
52+
protected function encryptMessage($message, $key)
53+
{
54+
$iv = implode(array_map("chr", array(0, 0, 0, 0, 0, 0, 0, 0)));
55+
56+
if (function_exists('mcrypt_encrypt')) {
57+
$ciphertext = mcrypt_encrypt(MCRYPT_3DES, $key, $message, MCRYPT_MODE_CBC, $iv);
58+
} else {
59+
throw new RuntimeException('No valid encryption extension installed');
60+
}
61+
62+
return $ciphertext;
63+
}
64+
65+
/**
66+
* Create signature hash used to verify messages
67+
*
68+
* @todo Add if-check on algorithm to match against signature version as new param?
69+
*
70+
* @param string $message The message to encrypt
71+
* @param string $salt Unique salt used to generate the ciphertext
72+
* @param string $key The key used to encrypt the message
73+
*
74+
* @return string Generated signature
75+
*/
76+
protected function createSignature($message, $salt, $key)
77+
{
78+
$ciphertext = $this->encryptMessage($salt, $key);
79+
return base64_encode(hash_hmac('sha256', $message, $ciphertext, true));
80+
}
81+
82+
protected function createReturnSignature($message, $salt, $key)
83+
{
84+
return strtr($this->createSignature($message, $salt, $key), '+/', '-_');
85+
}
86+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Omnipay\Redsys\Message;
4+
5+
/**
6+
* Redsys Complete Purchase Request
7+
*/
8+
class CompletePurchaseRequest extends PurchaseRequest
9+
{
10+
public function getData()
11+
{
12+
return $this->httpRequest->request->all();
13+
}
14+
15+
public function sendData($data)
16+
{
17+
return $this->response = new CompletePurchaseResponse($this, $data);
18+
}
19+
}

0 commit comments

Comments
 (0)