-
Notifications
You must be signed in to change notification settings - Fork 8
/
OgoneManager.php
224 lines (194 loc) · 6.13 KB
/
OgoneManager.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/*
* This file is part of the Snowcap OgoneBundle package.
*
* (c) Snowcap <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Snowcap\OgoneBundle;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Monolog\Logger;
use Ogone\Passphrase;
use Ogone\ShaComposer\AllParametersShaComposer;
use Ogone\ParameterFilter\ShaInParameterFilter;
use Ogone\ParameterFilter\ShaOutParameterFilter;
use Ogone\FormGenerator\FormGenerator;
use Ogone\Ecommerce\EcommercePaymentRequest,
Ogone\Ecommerce\EcommercePaymentResponse;
use Snowcap\OgoneBundle\Event\OgoneEvent;
class OgoneManager
{
/**
* @var string
*/
protected $pspid;
/**
* @var string
*/
protected $environment;
/**
* @var \Ogone\Passphrase
*/
protected $shaIn;
/**
* @var \Ogone\Passphrase
*/
protected $shaOut;
/**
* @var array
*/
protected $options = array();
/**
* @var array
*/
protected $listeners = array();
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* @var \Monolog\Logger
*/
protected $logger;
/**
* @var \Ogone\FormGenerator\FormGenerator
*/
protected $formGenerator;
/**
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* @param \Monolog\Logger $logger
* @param \Ogone\FormGenerator\FormGenerator $formGenerator
* @param $pspid
* @param $environment
* @param $shaIn
* @param $shaOut
* @param array $options
* @throws \Exception
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
Logger $logger,
FormGenerator $formGenerator,
$pspid,
$environment,
$shaIn,
$shaOut,
$options = array()
)
{
// TODO: use config validation
if ($pspid === "") {
throw new \Exception('No PSPID defined for Ogone');
}
if ($environment !== "test" && $environment !== "prod") {
throw new \Exception(sprintf('No valid Ogone environment ("test" or "prod"), "%s" given', $environment));
}
if ($shaIn === "") {
throw new \Exception('No SHA-IN passphrase defined for Ogone');
}
if ($shaOut === "") {
throw new \Exception('No SHA-OUT passphrase defined for Ogone');
}
$this->eventDispatcher = $eventDispatcher;
$this->logger = $logger;
$this->formGenerator = $formGenerator;
$this->pspid = $pspid;
$this->environment = $environment;
$this->shaIn = new Passphrase($shaIn);
$this->shaOut = new Passphrase($shaOut);
$this->options = $options;
}
/**
* @param string $locale
* @param string $orderId
* @param string $customerName
* @param integer $amount Amount in cents
* @param string $currency Example : EUR
* @param array $options
* @param bool $showSubmitButton
* @return string
*/
public function getRequestForm($locale, $orderId, $customerName, $amount, $currency = "EUR", $options = array(), $showSubmitButton = true)
{
$passphrase = $this->shaIn;
$shaComposer = new AllParametersShaComposer($passphrase);
$shaComposer->addParameterFilter(new ShaInParameterFilter); //optional
$paymentRequest = new EcommercePaymentRequest($shaComposer);
switch ($this->environment) {
case 'prod':
$paymentRequest->setOgoneUri(EcommercePaymentRequest::PRODUCTION);
break;
default:
$paymentRequest->setOgoneUri(EcommercePaymentRequest::TEST);
break;
}
$paymentRequest->setPspid($this->pspid);
$paymentRequest->setCn($customerName);
$paymentRequest->setOrderid($orderId);
$paymentRequest->setAmount($amount);
$paymentRequest->setCurrency($currency);
// setting options defined in config
foreach ($this->options as $option => $value) {
$setter = "set" . $option;
$paymentRequest->$setter($value);
}
// setting options defined in method call
foreach ($options as $option => $value) {
$setter = "set" . $option;
$paymentRequest->$setter($value);
}
$paymentRequest->setLanguage($this->localeToIso($locale));
$paymentRequest->validate();
$this->formGenerator->showSubmitButton($showSubmitButton);
return $this->formGenerator->render($paymentRequest);
}
/**
* @param array $parameters
* @return bool
*/
public function paymentResponse(array $parameters)
{
$paymentResponse = new EcommercePaymentResponse($parameters);
$passphrase = $this->shaOut;
$shaComposer = new AllParametersShaComposer($passphrase);
$shaComposer->addParameterFilter(new ShaOutParameterFilter); //optional
if ($paymentResponse->isValid($shaComposer) && $paymentResponse->isSuccessful()) {
$event = new OgoneEvent($parameters);
$this->eventDispatcher->dispatch(OgoneEvents::SUCCESS, $event);
// handle payment confirmation
$this->logger->info('Ogone payment success');
return true;
} else {
$event = new OgoneEvent($parameters);
$this->eventDispatcher->dispatch(OgoneEvents::ERROR, $event);
$this->logger->warn('Ogone payment failure', $parameters);
}
return false;
}
/**
* @param string $locale
* @return string
*/
private function localeToIso($locale)
{
switch ($locale) {
case 'de':
return 'de_DE';
break;
case 'fr':
return 'fr_FR';
break;
case 'nl':
return 'nl_NL';
break;
case 'en':
return 'en_US';
break;
default:
return $locale;
break;
}
}
}