dolibarr  16.0.5
charge.php
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 
19 // Put here all includes required by your class file
20 
21 require '../main.inc.php';
22 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
23 require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
24 require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';
25 //require_once DOL_DOCUMENT_ROOT.'/core/lib/stripe.lib.php';
26 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
27 require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
28 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
29 if (!empty($conf->accounting->enabled)) {
30  require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
31 }
32 
33 // Load translation files required by the page
34 $langs->loadLangs(array('compta', 'salaries', 'bills', 'hrm', 'stripe'));
35 
36 // Security check
37 $socid = GETPOST("socid", "int");
38 if ($user->socid) {
39  $socid = $user->socid;
40 }
41 //$result = restrictedArea($user, 'salaries', '', '', '');
42 
43 $limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
44 $rowid = GETPOST("rowid", 'alpha');
45 $sortfield = GETPOST('sortfield', 'aZ09comma');
46 $sortorder = GETPOST('sortorder', 'aZ09comma');
47 $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
48 if (empty($page) || $page == -1) {
49  $page = 0;
50 } // If $page is not defined, or '' or -1
51 $offset = $limit * $page;
52 $pageprev = $page - 1;
53 $pagenext = $page + 1;
54 
55 $result = restrictedArea($user, 'banque');
56 $optioncss = GETPOST('optioncss', 'alpha');
57 
58 /*
59  * View
60  */
61 
62 $form = new Form($db);
63 $societestatic = new Societe($db);
64 $memberstatic = new Adherent($db);
65 $acc = new Account($db);
66 $stripe = new Stripe($db);
67 
68 llxHeader('', $langs->trans("StripeChargeList"));
69 
70 if (!empty($conf->stripe->enabled) && (empty($conf->global->STRIPE_LIVE) || GETPOST('forcesandbox', 'alpha'))) {
71  $service = 'StripeTest';
72  $servicestatus = '0';
73  dol_htmloutput_mesg($langs->trans('YouAreCurrentlyInSandboxMode', 'Stripe'), '', 'warning');
74 } else {
75  $service = 'StripeLive';
76  $servicestatus = '1';
77 }
78 
79 $stripeacc = $stripe->getStripeAccount($service);
80 /*if (empty($stripeaccount))
81 {
82  print $langs->trans('ErrorStripeAccountNotDefined');
83 }*/
84 
85 if (!$rowid) {
86  $option = array('limit' => $limit + 1);
87  $num = 0;
88 
89  if (GETPOSTISSET('starting_after_'.$page)) {
90  $option['starting_after'] = GETPOST('starting_after_'.$page, 'alphanohtml');
91  }
92 
93  try {
94  if ($stripeacc) {
95  $list = \Stripe\Charge::all($option, array("stripe_account" => $stripeacc));
96  } else {
97  $list = \Stripe\Charge::all($option);
98  }
99 
100  $num = count($list->data);
101 
102  $totalnboflines = '';
103 
104  $param = '';
105  //if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param .= '&contextpage='.urlencode($contextpage);
106  if ($limit > 0 && $limit != $conf->liste_limit) {
107  $param .= '&limit='.urlencode($limit);
108  }
109  $param .= '&starting_after_'.($page + 1).'='.$list->data[($limit - 1)]->id;
110  //$param.='&ending_before_'.($page+1).'='.$list->data[($limit-1)]->id;
111 
112  $moreforfilter = '';
113  } catch (Exception $e) {
114  print $e->getMessage();
115  }
116 
117  print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
118  if ($optioncss != '') {
119  print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
120  }
121  print '<input type="hidden" name="token" value="'.newToken().'">';
122  print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
123  print '<input type="hidden" name="action" value="list">';
124  print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
125  print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
126  print '<input type="hidden" name="page" value="'.$page.'">';
127 
128  $title = $langs->trans("StripeChargeList");
129  $title .= ($stripeacc ? ' (Stripe connection with Stripe OAuth Connect account '.$stripeacc.')' : ' (Stripe connection with keys from Stripe module setup)');
130 
131  print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $totalnboflines, 'title_accountancy.png', 0, '', 'hidepaginationprevious', $limit);
132 
133  print '<div class="div-table-responsive">';
134  print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
135 
136  print '<tr class="liste_titre">';
137  print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "", "", "", "", $sortfield, $sortorder);
138  print_liste_field_titre("StripeCustomerId", $_SERVER["PHP_SELF"], "", "", "", "", $sortfield, $sortorder);
139  print_liste_field_titre("Customer", $_SERVER["PHP_SELF"], "", "", "", "", $sortfield, $sortorder);
140  print_liste_field_titre("Origin", $_SERVER["PHP_SELF"], "", "", "", "", $sortfield, $sortorder);
141  print_liste_field_titre("DatePayment", $_SERVER["PHP_SELF"], "", "", "", '', $sortfield, $sortorder, 'center ');
142  print_liste_field_titre("Type", $_SERVER["PHP_SELF"], "", "", "", '', $sortfield, $sortorder, 'left ');
143  print_liste_field_titre("Paid", $_SERVER["PHP_SELF"], "", "", "", '', $sortfield, $sortorder, 'right ');
144  print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "", "", "", '', '', '', 'right ');
145  print "</tr>\n";
146 
147  //print $list;
148  $i = 0;
149  foreach ($list->data as $charge) {
150  if ($i >= $limit) {
151  break;
152  }
153 
154  if ($charge->refunded == '1') {
155  $status = img_picto($langs->trans("refunded"), 'statut6');
156  } elseif ($charge->paid == '1') {
157  $status = img_picto($langs->trans((string) $charge->status), 'statut4');
158  } else {
159  $label = $langs->trans("Message").": ".$charge->failure_message."<br>";
160  $label .= $langs->trans("Network").": ".$charge->outcome->network_status."<br>";
161  $label .= $langs->trans("Status").": ".$langs->trans((string) $charge->outcome->seller_message);
162  $status = $form->textwithpicto(img_picto($langs->trans((string) $charge->status), 'statut8'), $label, -1);
163  }
164 
165  if (isset($charge->payment_method_details->type) && $charge->payment_method_details->type == 'card') {
166  $type = $langs->trans("card");
167  } elseif (isset($charge->source->type) && $charge->source->type == 'card') {
168  $type = $langs->trans("card");
169  } elseif (isset($charge->payment_method_details->type) && $charge->payment_method_details->type == 'three_d_secure') {
170  $type = $langs->trans("card3DS");
171  } elseif (isset($charge->payment_method_details->type) && $charge->payment_method_details->type == 'sepa_debit') {
172  $type = $langs->trans("sepadebit");
173  } elseif (isset($charge->payment_method_details->type) && $charge->payment_method_details->type == 'ideal') {
174  $type = $langs->trans("iDEAL");
175  }
176 
177  // Why this ?
178  /*if (! empty($charge->payment_intent)) {
179  if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage
180  $charge = \Stripe\PaymentIntent::retrieve($charge->payment_intent);
181  } else {
182  $charge = \Stripe\PaymentIntent::retrieve($charge->payment_intent, array("stripe_account" => $stripeacc));
183  }
184  }*/
185 
186  // The metadata FULLTAG is defined by the online payment page
187  $FULLTAG = $charge->metadata->FULLTAG;
188 
189  // Save into $tmparray all metadata
190  $tmparray = dolExplodeIntoArray($FULLTAG, '.', '=');
191  // Load origin object according to metadata
192  if (!empty($tmparray['CUS']) && $tmparray['CUS'] > 0) {
193  $societestatic->fetch($tmparray['CUS']);
194  } elseif (!empty($charge->metadata->dol_thirdparty_id) && $charge->metadata->dol_thirdparty_id > 0) {
195  $societestatic->fetch($charge->metadata->dol_thirdparty_id);
196  } else {
197  $societestatic->id = 0;
198  }
199  if (!empty($tmparray['MEM']) && $tmparray['MEM'] > 0) {
200  $memberstatic->fetch($tmparray['MEM']);
201  } else {
202  $memberstatic->id = 0;
203  }
204 
205  print '<tr class="oddeven">';
206 
207  if (!empty($stripeacc)) {
208  $connect = $stripeacc.'/';
209  } else {
210  $connect = '';
211  }
212 
213  // Ref
214  $url = 'https://dashboard.stripe.com/'.$connect.'test/payments/'.$charge->id;
215  if ($servicestatus) {
216  $url = 'https://dashboard.stripe.com/'.$connect.'payments/'.$charge->id;
217  }
218  print "<td>";
219  print "<a href='".$url."' target='_stripe'>".img_picto($langs->trans('ShowInStripe'), 'globe')." ".$charge->id."</a>";
220  if ($charge->payment_intent) {
221  print '<br><span class="opacitymedium">'.$charge->payment_intent.'</span>';
222  }
223  print "</td>\n";
224 
225  // Stripe customer
226  print "<td>";
227  if (!empty($conf->stripe->enabled) && !empty($stripeacc)) {
228  $connect = $stripeacc.'/';
229  }
230  $url = 'https://dashboard.stripe.com/'.$connect.'test/customers/'.$charge->customer;
231  if ($servicestatus) {
232  $url = 'https://dashboard.stripe.com/'.$connect.'customers/'.$charge->customer;
233  }
234  if (!empty($charge->customer)) {
235  print '<a href="'.$url.'" target="_stripe">'.img_picto($langs->trans('ShowInStripe'), 'globe').' '.$charge->customer.'</a>';
236  }
237  print "</td>\n";
238 
239  // Link
240  print "<td>";
241  if ($societestatic->id > 0) {
242  print $societestatic->getNomUrl(1);
243  } elseif ($memberstatic->id > 0) {
244  print $memberstatic->getNomUrl(1);
245  }
246  print "</td>\n";
247 
248  // Origin
249  print "<td>";
250  if ($charge->metadata->dol_type == "order" || $charge->metadata->dol_type == "commande") {
251  $object = new Commande($db);
252  $object->fetch($charge->metadata->dol_id);
253  if ($object->id > 0) {
254  print "<a href='".DOL_URL_ROOT."/commande/card.php?id=".$object->id."'>".img_picto('', 'order')." ".$object->ref."</a>";
255  } else {
256  print $FULLTAG;
257  }
258  } elseif ($charge->metadata->dol_type == "invoice" || $charge->metadata->dol_type == "facture") {
259  $object = new Facture($db);
260  $object->fetch($charge->metadata->dol_id);
261  if ($object->id > 0) {
262  print "<a href='".DOL_URL_ROOT."/compta/facture/card.php?facid=".$charge->metadata->dol_id."'>".img_picto('', 'bill')." ".$object->ref."</a>";
263  } else {
264  print $FULLTAG;
265  }
266  } else {
267  print $FULLTAG;
268  }
269  print "</td>\n";
270 
271  // Date payment
272  print '<td class="center">'.dol_print_date($charge->created, 'dayhour')."</td>\n";
273  // Type
274  print '<td>';
275  print $type;
276  print '</td>';
277  // Amount
278  print '<td class="right"><span class="amount">'.price(($charge->amount - $charge->amount_refunded) / 100, 0, '', 1, - 1, - 1, strtoupper($charge->currency))."</span></td>";
279  // Status
280  print '<td class="right">';
281  print $status;
282  print "</td>\n";
283 
284  print "</tr>\n";
285 
286  $i++;
287  }
288 
289  print '</table>';
290  print '</div>';
291  print '</form>';
292 }
293 
294 // End of page
295 llxFooter();
296 $db->close();
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
restrictedArea
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.
Definition: security.lib.php:234
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
Facture
Class to manage invoices.
Definition: facture.class.php:60
dolExplodeIntoArray
dolExplodeIntoArray($string, $delimiter=';', $kv='=')
Split a string with 2 keys into key array.
Definition: functions.lib.php:9354
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
Exception
Commande
Class to manage customers orders.
Definition: commande.class.php:46
print_barre_liste
print_barre_liste($titre, $page, $file, $options='', $sortfield='', $sortorder='', $morehtmlcenter='', $num=-1, $totalnboflines='', $picto='generic', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limit=-1, $hideselectlimit=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow='')
Print a title with navigation controls for pagination.
Definition: functions.lib.php:5257
Adherent
Class to manage members of a foundation.
Definition: adherent.class.php:46
dol_htmloutput_mesg
dol_htmloutput_mesg($mesgstring='', $mesgarray=array(), $style='ok', $keepembedded=0)
Print formated messages to output (Used to show messages on html output).
Definition: functions.lib.php:8299
GETPOSTISSET
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
Definition: functions.lib.php:386
print_liste_field_titre
print_liste_field_titre($name, $file="", $field="", $begin="", $moreparam="", $moreattrib="", $sortfield="", $sortorder="", $prefix="", $tooltip="", $forcenowrapcolumntitle=0)
Show title line of an array.
Definition: functions.lib.php:5026
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
Stripe
Stripe class.
Definition: stripe.class.php:29
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59
Account
Class to manage bank accounts.
Definition: account.class.php:38