charge.php 11 KB

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