Skip to content

Commit 1cf06a5

Browse files
committed
Improved gateway factory. Added different endpoints urls and hash methods.
1 parent 2bc4715 commit 1cf06a5

File tree

3 files changed

+85
-59
lines changed

3 files changed

+85
-59
lines changed

Api/Api.php

+47-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php /** @noinspection PhpUnusedParameterInspection */
22

33
namespace Ekyna\Component\Payum\Payzen\Api;
44

@@ -106,7 +106,7 @@ public function createRequestUrl(array $data)
106106

107107
$data = $this->createRequestData($data);
108108

109-
$url = $this->getEndpoint() . '?' .
109+
$url = $this->getUrl() . '?' .
110110
implode('&', array_map(function ($key, $value) {
111111
return $key . '=' . rawurlencode($value);
112112
}, array_keys($data), $data));
@@ -178,9 +178,14 @@ public function generateSignature(array $data, $hashed = true)
178178
$content .= $value . '+';
179179
}
180180
}
181+
181182
$content .= $this->config['certificate'];
182183

183-
return $hashed ? sha1($content) : $content;
184+
if ($hashed) {
185+
return $this->hash($content);
186+
}
187+
188+
return $content;
184189
}
185190

186191
/**
@@ -200,18 +205,6 @@ private function getDirectoryPath()
200205
return $path . DIRECTORY_SEPARATOR;
201206
}
202207

203-
/**
204-
* Returns the endpoint url.
205-
*
206-
* @return string
207-
*
208-
* @throws \Exception
209-
*/
210-
private function getEndpoint()
211-
{
212-
return 'https://paiement.systempay.fr/vads-payment/';
213-
}
214-
215208
/**
216209
* Check that the API has been configured.
217210
*
@@ -242,12 +235,16 @@ private function getConfigResolver()
242235
'certificate',
243236
'ctx_mode',
244237
'directory',
245-
'debug',
238+
])
239+
->setDefaults([
240+
'endpoint' => null,
241+
'debug' => false,
246242
])
247243
->setAllowedTypes('site_id', 'string')
248244
->setAllowedTypes('certificate', 'string')
249245
->setAllowedValues('ctx_mode', ['TEST', 'PRODUCTION'])
250246
->setAllowedTypes('directory', 'string')
247+
->setAllowedValues('endpoint', $this->getEndPoints())
251248
->setAllowedTypes('debug', 'bool')
252249
->setNormalizer('directory', function (Options $options, $value) {
253250
return rtrim($value, DIRECTORY_SEPARATOR);
@@ -435,21 +432,43 @@ private function getCardsCodes()
435432
{
436433
return [
437434
null,
438-
'AMEX', // American Express
435+
'AMEX', // American Express
439436
'AURORE-MULTI', // Aurore
440-
'BUYSTER', // Buyster
441-
'CB', // CB
442-
'COFINOGA', // Cofinoga
437+
'BUYSTER', // Buyster
438+
'CB', // CB
439+
'COFINOGA', // Cofinoga
443440
'E-CARTEBLEUE', // E-Carte bleue
444-
'MASTERCARD', // Eurocard / Mastercard
445-
'JCB', // JCB
446-
'MAESTRO', // Maestro
447-
'ONEY', // Oney
441+
'MASTERCARD', // Eurocard / Mastercard
442+
'JCB', // JCB
443+
'MAESTRO', // Maestro
444+
'ONEY', // Oney
448445
'ONEY_SANDBOX', // Oney (sandbox)
449-
'PAYPAL', // Paypal
450-
'PAYPAL_SB', // Paypal (sandbox)
451-
'PAYSAFECARD', // Paysafe card
452-
'VISA', // Visa
446+
'PAYPAL', // Paypal
447+
'PAYPAL_SB', // Paypal (sandbox)
448+
'PAYSAFECARD', // Paysafe card
449+
'VISA', // Visa
453450
];
454451
}
452+
453+
private function getEndPoints()
454+
{
455+
return [null, 'SYSTEMPAY'];
456+
}
457+
458+
private function getUrl()
459+
{
460+
if ($this->config['endpoint'] === 'SYSTEMPAY') {
461+
return 'https://paiement.systempay.fr/vads-payment/';
462+
}
463+
464+
return 'https://secure.payzen.eu/vads-payment/';
465+
}
466+
467+
private function hash(string $content) {
468+
if ($this->config['endpoint'] === 'SYSTEMPAY') {
469+
return sha1($content);
470+
}
471+
472+
return base64_encode(hash_hmac('sha256', $content, $this->config['certificate'], true));
473+
}
455474
}

PayzenGatewayFactory.php

+20-18
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,51 @@ public static function build(array $defaultConfig, GatewayFactoryInterface $core
3131
*/
3232
protected function populateConfig(ArrayObject $config)
3333
{
34-
$apiConfig = false != $config['payum.api_config']
35-
? (array)$config['payum.api_config']
36-
: [];
37-
3834
$config->defaults([
3935
'payum.factory_name' => 'payzen',
4036
'payum.factory_title' => 'Payzen',
4137

4238
'payum.action.capture' => new Action\CaptureAction(),
4339
'payum.action.convert_payment' => new Action\ConvertPaymentAction(),
44-
'payum.action.api_request' => new Action\Api\ApiRequestAction(),
45-
'payum.action.api_response' => new Action\Api\ApiResponseAction(),
4640
'payum.action.sync' => new Action\SyncAction(),
4741
'payum.action.refund' => new Action\RefundAction(),
4842
'payum.action.status' => new Action\StatusAction(),
43+
'payum.action.notify' => new Action\NotifyAction(),
44+
'payum.action.api.request' => new Action\Api\ApiRequestAction(),
45+
'payum.action.api.response' => new Action\Api\ApiResponseAction(),
4946
]);
5047

51-
$defaultOptions = [];
52-
$requiredOptions = [];
53-
5448
if (false == $config['payum.api']) {
55-
$defaultOptions['api'] = array_replace([
49+
$config['payum.default_options'] = [
5650
'site_id' => null,
5751
'certificate' => null,
5852
'ctx_mode' => null,
53+
'directory' => null,
54+
'endpoint' => null,
5955
'debug' => false,
60-
], $apiConfig);
56+
];
57+
58+
$config->defaults($config['payum.default_options']);
6159

62-
$requiredOptions[] = 'api';
60+
$config['payum.required_options'] = ['site_id', 'certificate', 'ctx_mode', 'directory'];
6361

6462
$config['payum.api'] = function (ArrayObject $config) {
6563
$config->validateNotEmpty($config['payum.required_options']);
6664

65+
$payzenConfig = [
66+
'endpoint' => $config['endpoint'],
67+
'site_id' => $config['site_id'],
68+
'certificate' => $config['certificate'],
69+
'ctx_mode' => $config['ctx_mode'],
70+
'directory' => $config['directory'],
71+
'debug' => $config['debug'],
72+
];
73+
6774
$api = new Api\Api();
68-
$api->setConfig($config['api']);
75+
$api->setConfig($payzenConfig);
6976

7077
return $api;
7178
};
7279
}
73-
74-
$config['payum.default_options'] = $defaultOptions;
75-
$config['payum.required_options'] = $requiredOptions;
76-
77-
$config->defaults($config['payum.default_options']);
7880
}
7981
}

