-
Notifications
You must be signed in to change notification settings - Fork 1
/
paypro.php
242 lines (193 loc) · 7.28 KB
/
paypro.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
<?php
use PrestaShop\PrestaShop\Core\Payment\PaymentOption;
if (!defined('_PS_VERSION_')) {
exit;
}
require_once __DIR__ . '/paypro/PayProHelper.php';
class PayPro extends PaymentModule {
private $paymentMethods;
public function __construct() {
$this->name = 'paypro';
$this->tab = 'payments_gateways';
$this->version = '1.1.0';
$this->ps_versions_compliancy = ['min' => '1.7', 'max' => _PS_VERSION_];
$this->author = 'PayPro';
$this->controllers = ['payment', 'validation', 'callback'];
$this->currencies = true;
$this->currencies_mode = 'checkbox';
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('PayPro');
$this->description = $this->l('PayPro payments');
if (!PayProHelper::getConfigValue('api_key', true)) {
$this->warning = $this->l('API key must be configured.');
}
if (!count(Currency::checkPaymentCurrencies($this->id))) {
$this->warning = $this->l('No currency has been set for this module.');
}
$this->paymentMethods = [
PayProHelper::AFTERPAY => ['label' => $this->l('Afterpay'), 'code' => 'afterpay/giro'],
PayProHelper::BANCONTACT => ['label' => $this->l('Bancontact'), 'code' => 'bancontact/mrcash'],
PayProHelper::IDEAL => ['label' => $this->l('iDEAL'), 'code' => 'ideal'],
PayProHelper::MASTERCARD => ['label' => $this->l('Mastercard'), 'code' => 'creditcard/mastercard'],
PayProHelper::PAYPAL => ['label' => $this->l('Paypal'), 'code' => 'paypal/direct'],
PayProHelper::SEPA => ['label' => $this->l('Banktransfer'), 'code' => 'banktransfer/sepa'],
PayProHelper::SEPA_ONCE => ['label' => $this->l('Direct Debit'), 'code' => 'directdebit/sepa-once'],
PayProHelper::SOFORT_DIGITAL => ['label' => $this->l('Sofort Digital'), 'code' => 'sofort/digital'],
PayProHelper::SOFORT_PHYSICAL => ['label' => $this->l('Sofort Physical'), 'code' => 'sofort/physical'],
PayProHelper::VISA => ['label' => $this->l('Visa'), 'code' => 'creditcard/visa'],
];
}
public function install() {
return parent::install() && $this->registerHook('paymentOptions') && $this->addOrderStatus() && Db::getInstance()->Execute(
'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'paypro` (`cart_id` int(11), `payment_hash` varchar(255)) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;');
}
public function uninstall() {
foreach (PayProHelper::getConfigFields() as $field) {
Configuration::deleteByName($field);
}
return Db::getInstance()->Execute( 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'paypro`') && parent::uninstall() && $this->removeOrderStatus();
}
private function addOrderStatus() {
$statusName = $this->l('Awaiting PayPro payment');
$states = OrderState::getOrderStates((int)$this->context->language->id);
// Check if order state exist
$stateExist = false;
foreach ($states as $state) {
if (in_array($statusName, $state)) {
$stateExist = true;
break;
}
}
// If the state does not exist, we create it
if (!$stateExist) {
$orderState = new OrderState();
$orderState->color = '#4169E1';
$orderState->send_email = false;
$orderState->module_name = $this->name;
$orderState->name = array();
$languages = Language::getLanguages(false);
foreach ($languages as $language)
$orderState->name[ $language['id_lang'] ] = $statusName;
// Update object
$orderState->add();
}
return true;
}
private function removeOrderStatus() {
$statusName = $this->l('Awaiting PayPro payment');
$states = OrderState::getOrderStates((int)$this->context->language->id);
foreach ($states as $state) {
if (in_array($statusName, $state)) {
$orderState = new OrderState($state['id_order_state']);
$orderState->deleted = 1;
$orderState->update();
}
}
return true;
}
public function getContent() {
// Check post
$postOutput = $this->handleSubmit();
$params = ['paymentMethods' => $this->paymentMethods];
foreach (PayProHelper::getConfigFields() as $field) {
$params[$field] = PayProHelper::getConfigValue($field);
}
$this->smarty->assign($params);
return $postOutput . $this->display(__FILE__, 'admin.tpl');
}
public function hookPaymentOptions($params) {
if (!$this->active) {
return;
}
// Only allow EUR
$currency = new Currency($params['cart']->id_currency);
if ($currency->iso_code !== 'EUR') {
return;
}
$paymentOptions = [];
foreach ($this->paymentMethods as $key => $paymentMethod) {
if (PayProHelper::getConfigValue($key) === '1') {
$paymentOptions[] = $this->createPaymentMethod($key, $paymentMethod['label'], $paymentMethod['code']);
}
}
return $paymentOptions;
}
private function createPaymentMethod($id, $label, $code) {
$paymentOption = new PaymentOption();
$inputs = [
'method' => [
'name' => 'method',
'type' => 'hidden',
'value' => $code,
]
];
if ($id === PayProHelper::IDEAL) {
$inputs['issuer'] = [
'name' => 'paypro_ideal_issuer',
'type' => 'hidden',
'value' => '',
];
$issuers = PayProHelper::createApi()->getIdealIssuers();
$this->smarty->assign(['issuers' => $issuers]);
$paymentOption->setAdditionalInformation($this->fetch('module:paypro/views/templates/hook/issuers.tpl'));
}
$paymentOption
->setCallToActionText($label)
->setInputs($inputs)
->setAction($this->context->link->getModuleLink($this->name, 'payment', [], true));
return $paymentOption;
}
private function handleSubmit() {
$apiKey = PayProHelper::getConfigValue('api_key', true);
if ($apiKey) {
foreach(PayProHelper::getConfigFields() as $configField) {
Configuration::updateValue($configField, PayProHelper::getConfigValue($configField));
}
return $this->displayConfirmation($this->l('Settings updated'));
}
return '';
}
private function getOrderStatusId() {
$statusName = $this->l('Awaiting PayPro payment');
$states = OrderState::getOrderStates((int)$this->context->language->id);
foreach ($states as $state) {
if (in_array($statusName, $state)) {
return $state['id_order_state'];
}
}
}
public function processPayment($cartID, $paymentHash) {
$api = PayProHelper::createApi();
$payment = $api->getPayment($paymentHash);
if ($payment) {
switch ($payment['current_status']) {
case 'completed':
$status = Configuration::get('PS_OS_PAYMENT');
break;
case 'cancelled':
$status = Configuration::get('PS_OS_ERROR');
break;
default:
$status = $this->getOrderStatusId();
}
// Order update
if (empty(Context::getContext()->link)) {
Context::getContext()->link = new Link(); // workaround a prestashop bug so email is sent
}
$orderID = Order::getIdByCartId($cartID);
if (!$orderID) {
// Create new order
$this->validateOrder($cartID, $status, $payment['amount_total'] / 100, $payment['pay_method'], null, [], null, false, $this->context->customer->secure_key);
} else {
$order = new Order($orderID);
if ($order->current_state !== $status) {
$orderHistory = new OrderHistory();
$orderHistory->id_order = $orderID;
$orderHistory->changeIdOrderState($status, $order, true);
$orderHistory->addWithemail(true);
}
}
}
}
}