charge.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /* Copyright (C) 2018-2022 Thibault FOUCART <support@ptibogxiv.net>
  3. * Copyright (C) 2019 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 <https://www.gnu.org/licenses/>.
  17. */
  18. // Put here all includes required by your class file
  19. // Load Dolibarr environment
  20. require '../main.inc.php';
  21. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  22. require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
  23. require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';
  24. //require_once DOL_DOCUMENT_ROOT.'/core/lib/stripe.lib.php';
  25. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  28. if (isModEnabled('accounting')) {
  29. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
  30. }
  31. // Load translation files required by the page
  32. $langs->loadLangs(array('compta', 'salaries', 'bills', 'hrm', 'stripe'));
  33. // Security check
  34. $socid = GETPOST("socid", "int");
  35. if ($user->socid) {
  36. $socid = $user->socid;
  37. }
  38. //$result = restrictedArea($user, 'salaries', '', '', '');
  39. $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
  40. $rowid = GETPOST("rowid", 'alpha');
  41. $sortfield = GETPOST('sortfield', 'aZ09comma');
  42. $sortorder = GETPOST('sortorder', 'aZ09comma');
  43. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  44. if (empty($page) || $page == -1) {
  45. $page = 0;
  46. } // If $page is not defined, or '' or -1
  47. $offset = $limit * $page;
  48. $pageprev = $page - 1;
  49. $pagenext = $page + 1;
  50. $result = restrictedArea($user, 'banque');
  51. $optioncss = GETPOST('optioncss', 'alpha');
  52. /*
  53. * View
  54. */
  55. $form = new Form($db);
  56. $societestatic = new Societe($db);
  57. $memberstatic = new Adherent($db);
  58. $acc = new Account($db);
  59. $stripe = new Stripe($db);
  60. llxHeader('', $langs->trans("StripeChargeList"));
  61. if (isModEnabled('stripe') && (empty($conf->global->STRIPE_LIVE) || GETPOST('forcesandbox', 'alpha'))) {
  62. $service = 'StripeTest';
  63. $servicestatus = '0';
  64. dol_htmloutput_mesg($langs->trans('YouAreCurrentlyInSandboxMode', 'Stripe'), '', 'warning');
  65. } else {
  66. $service = 'StripeLive';
  67. $servicestatus = '1';
  68. }
  69. $stripeacc = $stripe->getStripeAccount($service);
  70. /*if (empty($stripeaccount))
  71. {
  72. print $langs->trans('ErrorStripeAccountNotDefined');
  73. }*/
  74. if (!$rowid) {
  75. $option = array('limit' => $limit + 1);
  76. $num = 0;
  77. $param = '';
  78. $totalnboflines = '';
  79. $moreforfilter = '';
  80. $list = null;
  81. if (GETPOSTISSET('starting_after_'.$page)) {
  82. $option['starting_after'] = GETPOST('starting_after_'.$page, 'alphanohtml');
  83. }
  84. print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
  85. if ($optioncss != '') {
  86. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  87. }
  88. print '<input type="hidden" name="token" value="'.newToken().'">';
  89. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  90. print '<input type="hidden" name="action" value="list">';
  91. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  92. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  93. print '<input type="hidden" name="page" value="'.$page.'">';
  94. $title = $langs->trans("StripeChargeList");
  95. $title .= ($stripeacc ? ' (Stripe connection with Stripe OAuth Connect account '.$stripeacc.')' : ' (Stripe connection with keys from Stripe module setup)');
  96. print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $totalnboflines, 'title_accountancy.png', 0, '', 'hidepaginationprevious', $limit);
  97. print '<div class="div-table-responsive">';
  98. print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
  99. print '<tr class="liste_titre">';
  100. print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "", "", "", "", $sortfield, $sortorder);
  101. print_liste_field_titre("StripeCustomerId", $_SERVER["PHP_SELF"], "", "", "", "", $sortfield, $sortorder);
  102. print_liste_field_titre("Customer", $_SERVER["PHP_SELF"], "", "", "", "", $sortfield, $sortorder);
  103. print_liste_field_titre("Origin", $_SERVER["PHP_SELF"], "", "", "", "", $sortfield, $sortorder);
  104. print_liste_field_titre("DatePayment", $_SERVER["PHP_SELF"], "", "", "", '', $sortfield, $sortorder, 'center ');
  105. print_liste_field_titre("Type", $_SERVER["PHP_SELF"], "", "", "", '', $sortfield, $sortorder, 'left ');
  106. print_liste_field_titre("Paid", $_SERVER["PHP_SELF"], "", "", "", '', $sortfield, $sortorder, 'right ');
  107. print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "", "", "", '', '', '', 'right ');
  108. print "</tr>\n";
  109. try {
  110. if ($stripeacc) {
  111. $list = \Stripe\Charge::all($option, array("stripe_account" => $stripeacc));
  112. } else {
  113. $list = \Stripe\Charge::all($option);
  114. }
  115. $num = count($list->data);
  116. //if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param .= '&contextpage='.urlencode($contextpage);
  117. if ($limit > 0 && $limit != $conf->liste_limit) {
  118. $param .= '&limit='.((int) $limit);
  119. }
  120. $param .= '&starting_after_'.($page + 1).'='.$list->data[($limit - 1)]->id;
  121. //$param.='&ending_before_'.($page+1).'='.$list->data[($limit-1)]->id;
  122. } catch (Exception $e) {
  123. print '<tr><td colspan="6">'.$e->getMessage().'</td></td>';
  124. }
  125. //print $list;
  126. $i = 0;
  127. if (!empty($list)) {
  128. foreach ($list->data as $charge) {
  129. if ($i >= $limit) {
  130. break;
  131. }
  132. if ($charge->refunded == '1') {
  133. $status = img_picto($langs->trans("refunded"), 'statut6');
  134. } elseif ($charge->paid == '1') {
  135. $status = img_picto($langs->trans((string) $charge->status), 'statut4');
  136. } else {
  137. $label = $langs->trans("Message").": ".$charge->failure_message."<br>";
  138. $label .= $langs->trans("Network").": ".$charge->outcome->network_status."<br>";
  139. $label .= $langs->trans("Status").": ".$langs->trans((string) $charge->outcome->seller_message);
  140. $status = $form->textwithpicto(img_picto($langs->trans((string) $charge->status), 'statut8'), $label, -1);
  141. }
  142. if (isset($charge->payment_method_details->type) && $charge->payment_method_details->type == 'card') {
  143. $type = $langs->trans("card");
  144. } elseif (isset($charge->source->type) && $charge->source->type == 'card') {
  145. $type = $langs->trans("card");
  146. } elseif (isset($charge->payment_method_details->type) && $charge->payment_method_details->type == 'three_d_secure') {
  147. $type = $langs->trans("card3DS");
  148. } elseif (isset($charge->payment_method_details->type) && $charge->payment_method_details->type == 'sepa_debit') {
  149. $type = $langs->trans("sepadebit");
  150. } elseif (isset($charge->payment_method_details->type) && $charge->payment_method_details->type == 'ideal') {
  151. $type = $langs->trans("iDEAL");
  152. }
  153. // Why this ?
  154. /*if (!empty($charge->payment_intent)) {
  155. if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage
  156. $charge = \Stripe\PaymentIntent::retrieve($charge->payment_intent);
  157. } else {
  158. $charge = \Stripe\PaymentIntent::retrieve($charge->payment_intent, array("stripe_account" => $stripeacc));
  159. }
  160. }*/
  161. // The metadata FULLTAG is defined by the online payment page
  162. $FULLTAG = $charge->metadata->FULLTAG;
  163. // Save into $tmparray all metadata
  164. $tmparray = dolExplodeIntoArray($FULLTAG, '.', '=');
  165. // Load origin object according to metadata
  166. if (!empty($tmparray['CUS']) && $tmparray['CUS'] > 0) {
  167. $societestatic->fetch($tmparray['CUS']);
  168. } elseif (!empty($charge->metadata->dol_thirdparty_id) && $charge->metadata->dol_thirdparty_id > 0) {
  169. $societestatic->fetch($charge->metadata->dol_thirdparty_id);
  170. } else {
  171. $societestatic->id = 0;
  172. }
  173. if (!empty($tmparray['MEM']) && $tmparray['MEM'] > 0) {
  174. $memberstatic->fetch($tmparray['MEM']);
  175. } else {
  176. $memberstatic->id = 0;
  177. }
  178. print '<tr class="oddeven">';
  179. if (!empty($stripeacc)) {
  180. $connect = $stripeacc.'/';
  181. } else {
  182. $connect = '';
  183. }
  184. // Ref
  185. $url = 'https://dashboard.stripe.com/'.$connect.'test/payments/'.$charge->id;
  186. if ($servicestatus) {
  187. $url = 'https://dashboard.stripe.com/'.$connect.'payments/'.$charge->id;
  188. }
  189. print "<td>";
  190. print "<a href='".$url."' target='_stripe'>".img_picto($langs->trans('ShowInStripe'), 'globe')." ".$charge->id."</a>";
  191. if ($charge->payment_intent) {
  192. print '<br><span class="opacitymedium">'.$charge->payment_intent.'</span>';
  193. }
  194. print "</td>\n";
  195. // Stripe customer
  196. print "<td>";
  197. if (isModEnabled('stripe') && !empty($stripeacc)) {
  198. $connect = $stripeacc.'/';
  199. }
  200. $url = 'https://dashboard.stripe.com/'.$connect.'test/customers/'.$charge->customer;
  201. if ($servicestatus) {
  202. $url = 'https://dashboard.stripe.com/'.$connect.'customers/'.$charge->customer;
  203. }
  204. if (!empty($charge->customer)) {
  205. print '<a href="'.$url.'" target="_stripe">'.img_picto($langs->trans('ShowInStripe'), 'globe').' '.$charge->customer.'</a>';
  206. }
  207. print "</td>\n";
  208. // Link
  209. print "<td>";
  210. if ($societestatic->id > 0) {
  211. print $societestatic->getNomUrl(1);
  212. } elseif ($memberstatic->id > 0) {
  213. print $memberstatic->getNomUrl(1);
  214. }
  215. print "</td>\n";
  216. // Origin
  217. print "<td>";
  218. if ($charge->metadata->dol_type == "order" || $charge->metadata->dol_type == "commande") {
  219. $object = new Commande($db);
  220. $object->fetch($charge->metadata->dol_id);
  221. if ($object->id > 0) {
  222. print "<a href='".DOL_URL_ROOT."/commande/card.php?id=".$object->id."'>".img_picto('', 'order')." ".$object->ref."</a>";
  223. } else {
  224. print $FULLTAG;
  225. }
  226. } elseif ($charge->metadata->dol_type == "invoice" || $charge->metadata->dol_type == "facture") {
  227. $object = new Facture($db);
  228. $object->fetch($charge->metadata->dol_id);
  229. if ($object->id > 0) {
  230. print "<a href='".DOL_URL_ROOT."/compta/facture/card.php?facid=".$charge->metadata->dol_id."'>".img_picto('', 'bill')." ".$object->ref."</a>";
  231. } else {
  232. print $FULLTAG;
  233. }
  234. } else {
  235. print $FULLTAG;
  236. }
  237. print "</td>\n";
  238. // Date payment
  239. print '<td class="center">'.dol_print_date($charge->created, 'dayhour')."</td>\n";
  240. // Type
  241. print '<td>';
  242. print $type;
  243. print '</td>';
  244. // Amount
  245. print '<td class="right"><span class="amount">'.price(($charge->amount - $charge->amount_refunded) / 100, 0, '', 1, - 1, - 1, strtoupper($charge->currency))."</span></td>";
  246. // Status
  247. print '<td class="right">';
  248. print $status;
  249. print "</td>\n";
  250. print "</tr>\n";
  251. $i++;
  252. }
  253. }
  254. print '</table>';
  255. print '</div>';
  256. print '</form>';
  257. }
  258. // End of page
  259. llxFooter();
  260. $db->close();