composer.json

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
{
2-
"name" : "ekyna/payum-payzen",
3-
"description" : "PayZen Payum gateway",
4-
"type" : "component",
5-
"authors" : [{
6-
"name" : "Etienne Dauvergne",
7-
"homepage" : "http://ekyna.com"
2+
"name": "ekyna/payum-payzen",
3+
"description": "PayZen Payum gateway",
4+
"type": "component",
5+
"authors": [
6+
{
7+
"name": "Etienne Dauvergne",
8+
"homepage": "http://ekyna.com"
89
}
910
],
10-
"homepage" : "https://github.com/ekyna/PayumPayzen",
11-
"license" : [
11+
"keywords": [
12+
"payum",
13+
"payzen"
14+
],
15+
"homepage": "https://github.com/ekyna/PayumPayzen",
16+
"license": [
1217
"MIT"
1318
],
14-
"require" : {
15-
"php" : ">=5.4",
19+
"require": {
20+
"php": "^7.0",
1621
"payum/core": "^1.5",
1722
"php-http/guzzle6-adapter": "^1.1",
1823
"symfony/process": "~2.7|~3.0|~4.0",
1924
"psr/log": "~1.0"
2025
},
21-
"autoload" : {
22-
"psr-4" : {
23-
"Ekyna\\Component\\Payum\\Payzen\\" : ""
26+
"autoload": {
27+
"psr-4": {
28+
"Ekyna\\Component\\Payum\\Payzen\\": ""
2429
}
2530
}
2631
}

0 commit comments

Comments
 (0)