ipn.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. /* Copyright (C) 2018 Thibault FOUCART <support@ptibogxiv.net>
  3. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. define("NOLOGIN", 1); // This means this output page does not require to be logged.
  19. define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
  20. $entity=(! empty($_GET['entity']) ? (int) $_GET['entity'] : (! empty($_POST['entity']) ? (int) $_POST['entity'] : 1));
  21. if (is_numeric($entity)) define("DOLENTITY", $entity);
  22. require '../../main.inc.php';
  23. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  24. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/class/ccountry.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/includes/stripe/init.php';
  33. require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';
  34. if (empty($conf->stripe->enabled)) accessforbidden('', 0, 0, 1);
  35. // You can find your endpoint's secret in your webhook settings
  36. if (isset($_GET['connect']))
  37. {
  38. if (isset($_GET['test']))
  39. {
  40. $endpoint_secret = $conf->global->STRIPE_TEST_WEBHOOK_CONNECT_KEY;
  41. $service = 'StripeTest';
  42. $servicestatus = 0;
  43. }
  44. else
  45. {
  46. $endpoint_secret = $conf->global->STRIPE_LIVE_WEBHOOK_CONNECT_KEY;
  47. $service = 'StripeLive';
  48. $servicestatus = 1;
  49. }
  50. }
  51. else {
  52. if (isset($_GET['test']))
  53. {
  54. $endpoint_secret = $conf->global->STRIPE_TEST_WEBHOOK_KEY;
  55. $service = 'StripeTest';
  56. $servicestatus = 0;
  57. }
  58. else
  59. {
  60. $endpoint_secret = $conf->global->STRIPE_LIVE_WEBHOOK_KEY;
  61. $service = 'StripeLive';
  62. $servicestatus = 1;
  63. }
  64. }
  65. if (empty($endpoint_secret))
  66. {
  67. print 'Error: Setup of module Stripe not complete for mode '.$service.'. The WEBHOOK_KEY is not defined.';
  68. http_response_code(400); // PHP 5.4 or greater
  69. exit();
  70. }
  71. /*
  72. * Actions
  73. */
  74. $payload = @file_get_contents("php://input");
  75. $sig_header = $_SERVER["HTTP_STRIPE_SIGNATURE"];
  76. $event = null;
  77. $error = 0;
  78. try {
  79. $event = \Stripe\Webhook::constructEvent($payload, $sig_header, $endpoint_secret);
  80. }
  81. catch(\UnexpectedValueException $e) {
  82. // Invalid payload
  83. http_response_code(400); // PHP 5.4 or greater
  84. exit();
  85. } catch(\Stripe\Error\SignatureVerification $e) {
  86. // Invalid signature
  87. http_response_code(400); // PHP 5.4 or greater
  88. exit();
  89. }
  90. // Do something with $event
  91. $langs->load("main");
  92. // 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
  93. $user = new User($db);
  94. $user->fetch($conf->global->STRIPE_USER_ACCOUNT_FOR_ACTIONS);
  95. $user->getrights();
  96. if (! empty($conf->multicompany->enabled) && ! empty($conf->stripeconnect->enabled) && is_object($mc))
  97. {
  98. $sql = "SELECT entity";
  99. $sql.= " FROM ".MAIN_DB_PREFIX."oauth_token";
  100. $sql.= " WHERE service = '".$db->escape($service)."' and tokenstring = '%".$db->escape($event->account)."%'";
  101. dol_syslog(get_class($db) . "::fetch", LOG_DEBUG);
  102. $result = $db->query($sql);
  103. if ($result)
  104. {
  105. if ($db->num_rows($result))
  106. {
  107. $obj = $db->fetch_object($result);
  108. $key=$obj->entity;
  109. }
  110. else {
  111. $key=1;
  112. }
  113. }
  114. else {
  115. $key=1;
  116. }
  117. $ret=$mc->switchEntity($key);
  118. if (! $res && file_exists("../../main.inc.php")) $res=@include "../../main.inc.php";
  119. if (! $res) die("Include of main fails");
  120. }
  121. // list of action
  122. $stripe=new Stripe($db);
  123. // Subject
  124. $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM;
  125. if (! empty($conf->global->MAIN_APPLICATION_TITLE)) $societeName = $conf->global->MAIN_APPLICATION_TITLE;
  126. dol_syslog("***** Stripe IPN was called with event->type = ".$event->type);
  127. if ($event->type == 'payout.created') {
  128. $error=0;
  129. $result=dolibarr_set_const($db, $service."_NEXTPAYOUT", date('Y-m-d H:i:s', $event->data->object->arrival_date), 'chaine', 0, '', $conf->entity);
  130. if ($result > 0)
  131. {
  132. $subject = $societeName.' - [NOTIFICATION] Stripe payout scheduled';
  133. if (!empty($user->email)) {
  134. $sendto = dolGetFirstLastname($user->firstname, $user->lastname) . " <".$user->email.">";
  135. } else {
  136. $sendto = $conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>';
  137. }
  138. $replyto = $sendto;
  139. $sendtocc = '';
  140. if (!empty($conf->global->ONLINE_PAYMENT_SENDEMAIL)) {
  141. $sendtocc = $conf->global->ONLINE_PAYMENT_SENDEMAIL.'" <'.$conf->global->ONLINE_PAYMENT_SENDEMAIL.'>';
  142. }
  143. $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');
  144. $mailfile = new CMailFile(
  145. $subject,
  146. $sendto,
  147. $replyto,
  148. $message,
  149. array(),
  150. array(),
  151. array(),
  152. $sendtocc,
  153. '',
  154. 0,
  155. -1
  156. );
  157. $ret = $mailfile->sendfile();
  158. http_response_code(200); // PHP 5.4 or greater
  159. return 1;
  160. }
  161. else
  162. {
  163. $error++;
  164. http_response_code(500); // PHP 5.4 or greater
  165. return -1;
  166. }
  167. }
  168. elseif ($event->type == 'payout.paid') {
  169. global $conf;
  170. $error=0;
  171. $result=dolibarr_set_const($db, $service."_NEXTPAYOUT", null, 'chaine', 0, '', $conf->entity);
  172. if ($result)
  173. {
  174. $langs->load("errors");
  175. $dateo = dol_now();
  176. $label = $event->data->object->description;
  177. $amount= $event->data->object->amount/100;
  178. $amount_to= $event->data->object->amount/100;
  179. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  180. $accountfrom=new Account($db);
  181. $accountfrom->fetch($conf->global->STRIPE_BANK_ACCOUNT_FOR_PAYMENTS);
  182. $accountto=new Account($db);
  183. $accountto->fetch($conf->global->STRIPE_BANK_ACCOUNT_FOR_BANKTRANSFERS);
  184. if (($accountto->id != $accountfrom->id) && empty($error))
  185. {
  186. $bank_line_id_from=0;
  187. $bank_line_id_to=0;
  188. $result=0;
  189. // By default, electronic transfert from bank to bank
  190. $typefrom='PRE';
  191. $typeto='VIR';
  192. if (! $error) $bank_line_id_from = $accountfrom->addline($dateo, $typefrom, $label, -1*price2num($amount), '', '', $user);
  193. if (! ($bank_line_id_from > 0)) $error++;
  194. if (! $error) $bank_line_id_to = $accountto->addline($dateo, $typeto, $label, price2num($amount), '', '', $user);
  195. if (! ($bank_line_id_to > 0)) $error++;
  196. if (! $error) $result=$accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.'/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert');
  197. if (! ($result > 0)) $error++;
  198. if (! $error) $result=$accountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.'/compta/bank/line.php?rowid=', '(banktransfert)', 'banktransfert');
  199. if (! ($result > 0)) $error++;
  200. }
  201. $subject = $societeName.' - [NOTIFICATION] Stripe payout done';
  202. if (!empty($user->email)) {
  203. $sendto = dolGetFirstLastname($user->firstname, $user->lastname) . " <".$user->email.">";
  204. } else {
  205. $sendto = $conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>';
  206. }
  207. $replyto = $sendto;
  208. $sendtocc = '';
  209. if (!empty($conf->global->ONLINE_PAYMENT_SENDEMAIL)) {
  210. $sendtocc = $conf->global->ONLINE_PAYMENT_SENDEMAIL.'" <'.$conf->global->ONLINE_PAYMENT_SENDEMAIL.'>';
  211. }
  212. $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');
  213. $mailfile = new CMailFile(
  214. $subject,
  215. $sendto,
  216. $replyto,
  217. $message,
  218. array(),
  219. array(),
  220. array(),
  221. $sendtocc,
  222. '',
  223. 0,
  224. -1
  225. );
  226. $ret = $mailfile->sendfile();
  227. http_response_code(200); // PHP 5.4 or greater
  228. return 1;
  229. }
  230. else
  231. {
  232. $error++;
  233. http_response_code(500); // PHP 5.4 or greater
  234. return -1;
  235. }
  236. }
  237. elseif ($event->type == 'customer.source.created') {
  238. //TODO: save customer's source
  239. }
  240. elseif ($event->type == 'customer.source.updated') {
  241. //TODO: update customer's source
  242. }
  243. elseif ($event->type == 'customer.source.delete') {
  244. //TODO: delete customer's source
  245. }
  246. elseif ($event->type == 'customer.deleted') {
  247. $db->begin();
  248. $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_account WHERE key_account = '".$db->escape($event->data->object->id)."' and site='stripe'";
  249. $db->query($sql);
  250. $db->commit();
  251. }
  252. elseif ($event->type == 'payment_intent.succeeded') { // Called when making payment with PaymentIntent method ($conf->global->STRIPE_USE_NEW_CHECKOUT is on).
  253. // TODO: create fees
  254. // TODO: Redirect to paymentok.php
  255. }
  256. elseif ($event->type == 'payment_intent.payment_failed') {
  257. // TODO: Redirect to paymentko.php
  258. }
  259. elseif ($event->type == 'checkout.session.completed') // Called when making payment with new Checkout method ($conf->global->STRIPE_USE_NEW_CHECKOUT is on).
  260. {
  261. // TODO: create fees
  262. // TODO: Redirect to paymentok.php
  263. }
  264. elseif ($event->type == 'payment_method.attached') {
  265. require_once DOL_DOCUMENT_ROOT.'/societe/class/companypaymentmode.class.php';
  266. require_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php';
  267. $societeaccount = new SocieteAccount($db);
  268. $companypaymentmode = new CompanyPaymentMode($db);
  269. $idthirdparty = $societeaccount->getThirdPartyID($db->escape($event->data->object->customer), 'stripe', $servicestatus);
  270. if ($idthirdparty > 0) // If the payment mode is on an external customer that is known in societeaccount, we can create the payment mode
  271. {
  272. $companypaymentmode->stripe_card_ref = $db->escape($event->data->object->id);
  273. $companypaymentmode->fk_soc = $idthirdparty;
  274. $companypaymentmode->bank = null;
  275. $companypaymentmode->label = null;
  276. $companypaymentmode->number = $db->escape($event->data->object->id);
  277. $companypaymentmode->last_four = $db->escape($event->data->object->card->last4);
  278. $companypaymentmode->card_type = $db->escape($event->data->object->card->branding);
  279. $companypaymentmode->proprio = $db->escape($event->data->object->billing_details->name);
  280. $companypaymentmode->exp_date_month = $db->escape($event->data->object->card->exp_month);
  281. $companypaymentmode->exp_date_year = $db->escape($event->data->object->card->exp_year);
  282. $companypaymentmode->cvn = null;
  283. $companypaymentmode->datec = $db->escape($event->data->object->created);
  284. $companypaymentmode->default_rib = 0;
  285. $companypaymentmode->type = $db->escape($event->data->object->type);
  286. $companypaymentmode->country_code = $db->escape($event->data->object->card->country);
  287. $companypaymentmode->status = $servicestatus;
  288. $db->begin();
  289. if (! $error)
  290. {
  291. $result = $companypaymentmode->create($user);
  292. if ($result < 0)
  293. {
  294. $error++;
  295. }
  296. }
  297. if (! $error)
  298. {
  299. $db->commit();
  300. }
  301. else
  302. {
  303. $db->rollback();
  304. }
  305. }
  306. }
  307. elseif ($event->type == 'payment_method.updated') {
  308. require_once DOL_DOCUMENT_ROOT.'/societe/class/companypaymentmode.class.php';
  309. $companypaymentmode = new CompanyPaymentMode($db);
  310. $companypaymentmode->fetch(0, '', 0, '', " AND stripe_card_ref = '".$db->escape($event->data->object->id)."'");
  311. $companypaymentmode->bank = null;
  312. $companypaymentmode->label = null;
  313. $companypaymentmode->number = $db->escape($event->data->object->id);
  314. $companypaymentmode->last_four = $db->escape($event->data->object->card->last4);
  315. $companypaymentmode->proprio = $db->escape($event->data->object->billing_details->name);
  316. $companypaymentmode->exp_date_month = $db->escape($event->data->object->card->exp_month);
  317. $companypaymentmode->exp_date_year = $db->escape($event->data->object->card->exp_year);
  318. $companypaymentmode->cvn = null;
  319. $companypaymentmode->datec = $db->escape($event->data->object->created);
  320. $companypaymentmode->default_rib = 0;
  321. $companypaymentmode->type = $db->escape($event->data->object->type);
  322. $companypaymentmode->country_code = $db->escape($event->data->object->card->country);
  323. $companypaymentmode->status = $servicestatus;
  324. $db->begin();
  325. if (! $error)
  326. {
  327. $result = $companypaymentmode->update($user);
  328. if ($result < 0)
  329. {
  330. $error++;
  331. }
  332. }
  333. if (! $error)
  334. {
  335. $db->commit();
  336. }
  337. else
  338. {
  339. $db->rollback();
  340. }
  341. }
  342. elseif ($event->type == 'payment_method.detached') {
  343. $db->begin();
  344. $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_rib WHERE ref = '".$db->escape($event->data->object->id)."' and status = ".$servicestatus;
  345. $db->query($sql);
  346. $db->commit();
  347. }
  348. elseif ($event->type == 'charge.succeeded') {
  349. // TODO: create fees
  350. // TODO: Redirect to paymentok.php
  351. }
  352. elseif ($event->type == 'charge.failed') {
  353. // TODO: Redirect to paymentko.php
  354. }
  355. elseif (($event->type == 'source.chargeable') && ($event->data->object->type == 'three_d_secure') && ($event->data->object->three_d_secure->authenticated==true)) {
  356. // This event is deprecated.
  357. }
  358. http_response_code(200); // PHP 5.4 or greater