forked from PrestaShopCorp/payplug
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipn.php
More file actions
executable file
·261 lines (235 loc) · 7.75 KB
/
ipn.php
File metadata and controls
executable file
·261 lines (235 loc) · 7.75 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
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
<?php
/**
* 2013 - 2014 PayPlug SAS
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* 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 license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PayPlug SAS
* @copyright 2013 - 2014 PayPlug SAS
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PayPlug SAS
*/
require_once(dirname(__FILE__).'/../../config/config.inc.php');
/** Call init.php to initialize context */
require_once(dirname(__FILE__).'/../../init.php');
require_once(dirname(__FILE__).'/classes/PayplugLock.php');
/** Tips to include class of module and backward_compatibility */
$payplug = Module::getInstanceByName('payplug');
/** Check if logs is enable */
if (Payplug::getConfiguration('PAYPLUG_DEBUG'))
{
/** Get display errors configuration */
$display_errors = @ini_get('display_errors');
/** Set display errors to true */
@ini_set('display_errors', true);
}
/**
* Check that payplug module is enabled
*/
if (!Payplug::moduleIsActive())
die('PayPlug module is not enabled.');
/**
* define getallheaders function for nginx web server
*/
if (!function_exists('getallheaders'))
{
function getallheaders()
{
$headers = array();
foreach ($_SERVER as $name => $value)
{
if (Tools::substr($name, 0, 5) == 'HTTP_')
{
$name = str_replace(' ', '-', ucwords(Tools::strtolower(str_replace('_', ' ', Tools::substr($name, 5)))));
$headers[$name] = $value;
}
else if ($name == 'CONTENT_TYPE')
$headers['Content-Type'] = $value;
else if ($name == 'CONTENT_LENGTH')
$headers['Content-Length'] = $value;
else
$headers[$name] = $value;
}
return $headers;
}
}
/**
* Get data from http request
*/
$headers = getallheaders();
/**
* Avoid problems with lowercase/uppercase transaformations
*/
$headers = array_change_key_case($headers, CASE_UPPER);
if (!isset($headers['PAYPLUG-SIGNATURE']))
{
header($_SERVER['SERVER_PROTOCOL'].' 403 Signature not provided', true, 403);
die;
}
$signature = base64_decode($headers['PAYPLUG-SIGNATURE']);
$body = Tools::file_get_contents('php://input');
/** Use the method of "Tools" for compatibility with future and older versions. */
$data = Tools::jsonDecode($body);
$status = (int)$data->status;
/** Available status */
$status_available = array(
Payplug::PAYMENT_STATUS_PAID,
Payplug::PAYMENT_STATUS_REFUND
);
/** if status is an available status */
if (in_array($status, $status_available))
{
$public_key = Configuration::get('PAYPLUG_MODULE_PUBLIC_KEY');
$check_signature = openssl_verify($body, $signature, $public_key, OPENSSL_ALGO_SHA1);
$bool_sign = false;
if ($check_signature == 1)
$bool_sign = true;
else if ($check_signature == 0)
{
echo 'Invalid signature';
header($_SERVER['SERVER_PROTOCOL'].' 403 Invalid signature', true, 403);
die;
}
else
{
echo 'Error while checking signature';
header($_SERVER['SERVER_PROTOCOL'].' 500 Error while checking signature', true, 500);
die;
}
if ($data && $bool_sign)
{
/** Data is an object */
$cart = new Cart($data->custom_data);
if (Validate::isLoadedObject($cart))
{
$address = new Address((int)$cart->id_address_invoice);
if (Validate::isLoadedObject($address))
{
Context::getContext()->country = new Country((int)$address->id_country);
Context::getContext()->customer = new Customer((int)$cart->id_customer);
Context::getContext()->language = new Language((int)$cart->id_lang);
Context::getContext()->currency = new Currency((int)$cart->id_currency);
PayplugLock::check($cart->id);
$order = new Order();
$order_id = $order->getOrderByCartId($cart->id);
/**
* If existing order
*/
if ($order_id)
{
/**
* If status paid
*/
if ($status == Payplug::PAYMENT_STATUS_PAID)
{
$order = new Order($order_id);
/** Get the right order status following module configuration (Sandbox or not) */
$order_state = Payplug::getOsConfiguration('waiting');
$current_state = $order->getCurrentState();
if ($current_state == $order_state)
{
$order_history = new OrderHistory();
/**
* Change order state to payment paid by payplug
*/
$order_history->id_order = $order_id;
/** Get the right order status following module configuration (Sandbox or not) */
$new_order_state = Payplug::getOsConfiguration('paid');
$order_history->changeIdOrderState((int)$new_order_state, $order_id);
$order_history->save();
if (version_compare(_PS_VERSION_, '1.5', '>') && version_compare(_PS_VERSION_, '1.5.2', '<'))
{
$order->current_state = $order_history->id_order_state;
$order->update();
}
}
}
/**
* If status refund
*/
else if ($status == Payplug::PAYMENT_STATUS_REFUND)
{
$order_history = new OrderHistory();
/**
* Change order state to refund by payplug
*/
$order_history->id_order = $order_id;
/** Get the right order status following module configuration (Sandbox or not) */
$new_order_state = Payplug::getOsConfiguration('refund');
$order_history->changeIdOrderState((int)$new_order_state, $order_id);
$order_history->save();
if (version_compare(_PS_VERSION_, '1.5', '>') && version_compare(_PS_VERSION_, '1.5.2', '<'))
{
$order->current_state = $order_history->id_order_state;
$order->update();
}
}
}
/**
* Else validate order
*/
else
{
PayplugLock::addLock($cart->id);
if ($status == Payplug::PAYMENT_STATUS_PAID)
{
$extra_vars = array();
/** Data is an object */
$extra_vars['transaction_id'] = $data->id_transaction;
$currency = (int)$cart->id_currency;
$customer = new Customer((int)$cart->id_customer);
/** Get the right order status following module configuration (Sandbox or not) */
$order_state = Payplug::getOsConfiguration('paid');
$amount = (float)$data->amount / 100;
$payplug->validateOrder($cart->id, $order_state, $amount, $payplug->displayName, null, $extra_vars, $currency, false, $customer->secure_key);
if (version_compare(_PS_VERSION_, '1.5', '>') && version_compare(_PS_VERSION_, '1.5.2', '<'))
{
$order_id = Order::getOrderByCartId($cart->id);
$order = new Order($order_id);
$order_payment = end($order->getOrderPayments());
$order_payment->transaction_id = $extra_vars['transaction_id'];
$order_payment->update();
}
}
PayplugLock::deleteLock($cart->id);
}
Configuration::updateValue('PAYPLUG_CONFIGURATION_OK', true);
}
else
{
echo 'Error : missing or wrong parameters.';
header($_SERVER['SERVER_PROTOCOL'].' 400 Missing or wrong parameters for address', true, 400);
die;
}
}
else
{
echo 'Error : missing or wrong parameters.';
header($_SERVER['SERVER_PROTOCOL'].' 400 Missing or wrong parameters for cart', true, 400);
die;
}
}
else
{
echo 'Error : missing or wrong parameters.';
header($_SERVER['SERVER_PROTOCOL'].' 400 Missing or wrong parameters', true, 400);
die;
}
}
/** Restore display errors configuration */
if (Payplug::getConfiguration('PAYPLUG_DEBUG'))
@ini_set('display_errors', $display_errors);