-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPayzenEmbedded.php
More file actions
215 lines (176 loc) · 7.63 KB
/
PayzenEmbedded.php
File metadata and controls
215 lines (176 loc) · 7.63 KB
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
<?php
/*************************************************************************************/
/* This file is part of the Thelia package. */
/* */
/* Copyright (c) OpenStudio */
/* email : dev@thelia.net */
/* web : http://www.thelia.net */
/* */
/* For the full copyright and license information, please view the LICENSE.txt */
/* file that was distributed with this source code. */
/*************************************************************************************/
namespace PayzenEmbedded;
use PayzenEmbedded\LyraClient\LyraJavascriptClientManagementWrapper;
use Propel\Runtime\Connection\ConnectionInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ServicesConfigurator;
use Thelia\Core\HttpFoundation\JsonResponse;
use Thelia\Core\HttpFoundation\Response;
use Thelia\Core\Template\ParserInterface;
use Thelia\Core\Translation\Translator;
use Thelia\Install\Database;
use Thelia\Model\Lang;
use Thelia\Model\LangQuery;
use Thelia\Model\Message;
use Thelia\Model\MessageQuery;
use Thelia\Model\ModuleImageQuery;
use Thelia\Model\Order;
use Thelia\Module\AbstractPaymentModule;
class PayzenEmbedded extends AbstractPaymentModule
{
/** @var string */
const DOMAIN_NAME = 'payzenembedded';
/** The confirmation message identifier */
const CONFIRMATION_MESSAGE_NAME = 'payzen_embedded_payment_confirmation';
/** The transaction update event identifier */
const TRANSACTION_UPDATE_EVENT = "payzenembedded.transaction_update_event";
/**
* Process a payment using the PayZen javascript client
*
* @param Order $order
*
* @return \Symfony\Component\HttpFoundation\Response
* @throws \Propel\Runtime\Exception\PropelException
*/
public function pay(Order $order)
{
// Use the embedded javascript client
$lyraClient = new LyraJavascriptClientManagementWrapper($this->getDispatcher());
$resultData = $lyraClient->payOrder($order);
$popupMode = PayzenEmbedded::getConfigValue('popup_mode', false);
if ($popupMode) {
// Pass the $resultData to the order-invoice script
return new JsonResponse($resultData);
} else {
/** @var ParserInterface $parser */
$parser = $this->getContainer()->get("thelia.parser");
$parser->setTemplateDefinition(
$parser->getTemplateHelper()->getActiveFrontTemplate(),
true
);
// Display the payment page which includes the javascript form.
$renderedTemplate = $parser->render(
"payzen-embedded/embedded-payment-page.html",
array_merge(
[
"order_id" => $order->getId(),
"cart_count" => $this->getRequest()->getSession()->getSessionCart($this->getDispatcher())->getCartItems()->count(),
],
$resultData
)
);
return new Response($renderedTemplate);
}
}
/**
*
* This method is call on Payment loop.
*
* If you return true, the payment method will de display
* If you return false, the payment method will not be display
*
* @return boolean
*/
public function isValidPayment()
{
$valid = false;
// CHeck if the module has been configured.
if (! empty(PayzenEmbedded::getConfigValue('site_id'))) {
$mode = self::getConfigValue('mode', false);
// If we're in test / restricted production mode, do not display module on the front office, except for allowed IP addresses.
if ('TEST' === $mode || $mode === 'PRODUCTION_RESTRICTED') {
$raw_ips = explode("\n", self::getConfigValue('allowed_ip_list', ''));
$allowed_client_ips = [];
foreach ($raw_ips as $ip) {
$allowed_client_ips[] = trim($ip);
}
$client_ip = $this->getRequest()->getClientIp();
$valid = in_array($client_ip, $allowed_client_ips);
} elseif ('PRODUCTION' == $mode) {
$valid = true;
}
if ($valid) {
// Check if total order amount is in the module's limits
$valid = $this->checkMinMaxAmount();
}
}
return $valid;
}
/**
* Check if total order amount is in the module's limits
*
* @return bool true if the current order total is within the min and max limits
*/
protected function checkMinMaxAmount()
{
// Check if total order amount is between the module's limits
$order_total = $this->getCurrentOrderTotalAmount();
$min_amount = self::getConfigValue('minimum_amount', 0);
$max_amount = self::getConfigValue('maximum_amount', 0);
return $order_total > 0 && ($min_amount <= 0 || $order_total >= $min_amount) && ($max_amount <= 0 || $order_total <= $max_amount);
}
/**
* @param ConnectionInterface|null $con
* @throws \Exception
*/
public function postActivation(ConnectionInterface $con = null): void
{
$languages = LangQuery::create()->find();
if (null === MessageQuery::create()->findOneByName(self::CONFIRMATION_MESSAGE_NAME)) {
$message = new Message();
$message
->setName(self::CONFIRMATION_MESSAGE_NAME)
->setHtmlLayoutFileName('')
->setHtmlTemplateFileName(self::CONFIRMATION_MESSAGE_NAME.'.html')
->setTextLayoutFileName('')
->setTextTemplateFileName(self::CONFIRMATION_MESSAGE_NAME.'.txt')
;
foreach ($languages as $language) {
/** @var Lang $language */
$locale = $language->getLocale();
$message->setLocale($locale);
$message->setTitle(
Translator::getInstance()->trans('Order payment confirmation', [], $locale)
);
$message->setSubject(
Translator::getInstance()->trans('Order {$order_ref} payment confirmation', [], $locale)
);
}
$message->save();
}
/* Deploy the module's image */
$module = $this->getModuleModel();
if (ModuleImageQuery::create()->filterByModule($module)->count() == 0) {
$this->deployImageFolder($module, sprintf('%s/images', __DIR__), $con);
}
}
public function preActivation(ConnectionInterface $con = null)
{
$database = new Database($con);
$database->insertSql(null, array(__DIR__ . '/Config/create.sql'));
return true;
}
public function destroy(ConnectionInterface $con = null, $deleteModuleData = false): void
{
if ($deleteModuleData) {
$database = new Database($con);
$database->insertSql(null, array(__DIR__ . '/Config/destroy.sql'));
}
}
public static function configureServices(ServicesConfigurator $servicesConfigurator): void
{
$servicesConfigurator->load(self::getModuleCode().'\\', __DIR__)
->exclude([THELIA_MODULE_DIR . ucfirst(self::getModuleCode()). "/I18n/*"])
->autowire(true)
->autoconfigure(true);
}
}