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