/
ipn.php
334 lines (284 loc) · 10.8 KB
/
ipn.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/* Copyright (C) 2018 Thibault FOUCART <support@ptibogxiv.net>
* Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
define("NOLOGIN", 1); // This means this output page does not require to be logged.
define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
$entity=(! empty($_GET['entity']) ? (int) $_GET['entity'] : (! empty($_POST['entity']) ? (int) $_POST['entity'] : 1));
if (is_numeric($entity)) define("DOLENTITY", $entity);
require '../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/ccountry.class.php';
require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
require_once DOL_DOCUMENT_ROOT.'/includes/stripe/init.php';
require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';
if (empty($conf->stripe->enabled)) accessforbidden('', 0, 0, 1);
// You can find your endpoint's secret in your webhook settings
if (isset($_GET['connect']))
{
if (isset($_GET['test']))
{
$endpoint_secret = $conf->global->STRIPE_TEST_WEBHOOK_CONNECT_KEY;
$service = 'StripeTest';
$servicestatus = 0;
}
else
{
$endpoint_secret = $conf->global->STRIPE_LIVE_WEBHOOK_CONNECT_KEY;
$service = 'StripeLive';
$servicestatus = 1;
}
}
else {
if (isset($_GET['test']))
{
$endpoint_secret = $conf->global->STRIPE_TEST_WEBHOOK_KEY;
$service = 'StripeTest';
$servicestatus = 0;
}
else
{
$endpoint_secret = $conf->global->STRIPE_LIVE_WEBHOOK_KEY;
$service = 'StripeLive';
$servicestatus = 1;
}
}
if (empty($endpoint_secret))
{
print 'Error: Setup of module Stripe not complete for mode '.$service.'. The WEBHOOK_KEY is not defined.';
http_response_code(400); // PHP 5.4 or greater
exit();
}
/*
* Actions
*/
$payload = @file_get_contents("php://input");
$sig_header = $_SERVER["HTTP_STRIPE_SIGNATURE"];
$event = null;
$error = 0;
try {
$event = \Stripe\Webhook::constructEvent($payload, $sig_header, $endpoint_secret);
}
catch(\UnexpectedValueException $e) {
// Invalid payload
http_response_code(400); // PHP 5.4 or greater
exit();
} catch(\Stripe\Error\SignatureVerification $e) {
// Invalid signature
http_response_code(400); // PHP 5.4 or greater
exit();
}
// Do something with $event
$langs->load("main");
// TODO Do we really need a user in setup just to have a name to fill an email topic when it is a technical system notification email
$user = new User($db);
$user->fetch($conf->global->STRIPE_USER_ACCOUNT_FOR_ACTIONS);
$user->getrights();
if (! empty($conf->multicompany->enabled) && ! empty($conf->stripeconnect->enabled) && is_object($mc))
{
$sql = "SELECT entity";
$sql.= " FROM ".MAIN_DB_PREFIX."oauth_token";
$sql.= " WHERE service = '".$db->escape($service)."' and tokenstring = '%".$db->escape($event->account)."%'";
dol_syslog(get_class($db) . "::fetch", LOG_DEBUG);
$result = $db->query($sql);
if ($result)
{
if ($db->num_rows($result))
{
$obj = $db->fetch_object($result);
$key=$obj->entity;
}
else {
$key=1;
}
}
else {
$key=1;
}
$ret=$mc->switchEntity($key);
if (! $res && file_exists("../../main.inc.php")) $res=@include "../../main.inc.php";
if (! $res) die("Include of main fails");
}
// list of action
$stripe=new Stripe($db);
// Subject
$societeName = $conf->global->MAIN_INFO_SOCIETE_NOM;
if (! empty($conf->global->MAIN_APPLICATION_TITLE)) $societeName = $conf->global->MAIN_APPLICATION_TITLE;
dol_syslog("Stripe IPN was called with event->type = ".$event->type);
if ($event->type == 'payout.created') {
$error=0;
$result=dolibarr_set_const($db, $service."_NEXTPAYOUT", date('Y-m-d H:i:s', $event->data->object->arrival_date), 'chaine', 0, '', $conf->entity);
if ($result > 0)
{
$subject = $societeName.' - [NOTIFICATION] Stripe payout scheduled';
if (!empty($user->email)) {
$sendto = dolGetFirstLastname($user->firstname, $user->lastname) . " <".$user->email.">";
} else {
$sendto = $conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>';
}
$replyto = $sendto;
$sendtocc = '';
if (!empty($conf->global->ONLINE_PAYMENT_SENDEMAIL)) {
$sendtocc = $conf->global->ONLINE_PAYMENT_SENDEMAIL.'" <'.$conf->global->ONLINE_PAYMENT_SENDEMAIL.'>';
}
$message = "A bank transfer of ".price2num($event->data->object->amount/100)." ".$event->data->object->currency." should arrive in your account the ".dol_print_date($event->data->object->arrival_date, 'dayhour');
$mailfile = new CMailFile(
$subject,
$sendto,
$replyto,
$message,
array(),
array(),
array(),
$sendtocc,
'',
0,
-1
);
$ret = $mailfile->sendfile();
http_response_code(200); // PHP 5.4 or greater
return 1;
}
else
{
$error++;
http_response_code(500); // PHP 5.4 or greater
return -1;
}
}
elseif ($event->type == 'payout.paid') {
global $conf;
$error=0;
$result=dolibarr_set_const($db, $service."_NEXTPAYOUT", null, 'chaine', 0, '', $conf->entity);
if ($result)
{
$langs->load("errors");
$dateo = dol_now();
$label = $event->data->object->description;
$amount= $event->data->object->amount/100;
$amount_to= $event->data->object->amount/100;
require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
$accountfrom=new Account($db);
$accountfrom->fetch($conf->global->STRIPE_BANK_ACCOUNT_FOR_PAYMENTS);
$accountto=new Account($db);
$accountto->fetch($conf->global->STRIPE_BANK_ACCOUNT_FOR_BANKTRANSFERS);
if (($accountto->id != $accountfrom->id) && empty($error))
{
$bank_line_id_from=0;
$bank_line_id_to=0;
$result=0;
// By default, electronic transfert from bank to bank
$typefrom='PRE';
$typeto='VIR';
if (! $error) $bank_line_id_from = $accountfrom->addline($dateo, $typefrom, $label, -1*price2num($amount), '', '', $user);
if (! ($bank_line_id_from > 0)) $error++;
if (! $error) $bank_line_id_to = $accountto->addline($dateo, $typeto, $label, price2num($amount), '', '', $user);
if (! ($bank_line_id_to > 0)) $error++;
if (! $error) $result=$accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.'/compta/bank/ligne.php?rowid=', '(banktransfert)', 'banktransfert');
if (! ($result > 0)) $error++;
if (! $error) $result=$accountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.'/compta/bank/ligne.php?rowid=', '(banktransfert)', 'banktransfert');
if (! ($result > 0)) $error++;
}
$subject = $societeName.' - [NOTIFICATION] Stripe payout done';
if (!empty($user->email)) {
$sendto = dolGetFirstLastname($user->firstname, $user->lastname) . " <".$user->email.">";
} else {
$sendto = $conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>';
}
$replyto = $sendto;
$sendtocc = '';
if (!empty($conf->global->ONLINE_PAYMENT_SENDEMAIL)) {
$sendtocc = $conf->global->ONLINE_PAYMENT_SENDEMAIL.'" <'.$conf->global->ONLINE_PAYMENT_SENDEMAIL.'>';
}
$message = "A bank transfer of ".price2num($event->data->object->amount/100)." ".$event->data->object->currency." has been done to your account the ".dol_print_date($event->data->object->arrival_date, 'dayhour');
$mailfile = new CMailFile(
$subject,
$sendto,
$replyto,
$message,
array(),
array(),
array(),
$sendtocc,
'',
0,
-1
);
$ret = $mailfile->sendfile();
http_response_code(200); // PHP 5.4 or greater
return 1;
}
else
{
$error++;
http_response_code(500); // PHP 5.4 or greater
return -1;
}
}
elseif ($event->type == 'customer.source.created') {
//TODO: save customer's source
}
elseif ($event->type == 'customer.source.updated') {
//TODO: update customer's source
}
elseif ($event->type == 'customer.source.delete') {
//TODO: delete customer's source
}
elseif ($event->type == 'customer.deleted') {
$db->begin();
$sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_account WHERE key_account = '".$db->escape($event->data->object->id)."' and site='stripe'";
dol_syslog(get_class($this) . "::delete sql=" . $sql, LOG_DEBUG);
$db->query($sql);
$db->commit();
}
elseif ($event->type == 'payment_intent.succeeded') { // Called when making payment with PaymentIntent method ($conf->global->STRIPE_USE_NEW_CHECKOUT is on).
// TODO: create fees
// TODO: Redirect to paymentok.php
}
elseif ($event->type == 'payment_intent.payment_failed') {
// TODO: Redirect to paymentko.php
}
elseif ($event->type == 'checkout.session.completed') // Called when making payment with new Checkout method ($conf->global->STRIPE_USE_NEW_CHECKOUT is on).
{
// TODO: create fees
// TODO: Redirect to paymentok.php
}
elseif ($event->type == 'charge.succeeded') {
// TODO: create fees
// TODO: Redirect to paymentok.php
}
elseif ($event->type == 'charge.failed') {
// TODO: Redirect to paymentko.php
}
elseif (($event->type == 'source.chargeable') && ($event->data->object->type == 'three_d_secure') && ($event->data->object->three_d_secure->authenticated==true)) {
$fulltag=$event->data->object->metadata->FULLTAG;
dol_syslog("fulltag=".$fulltag);
// Save into $tmptag all metadata
$tmptag=dolExplodeIntoArray($fulltag, '.', '=');
$stripe=new Stripe($db);
/*
$stripeacc = $stripe->getStripeAccount($service); // Stripe OAuth connect account of dolibarr user (no network access here)
$stripecu = $stripe->getStripeCustomerAccount($tmptag['CUS'], $servicestatus); // Get thirdparty cu_...
$charge=$stripe->createPaymentStripe($event->data->object->amount/100, $event->data->object->currency, $origin, $item, $event->data->object->id, $stripecu, $stripeacc, $servicestatus);
*/
}
http_response_code(200); // PHP 5.4 or greater