dolibarr/htdocs/stripe/charge.php

297 lines
11 KiB
PHP
Raw Normal View History

2018-03-05 19:58:50 +01:00
<?php
2022-07-28 19:43:58 +02:00
/* Copyright (C) 2018-2022 Thibault FOUCART <support@ptibogxiv.net>
2019-10-27 17:01:23 +01:00
* Copyright (C) 2019 Frédéric France <frederic.france@netlogic.fr>
2018-03-05 19:58:50 +01:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-03-05 19:58:50 +01:00
*/
// Put here all includes required by your class file
require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
require_once DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php';
2018-03-05 19:58:50 +01:00
require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';
2018-03-05 20:01:07 +01:00
//require_once DOL_DOCUMENT_ROOT.'/core/lib/stripe.lib.php';
2018-03-05 19:58:50 +01:00
require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
2021-02-26 21:17:52 +01:00
if (!empty($conf->accounting->enabled)) {
require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php';
}
2018-03-05 19:58:50 +01:00
2018-05-26 16:35:29 +02:00
// Load translation files required by the page
$langs->loadLangs(array('compta', 'salaries', 'bills', 'hrm', 'stripe'));
2018-03-05 19:58:50 +01:00
// Security check
$socid = GETPOST("socid", "int");
2021-02-26 21:17:52 +01:00
if ($user->socid) {
$socid = $user->socid;
}
2018-03-05 19:58:50 +01:00
//$result = restrictedArea($user, 'salaries', '', '', '');
2020-04-20 15:06:27 +02:00
$limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit;
$rowid = GETPOST("rowid", 'alpha');
2022-01-13 11:09:37 +01:00
$sortfield = GETPOST('sortfield', 'aZ09comma');
$sortorder = GETPOST('sortorder', 'aZ09comma');
$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
2021-02-26 21:17:52 +01:00
if (empty($page) || $page == -1) {
$page = 0;
} // If $page is not defined, or '' or -1
2020-04-20 15:06:27 +02:00
$offset = $limit * $page;
2018-03-05 19:58:50 +01:00
$pageprev = $page - 1;
$pagenext = $page + 1;
2021-03-23 17:46:06 +01:00
$result = restrictedArea($user, 'banque');
2022-06-12 15:08:19 +02:00
$optioncss = GETPOST('optioncss', 'alpha');
2018-03-05 19:58:50 +01:00
/*
* View
*/
2018-03-05 19:58:50 +01:00
$form = new Form($db);
$societestatic = new Societe($db);
$memberstatic = new Adherent($db);
2018-03-05 19:58:50 +01:00
$acc = new Account($db);
$stripe = new Stripe($db);
2018-04-06 14:19:11 +02:00
llxHeader('', $langs->trans("StripeChargeList"));
2021-02-26 21:17:52 +01:00
if (!empty($conf->stripe->enabled) && (empty($conf->global->STRIPE_LIVE) || GETPOST('forcesandbox', 'alpha'))) {
$service = 'StripeTest';
2018-09-21 09:50:31 +02:00
$servicestatus = '0';
2018-04-06 14:19:11 +02:00
dol_htmloutput_mesg($langs->trans('YouAreCurrentlyInSandboxMode', 'Stripe'), '', 'warning');
2020-05-21 15:05:19 +02:00
} else {
2018-09-21 09:50:31 +02:00
$service = 'StripeLive';
$servicestatus = '1';
}
2018-03-05 19:58:50 +01:00
2018-09-14 17:46:02 +02:00
$stripeacc = $stripe->getStripeAccount($service);
2018-04-06 14:05:07 +02:00
/*if (empty($stripeaccount))
{
print $langs->trans('ErrorStripeAccountNotDefined');
2018-04-06 14:05:07 +02:00
}*/
2020-10-16 13:30:20 +02:00
if (!$rowid) {
$option = array('limit' => $limit + 1);
2020-09-24 21:54:21 +02:00
$num = 0;
2021-02-26 21:17:52 +01:00
if (GETPOSTISSET('starting_after_'.$page)) {
$option['starting_after'] = GETPOST('starting_after_'.$page, 'alphanohtml');
}
2020-09-24 21:54:21 +02:00
try {
2021-02-26 21:17:52 +01:00
if ($stripeacc) {
2020-09-24 21:54:21 +02:00
$list = \Stripe\Charge::all($option, array("stripe_account" => $stripeacc));
} else {
$list = \Stripe\Charge::all($option);
}
2020-09-24 21:54:21 +02:00
$num = count($list->data);
2020-09-24 21:54:21 +02:00
$totalnboflines = '';
2020-09-24 21:54:21 +02:00
$param = '';
//if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param .= '&contextpage='.urlencode($contextpage);
2021-02-26 21:17:52 +01:00
if ($limit > 0 && $limit != $conf->liste_limit) {
$param .= '&limit='.urlencode($limit);
}
2020-09-24 21:54:21 +02:00
$param .= '&starting_after_'.($page + 1).'='.$list->data[($limit - 1)]->id;
//$param.='&ending_before_'.($page+1).'='.$list->data[($limit-1)]->id;
2020-09-24 21:54:21 +02:00
$moreforfilter = '';
2020-10-16 13:30:20 +02:00
} catch (Exception $e) {
print $e->getMessage();
}
print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
2021-02-26 21:17:52 +01:00
if ($optioncss != '') {
print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
}
2020-10-16 13:30:20 +02:00
print '<input type="hidden" name="token" value="'.newToken().'">';
print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
print '<input type="hidden" name="action" value="list">';
print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
print '<input type="hidden" name="page" value="'.$page.'">';
$title = $langs->trans("StripeChargeList");
$title .= ($stripeacc ? ' (Stripe connection with Stripe OAuth Connect account '.$stripeacc.')' : ' (Stripe connection with keys from Stripe module setup)');
print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $totalnboflines, 'title_accountancy.png', 0, '', 'hidepaginationprevious', $limit);
print '<div class="div-table-responsive">';
print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
print '<tr class="liste_titre">';
print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "", "", "", "", $sortfield, $sortorder);
print_liste_field_titre("StripeCustomerId", $_SERVER["PHP_SELF"], "", "", "", "", $sortfield, $sortorder);
print_liste_field_titre("Customer", $_SERVER["PHP_SELF"], "", "", "", "", $sortfield, $sortorder);
print_liste_field_titre("Origin", $_SERVER["PHP_SELF"], "", "", "", "", $sortfield, $sortorder);
print_liste_field_titre("DatePayment", $_SERVER["PHP_SELF"], "", "", "", '', $sortfield, $sortorder, 'center ');
print_liste_field_titre("Type", $_SERVER["PHP_SELF"], "", "", "", '', $sortfield, $sortorder, 'left ');
print_liste_field_titre("Paid", $_SERVER["PHP_SELF"], "", "", "", '', $sortfield, $sortorder, 'right ');
print_liste_field_titre("Status", $_SERVER["PHP_SELF"], "", "", "", '', '', '', 'right ');
print "</tr>\n";
//print $list;
$i = 0;
2021-02-26 21:17:52 +01:00
foreach ($list->data as $charge) {
2020-10-16 13:30:20 +02:00
if ($i >= $limit) {
break;
}
if ($charge->refunded == '1') {
$status = img_picto($langs->trans("refunded"), 'statut6');
} elseif ($charge->paid == '1') {
$status = img_picto($langs->trans((string) $charge->status), 'statut4');
} else {
$label = $langs->trans("Message").": ".$charge->failure_message."<br>";
$label .= $langs->trans("Network").": ".$charge->outcome->network_status."<br>";
$label .= $langs->trans("Status").": ".$langs->trans((string) $charge->outcome->seller_message);
$status = $form->textwithpicto(img_picto($langs->trans((string) $charge->status), 'statut8'), $label, -1);
}
2022-06-12 15:08:19 +02:00
if (isset($charge->payment_method_details->type) && $charge->payment_method_details->type == 'card') {
2020-10-16 13:30:20 +02:00
$type = $langs->trans("card");
2022-06-12 15:08:19 +02:00
} elseif (isset($charge->source->type) && $charge->source->type == 'card') {
2020-10-16 13:30:20 +02:00
$type = $langs->trans("card");
2022-06-12 15:08:19 +02:00
} elseif (isset($charge->payment_method_details->type) && $charge->payment_method_details->type == 'three_d_secure') {
2020-10-16 13:30:20 +02:00
$type = $langs->trans("card3DS");
2022-06-12 15:08:19 +02:00
} elseif (isset($charge->payment_method_details->type) && $charge->payment_method_details->type == 'sepa_debit') {
2020-10-16 13:30:20 +02:00
$type = $langs->trans("sepadebit");
2022-06-12 15:08:19 +02:00
} elseif (isset($charge->payment_method_details->type) && $charge->payment_method_details->type == 'ideal') {
2020-10-16 13:30:20 +02:00
$type = $langs->trans("iDEAL");
}
2020-10-16 13:30:20 +02:00
// Why this ?
/*if (! empty($charge->payment_intent)) {
if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage
$charge = \Stripe\PaymentIntent::retrieve($charge->payment_intent);
} else {
$charge = \Stripe\PaymentIntent::retrieve($charge->payment_intent, array("stripe_account" => $stripeacc));
}
}*/
// The metadata FULLTAG is defined by the online payment page
$FULLTAG = $charge->metadata->FULLTAG;
// Save into $tmparray all metadata
$tmparray = dolExplodeIntoArray($FULLTAG, '.', '=');
// Load origin object according to metadata
2021-02-26 21:17:52 +01:00
if (!empty($tmparray['CUS']) && $tmparray['CUS'] > 0) {
2020-10-16 13:30:20 +02:00
$societestatic->fetch($tmparray['CUS']);
2021-02-26 21:17:52 +01:00
} elseif (!empty($charge->metadata->dol_thirdparty_id) && $charge->metadata->dol_thirdparty_id > 0) {
2020-10-16 13:30:20 +02:00
$societestatic->fetch($charge->metadata->dol_thirdparty_id);
} else {
$societestatic->id = 0;
}
2021-02-26 21:17:52 +01:00
if (!empty($tmparray['MEM']) && $tmparray['MEM'] > 0) {
2020-10-16 13:30:20 +02:00
$memberstatic->fetch($tmparray['MEM']);
} else {
$memberstatic->id = 0;
}
2020-09-24 21:54:21 +02:00
2020-10-16 13:30:20 +02:00
print '<tr class="oddeven">';
2020-09-24 21:54:21 +02:00
2021-02-26 21:17:52 +01:00
if (!empty($stripeacc)) {
$connect = $stripeacc.'/';
2022-06-12 15:08:19 +02:00
} else {
$connect = '';
2021-02-26 21:17:52 +01:00
}
2020-10-16 13:30:20 +02:00
// Ref
$url = 'https://dashboard.stripe.com/'.$connect.'test/payments/'.$charge->id;
2021-02-26 21:17:52 +01:00
if ($servicestatus) {
2020-10-16 13:30:20 +02:00
$url = 'https://dashboard.stripe.com/'.$connect.'payments/'.$charge->id;
}
print "<td>";
print "<a href='".$url."' target='_stripe'>".img_picto($langs->trans('ShowInStripe'), 'globe')." ".$charge->id."</a>";
2021-02-26 21:17:52 +01:00
if ($charge->payment_intent) {
print '<br><span class="opacitymedium">'.$charge->payment_intent.'</span>';
}
2020-10-16 13:30:20 +02:00
print "</td>\n";
// Stripe customer
print "<td>";
2021-02-26 21:17:52 +01:00
if (!empty($conf->stripe->enabled) && !empty($stripeacc)) {
$connect = $stripeacc.'/';
}
2020-10-16 13:30:20 +02:00
$url = 'https://dashboard.stripe.com/'.$connect.'test/customers/'.$charge->customer;
2021-02-26 21:17:52 +01:00
if ($servicestatus) {
2020-10-16 13:30:20 +02:00
$url = 'https://dashboard.stripe.com/'.$connect.'customers/'.$charge->customer;
}
2021-02-26 21:17:52 +01:00
if (!empty($charge->customer)) {
2020-10-16 13:30:20 +02:00
print '<a href="'.$url.'" target="_stripe">'.img_picto($langs->trans('ShowInStripe'), 'globe').' '.$charge->customer.'</a>';
}
print "</td>\n";
// Link
print "<td>";
2021-02-26 21:17:52 +01:00
if ($societestatic->id > 0) {
2020-10-16 13:30:20 +02:00
print $societestatic->getNomUrl(1);
2021-02-26 21:17:52 +01:00
} elseif ($memberstatic->id > 0) {
2020-10-16 13:30:20 +02:00
print $memberstatic->getNomUrl(1);
}
print "</td>\n";
// Origin
print "<td>";
if ($charge->metadata->dol_type == "order" || $charge->metadata->dol_type == "commande") {
$object = new Commande($db);
$object->fetch($charge->metadata->dol_id);
if ($object->id > 0) {
2022-07-28 19:43:58 +02:00
print "<a href='".DOL_URL_ROOT."/commande/card.php?id=".$object->id."'>".img_picto('', 'order')." ".$object->ref."</a>";
2020-10-16 13:30:20 +02:00
} else {
print $FULLTAG;
2020-09-24 21:54:21 +02:00
}
2020-10-16 13:30:20 +02:00
} elseif ($charge->metadata->dol_type == "invoice" || $charge->metadata->dol_type == "facture") {
$object = new Facture($db);
$object->fetch($charge->metadata->dol_id);
if ($object->id > 0) {
2022-07-28 19:43:58 +02:00
print "<a href='".DOL_URL_ROOT."/compta/facture/card.php?facid=".$charge->metadata->dol_id."'>".img_picto('', 'bill')." ".$object->ref."</a>";
2020-09-24 21:54:21 +02:00
} else {
2019-10-27 17:01:23 +01:00
print $FULLTAG;
}
2020-10-16 13:30:20 +02:00
} else {
print $FULLTAG;
2019-10-27 17:01:23 +01:00
}
2020-10-16 13:30:20 +02:00
print "</td>\n";
// Date payment
2022-05-18 23:11:59 +02:00
print '<td class="center">'.dol_print_date($charge->created, 'dayhour')."</td>\n";
2020-10-16 13:30:20 +02:00
// Type
print '<td>';
print $type;
print '</td>';
// Amount
2022-05-18 23:11:59 +02:00
print '<td class="right"><span class="amount">'.price(($charge->amount - $charge->amount_refunded) / 100, 0, '', 1, - 1, - 1, strtoupper($charge->currency))."</span></td>";
2020-10-16 13:30:20 +02:00
// Status
print '<td class="right">';
print $status;
print "</td>\n";
print "</tr>\n";
$i++;
}
2020-10-16 13:30:20 +02:00
print '</table>';
print '</div>';
print '</form>';
}
2018-03-05 19:58:50 +01:00
2018-08-05 10:56:39 +02:00
// End of page
2018-03-05 19:58:50 +01:00
llxFooter();
$db->close();