-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller-redirect.php
114 lines (97 loc) · 4.14 KB
/
controller-redirect.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
<?php
/*
* Copyright (c) 2016, Raphaël Droz <raphael.droz+floss@gmail.com>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
class WP_Paybox_RedirectController {
public function logger($args) {
call_user_func_array(['WP_Paybox_IPN', 'default_logger'], func_get_args() + [1 => NULL, 2=>'redir']);
}
public function init() {
$this->ensureValidConfiguration();
if($this::PERSIST_MODE === Payboxable::MODE_PERSIST_BEFORE_REDIRECT) {
$this->set();
}
else {
if($this->exists()) {
$this->create();
}
else {
$this->update();
}
}
list($params, $hmac) = $paybox->preparePayboxData($this);
self::makeHMACForm(WP_Paybox::getPayboxURL(), $params, $hmac);
return;
}
public static function makeHMACForm($PBX_PAYBOX, $params, $hmac) {
$pbx_parameters = $params;
$hmac = $hmac;
require( __DIR__ . '/templates/hmac-payment.php' );
}
public function create() {
// we are going to output a redirection to Paybox
// we want to communicate our entity ID as PBX_CMD
// Thus, here, just before the redirection gets done we create a persistent entity
$this->logger(sprintf("save(ref=%s, total=%.2f €)", $this->getUniqId(), $this->getAmount()));
}
public function update() {
$id_order = Order::getOrderByCartId($cart->id);
$uniqid = Order::getUniqReferenceOf($id_order);
if (! $uniqid) {
$this->logger("can't find a reference for id_cart={$cart->id} => id_order=$id_order. Exit.", LOG_ERR);
return displayError($paybox->l("missing order for this cart", 'redirect'));
}
$order = new Order($id_order);
if($order->total_paid_real > 0) { // TODO total_paid_real != total_paid ?
$this->logger("Existing order found: n°$id_order - $uniqid for cart {$cart->id}. But total_paid_real = {$order->total_paid_real} > 0. Exit", LOG_ERR);
exit; // TODO: redirect: invalid cart/order
}
if($order->module != 'paybox') {
$this->logger("Existing order found: n°$id_order - $uniqid for cart {$cart->id}. But order not created using paybox. Exit", LOG_ERR);
exit; // TODO: redirect: invalid cart/order
}
if($order->current_state != Configuration::get('PAYBOX_OS_AUTHORIZATION_PENDING') &&
$order->current_state != Configuration::get('PS_OS_ERROR')) {
$this->logger(sprintf("Existing order found: n°$id_order - $uniqid for cart {$cart->id}. But order state = %d which is different from the expected PAYBOX_OS_AUTHORIZATION_PENDING/%d value. Exit",
$order->current_state,
Configuration::get('PAYBOX_OS_AUTHORIZATION_PENDING')), LOG_ERR);
exit; // TODO: redirect: invalid cart/order
}
$this->logger("Existing order found: n°$id_order - $uniqid for cart {$cart->id}. total_paid_real = 0. Doing paybox redirect. Exit");
}
public function ensureValidConfiguration() {
$is_paybox3x = (int)WP_Paybox::opt('paybox3x');
$address = new Address((int)($cart->id_address_delivery));
$customer = new Customer((int)($cart->id_customer));
if (!Validate::isLoadedObject($address) OR !Validate::isLoadedObject($customer)) {
Tools::displayError($paybox->l('invalid address or customer', 'redirect'));
$this->setTemplate('errors.tpl');
return;
}
if (!Validate::isLoadedObject($this->context->shop)) {
Tools::displayError($paybox->l('invalid shop reference', 'redirect'));
$this->setTemplate('errors.tpl');
return;
}
}
// to use with
// add_action('init', ['WP_Paybox_RedirectController', 'default_endpoints']);
function default_endpoints() {
$url_path = trim(parse_url(add_query_arg([]), PHP_URL_PATH), '/');
if ( $url_path === 'paybox/confirm') {
}
if ( $url_path === 'paybox/redirect') {
// WP_Paybox_RedirectController
}
if ( $url_path === 'paybox/confirm') {
}
if ( $url_path === 'paybox/error') {
}
if ( $url_path === 'paybox/cancel') {
}
}
}