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