1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
//namespace empty
|
4
|
|
|
use Pagantis\ModuleUtils\Exception\OrderNotFoundException;
|
5
|
|
|
use Pagantis\OrdersApiClient\Model\Order\User\Address;
|
6
|
|
|
|
7
|
|
|
if (!defined('ABSPATH')) {
|
8
|
|
|
exit;
|
9
|
|
|
}
|
10
|
|
|
|
11
|
|
|
define('__ROOT__', dirname(dirname(__FILE__)));
|
12
|
|
|
|
13
|
|
|
class WcPagantisGateway extends WC_Payment_Gateway
|
|
|
|
|
14
|
|
|
{
|
15
|
|
|
const METHOD_ID = "pagantis";
|
16
|
|
|
|
17
|
|
|
/** Orders tablename */
|
18
|
|
|
const ORDERS_TABLE = 'cart_process';
|
19
|
|
|
|
20
|
|
|
/** Concurrency tablename */
|
21
|
|
|
const LOGS_TABLE = 'pagantis_logs';
|
22
|
|
|
|
23
|
|
|
const NOT_CONFIRMED = 'No se ha podido confirmar el pago';
|
24
|
|
|
|
25
|
|
|
/**
|
26
|
|
|
* WcPagantisGateway constructor.
|
27
|
|
|
*/
|
28
|
|
|
public function __construct()
|
29
|
|
|
{
|
30
|
|
|
//Mandatory vars for plugin
|
31
|
|
|
$this->id = WcPagantisGateway::METHOD_ID;
|
|
|
|
|
32
|
|
|
$this->icon = esc_url(plugins_url('../assets/images/logo.png', __FILE__));
|
|
|
|
|
33
|
|
|
$this->has_fields = true;
|
|
|
|
|
34
|
|
|
$this->method_title = ucfirst($this->id);
|
|
|
|
|
35
|
|
|
$this->title = getenv('PAGANTIS_TITLE');
|
|
|
|
|
36
|
|
|
|
37
|
|
|
//Useful vars
|
38
|
|
|
$this->template_path = plugin_dir_path(__FILE__) . '../templates/';
|
|
|
|
|
39
|
|
|
$this->allowed_currencies = array("EUR");
|
|
|
|
|
40
|
|
|
$this->mainFileLocation = dirname(plugin_dir_path(__FILE__)) . '/WC_Pagantis.php';
|
|
|
|
|
41
|
|
|
$this->plugin_info = get_file_data($this->mainFileLocation, array('Version' => 'Version'), false);
|
|
|
|
|
42
|
|
|
|
43
|
|
|
//Panel form fields
|
44
|
|
|
$this->form_fields = include(plugin_dir_path(__FILE__).'../includes/settings-pagantis.php');//Panel options
|
|
|
|
|
45
|
|
|
$this->init_settings();
|
46
|
|
|
|
47
|
|
|
$this->settings['ok_url'] = (getenv('PAGANTIS_URL_OK')!='')?getenv('PAGANTIS_URL_OK'):$this->generateOkUrl();
|
|
|
|
|
48
|
|
|
$this->settings['ko_url'] = (getenv('PAGANTIS_URL_KO')!='')?getenv('PAGANTIS_URL_KO'):$this->generateKoUrl();
|
49
|
|
|
foreach ($this->settings as $setting_key => $setting_value) {
|
50
|
|
|
$this->$setting_key = $setting_value;
|
51
|
|
|
}
|
52
|
|
|
|
53
|
|
|
//Hooks
|
54
|
|
|
add_action('woocommerce_update_options_payment_gateways_'.$this->id, array($this,'process_admin_options')); //Save plugin options
|
|
|
|
|
55
|
|
|
add_action('admin_notices', array($this, 'pagantisCheckFields')); //Check config fields
|
56
|
|
|
add_action('woocommerce_receipt_'.$this->id, array($this, 'pagantisReceiptPage')); //Pagantis form
|
57
|
|
|
add_action('woocommerce_api_wcpagantisgateway', array($this, 'pagantisNotification')); //Json Notification
|
58
|
|
|
add_filter('woocommerce_payment_complete_order_status', array($this,'pagantisCompleteStatus'), 10, 3);
|
|
|
|
|
59
|
|
|
add_filter('load_textdomain_mofile', array($this, 'loadPagantisTranslation'), 10, 2);
|
60
|
|
|
}
|
61
|
|
|
|
62
|
|
|
/**
|
63
|
|
|
* @param $mofile
|
64
|
|
|
* @param $domain
|
65
|
|
|
*
|
66
|
|
|
* @return string
|
67
|
|
|
*/
|
68
|
|
|
public function loadPagantisTranslation($mofile, $domain)
|
69
|
|
|
{
|
70
|
|
|
if ('pagantis' === $domain) {
|
71
|
|
|
$mofile = WP_LANG_DIR . '/../plugins/pagantis/languages/pagantis-' . get_locale() . '.mo';
|
|
|
|
|
72
|
|
|
}
|
73
|
|
|
return $mofile;
|
74
|
|
|
}
|
75
|
|
|
|
76
|
|
|
/***********
|
77
|
|
|
*
|
78
|
|
|
* HOOKS
|
79
|
|
|
*
|
80
|
|
|
***********/
|
81
|
|
|
|
82
|
|
|
/**
|
83
|
|
|
* PANEL - Display admin panel -> Hook: woocommerce_update_options_payment_gateways_pagantis
|
84
|
|
|
*/
|
85
|
|
|
public function admin_options()
|
86
|
|
|
{
|
87
|
|
|
$template_fields = array(
|
88
|
|
|
'panel_header' => $this->title,
|
89
|
|
|
'panel_description' => $this->method_description,
|
90
|
|
|
'button1_label' => __('Login to panel of ', 'pagantis') . ucfirst(WcPagantisGateway::METHOD_ID),
|
|
|
|
|
91
|
|
|
'button2_label' => __('Documentation', 'pagantis'),
|
92
|
|
|
'logo' => $this->icon,
|
93
|
|
|
'settings' => $this->generate_settings_html($this->form_fields, false)
|
94
|
|
|
);
|
95
|
|
|
wc_get_template('admin_header.php', $template_fields, '', $this->template_path);
|
|
|
|
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
/**
|
99
|
|
|
* PANEL - Check admin panel fields -> Hook: admin_notices
|
100
|
|
|
*/
|
101
|
|
|
public function pagantisCheckFields()
|
102
|
|
|
{
|
103
|
|
|
$error_string = '';
|
104
|
|
|
if ($this->settings['enabled'] !== 'yes') {
|
105
|
|
|
return;
|
106
|
|
|
} elseif (!version_compare(phpversion(), '5.3.0', '>=')) {
|
107
|
|
|
$error_string = __(' is not compatible with your php and/or curl version', 'pagantis');
|
|
|
|
|
108
|
|
|
$this->settings['enabled'] = 'no';
|
|
|
|
|
109
|
|
|
} elseif ($this->settings['pagantis_public_key']=="" || $this->settings['pagantis_private_key']=="") {
|
110
|
|
|
$error_string = __(' is not configured correctly, the fields Public Key and Secret Key are mandatory for use this plugin', 'pagantis');
|
111
|
|
|
$this->settings['enabled'] = 'no';
|
112
|
|
|
} elseif (!in_array(get_woocommerce_currency(), $this->allowed_currencies)) {
|
|
|
|
|
113
|
|
|
$error_string = __(' only can be used in Euros', 'pagantis');
|
114
|
|
|
$this->settings['enabled'] = 'no';
|
115
|
|
|
} elseif (getenv('PAGANTIS_SIMULATOR_MAX_INSTALLMENTS')<'2'
|
116
|
|
|
|| getenv('PAGANTIS_SIMULATOR_MAX_INSTALLMENTS')>'12') {
|
117
|
|
|
$error_string = __(' only can be payed from 2 to 12 installments', 'pagantis');
|
118
|
|
|
} elseif (getenv('PAGANTIS_SIMULATOR_START_INSTALLMENTS')<'2'
|
119
|
|
|
|| getenv('PAGANTIS_SIMULATOR_START_INSTALLMENTS')>'12') {
|
120
|
|
|
$error_string = __(' only can be payed from 2 to 12 installments', 'pagantis');
|
121
|
|
|
} elseif (getenv('PAGANTIS_DISPLAY_MIN_AMOUNT')<0) {
|
122
|
|
|
$error_string = __(' can not have a minimum amount less than 0', 'pagantis');
|
123
|
|
|
}
|
124
|
|
|
|
125
|
|
|
if ($error_string!='') {
|
126
|
|
|
$template_fields = array(
|
127
|
|
|
'error_msg' => ucfirst(WcPagantisGateway::METHOD_ID).' '.$error_string,
|
128
|
|
|
);
|
129
|
|
|
wc_get_template('error_msg.php', $template_fields, '', $this->template_path);
|
|
|
|
|
130
|
|
|
}
|
131
|
|
|
}
|
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/**
|
135
|
|
|
* CHECKOUT - Generate the pagantis form. "Return" iframe or redirect. - Hook: woocommerce_receipt_pagantis
|
136
|
|
|
* @param $order_id
|
137
|
|
|
*
|
138
|
|
|
* @throws Exception
|
139
|
|
|
*/
|
140
|
|
|
public function pagantisReceiptPage($order_id)
|
141
|
|
|
{
|
142
|
|
|
try {
|
143
|
|
|
require_once(__ROOT__.'/vendor/autoload.php');
|
144
|
|
|
global $woocommerce;
|
145
|
|
|
$order = new WC_Order($order_id);
|
|
|
|
|
146
|
|
|
$order->set_payment_method(ucfirst($this->id)); //Method showed in confirmation page.
|
147
|
|
|
$order->save();
|
148
|
|
|
|
149
|
|
|
if (!isset($order)) {
|
150
|
|
|
throw new Exception(_("Order not found"));
|
151
|
|
|
}
|
152
|
|
|
|
153
|
|
|
$shippingAddress = $order->get_address('shipping');
|
154
|
|
|
$billingAddress = $order->get_address('billing');
|
155
|
|
|
if ($shippingAddress['address_1'] == '') {
|
156
|
|
|
$shippingAddress = $billingAddress;
|
157
|
|
|
}
|
158
|
|
|
|
159
|
|
|
$userAddress = new Address();
|
160
|
|
|
$userAddress
|
161
|
|
|
->setZipCode($shippingAddress['postcode'])
|
162
|
|
|
->setFullName($shippingAddress['fist_name']." ".$shippingAddress['last_name'])
|
163
|
|
|
->setCountryCode('ES')
|
164
|
|
|
->setCity($shippingAddress['city'])
|
165
|
|
|
->setAddress($shippingAddress['address_1']." ".$shippingAddress['address_2'])
|
166
|
|
|
;
|
167
|
|
|
$orderShippingAddress = new Address();
|
168
|
|
|
$orderShippingAddress
|
169
|
|
|
->setZipCode($shippingAddress['postcode'])
|
170
|
|
|
->setFullName($shippingAddress['fist_name']." ".$shippingAddress['last_name'])
|
171
|
|
|
->setCountryCode('ES')
|
172
|
|
|
->setCity($shippingAddress['city'])
|
173
|
|
|
->setAddress($shippingAddress['address_1']." ".$shippingAddress['address_2'])
|
174
|
|
|
->setFixPhone($shippingAddress['phone'])
|
175
|
|
|
->setMobilePhone($shippingAddress['phone'])
|
176
|
|
|
;
|
177
|
|
|
$orderBillingAddress = new Address();
|
178
|
|
|
$orderBillingAddress
|
179
|
|
|
->setZipCode($billingAddress['postcode'])
|
180
|
|
|
->setFullName($billingAddress['fist_name']." ".$billingAddress['last_name'])
|
181
|
|
|
->setCountryCode('ES')
|
182
|
|
|
->setCity($billingAddress['city'])
|
183
|
|
|
->setAddress($billingAddress['address_1']." ".$billingAddress['address_2'])
|
184
|
|
|
->setFixPhone($billingAddress['phone'])
|
185
|
|
|
->setMobilePhone($billingAddress['phone'])
|
186
|
|
|
;
|
187
|
|
|
$orderUser = new \Pagantis\OrdersApiClient\Model\Order\User();
|
188
|
|
|
$orderUser
|
189
|
|
|
->setAddress($userAddress)
|
190
|
|
|
->setFullName($billingAddress['fist_name']." ".$billingAddress['last_name'])
|
191
|
|
|
->setBillingAddress($orderBillingAddress)
|
192
|
|
|
->setEmail($billingAddress['email'])
|
193
|
|
|
->setFixPhone($billingAddress['phone'])
|
194
|
|
|
->setMobilePhone($billingAddress['phone'])
|
195
|
|
|
->setShippingAddress($orderShippingAddress)
|
196
|
|
|
;
|
197
|
|
|
|
198
|
|
|
$previousOrders = $this->getOrders($order->get_user(), $billingAddress['email']);
|
199
|
|
|
foreach ($previousOrders as $previousOrder) {
|
200
|
|
|
$orderHistory = new \Pagantis\OrdersApiClient\Model\Order\User\OrderHistory();
|
201
|
|
|
$orderElement = wc_get_order($previousOrder);
|
|
|
|
|
202
|
|
|
$orderCreated = $orderElement->get_date_created();
|
203
|
|
|
$orderHistory
|
204
|
|
|
->setAmount(intval(100 * $orderElement->get_total()))
|
205
|
|
|
->setDate(new \DateTime($orderCreated->date('Y-m-d H:i:s')))
|
|
|
|
|
206
|
|
|
;
|
207
|
|
|
$orderUser->addOrderHistory($orderHistory);
|
208
|
|
|
}
|
209
|
|
|
|
210
|
|
|
$details = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details();
|
211
|
|
|
$shippingCost = $order->shipping_total;
|
212
|
|
|
$details->setShippingCost(intval(strval(100 * $shippingCost)));
|
213
|
|
|
$items = $woocommerce->cart->get_cart();
|
214
|
|
|
foreach ($items as $key => $item) {
|
215
|
|
|
$product = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart\Details\Product();
|
216
|
|
|
$product
|
217
|
|
|
->setAmount(intval(100 * $item['line_total']))
|
218
|
|
|
->setQuantity($item['quantity'])
|
219
|
|
|
->setDescription($item['data']->get_description());
|
220
|
|
|
$details->addProduct($product);
|
221
|
|
|
}
|
222
|
|
|
|
223
|
|
|
$orderShoppingCart = new \Pagantis\OrdersApiClient\Model\Order\ShoppingCart();
|
224
|
|
|
$orderShoppingCart
|
225
|
|
|
->setDetails($details)
|
226
|
|
|
->setOrderReference($order->get_id())
|
227
|
|
|
->setPromotedAmount(0)
|
228
|
|
|
->setTotalAmount(intval(strval(100 * $order->total)))
|
229
|
|
|
;
|
230
|
|
|
$orderConfigurationUrls = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Urls();
|
231
|
|
|
$cancelUrl = $this->getKoUrl($order);
|
232
|
|
|
$callback_arg = array(
|
233
|
|
|
'wc-api'=>'wcpagantisgateway',
|
234
|
|
|
'key'=>$order->get_order_key(),
|
235
|
|
|
'order-received'=>$order->get_id());
|
236
|
|
|
$callback_url = add_query_arg($callback_arg, home_url('/'));
|
|
|
|
|
237
|
|
|
$orderConfigurationUrls
|
238
|
|
|
->setCancel($cancelUrl)
|
239
|
|
|
->setKo($callback_url)
|
240
|
|
|
->setAuthorizedNotificationCallback($callback_url)
|
241
|
|
|
->setRejectedNotificationCallback($callback_url)
|
242
|
|
|
->setOk($callback_url)
|
243
|
|
|
;
|
244
|
|
|
$orderChannel = new \Pagantis\OrdersApiClient\Model\Order\Configuration\Channel();
|
245
|
|
|
$orderChannel
|
246
|
|
|
->setAssistedSale(false)
|
247
|
|
|
->setType(\Pagantis\OrdersApiClient\Model\Order\Configuration\Channel::ONLINE)
|
248
|
|
|
;
|
249
|
|
|
$orderConfiguration = new \Pagantis\OrdersApiClient\Model\Order\Configuration();
|
250
|
|
|
$orderConfiguration
|
251
|
|
|
->setChannel($orderChannel)
|
252
|
|
|
->setUrls($orderConfigurationUrls)
|
253
|
|
|
;
|
254
|
|
|
$metadataOrder = new \Pagantis\OrdersApiClient\Model\Order\Metadata();
|
255
|
|
|
$metadata = array(
|
256
|
|
|
'woocommerce' => WC()->version,
|
|
|
|
|
257
|
|
|
'pagantis' => $this->plugin_info['Version'],
|
258
|
|
|
'php' => phpversion()
|
259
|
|
|
);
|
260
|
|
|
foreach ($metadata as $key => $metadatum) {
|
261
|
|
|
$metadataOrder->addMetadata($key, $metadatum);
|
262
|
|
|
}
|
263
|
|
|
$orderApiClient = new \Pagantis\OrdersApiClient\Model\Order();
|
264
|
|
|
$orderApiClient
|
265
|
|
|
->setConfiguration($orderConfiguration)
|
266
|
|
|
->setMetadata($metadataOrder)
|
267
|
|
|
->setShoppingCart($orderShoppingCart)
|
268
|
|
|
->setUser($orderUser)
|
269
|
|
|
;
|
270
|
|
|
|
271
|
|
|
if ($this->pagantis_public_key=='' || $this->pagantis_private_key=='') {
|
272
|
|
|
throw new \Exception('Public and Secret Key not found');
|
273
|
|
|
}
|
274
|
|
|
$orderClient = new \Pagantis\OrdersApiClient\Client($this->pagantis_public_key, $this->pagantis_private_key);
|
275
|
|
|
$pagantisOrder = $orderClient->createOrder($orderApiClient);
|
276
|
|
|
if ($pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) {
|
|
|
|
|
277
|
|
|
$url = $pagantisOrder->getActionUrls()->getForm();
|
278
|
|
|
$this->insertRow($order->get_id(), $pagantisOrder->getId());
|
279
|
|
|
} else {
|
280
|
|
|
throw new OrderNotFoundException();
|
281
|
|
|
}
|
282
|
|
|
|
283
|
|
|
if ($url=="") {
|
284
|
|
|
throw new Exception(_("No ha sido posible obtener una respuesta de Pagantis"));
|
285
|
|
|
} elseif (getenv('PAGANTIS_FORM_DISPLAY_TYPE')=='0') {
|
286
|
|
|
wp_redirect($url);
|
|
|
|
|
287
|
|
|
exit;
|
|
|
|
|
288
|
|
|
} else {
|
289
|
|
|
$template_fields = array(
|
290
|
|
|
'url' => $url,
|
291
|
|
|
'checkoutUrl' => $cancelUrl
|
292
|
|
|
);
|
293
|
|
|
wc_get_template('iframe.php', $template_fields, '', $this->template_path);
|
|
|
|
|
294
|
|
|
}
|
295
|
|
|
} catch (\Exception $exception) {
|
296
|
|
|
wc_add_notice(__('Payment error ', 'pagantis') . $exception->getMessage(), 'error');
|
|
|
|
|
297
|
|
|
$checkout_url = get_permalink(wc_get_page_id('checkout'));
|
|
|
|
|
298
|
|
|
wp_redirect($checkout_url);
|
299
|
|
|
exit;
|
|
|
|
|
300
|
|
|
}
|
301
|
|
|
}
|
302
|
|
|
|
303
|
|
|
/**
|
304
|
|
|
* NOTIFICATION - Endpoint for Json notification - Hook: woocommerce_api_wcpagantisgateway
|
305
|
|
|
*/
|
306
|
|
|
public function pagantisNotification()
|
307
|
|
|
{
|
308
|
|
|
try {
|
309
|
|
|
$origin = ($_SERVER['REQUEST_METHOD'] == 'POST') ? 'Notify' : 'Order';
|
310
|
|
|
|
311
|
|
|
include_once('notifyController.php');
|
312
|
|
|
$notify = new WcPagantisNotify();
|
313
|
|
|
$notify->setOrigin($origin);
|
314
|
|
|
/** @var \Pagantis\ModuleUtils\Model\Response\AbstractJsonResponse $result */
|
315
|
|
|
$result = $notify->processInformation();
|
316
|
|
|
} catch (Exception $exception) {
|
317
|
|
|
$result['notification_message'] = $exception->getMessage();
|
318
|
|
|
$result['notification_error'] = true;
|
319
|
|
|
}
|
320
|
|
|
|
321
|
|
|
$paymentOrder = new WC_Order($result->getMerchantOrderId());
|
322
|
|
|
if ($paymentOrder instanceof WC_Order) {
|
323
|
|
|
$orderStatus = strtolower($paymentOrder->get_status());
|
324
|
|
|
} else {
|
325
|
|
|
$orderStatus = 'cancelled';
|
326
|
|
|
}
|
327
|
|
|
$acceptedStatus = array('processing', 'completed');
|
328
|
|
|
if (in_array($orderStatus, $acceptedStatus)) {
|
329
|
|
|
$returnUrl = $this->getOkUrl($paymentOrder);
|
330
|
|
|
} else {
|
331
|
|
|
$returnUrl = $this->getKoUrl($paymentOrder);
|
332
|
|
|
}
|
333
|
|
|
|
334
|
|
|
wp_redirect($returnUrl);
|
|
|
|
|
335
|
|
|
exit;
|
|
|
|
|
336
|
|
|
}
|
337
|
|
|
|
338
|
|
|
/**
|
339
|
|
|
* After failed status, set to processing not complete -> Hook: woocommerce_payment_complete_order_status
|
340
|
|
|
* @param $status
|
341
|
|
|
* @param $order_id
|
342
|
|
|
* @param $order
|
343
|
|
|
*
|
344
|
|
|
* @return string
|
345
|
|
|
*/
|
346
|
|
|
public function pagantisCompleteStatus($status, $order_id, $order)
|
347
|
|
|
{
|
348
|
|
|
if ($order->get_payment_method() == WcPagantisGateway::METHOD_ID) {
|
349
|
|
|
if ($order->get_status() == 'failed') {
|
350
|
|
|
$status = 'processing';
|
351
|
|
|
} elseif ($order->get_status() == 'pending' && $status=='completed') {
|
352
|
|
|
$status = 'processing';
|
353
|
|
|
}
|
354
|
|
|
}
|
355
|
|
|
|
356
|
|
|
return $status;
|
357
|
|
|
}
|
358
|
|
|
|
359
|
|
|
/***********
|
360
|
|
|
*
|
361
|
|
|
* REDEFINED FUNCTIONS
|
362
|
|
|
*
|
363
|
|
|
***********/
|
364
|
|
|
|
365
|
|
|
/**
|
366
|
|
|
* CHECKOUT - Check if payment method is available (called by woocommerce, can't apply cammel caps)
|
367
|
|
|
* @return bool
|
368
|
|
|
*/
|
369
|
|
|
public function is_available()
|
370
|
|
|
{
|
371
|
|
|
if ($this->enabled==='yes' && $this->pagantis_public_key!='' && $this->pagantis_private_key!='' &&
|
372
|
|
|
$this->get_order_total()>getenv('PAGANTIS_DISPLAY_MIN_AMOUNT')) {
|
373
|
|
|
return true;
|
374
|
|
|
}
|
375
|
|
|
|
376
|
|
|
return false;
|
377
|
|
|
}
|
378
|
|
|
|
379
|
|
|
/**
|
380
|
|
|
* CHECKOUT - Checkout + admin panel title(method_title - get_title) (called by woocommerce,can't apply cammel caps)
|
381
|
|
|
* @return string
|
382
|
|
|
*/
|
383
|
|
|
public function get_title()
|
384
|
|
|
{
|
385
|
|
|
return getenv('PAGANTIS_TITLE');
|
386
|
|
|
}
|
387
|
|
|
|
388
|
|
|
/**
|
389
|
|
|
* CHECKOUT - Called after push pagantis button on checkout(called by woocommerce, can't apply cammel caps
|
390
|
|
|
* @param $order_id
|
391
|
|
|
* @return array
|
392
|
|
|
*/
|
393
|
|
|
public function process_payment($order_id)
|
394
|
|
|
{
|
395
|
|
|
try {
|
396
|
|
|
$order = new WC_Order($order_id);
|
397
|
|
|
|
398
|
|
|
$redirectUrl = $order->get_checkout_payment_url(true); //pagantisReceiptPage function
|
399
|
|
|
if (strpos($redirectUrl, 'order-pay=')===false) {
|
400
|
|
|
$redirectUrl.= "&order-pay=".$order_id;
|
401
|
|
|
}
|
402
|
|
|
|
403
|
|
|
return array(
|
404
|
|
|
'result' => 'success',
|
405
|
|
|
'redirect' => $redirectUrl
|
406
|
|
|
);
|
407
|
|
|
|
408
|
|
|
} catch (Exception $e) {
|
409
|
|
|
wc_add_notice(__('Payment error ', 'pagantis') . $e->getMessage(), 'error');
|
|
|
|
|
410
|
|
|
return array();
|
411
|
|
|
}
|
412
|
|
|
}
|
413
|
|
|
|
414
|
|
|
/**
|
415
|
|
|
* CHECKOUT - simulator (called by woocommerce, can't apply cammel caps)
|
416
|
|
|
*/
|
417
|
|
|
public function payment_fields()
|
418
|
|
|
{
|
419
|
|
|
$template_fields = array(
|
420
|
|
|
'public_key' => $this->pagantis_public_key,
|
421
|
|
|
'total' => WC()->session->cart_totals['total'],
|
|
|
|
|
422
|
|
|
'enabled' => $this->settings['enabled'],
|
423
|
|
|
'min_installments' => getenv('PAGANTIS_DISPLAY_MIN_AMOUNT'),
|
424
|
|
|
'message' => __(getenv('PAGANTIS_TITLE_EXTRA'))
|
|
|
|
|
425
|
|
|
);
|
426
|
|
|
wc_get_template('checkout_description.php', $template_fields, '', $this->template_path);
|
|
|
|
|
427
|
|
|
}
|
428
|
|
|
|
429
|
|
|
/***********
|
430
|
|
|
*
|
431
|
|
|
* UTILS FUNCTIONS
|
432
|
|
|
*
|
433
|
|
|
***********/
|
434
|
|
|
|
435
|
|
|
/**
|
436
|
|
|
* PANEL KO_URL FIELD
|
437
|
|
|
* CHECKOUT PAGE => ?page_id=91 // ORDER-CONFIRMATION PAGE => ?page_id=91&order-pay=<order_id>&key=<order_key>
|
438
|
|
|
*/
|
439
|
|
|
private function generateOkUrl()
|
440
|
|
|
{
|
441
|
|
|
return $this->generateUrl($this->get_return_url());
|
442
|
|
|
}
|
443
|
|
|
|
444
|
|
|
/**
|
445
|
|
|
* PANEL OK_URL FIELD
|
446
|
|
|
*/
|
447
|
|
|
private function generateKoUrl()
|
448
|
|
|
{
|
449
|
|
|
return $this->generateUrl(get_permalink(wc_get_page_id('checkout')));
|
|
|
|
|
450
|
|
|
}
|
451
|
|
|
|
452
|
|
|
/**
|
453
|
|
|
* Replace empty space by {{var}}
|
454
|
|
|
* @param $url
|
455
|
|
|
*
|
456
|
|
|
* @return string
|
457
|
|
|
*/
|
458
|
|
|
private function generateUrl($url)
|
459
|
|
|
{
|
460
|
|
|
$parsed_url = parse_url($url);
|
461
|
|
|
if ($parsed_url !== false) {
|
462
|
|
|
$parsed_url['query'] = !isset($parsed_url['query']) ? '' : $parsed_url['query'];
|
463
|
|
|
parse_str($parsed_url['query'], $arrayParams);
|
464
|
|
|
foreach ($arrayParams as $keyParam => $valueParam) {
|
465
|
|
|
if ($valueParam=='') {
|
466
|
|
|
$arrayParams[$keyParam] = '{{'.$keyParam.'}}';
|
467
|
|
|
}
|
468
|
|
|
}
|
469
|
|
|
$parsed_url['query'] = http_build_query($arrayParams);
|
470
|
|
|
$return_url = $this->unparseUrl($parsed_url);
|
471
|
|
|
return urldecode($return_url);
|
472
|
|
|
} else {
|
473
|
|
|
return $url;
|
474
|
|
|
}
|
475
|
|
|
}
|
476
|
|
|
|
477
|
|
|
/**
|
478
|
|
|
* Replace {{}} by vars values inside ok_url
|
479
|
|
|
* @param $order
|
480
|
|
|
*
|
481
|
|
|
* @return string
|
482
|
|
|
*/
|
483
|
|
|
private function getOkUrl($order)
|
484
|
|
|
{
|
485
|
|
|
return $this->getKeysUrl($order, $this->ok_url);
|
486
|
|
|
}
|
487
|
|
|
|
488
|
|
|
/**
|
489
|
|
|
* Replace {{}} by vars values inside ko_url
|
490
|
|
|
* @param $order
|
491
|
|
|
*
|
492
|
|
|
* @return string
|
493
|
|
|
*/
|
494
|
|
|
private function getKoUrl($order)
|
495
|
|
|
{
|
496
|
|
|
return $this->getKeysUrl($order, $this->ko_url);
|
497
|
|
|
}
|
498
|
|
|
|
499
|
|
|
/**
|
500
|
|
|
* Replace {{}} by vars values
|
501
|
|
|
* @param $order
|
502
|
|
|
* @param $url
|
503
|
|
|
*
|
504
|
|
|
* @return string
|
505
|
|
|
*/
|
506
|
|
|
private function getKeysUrl($order, $url)
|
507
|
|
|
{
|
508
|
|
|
$defaultFields = (get_class($order)=='WC_Order') ?
|
509
|
|
|
array('order-received'=>$order->get_id(), 'key'=>$order->get_order_key()) :
|
510
|
|
|
array();
|
511
|
|
|
|
512
|
|
|
$parsedUrl = parse_url($url);
|
513
|
|
|
if ($parsedUrl !== false) {
|
514
|
|
|
//Replace parameters from url
|
515
|
|
|
$parsedUrl['query'] = $this->getKeysParametersUrl($parsedUrl['query'], $defaultFields);
|
516
|
|
|
|
517
|
|
|
//Replace path from url
|
518
|
|
|
$parsedUrl['path'] = $this->getKeysPathUrl($parsedUrl['path'], $defaultFields);
|
519
|
|
|
|
520
|
|
|
$returnUrl = $this->unparseUrl($parsedUrl);
|
521
|
|
|
return $returnUrl;
|
522
|
|
|
}
|
523
|
|
|
return $url;
|
524
|
|
|
}
|
525
|
|
|
|
526
|
|
|
/**
|
527
|
|
|
* Replace {{}} by vars values inside parameters
|
528
|
|
|
* @param $queryString
|
529
|
|
|
* @param $defaultFields
|
530
|
|
|
*
|
531
|
|
|
* @return string
|
532
|
|
|
*/
|
533
|
|
|
private function getKeysParametersUrl($queryString, $defaultFields)
|
534
|
|
|
{
|
535
|
|
|
parse_str(html_entity_decode($queryString), $arrayParams);
|
536
|
|
|
$commonKeys = array_intersect_key($arrayParams, $defaultFields);
|
537
|
|
|
if (count($commonKeys)) {
|
538
|
|
|
$arrayResult = array_merge($arrayParams, $defaultFields);
|
539
|
|
|
} else {
|
540
|
|
|
$arrayResult = $arrayParams;
|
541
|
|
|
}
|
542
|
|
|
return urldecode(http_build_query($arrayResult));
|
543
|
|
|
}
|
544
|
|
|
|
545
|
|
|
/**
|
546
|
|
|
* Replace {{}} by vars values inside path
|
547
|
|
|
* @param $pathString
|
548
|
|
|
* @param $defaultFields
|
549
|
|
|
*
|
550
|
|
|
* @return string
|
551
|
|
|
*/
|
552
|
|
|
private function getKeysPathUrl($pathString, $defaultFields)
|
553
|
|
|
{
|
554
|
|
|
$arrayParams = explode("/", $pathString);
|
555
|
|
|
foreach ($arrayParams as $keyParam => $valueParam) {
|
556
|
|
|
preg_match('#\{{.*?}\}#', $valueParam, $match);
|
557
|
|
|
if (count($match)) {
|
558
|
|
|
$key = str_replace(array('{{','}}'), array('',''), $match[0]);
|
559
|
|
|
$arrayParams[$keyParam] = $defaultFields[$key];
|
560
|
|
|
}
|
561
|
|
|
}
|
562
|
|
|
return implode('/', $arrayParams);
|
563
|
|
|
}
|
564
|
|
|
|
565
|
|
|
/**
|
566
|
|
|
* Replace {{var}} by empty space
|
567
|
|
|
* @param $parsed_url
|
568
|
|
|
*
|
569
|
|
|
* @return string
|
570
|
|
|
*/
|
571
|
|
|
private function unparseUrl($parsed_url)
|
572
|
|
|
{
|
573
|
|
|
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
|
574
|
|
|
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
|
575
|
|
|
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
|
576
|
|
|
$query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
|
577
|
|
|
$fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
|
578
|
|
|
$path = $parsed_url['path'];
|
579
|
|
|
return $scheme . $host . $port . $path . $query . $fragment;
|
580
|
|
|
}
|
581
|
|
|
|
582
|
|
|
/**
|
583
|
|
|
* Get the orders of a customer
|
584
|
|
|
* @param $current_user
|
585
|
|
|
* @param $billingEmail
|
586
|
|
|
*
|
587
|
|
|
* @return mixed
|
588
|
|
|
*/
|
589
|
|
|
private function getOrders($current_user, $billingEmail)
|
590
|
|
|
{
|
591
|
|
|
$sign_up = '';
|
592
|
|
|
$total_orders = 0;
|
|
|
|
|
593
|
|
|
$total_amt = 0;
|
|
|
|
|
594
|
|
|
$refund_amt = 0;
|
|
|
|
|
595
|
|
|
$total_refunds = 0;
|
|
|
|
|
596
|
|
|
$partial_refunds = 0;
|
|
|
|
|
597
|
|
|
if ($current_user->user_login) {
|
598
|
|
|
$is_guest = "false";
|
|
|
|
|
599
|
|
|
$sign_up = substr($current_user->user_registered, 0, 10);
|
|
|
|
|
600
|
|
|
$customer_orders = get_posts(array(
|
|
|
|
|
601
|
|
|
'numberposts' => - 1,
|
602
|
|
|
'meta_key' => '_customer_user',
|
603
|
|
|
'meta_value' => $current_user->ID,
|
604
|
|
|
'post_type' => array( 'shop_order' ),
|
605
|
|
|
'post_status' => array( 'wc-completed', 'wc-processing', 'wc-refunded' ),
|
606
|
|
|
));
|
607
|
|
|
} else {
|
608
|
|
|
$is_guest = "true";
|
609
|
|
|
$customer_orders = get_posts(array(
|
610
|
|
|
'numberposts' => - 1,
|
611
|
|
|
'meta_key' => '_billing_email',
|
612
|
|
|
'meta_value' => $billingEmail,
|
613
|
|
|
'post_type' => array( 'shop_order' ),
|
614
|
|
|
'post_status' => array( 'wc-completed', 'wc-processing', 'wc-refunded'),
|
615
|
|
|
));
|
616
|
|
|
foreach ($customer_orders as $customer_order) {
|
617
|
|
|
if (trim($sign_up)=='' ||
|
618
|
|
|
strtotime(substr($customer_order->post_date, 0, 10)) <= strtotime($sign_up)) {
|
619
|
|
|
$sign_up = substr($customer_order->post_date, 0, 10);
|
620
|
|
|
}
|
621
|
|
|
}
|
622
|
|
|
}
|
623
|
|
|
|
624
|
|
|
return $customer_orders;
|
625
|
|
|
}
|
626
|
|
|
|
627
|
|
|
|
628
|
|
|
/**
|
629
|
|
|
* @param $orderId
|
630
|
|
|
* @param $pagantisOrderId
|
631
|
|
|
*
|
632
|
|
|
* @throws Exception
|
633
|
|
|
*/
|
634
|
|
|
private function insertRow($orderId, $pagantisOrderId)
|
635
|
|
|
{
|
636
|
|
|
global $wpdb;
|
637
|
|
|
$this->checkDbTable();
|
638
|
|
|
$tableName = $wpdb->prefix.self::ORDERS_TABLE;
|
639
|
|
|
|
640
|
|
|
//Check if id exists
|
641
|
|
|
$resultsSelect = $wpdb->get_results("select * from $tableName where id='$orderId'");
|
642
|
|
|
$countResults = count($resultsSelect);
|
643
|
|
|
if ($countResults == 0) {
|
644
|
|
|
$wpdb->insert(
|
645
|
|
|
$tableName,
|
646
|
|
|
array('id' => $orderId, 'order_id' => $pagantisOrderId),
|
647
|
|
|
array('%d', '%s')
|
648
|
|
|
);
|
649
|
|
|
} else {
|
650
|
|
|
$wpdb->update(
|
651
|
|
|
$tableName,
|
652
|
|
|
array('order_id' => $pagantisOrderId),
|
653
|
|
|
array('id' => $orderId),
|
654
|
|
|
array('%s'),
|
655
|
|
|
array('%d')
|
656
|
|
|
);
|
657
|
|
|
}
|
658
|
|
|
}
|
659
|
|
|
|
660
|
|
|
/**
|
661
|
|
|
* Check if orders table exists
|
662
|
|
|
*/
|
663
|
|
|
private function checkDbTable()
|
664
|
|
|
{
|
665
|
|
|
global $wpdb;
|
666
|
|
|
$tableName = $wpdb->prefix.self::ORDERS_TABLE;
|
667
|
|
|
|
668
|
|
|
if ($wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName) {
|
669
|
|
|
$charset_collate = $wpdb->get_charset_collate();
|
670
|
|
|
$sql = "CREATE TABLE $tableName ( id int, order_id varchar(50), wc_order_id varchar(50),
|
671
|
|
|
UNIQUE KEY id (id)) $charset_collate";
|
672
|
|
|
|
673
|
|
|
require_once(ABSPATH.'wp-admin/includes/upgrade.php');
|
|
|
|
|
674
|
|
|
dbDelta($sql);
|
|
|
|
|
675
|
|
|
}
|
676
|
|
|
}
|
677
|
|
|
}
|
678
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths