-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathps_cashondelivery.php
executable file
·297 lines (263 loc) · 11.8 KB
/
ps_cashondelivery.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
*/
if (!defined('_PS_VERSION_')) {
exit;
}
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
use PrestaShop\PrestaShop\Core\Payment\PaymentOption;
class Ps_Cashondelivery extends PaymentModule
{
const HOOKS = [
'displayOrderConfirmation',
'paymentOptions',
];
const CONFIG_OS_CASH_ON_DELIVERY = 'PS_OS_COD_VALIDATION';
/**
* {@inheritdoc}
*/
public function __construct()
{
$this->name = 'ps_cashondelivery';
$this->tab = 'payments_gateways';
$this->author = 'PrestaShop';
$this->version = '2.0.1';
$this->need_instance = 1;
$this->ps_versions_compliancy = ['min' => '1.7.6.0', 'max' => _PS_VERSION_];
$this->controllers = ['validation'];
$this->currencies = false;
parent::__construct();
$this->displayName = $this->trans('Cash on delivery (COD)', [], 'Modules.Cashondelivery.Admin');
$this->description = $this->trans('Accept cash payments on delivery to make it easy for customers to purchase on your store.', [], 'Modules.Cashondelivery.Admin');
}
/**
* {@inheritdoc}
*/
public function install()
{
return parent::install()
&& (bool) $this->registerHook(static::HOOKS)
&& $this->installOrderState();
}
/**
* @param array{cookie: Cookie, cart: Cart, altern: int} $params
*
* @return array|PaymentOption[] Should always returns an array to avoid issue
*/
public function hookPaymentOptions(array $params)
{
if (empty($params['cart'])) {
return [];
}
/** @var Cart $cart */
$cart = $params['cart'];
if ($cart->isVirtualCart()) {
return [];
}
$cashOnDeliveryOption = new PaymentOption();
$cashOnDeliveryOption->setModuleName($this->name);
$cashOnDeliveryOption->setCallToActionText($this->trans('Pay by Cash on Delivery', [], 'Modules.Cashondelivery.Shop'));
$cashOnDeliveryOption->setAction($this->context->link->getModuleLink($this->name, 'validation', [], true));
$cashOnDeliveryOption->setAdditionalInformation($this->fetch('module:ps_cashondelivery/views/templates/hook/paymentOptions-additionalInformation.tpl'));
return [$cashOnDeliveryOption];
}
/**
* @param array{cookie: Cookie, cart: Cart, altern: int, order: Order, objOrder: Order} $params
*
* @return string
*/
public function hookDisplayOrderConfirmation(array $params)
{
/** @var Order $order */
$order = (isset($params['objOrder'])) ? $params['objOrder'] : $params['order'];
if (!Validate::isLoadedObject($order) || $order->module !== $this->name) {
return '';
}
$this->context->smarty->assign([
'shop_name' => $this->context->shop->name,
'total' => $this->context->getCurrentLocale()->formatPrice($params['order']->getOrdersTotalPaid(), (new Currency($params['order']->id_currency))->iso_code),
'reference' => $order->reference,
'contact_url' => $this->context->link->getPageLink('contact', true),
]);
return $this->fetch('module:ps_cashondelivery/views/templates/hook/displayOrderConfirmation.tpl');
}
/**
* @return bool
*/
public function installOrderState()
{
if (Configuration::getGlobalValue(Ps_Cashondelivery::CONFIG_OS_CASH_ON_DELIVERY)) {
$orderState = new OrderState((int) Configuration::getGlobalValue(Ps_Cashondelivery::CONFIG_OS_CASH_ON_DELIVERY));
if (Validate::isLoadedObject($orderState) && $this->name === $orderState->module_name) {
return true;
}
}
return $this->createOrderState(
static::CONFIG_OS_CASH_ON_DELIVERY,
[
'en' => 'Awaiting Cash On Delivery validation',
'bs' => 'Čeka validaciju Plaćanje po dostavi',
'ca' => 'Esperant la validació del pagament contra reemborsament',
'da' => 'Afventer godkendelse af levering pr. efterkrav',
'de' => 'Warten auf Zahlungseingang Nachnahme',
'et' => 'Ootab sularaha kauba kättesaamisel kinnitust',
'es' => 'En espera de validación por contra reembolso.',
'mx' => 'En espera de validación por pago contra entrega',
'fr' => 'En attente de paiement à la livraison',
'qc' => 'En attente de paiement à la livraison',
'gl' => 'Agardando a validación do Pago Contra Reembolso',
'hr' => 'Awaiting cod validation',
'id' => 'Awaiting cod validation',
'it' => 'In attesa verifica contrassegno',
'lv' => 'Gaida skaidrās naudas apmaksas apstiprinājumu',
'hu' => 'Awaiting cod validation',
'nl' => 'Wachten op bevestiging (rembours)',
'no' => 'Awaiting cod validation',
'pl' => 'Oczekiwanie na płatność przy odbiorze',
'br' => 'Aguardando validação de pagamento na entrega',
'pt' => 'Awaiting cod validation',
'ro' => 'In asteptarea confirmarii platii la livrare',
'sq' => 'Në pritje të pagesës gjatë dorëzimit',
'sk' => 'Čaká sa na potvrdenie platby dobierkou',
'sr' => 'Čeka se potvrda keširanja pri isporuci',
'fi' => 'Odottaa maksuvahvistusta',
'sv' => 'Väntar på postförskott validering',
'tr' => 'Kapıda ödeme onayı bekleniyor',
'lt' => 'Awaiting cod validation',
'si' => 'Čaka potrdilo za plačilo po povzetju',
'vn' => 'Chờ xác nhận thanh toán COD',
'cs' => 'Čeká se na potvrzení dobírky',
'el' => 'Αναμονή επικύρωσης Αντικαταβολής',
'ru' => 'Ожидается подтверждение оплаты наличными',
'uk' => 'Очікується платіж післяплатою',
'bg' => 'В очакване на валидиране на плащане при доставка',
'mk' => 'Awaiting cod validation',
'he' => 'Awaiting cod validation',
'fa' => 'Awaiting cod validation',
'hi' => 'Awaiting Cash On Delivery validation',
'bn' => 'Awaiting cod validation',
'ar' => 'بإنتظار المصادقة على الدفع عند الإستلام',
'ja' => '代金引換払い確認待ち',
'zh' => '接受远程付费',
'tw' => '等待貨到付款驗證',
'ko' => '배송시 현금 지불 확인 대기',
],
true === (bool) version_compare(_PS_VERSION_, '1.7.7.0', '>=') ? '#4169E1' : '#34219E'
);
}
/**
* Create custom OrderState used for payment
*
* @param string $configurationKey Configuration key used to store OrderState identifier
* @param array $nameByLangIsoCode An array of name for all languages, default is en
* @param string $color Color of the label
* @param bool $isLogable consider the associated order as validated
* @param bool $isPaid set the order as paid
* @param bool $isInvoice allow a customer to download and view PDF versions of his/her invoices
* @param bool $isShipped set the order as shipped
* @param bool $isDelivery show delivery PDF
* @param bool $isPdfDelivery attach delivery slip PDF to email
* @param bool $isPdfInvoice attach invoice PDF to email
* @param bool $isSendEmail send an email to the customer when his/her order status has changed
* @param string $template Only letters, numbers and underscores are allowed. Email template for both .html and .txt
* @param bool $isHidden hide this status in all customer orders
* @param bool $isUnremovable Disallow delete action for this OrderState
* @param bool $isDeleted Set OrderState deleted
*
* @return bool
*/
private function createOrderState(
$configurationKey,
array $nameByLangIsoCode,
$color,
$isLogable = false,
$isPaid = false,
$isInvoice = false,
$isShipped = false,
$isDelivery = false,
$isPdfDelivery = false,
$isPdfInvoice = false,
$isSendEmail = false,
$template = '',
$isHidden = false,
$isUnremovable = true,
$isDeleted = false
) {
$tabNameByLangId = [];
foreach ($nameByLangIsoCode as $langIsoCode => $name) {
foreach (Language::getLanguages(false) as $language) {
if (Tools::strtolower($language['iso_code']) === $langIsoCode) {
$tabNameByLangId[(int) $language['id_lang']] = $name;
} elseif (isset($nameByLangIsoCode['en'])) {
$tabNameByLangId[(int) $language['id_lang']] = $nameByLangIsoCode['en'];
}
}
}
$orderState = new OrderState();
$orderState->module_name = $this->name;
$orderState->name = $tabNameByLangId;
$orderState->color = $color;
$orderState->logable = $isLogable;
$orderState->paid = $isPaid;
$orderState->invoice = $isInvoice;
$orderState->shipped = $isShipped;
$orderState->delivery = $isDelivery;
$orderState->pdf_delivery = $isPdfDelivery;
$orderState->pdf_invoice = $isPdfInvoice;
$orderState->send_email = $isSendEmail;
$orderState->hidden = $isHidden;
$orderState->unremovable = $isUnremovable;
$orderState->template = $template;
$orderState->deleted = $isDeleted;
$result = (bool) $orderState->add();
if (false === $result) {
$this->_errors[] = sprintf(
'Failed to create OrderState %s',
$configurationKey
);
return false;
}
$result = (bool) Configuration::updateGlobalValue($configurationKey, (int) $orderState->id);
if (false === $result) {
$this->_errors[] = sprintf(
'Failed to save OrderState %s to Configuration',
$configurationKey
);
return false;
}
$orderStateImgPath = $this->getLocalPath() . 'views/img/orderstate/' . $configurationKey . '.gif';
if (false === (bool) Tools::file_exists_cache($orderStateImgPath)) {
$this->_errors[] = sprintf(
'Failed to find icon file of OrderState %s',
$configurationKey
);
return false;
}
if (false === (bool) Tools::copy($orderStateImgPath, _PS_ORDER_STATE_IMG_DIR_ . $orderState->id . '.gif')) {
$this->_errors[] = sprintf(
'Failed to copy icon of OrderState %s',
$configurationKey
);
return false;
}
return true;
}
}