-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMoneticoPaymentGateway.php
150 lines (132 loc) · 4.37 KB
/
MoneticoPaymentGateway.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace IDCI\Bundle\PaymentBundle\Gateway;
use IDCI\Bundle\PaymentBundle\Model\GatewayResponse;
use IDCI\Bundle\PaymentBundle\Model\PaymentGatewayConfigurationInterface;
use IDCI\Bundle\PaymentBundle\Model\Transaction;
use Symfony\Component\HttpFoundation\Request;
class MoneticoPaymentGateway extends AbstractPaymentGateway
{
/**
* Get Monetico server url.
*
* @method getServerUrl
*/
private function getServerUrl(): string
{
return 'https://p.monetico-services.com/test/paiement.cgi'; //raw
}
/**
* Build payment gateway options.
*
* @method buildOptions
*/
private function buildOptions(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): array {
return [
'TPE' => $paymentGatewayConfiguration->get('TPE'),
'date' => (new \DateTime())->format('d/m/Y:H:i:s'),
'montant' => sprintf('%s%s', $transaction->getAmount() / 100, $transaction->getCurrencyCode()),
'reference' => $transaction->getId(),
'text-libre' => 'test',
'version' => $paymentGatewayConfiguration->get('version'),
'lgue' => 'FR',
'societe' => $paymentGatewayConfiguration->get('societe'),
'mail' => '[email protected]',
'url_retour' => $paymentGatewayConfiguration->get('callback_url'),
'url_retour_ok' => $paymentGatewayConfiguration->get('return_url'),
'url_retour_err' => $paymentGatewayConfiguration->get('return_url'),
];
}
/**
* Build payment gateway HMAC signature.
*
* @method buildMAC
*
* @param array $options [description]
* @param string $secretKey [description]
*
* @return [type] [description]
*/
private function buildMAC(array $options, string $secretKey)
{
$hexStrKey = substr($secretKey, 0, 38);
$hexFinal = sprintf('%s00', substr($secretKey, 38, 2));
$char = ord($hexFinal);
if ($char > 70 && $char < 97) {
$hexStrKey .= sprintf('%s%s', chr($char - 23), substr($hexFinal, 1, 1));
} elseif ('M' == substr($hexFinal, 1, 1)) {
$hexStrKey .= sprintf('%s0', substr($hexFinal, 0, 1));
} else {
$hexStrKey .= substr($hexFinal, 0, 2);
}
$usableKey = pack('H*', $hexStrKey);
unset($options['url_retour']);
unset($options['url_retour_ok']);
unset($options['url_retour_err']);
$sData = implode('*', array_map(function ($value) {
return $value;
}, $options));
return strtolower(hash_hmac('sha1', $sData, $usableKey));
}
/**
* {@inheritdoc}
*/
public function initialize(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): array {
$options = $this->buildOptions($paymentGatewayConfiguration, $transaction);
$options['MAC'] = $this->buildMAC($options, $paymentGatewayConfiguration->get('secret'));
return [
'url' => $this->getServerUrl(),
'options' => $options,
];
}
/**
* {@inheritdoc}
*/
public function buildHTMLView(
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration,
Transaction $transaction
): string {
$initializationData = $this->initialize($paymentGatewayConfiguration, $transaction);
return $this->templating->render('@IDCIPayment/Gateway/monetico.html.twig', [
'initializationData' => $initializationData,
]);
}
/**
* {@inheritdoc}
*/
public function getReturnResponse(
Request $request,
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration
): GatewayResponse {
return new GatewayResponse();
}
/**
* {@inheritdoc}
*/
public function getCallbackResponse(
Request $request,
PaymentGatewayConfigurationInterface $paymentGatewayConfiguration
): GatewayResponse {
return null;
}
/**
* {@inheritdoc}
*/
public static function getParameterNames(): ?array
{
return array_merge(
parent::getParameterNames(),
[
'version',
'secret',
'TPE',
'societe',
]
);
}
}