From 415332c22a9c47fcc9686e604bc4880f4d4d682a Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Thu, 8 Mar 2018 20:05:40 +0100 Subject: [PATCH 01/12] ADD IPN/Webhook for stripe TODO: optimise code and uncustomize of ptibogxiv.net but good and fonctionnal for test --- htdocs/public/stripe/ipn.php | 335 +++++++++++++++++++++++++++++++++++ 1 file changed, 335 insertions(+) create mode 100644 htdocs/public/stripe/ipn.php diff --git a/htdocs/public/stripe/ipn.php b/htdocs/public/stripe/ipn.php new file mode 100644 index 00000000000..d60d7d618bd --- /dev/null +++ b/htdocs/public/stripe/ipn.php @@ -0,0 +1,335 @@ +stripeconnect->enabled)) accessforbidden('',0,0,1); +require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php'; +require_once DOL_DOCUMENT_ROOT.'/includes/stripe/init.php'; +require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php'; +//dol_include_once('/stripeconnect/class/paymentfee.class.php'); +require_once DOL_DOCUMENT_ROOT.'/core/class/ccountry.class.php'; +require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php'; +require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php'; +require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; +require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; +require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php'; + +// You can find your endpoint's secret in your webhook settings +if (isset($_GET['connect'])){ +if (isset($_GET['test'])) +{ +$endpoint_secret = $conf->global->STRIPE_TEST_WEBHOOK_CONNECT_KEY; +} +else +{ +$endpoint_secret = $conf->global->STRIPE_LIVE_WEBHOOK_CONNECT_KEY; +} +}else { +if (isset($_GET['test'])) +{ +$endpoint_secret = $conf->global->STRIPE_TEST_WEBHOOK_KEY; +} +else +{ +$endpoint_secret = $conf->global->STRIPE_LIVE_WEBHOOK_KEY; +} +} +$payload = @file_get_contents("php://input"); +$sig_header = $_SERVER["HTTP_STRIPE_SIGNATURE"]; +$event = null; + +$error = 0; + +try { +$event = \Stripe\Webhook::constructEvent($payload, $sig_header, $endpoint_secret); +} +catch(\UnexpectedValueException $e) { + // Invalid payload + http_response_code(400); // PHP 5.4 or greater + exit(); +} catch(\Stripe\Error\SignatureVerification $e) { + // Invalid signature + http_response_code(400); // PHP 5.4 or greater + exit(); +} + +// Do something with $event + +http_response_code(200); // PHP 5.4 or greater +$langs->load("main"); +$user = new User($db); +$user->fetch(5); +$user->getrights(); + +if (! empty($conf->stripeconnect->enabled) && ! empty($conf->multicompany>enabled)) { + $sql = "SELECT entity"; + $sql.= " FROM ".MAIN_DB_PREFIX."stripe_entity"; + $sql.= " WHERE key_account = '".$event->account."'"; + + dol_syslog(get_class($db) . "::fetch", LOG_DEBUG); + $result = $db->query($sql); + if ($result) + { + if ($db->num_rows($result)) + { + $obj = $db->fetch_object($result); + $key=$obj->entity; + } + else {$key=1;} + } + else {$key=1;} +$ret=$mc->switchEntity($key); +if (! $res && file_exists("../../main.inc.php")) $res=@include("../../main.inc.php"); // to work if your module directory is into a subdir of root htdocs directory +if (! $res) die("Include of main fails"); +} + +// list of action +$stripeconnect=new StripeConnexion($db); +if ($event->type == 'payout.created') { + $error=0; + $db->begin(); + $sql = "UPDATE ".MAIN_DB_PREFIX."stripeconnect_entity SET next_payout = '".date('Y-m-d H:i:s',$event->data->object->arrival_date)."'"; + $sql.= " WHERE entity = ".$key; + + $result = $db->query($sql); + if ($result) + { +$body = "Un virement de ".price2num($event->data->object->amount/100)." ".$event->data->object->currency." est attendu sur votre compte le ".date('d-m-Y H:i:s',$event->data->object->arrival_date); +$subject = '[NOTIFICATION] Virement programmée'; +$headers = 'From: "'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>'; +mail(''.$conf->global->MAIN_INFO_SOCIETE_MAIL.'', $subject, $body, $headers); + $db->commit(); + return 1; + } + else + { + $db->error(); + $db->rollback(); + return -1; + } +} +elseif ($event->type == 'payout.paid') { +global $conf; + $error=0; + $db->begin(); + $sql = "UPDATE ".MAIN_DB_PREFIX."stripeconnect_entity SET next_payout = null"; + $sql.= " WHERE entity = ".$key; + + $result = $db->query($sql); + if ($result) + { + // Call trigger + //$result=$this->call_trigger('MEMBER_RESILIATE',$user); + //if ($result < 0) { $error++; $this->db->rollback(); return -1; } + // End call triggers + $langs->load("errors"); + + $dateo = dol_now(); + $label = $event->data->object->description; + $amount= $event->data->object->amount/100; + $amount_to= $event->data->object->amount/100; +require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; + + $accountfrom=new Account($db); + $accountfrom->fetch($conf->global->STRIPE_BANK_ACCOUNT_FOR_PAYMENTS); + + $accountto=new Account($db); + $accountto->fetch($conf->global->STRIPE_BANK_ACCOUNT_FOR_BANKTRANFERS); + + if ($accountto->currency_code != $accountfrom->currency_code) { + $error++; + setEventMessages($langs->trans("ErrorTransferBetweenDifferentCurrencyNotPossible"), null, 'errors'); + } + + if ($accountto->id != $accountfrom->id) + { + + $bank_line_id_from=0; + $bank_line_id_to=0; + $result=0; + + // By default, electronic transfert from bank to bank + $typefrom='PRE'; + $typeto='VIR'; + + if (! $error) $bank_line_id_from = $accountfrom->addline($dateo, $typefrom, $label, -1*price2num($amount), '', '', $user); + if (! ($bank_line_id_from > 0)) $error++; + if ((! $error) && ($accountto->currency_code == $accountfrom->currency_code)) $bank_line_id_to = $accountto->addline($dateo, $typeto, $label, price2num($amount), '', '', $user); + if ((! $error) && ($accountto->currency_code != $accountfrom->currency_code)) $bank_line_id_to = $accountto->addline($dateo, $typeto, $label, price2num($amount_to), '', '', $user); + if (! ($bank_line_id_to > 0)) $error++; + + if (! $error) $result=$accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.'/compta/bank/ligne.php?rowid=', '(banktransfert)', 'banktransfert'); + if (! ($result > 0)) $error++; + if (! $error) $result=$accountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.'/compta/bank/ligne.php?rowid=', '(banktransfert)', 'banktransfert'); + if (! ($result > 0)) $error++; + } + +$body = "Un virement de ".price2num($event->data->object->amount/100)." ".$event->data->object->currency." a ete effectue sur votre compte le ".date('d-m-Y H:i:s',$event->data->object->arrival_date); +$subject = '[NOTIFICATION] Virement effectué'; +$headers = 'From: "'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>'; +mail(''.$conf->global->MAIN_INFO_SOCIETE_MAIL.'', $subject, $body, $headers); + $db->commit(); + return 1; + } + else + { + $db->error(); + $db->rollback(); + return -1; + } +} +elseif ($event->type == 'charge.succeeded') { +//if (isset($_GET['connect'])){ +//$txn=\Stripe\BalanceTransaction::retrieve("".$event->data->object->balance_transaction."",array("stripe_account" => $event->account)); +//} +//else { +//$txn=\Stripe\BalanceTransaction::retrieve("".$event->data->object->balance_transaction.""); +//} + +//$input=$txn->fee_details; +//$fee = new PaymentStripeFee($db); +//$fee->accountid=10; +//$fee->fk_soc=473; +///foreach ($input as $src) { +//if ($src->type=='stripe_fee') { +//$fee->stripe_fee=price2num($src->amount/100); +//} +//elseif ($src->type=='application_fee') { +//$fee->application_fee=price2num($src->amount/100); +//} + +//}// +//$fee->currency=$txn->currency; +//$fee->datev=dol_now(); +//$fee->datep=dol_now(); +//$fee->datesp=dol_now(); +//$fee->dateep=dol_now(); +//$fee->type_payment=3; +//$fee->num_payment=$txn->source; +//$fee->fk_user_author=$user->id; +//$fee->create($user); + +} +elseif ($event->type == 'charge.failed') { + +$subject = 'Your payment has been received: '.$event->data->object->id.''; +$headers = 'From: "test webhook" '; +//mail('ptibogxiv@msn.com', $subject, 'test', $headers); + +} +elseif (($event->type == 'source.chargeable') && ($event->data->object->type == 'three_d_secure') && ($event->data->object->three_d_secure->authenticated==true)) { + +$stripeconnect=new StripeConnexion($db); +$charge=$stripeconnect->CreatePaymentStripe($event->data->object->amount/100,$event->data->object->currency,$event->data->object->metadata->source,$event->data->object->metadata->idsource,$event->data->object->id,$event->data->object->metadata->customer,$stripeconnect->GetStripeAccount($entity)); + +if (isset($charge->id) && $charge->statut=='error'){ +$msg=$charge->message; +$code=$charge->code; +$error++; +} +elseif (isset($charge->id) && $charge->statut=='success' && $event->data->object->metadata->source=='order') { +$order=new Commande($db); +$order->fetch($event->data->object->metadata->idsource); +$invoice = new Facture($db); +$idinv=$invoice->createFromOrder($order); + +if ($idinv > 0) +{ +$result=$invoice->validate($user); +if ($result > 0) { +$invoice->fetch($idinv); +$paiement = $invoice->getSommePaiement(); +$creditnotes=$invoice->getSumCreditNotesUsed(); +$deposits=$invoice->getSumDepositsUsed(); +$ref=$invoice->ref; +$ifverif=$invoice->socid; +$currency=$invoice->multicurrency_code; +$total=price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,'MT'); +}else{ +$msg=$invoice->error; +$error++; +} +}else{ +$msg=$invoice->error; +$error++; +} +} + +if (!$error){ +$datepaye = dol_now(); +$paiementcode ="CB"; +$amounts=array(); +$amounts[$invoice->id] = $total; +$multicurrency_amounts=array(); +//$multicurrency_amounts[$item] = $total; + $paiement = new Paiement($db); + $paiement->datepaye = $datepaye; + $paiement->amounts = $amounts; // Array with all payments dispatching + $paiement->multicurrency_amounts = $multicurrency_amounts; // Array with all payments dispatching + $paiement->paiementid = dol_getIdFromCode($db,$paiementcode,'c_paiement'); + $paiement->num_paiement = $charge->message; + $paiement->note = ''; +} + +if (! $error){ +$paiement_id=$paiement->create($user, 0); + +if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE) && count($invoice->lines)){ + $outputlangs = $langs; + $newlang = ''; + if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) $newlang = GETPOST('lang_id','aZ09'); + if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $invoice->thirdparty->default_lang; + if (! empty($newlang)) { + $outputlangs = new Translate("", $conf); + $outputlangs->setDefaultLang($newlang); + } + $model=$invoice->modelpdf; + $ret = $invoice->fetch($invoice->id); // Reload to get new records + +$invoice->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);} +if ($paiement_id < 0){ + $msg=$paiement->errors; + $error++; +}else{ + if ($event->data->object->metadata->source=='order') { + $order->classifyBilled($user); + } + } + } + +if (! $error){ + $label='(CustomerInvoicePayment)'; + if (GETPOST('type') == 2) $label='(CustomerInvoicePaymentBack)'; + $paiement->addPaymentToBank($user,'payment',$label,$conf->global->STRIPE_BANK_ACCOUNT_FOR_PAYMENTS,'',''); + if ($result < 0) + { + $msg=$paiement->errors; + $error++; + } +$invoice->set_paid($user); +} + +$body = ""; +$subject = 'Facture '.$invoice->ref; +$headers = 'From: "test webhook" '; +//mail('ptibogxiv@msn.com', $subject, $body, $headers); +} +elseif ($event->type == 'customer.deleted') { + $db->begin(); + $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_stripe WHERE fk_key = '".$event->data->object->id."' "; + dol_syslog(get_class($this) . "::delete sql=" . $sql, LOG_DEBUG); + $db->query($sql); + $db->commit(); +} + + + +?> \ No newline at end of file From 432aa5081262ab0a7fb2ff461816e2de4e064535 Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Thu, 8 Mar 2018 20:08:43 +0100 Subject: [PATCH 02/12] Update ipn.php --- htdocs/public/stripe/ipn.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/public/stripe/ipn.php b/htdocs/public/stripe/ipn.php index d60d7d618bd..1f4b4cbb3d6 100644 --- a/htdocs/public/stripe/ipn.php +++ b/htdocs/public/stripe/ipn.php @@ -69,7 +69,7 @@ $user = new User($db); $user->fetch(5); $user->getrights(); -if (! empty($conf->stripeconnect->enabled) && ! empty($conf->multicompany>enabled)) { +if (! empty($conf->stripeconnect->enabled) && ! empty($conf->multicompany->enabled)) { $sql = "SELECT entity"; $sql.= " FROM ".MAIN_DB_PREFIX."stripe_entity"; $sql.= " WHERE key_account = '".$event->account."'"; @@ -332,4 +332,4 @@ elseif ($event->type == 'customer.deleted') { -?> \ No newline at end of file +?> From 264112ab59534b3cb145f84c6e3d804ebbd813bf Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Thu, 8 Mar 2018 23:07:58 +0100 Subject: [PATCH 03/12] NEW actions on card.php for company and member --- htdocs/stripe/class/actions_stripe.class.php | 217 +++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 htdocs/stripe/class/actions_stripe.class.php diff --git a/htdocs/stripe/class/actions_stripe.class.php b/htdocs/stripe/class/actions_stripe.class.php new file mode 100644 index 00000000000..6101ce31f70 --- /dev/null +++ b/htdocs/stripe/class/actions_stripe.class.php @@ -0,0 +1,217 @@ + + * Copyright (C) 2011 Herve Prot + * Copyright (C) 2014 Philippe Grand + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +/** + * \file htdocs/multicompany/actions_multicompany.class.php + * \ingroup multicompany + * \brief File Class multicompany + */ +require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';; +$langs->load("stripeconnect@stripeconnect"); + +/** + * \class ActionsMulticompany + * \brief Class Actions of the module multicompany + */ +class ActionsStripeconnect +{ + /** @var DoliDB */ + var $db; + + private $config=array(); + + // For Hookmanager return + var $resprints; + var $results=array(); + + + /** + * Constructor + * + * @param DoliDB $db Database handler + */ + function __construct($db) + { + $this->db = $db; + } + + + /** + * + */ + function formObjectOptions($parameters=false, &$object, &$action='') + { + global $db,$conf,$user,$langs,$form; + + if (! empty($conf->stripe->enabled) && (empty($conf->global->STRIPE_LIVE) || empty($conf->global->STRIPECONNECT_LIVE) || GETPOST('forcesandbox','alpha'))) +{ + $service = 'StripeTest'; + dol_htmloutput_mesg($langs->trans('YouAreCurrentlyInSandboxMode','Stripe'),'','warning'); +} +else +{ + $service = 'StripeLive'; +} + + if (is_array($parameters) && ! empty($parameters)) + { + foreach($parameters as $key=>$value) + { + $key=$value; + } + } + + + if (is_object($object) && $object->element == 'societe') + { + $this->resprints.= ''; + $this->resprints.= '
'; + $this->resprints.= $langs->trans('StripeCustomer'); + $this->resprints.= ''; +// $this->resprints.= ''.img_edit().''; + $this->resprints.= '
'; + $this->resprints.= ''; + $this->resprints.= ''; + $stripe=new Stripe($db); +if ($stripe->getStripeAccount($service)&&$object->client!=0) { +$customer=$stripe->customerStripe($object->id,$stripe->getStripeAccount($service)); +$this->resprints.= $customer->id; +} +else { +$this->resprints.= $langs->trans("NoStripe"); +} + $this->resprints.= ''; + + } + elseif (is_object($object) && $object->element == 'member'){ + $this->resprints.= ''; + $this->resprints.= '
'; + $this->resprints.= $langs->trans('StripeCustomer'); + $this->resprints.= ''; + $this->resprints.= '
'; + $this->resprints.= ''; + $this->resprints.= ''; + $stripe=new Stripe($db); +if ($stripe->getStripeAccount($service)&&$object->fk_soc>'0') { +$customer=$stripe->customerStripe($object->fk_soc,$stripe->getStripeAccount($service)); +$this->resprints.= $customer->id; +} +else { +$this->resprints.= $langs->trans("NoStripe"); +} + $this->resprints.= ''; + + $this->resprints.= ''; + $this->resprints.= '
'; + $this->resprints.= $langs->trans('SubscriptionStripe'); + $this->resprints.= ''; + $this->resprints.= '
'; + $this->resprints.= ''; + $this->resprints.= ''; + $stripe=new Stripe($db); +if (7==4) { +$customer=$stripe->customerStripe($object->id,$stripe->getStripeAccount($service)); +$this->resprints.= $customer->id; +} +else { +$this->resprints.= $langs->trans("NoStripe"); +} + $this->resprints.= ''; + } + elseif (is_object($object) && $object->element == 'adherent_type'){ + $this->resprints.= ''; + $this->resprints.= '
'; + $this->resprints.= $langs->trans('PlanStripe'); + $this->resprints.= ''; +// $this->resprints.= ''.img_edit().''; + $this->resprints.= '
'; + $this->resprints.= ''; + $this->resprints.= ''; + $stripe=new Stripe($db); +if (7==4) { +$customer=$stripe->customerStripe($object->id,$stripe->getStripeAccount($service)); +$this->resprints.= $customer->id; +} +else { +$this->resprints.= $langs->trans("NoStripe"); +} + $this->resprints.= ''; + } + return 0; + } + + function addMoreActionsButtons($parameters=false, &$object, &$action='') + { + global $db,$conf,$user,$langs,$form; + if (is_object($object) && $object->element == 'facture'){ + // On verifie si la facture a des paiements + $sql = 'SELECT pf.amount'; + $sql .= ' FROM ' . MAIN_DB_PREFIX . 'paiement_facture as pf'; + $sql .= ' WHERE pf.fk_facture = ' . $object->id; + + $result = $db->query($sql); + if ($result) { + $i = 0; + $num = $db->num_rows($result); + + while ($i < $num) { + $objp = $db->fetch_object($result); + $totalpaye += $objp->amount; + $i ++; + } + } else { + dol_print_error($db, ''); + } + + $resteapayer = $object->total_ttc - $totalpaye; + // Request a direct debit order + if ($object->statut > Facture::STATUS_DRAFT && $object->statut < Facture::STATUS_ABANDONED && $object->paye == 0) + { $stripe=new Stripe($db); + if ($resteapayer > 0) + { + if ($stripe->getStripeAccount($conf->entity)) // a modifier avec droit stripe + { + $langs->load("withdrawals"); + print ''.$langs->trans("StripeConnectPay").''; + } + else + { + print ''.$langs->trans("StripeConnectPay").''; + } + } + elseif ($resteapayer == 0) + { + print ''.$langs->trans("StripeConnectPay").''; + } + } + else { + print ''.$langs->trans("StripeConnectPay").''; + } + } + elseif (is_object($object) && $object->element == 'invoice_supplier'){ +print ''.$langs->trans("StripeConnectPay").''; + } + elseif (is_object($object) && $object->element == 'member'){ +print ''.$langs->trans("StripeAutoSubscription").''; + } + return 0; + } + +} From 3d411f09fc8f9e94bed1c3645ce5686fe8e60f61 Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Thu, 8 Mar 2018 23:13:37 +0100 Subject: [PATCH 04/12] Update actions_stripe.class.php --- htdocs/stripe/class/actions_stripe.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/stripe/class/actions_stripe.class.php b/htdocs/stripe/class/actions_stripe.class.php index 6101ce31f70..513999854e8 100644 --- a/htdocs/stripe/class/actions_stripe.class.php +++ b/htdocs/stripe/class/actions_stripe.class.php @@ -24,7 +24,7 @@ * \brief File Class multicompany */ require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';; -$langs->load("stripeconnect@stripeconnect"); +$langs->load("stripe@stripe"); /** * \class ActionsMulticompany From 51398ad060f955710bfc715d070c8b00973bee54 Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Thu, 8 Mar 2018 23:27:20 +0100 Subject: [PATCH 05/12] FIX dolibarr version --- htdocs/stripe/config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/stripe/config.php b/htdocs/stripe/config.php index a692e541050..0e207474a51 100644 --- a/htdocs/stripe/config.php +++ b/htdocs/stripe/config.php @@ -65,4 +65,4 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php'; -\Stripe\Stripe::setAppInfo("Stripe", "dolibarr version", "https://www.dolibarr.org"); // add dolibarr version +\Stripe\Stripe::setAppInfo("Stripe", DOL_VERSION, "https://www.dolibarr.org"); // add dolibarr version From a2fd2bc3f36c6a9a4cfdbdd5d517f253b5b139c1 Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Fri, 9 Mar 2018 00:03:44 +0100 Subject: [PATCH 06/12] FIX entity TO DO: save stripe account in multicompany mode and without stripe connect in theadmin of stripe module because stripe doesn't send custom field for all request RISK: confusion and error in payment and transfert bank without this --- htdocs/public/stripe/ipn.php | 607 +++++++++++++++++------------------ 1 file changed, 297 insertions(+), 310 deletions(-) diff --git a/htdocs/public/stripe/ipn.php b/htdocs/public/stripe/ipn.php index 1f4b4cbb3d6..d56fa61422a 100644 --- a/htdocs/public/stripe/ipn.php +++ b/htdocs/public/stripe/ipn.php @@ -1,335 +1,322 @@ + * + * 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 + * along with this program. If not, see . + */ -define("NOLOGIN",1); // This means this output page does not require to be logged. -define("NOCSRFCHECK",1); // We accept to go on this page from external web site. - -$entity=(! empty($_GET['entity']) ? (int) $_GET['entity'] : (! empty($_POST['entity']) ? (int) $_POST['entity'] : 1)); -if (is_numeric($entity)) define("DOLENTITY", $entity); - -$res=0; -if (! $res && file_exists("../../main.inc.php")) $res=@include("../../main.inc.php"); // to work if your module directory is into a subdir of root htdocs directory -if (! $res) die("Include of main fails"); - -if (empty($conf->stripeconnect->enabled)) accessforbidden('',0,0,1); -require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php'; -require_once DOL_DOCUMENT_ROOT.'/includes/stripe/init.php'; -require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php'; -//dol_include_once('/stripeconnect/class/paymentfee.class.php'); -require_once DOL_DOCUMENT_ROOT.'/core/class/ccountry.class.php'; -require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php'; -require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php'; -require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; -require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; +// Put here all includes required by your class file +require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php'; require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php'; +require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php'; +require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; +require_once DOL_DOCUMENT_ROOT.'/stripe/config.php'; -// You can find your endpoint's secret in your webhook settings -if (isset($_GET['connect'])){ -if (isset($_GET['test'])) + +/** + * Stripe class + */ +class Stripe extends CommonObject { -$endpoint_secret = $conf->global->STRIPE_TEST_WEBHOOK_CONNECT_KEY; -} -else -{ -$endpoint_secret = $conf->global->STRIPE_LIVE_WEBHOOK_CONNECT_KEY; -} -}else { -if (isset($_GET['test'])) -{ -$endpoint_secret = $conf->global->STRIPE_TEST_WEBHOOK_KEY; -} -else -{ -$endpoint_secret = $conf->global->STRIPE_LIVE_WEBHOOK_KEY; -} -} -$payload = @file_get_contents("php://input"); -$sig_header = $_SERVER["HTTP_STRIPE_SIGNATURE"]; -$event = null; + public $rowid; + public $fk_soc; + public $fk_key; + public $id; + public $mode; + public $entity; + public $statut; + public $type; + public $code; + public $message; -$error = 0; - -try { -$event = \Stripe\Webhook::constructEvent($payload, $sig_header, $endpoint_secret); -} -catch(\UnexpectedValueException $e) { - // Invalid payload - http_response_code(400); // PHP 5.4 or greater - exit(); -} catch(\Stripe\Error\SignatureVerification $e) { - // Invalid signature - http_response_code(400); // PHP 5.4 or greater - exit(); -} + /** + * Constructor + * + * @param DoliDB $db Database handler + */ + public function __construct($db) + { + $this->db = $db; -// Do something with $event + } -http_response_code(200); // PHP 5.4 or greater -$langs->load("main"); -$user = new User($db); -$user->fetch(5); -$user->getrights(); -if (! empty($conf->stripeconnect->enabled) && ! empty($conf->multicompany->enabled)) { - $sql = "SELECT entity"; - $sql.= " FROM ".MAIN_DB_PREFIX."stripe_entity"; - $sql.= " WHERE key_account = '".$event->account."'"; + /** + * Return stripe account + * + * @param string $mode 'StripeTest' or 'StripeLive' + * @return int ??? + */ + public function getStripeAccount($mode='StripeTest') + { + global $conf; - dol_syslog(get_class($db) . "::fetch", LOG_DEBUG); - $result = $db->query($sql); - if ($result) + $sql = "SELECT tokenstring"; + $sql.= " FROM ".MAIN_DB_PREFIX."oauth_token"; + $sql.= " WHERE entity = ".$conf->entity; + $sql.= " AND service = '".$mode."'"; + + dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); + $result = $this->db->query($sql); + if ($result) { - if ($db->num_rows($result)) + if ($this->db->num_rows($result)) { - $obj = $db->fetch_object($result); - $key=$obj->entity; - } - else {$key=1;} - } - else {$key=1;} -$ret=$mc->switchEntity($key); -if (! $res && file_exists("../../main.inc.php")) $res=@include("../../main.inc.php"); // to work if your module directory is into a subdir of root htdocs directory -if (! $res) die("Include of main fails"); -} + $obj = $this->db->fetch_object($result); + $tokenstring=$obj->tokenstring; -// list of action -$stripeconnect=new StripeConnexion($db); -if ($event->type == 'payout.created') { - $error=0; - $db->begin(); - $sql = "UPDATE ".MAIN_DB_PREFIX."stripeconnect_entity SET next_payout = '".date('Y-m-d H:i:s',$event->data->object->arrival_date)."'"; - $sql.= " WHERE entity = ".$key; + $tmparray = dol_json_decode($tokenstring); + $key = $tmparray->stripe_user_id; + } + else { + $tokenstring=''; + } + } + else { + dol_print_error($this->db); + } - $result = $db->query($sql); - if ($result) - { -$body = "Un virement de ".price2num($event->data->object->amount/100)." ".$event->data->object->currency." est attendu sur votre compte le ".date('d-m-Y H:i:s',$event->data->object->arrival_date); -$subject = '[NOTIFICATION] Virement programmée'; -$headers = 'From: "'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>'; -mail(''.$conf->global->MAIN_INFO_SOCIETE_MAIL.'', $subject, $body, $headers); - $db->commit(); - return 1; - } - else - { - $db->error(); - $db->rollback(); - return -1; - } -} -elseif ($event->type == 'payout.paid') { -global $conf; - $error=0; - $db->begin(); - $sql = "UPDATE ".MAIN_DB_PREFIX."stripeconnect_entity SET next_payout = null"; - $sql.= " WHERE entity = ".$key; + return $key; + } - $result = $db->query($sql); - if ($result) - { - // Call trigger - //$result=$this->call_trigger('MEMBER_RESILIATE',$user); - //if ($result < 0) { $error++; $this->db->rollback(); return -1; } - // End call triggers - $langs->load("errors"); + /** + * customerStripe + * + * @param int $id ??? + * @param string $key ??? + * @return \Stripe\StripeObject|\Stripe\ApiResource + */ + public function customerStripe($id,$key) + { + global $conf; + if (empty($conf->global->STRIPECONNECT_LIVE)) { + $mode = 0; + } else { + if (empty($conf->global->STRIPE_LIVE)) { + $mode = 0; + } else { + $mode = $conf->global->STRIPE_LIVE; + } + } + $sql = "SELECT rowid,fk_soc,fk_key,mode,entity"; + $sql .= " FROM " . MAIN_DB_PREFIX . "societe_stripe"; + $sql .= " WHERE fk_soc = " . $id . " "; + $sql .= " AND mode=" . $mode . " AND entity IN (" . getEntity('stripe') . ")"; - $dateo = dol_now(); - $label = $event->data->object->description; - $amount= $event->data->object->amount/100; - $amount_to= $event->data->object->amount/100; -require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; + dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); + $resql = $this->db->query($sql); + if ($resql) { + $soc = new Societe($this->db); + $soc->fetch($id); + $num = $this->db->num_rows($resql); + if ($num) { + $obj = $this->db->fetch_object($resql); + $tiers = $obj->fk_key; + if ($conf->entity == 1) { + $customer = \Stripe\Customer::retrieve("$tiers"); + } else { + $customer = \Stripe\Customer::retrieve("$tiers", array( + "stripe_account" => $key + )); + } + } else { + if ($conf->entity == 1) { + $customer = \Stripe\Customer::create(array( + "email" => $soc->email, + "description" => $soc->name + )); + } else { + $customer = \Stripe\Customer::create(array( + "email" => $soc->email, + "description" => $soc->name + ), array( + "stripe_account" => $key + )); + } + $customer_id = "" . $customer->id . ""; + $sql = "INSERT INTO " . MAIN_DB_PREFIX . "societe_stripe (fk_soc,fk_key,mode,entity)"; + $sql .= " VALUES ($id,'$customer_id'," . $mode . "," . $conf->entity . ")"; + dol_syslog(get_class($this) . "::create sql=" . $sql, LOG_DEBUG); + $resql = $this->db->query($sql); + } + } + return $customer; + } - $accountfrom=new Account($db); - $accountfrom->fetch($conf->global->STRIPE_BANK_ACCOUNT_FOR_PAYMENTS); + /** + * createPaymentStripe + * + * @param unknown $amount ??? + * @param unknown $currency ??? + * @param unknown $origin ??? + * @param unknown $item ??? + * @param unknown $source ??? + * @param unknown $customer ??? + * @param unknown $account ??? + * @return Stripe + */ + public function createPaymentStripe($amount, $currency, $origin, $item, $source, $customer, $account) + { + global $conf; + if (empty($conf->global->STRIPECONNECT_LIVE)) { + $mode = 0; + } else { + if (empty($conf->global->STRIPE_LIVE)) { + $mode = 0; + } else { + $mode = $conf->global->STRIPE_LIVE; + } + } + $sql = "SELECT fk_soc,fk_key,mode,entity"; + $sql .= " FROM " . MAIN_DB_PREFIX . "societe_stripe"; + $sql .= " WHERE fk_key = '$customer' "; + $sql .= " AND mode=" . $mode . " "; - $accountto=new Account($db); - $accountto->fetch($conf->global->STRIPE_BANK_ACCOUNT_FOR_BANKTRANFERS); + dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); + $result = $this->db->query($sql); + if ($result) { + if ($this->db->num_rows($result)) { + $obj = $this->db->fetch_object($result); + $entite = $obj->entity; + $fksoc = $obj->fk_soc; + } + } + $stripeamount = round($amount * 100); + $societe = new Societe($this->db); + $societe->fetch($fksoc); - if ($accountto->currency_code != $accountfrom->currency_code) { - $error++; - setEventMessages($langs->trans("ErrorTransferBetweenDifferentCurrencyNotPossible"), null, 'errors'); + if ($origin == order) { + $order = new Commande($this->db); + $order->fetch($item); + $ref = $order->ref; + $description = "ORD=" . $ref . ".CUS=" . $societe->code_client; + } elseif ($origin == invoice) { + $invoice = new Facture($this->db); + $invoice->fetch($item); + $ref = $invoice->ref; + $description = "INV=" . $ref . ".CUS=" . $societe->code_client; } - if ($accountto->id != $accountfrom->id) - { - - $bank_line_id_from=0; - $bank_line_id_to=0; - $result=0; - - // By default, electronic transfert from bank to bank - $typefrom='PRE'; - $typeto='VIR'; - - if (! $error) $bank_line_id_from = $accountfrom->addline($dateo, $typefrom, $label, -1*price2num($amount), '', '', $user); - if (! ($bank_line_id_from > 0)) $error++; - if ((! $error) && ($accountto->currency_code == $accountfrom->currency_code)) $bank_line_id_to = $accountto->addline($dateo, $typeto, $label, price2num($amount), '', '', $user); - if ((! $error) && ($accountto->currency_code != $accountfrom->currency_code)) $bank_line_id_to = $accountto->addline($dateo, $typeto, $label, price2num($amount_to), '', '', $user); - if (! ($bank_line_id_to > 0)) $error++; - - if (! $error) $result=$accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.'/compta/bank/ligne.php?rowid=', '(banktransfert)', 'banktransfert'); - if (! ($result > 0)) $error++; - if (! $error) $result=$accountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.'/compta/bank/ligne.php?rowid=', '(banktransfert)', 'banktransfert'); - if (! ($result > 0)) $error++; - } - -$body = "Un virement de ".price2num($event->data->object->amount/100)." ".$event->data->object->currency." a ete effectue sur votre compte le ".date('d-m-Y H:i:s',$event->data->object->arrival_date); -$subject = '[NOTIFICATION] Virement effectué'; -$headers = 'From: "'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>'; -mail(''.$conf->global->MAIN_INFO_SOCIETE_MAIL.'', $subject, $body, $headers); - $db->commit(); - return 1; - } - else - { - $db->error(); - $db->rollback(); - return -1; - } -} -elseif ($event->type == 'charge.succeeded') { -//if (isset($_GET['connect'])){ -//$txn=\Stripe\BalanceTransaction::retrieve("".$event->data->object->balance_transaction."",array("stripe_account" => $event->account)); -//} -//else { -//$txn=\Stripe\BalanceTransaction::retrieve("".$event->data->object->balance_transaction.""); -//} - -//$input=$txn->fee_details; -//$fee = new PaymentStripeFee($db); -//$fee->accountid=10; -//$fee->fk_soc=473; -///foreach ($input as $src) { -//if ($src->type=='stripe_fee') { -//$fee->stripe_fee=price2num($src->amount/100); -//} -//elseif ($src->type=='application_fee') { -//$fee->application_fee=price2num($src->amount/100); -//} - -//}// -//$fee->currency=$txn->currency; -//$fee->datev=dol_now(); -//$fee->datep=dol_now(); -//$fee->datesp=dol_now(); -//$fee->dateep=dol_now(); -//$fee->type_payment=3; -//$fee->num_payment=$txn->source; -//$fee->fk_user_author=$user->id; -//$fee->create($user); - -} -elseif ($event->type == 'charge.failed') { - -$subject = 'Your payment has been received: '.$event->data->object->id.''; -$headers = 'From: "test webhook" '; -//mail('ptibogxiv@msn.com', $subject, 'test', $headers); - -} -elseif (($event->type == 'source.chargeable') && ($event->data->object->type == 'three_d_secure') && ($event->data->object->three_d_secure->authenticated==true)) { - -$stripeconnect=new StripeConnexion($db); -$charge=$stripeconnect->CreatePaymentStripe($event->data->object->amount/100,$event->data->object->currency,$event->data->object->metadata->source,$event->data->object->metadata->idsource,$event->data->object->id,$event->data->object->metadata->customer,$stripeconnect->GetStripeAccount($entity)); - -if (isset($charge->id) && $charge->statut=='error'){ -$msg=$charge->message; -$code=$charge->code; -$error++; -} -elseif (isset($charge->id) && $charge->statut=='success' && $event->data->object->metadata->source=='order') { -$order=new Commande($db); -$order->fetch($event->data->object->metadata->idsource); -$invoice = new Facture($db); -$idinv=$invoice->createFromOrder($order); - -if ($idinv > 0) -{ -$result=$invoice->validate($user); -if ($result > 0) { -$invoice->fetch($idinv); -$paiement = $invoice->getSommePaiement(); -$creditnotes=$invoice->getSumCreditNotesUsed(); -$deposits=$invoice->getSumDepositsUsed(); -$ref=$invoice->ref; -$ifverif=$invoice->socid; -$currency=$invoice->multicurrency_code; -$total=price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,'MT'); -}else{ -$msg=$invoice->error; -$error++; -} -}else{ -$msg=$invoice->error; -$error++; -} -} - -if (!$error){ -$datepaye = dol_now(); -$paiementcode ="CB"; -$amounts=array(); -$amounts[$invoice->id] = $total; -$multicurrency_amounts=array(); -//$multicurrency_amounts[$item] = $total; - $paiement = new Paiement($db); - $paiement->datepaye = $datepaye; - $paiement->amounts = $amounts; // Array with all payments dispatching - $paiement->multicurrency_amounts = $multicurrency_amounts; // Array with all payments dispatching - $paiement->paiementid = dol_getIdFromCode($db,$paiementcode,'c_paiement'); - $paiement->num_paiement = $charge->message; - $paiement->note = ''; -} - -if (! $error){ -$paiement_id=$paiement->create($user, 0); - -if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE) && count($invoice->lines)){ - $outputlangs = $langs; - $newlang = ''; - if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) $newlang = GETPOST('lang_id','aZ09'); - if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $invoice->thirdparty->default_lang; - if (! empty($newlang)) { - $outputlangs = new Translate("", $conf); - $outputlangs->setDefaultLang($newlang); + $metadata = array( + "source" => "" . $origin . "", + "idsource" => "" . $item . "", + "idcustomer" => "" . $societe->id . "" + ); + $return = new Stripe($this->db); + try { + if ($stripeamount >= 100) { + if ($entite == '1' or empty($conf->stripeconnect->enabled)) { + if (preg_match('/acct_/i', $source)) { + $charge = \Stripe\Charge::create(array( + "amount" => "$stripeamount", + "currency" => "$currency", + // "statement_descriptor" => " ", + "metadata" => $metadata, + "source" => "$source" + )); + } else { + $charge = \Stripe\Charge::create(array( + "amount" => "$stripeamount", + "currency" => "$currency", + // "statement_descriptor" => " ", + "description" => "$description", + "metadata" => $metadata, + "receipt_email" => $societe->email, + "source" => "$source", + "customer" => "$customer" + ), array( + "idempotency_key" => "$ref" + )); + } + } else { + $fee = round(($amount * ($conf->global->STRIPE_APPLICATION_FEE_PERCENT / 100) + $conf->global->STRIPE_APPLICATION_FEE) * 100); + if ($fee < ($conf->global->STRIPE_APPLICATION_FEE_MINIMAL * 100)) { + $fee = round($conf->global->STRIPE_APPLICATION_FEE_MINIMAL * 100); + } + $charge = \Stripe\Charge::create(array( + "amount" => "$stripeamount", + "currency" => "$currency", + // "statement_descriptor" => " ", + "description" => "$description", + "metadata" => $metadata, + "source" => "$source", + "customer" => "$customer", + "application_fee" => "$fee" + ), array( + "idempotency_key" => "$ref", + "stripe_account" => "$account" + )); } - $model=$invoice->modelpdf; - $ret = $invoice->fetch($invoice->id); // Reload to get new records + if (isset($charge->id)) {} + } -$invoice->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);} -if ($paiement_id < 0){ - $msg=$paiement->errors; - $error++; -}else{ - if ($event->data->object->metadata->source=='order') { - $order->classifyBilled($user); - } - } - } - -if (! $error){ - $label='(CustomerInvoicePayment)'; - if (GETPOST('type') == 2) $label='(CustomerInvoicePaymentBack)'; - $paiement->addPaymentToBank($user,'payment',$label,$conf->global->STRIPE_BANK_ACCOUNT_FOR_PAYMENTS,'',''); - if ($result < 0) - { - $msg=$paiement->errors; - $error++; - } -$invoice->set_paid($user); -} + $return->statut = 'success'; + $return->id = $charge->id; + if ($charge->source->type == 'card') { + $return->message = $charge->source->card->brand . " ****" . $charge->source->card->last4; + } elseif ($charge->source->type == 'three_d_secure') { + $stripe = new Stripe($this->db); + $src = \Stripe\Source::retrieve("" . $charge->source->three_d_secure->card . "", array( + "stripe_account" => $stripe->getStripeAccount($conf->entity) + )); + $return->message = $src->card->brand . " ****" . $src->card->last4; + } else { + $return->message = $charge->id; + } + } catch (\Stripe\Error\Card $e) { + // Since it's a decline, \Stripe\Error\Card will be caught + $body = $e->getJsonBody(); + $err = $body['error']; + + $return->statut = 'error'; + $return->id = $err['charge']; + $return->type = $err['type']; + $return->code = $err['code']; + $return->message = $err['message']; + $body = "Une erreur de paiement est survenue. Voici le code d'erreur:
" . $return->id . " " . $return->message . " "; + $subject = '[NOTIFICATION] Erreur de paiement'; + $headers = 'From: "ptibogxiv.net" <' . $conf->global->MAIN_INFO_SOCIETE_MAIL . '>'; + mail('' . $conf->global->MAIN_INFO_SOCIETE_MAIL . '', $subject, $body, $headers); + $error ++; + dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); + } catch (\Stripe\Error\RateLimit $e) { + // Too many requests made to the API too quickly + $error ++; + dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); + } catch (\Stripe\Error\InvalidRequest $e) { + // Invalid parameters were supplied to Stripe's API + $error ++; + dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); + } catch (\Stripe\Error\Authentication $e) { + // Authentication with Stripe's API failed + // (maybe you changed API keys recently) + $error ++; + dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); + } catch (\Stripe\Error\ApiConnection $e) { + // Network communication with Stripe failed + $error ++; + dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); + } catch (\Stripe\Error\Base $e) { + // Display a very generic error to the user, and maybe send + // yourself an email + $error ++; + dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); + } catch (Exception $e) { + // Something else happened, completely unrelated to Stripe + $error ++; + dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); + } + return $return; + } -$body = ""; -$subject = 'Facture '.$invoice->ref; -$headers = 'From: "test webhook" '; -//mail('ptibogxiv@msn.com', $subject, $body, $headers); } -elseif ($event->type == 'customer.deleted') { - $db->begin(); - $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_stripe WHERE fk_key = '".$event->data->object->id."' "; - dol_syslog(get_class($this) . "::delete sql=" . $sql, LOG_DEBUG); - $db->query($sql); - $db->commit(); -} - - - -?> From bfa74785682a7a38c975b5490648b70fb2314742 Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Fri, 9 Mar 2018 14:40:20 +0100 Subject: [PATCH 07/12] FIX compatibility for multicompany (stripeconnect or not) TODO: more test and create fee with stripe callback --- htdocs/public/stripe/ipn.php | 568 +++++++++++++++++------------------ 1 file changed, 272 insertions(+), 296 deletions(-) diff --git a/htdocs/public/stripe/ipn.php b/htdocs/public/stripe/ipn.php index d56fa61422a..32d874eafbe 100644 --- a/htdocs/public/stripe/ipn.php +++ b/htdocs/public/stripe/ipn.php @@ -1,322 +1,298 @@ - * - * 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 - * along with this program. If not, see . - */ -// Put here all includes required by your class file -require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php'; -require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php'; +define("NOLOGIN",1); // This means this output page does not require to be logged. +define("NOCSRFCHECK",1); // We accept to go on this page from external web site. + +$entity=(! empty($_GET['entity']) ? (int) $_GET['entity'] : (! empty($_POST['entity']) ? (int) $_POST['entity'] : 1)); +if (is_numeric($entity)) define("DOLENTITY", $entity); + +$res=0; +if (! $res && file_exists("../../main.inc.php")) $res=@include("../../main.inc.php"); // to work if your module directory is into a subdir of root htdocs directory +if (! $res) die("Include of main fails"); + +if (empty($conf->stripe->enabled)) accessforbidden('',0,0,1); +require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php'; +require_once DOL_DOCUMENT_ROOT.'/includes/stripe/init.php'; +require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/ccountry.class.php'; require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php'; +require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php'; require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; -require_once DOL_DOCUMENT_ROOT.'/stripe/config.php'; +require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; +require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php'; - -/** - * Stripe class - */ -class Stripe extends CommonObject +// You can find your endpoint's secret in your webhook settings +if (isset($_GET['connect'])){ +if (isset($_GET['test'])) { - public $rowid; - public $fk_soc; - public $fk_key; - public $id; - public $mode; - public $entity; - public $statut; - public $type; - public $code; - public $message; +$endpoint_secret = $conf->global->STRIPE_TEST_WEBHOOK_CONNECT_KEY; +$service = 'StripeTest'; +} +else +{ +$endpoint_secret = $conf->global->STRIPE_LIVE_WEBHOOK_CONNECT_KEY; +$service = 'StripeLive'; +} +}else { +if (isset($_GET['test'])) +{ +$endpoint_secret = $conf->global->STRIPE_TEST_WEBHOOK_KEY; +$service = 'StripeTest'; +} +else +{ +$endpoint_secret = $conf->global->STRIPE_LIVE_WEBHOOK_KEY; +$service = 'StripeLive'; +} +} +$payload = @file_get_contents("php://input"); +$sig_header = $_SERVER["HTTP_STRIPE_SIGNATURE"]; +$event = null; - /** - * Constructor - * - * @param DoliDB $db Database handler - */ - public function __construct($db) - { - $this->db = $db; +$error = 0; + +try { +$event = \Stripe\Webhook::constructEvent($payload, $sig_header, $endpoint_secret); +} +catch(\UnexpectedValueException $e) { + // Invalid payload + http_response_code(400); // PHP 5.4 or greater + exit(); +} catch(\Stripe\Error\SignatureVerification $e) { + // Invalid signature + http_response_code(400); // PHP 5.4 or greater + exit(); +} - } +// Do something with $event +http_response_code(200); // PHP 5.4 or greater +$langs->load("main"); +$user = new User($db); +$user->fetch(5); +$user->getrights(); - /** - * Return stripe account - * - * @param string $mode 'StripeTest' or 'StripeLive' - * @return int ??? - */ - public function getStripeAccount($mode='StripeTest') - { - global $conf; - - $sql = "SELECT tokenstring"; +if (! empty($conf->multicompany->enabled) && ! empty($conf->stripeconnect->enabled)) { + $sql = "SELECT entity"; $sql.= " FROM ".MAIN_DB_PREFIX."oauth_token"; - $sql.= " WHERE entity = ".$conf->entity; - $sql.= " AND service = '".$mode."'"; + $sql.= " WHERE service = '$service' and tokenstring = '%".$event->account."%'"; - dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); - $result = $this->db->query($sql); - if ($result) + dol_syslog(get_class($db) . "::fetch", LOG_DEBUG); + $result = $db->query($sql); + if ($result) { - if ($this->db->num_rows($result)) + if ($db->num_rows($result)) { - $obj = $this->db->fetch_object($result); - $tokenstring=$obj->tokenstring; + $obj = $db->fetch_object($result); + $key=$obj->entity; + } + else {$key=1;} + } + else {$key=1;} +$ret=$mc->switchEntity($key); +if (! $res && file_exists("../../main.inc.php")) $res=@include("../../main.inc.php"); // to work if your module directory is into a subdir of root htdocs directory +if (! $res) die("Include of main fails"); +} - $tmparray = dol_json_decode($tokenstring); - $key = $tmparray->stripe_user_id; - } - else { - $tokenstring=''; - } - } - else { - dol_print_error($this->db); - } +// list of action +$stripe=new Stripe($db); +if ($event->type == 'payout.created') { + $error=0; - return $key; - } + $result=dolibarr_set_const($db, $service."_NEXTPAYOUT",date('Y-m-d H:i:s',$event->data->object->arrival_date),'chaine',0,'',$conf->entity); + + if ($result > 0) + { +$body = "Un virement de ".price2num($event->data->object->amount/100)." ".$event->data->object->currency." est attendu sur votre compte le ".date('d-m-Y H:i:s',$event->data->object->arrival_date); +$subject = '[NOTIFICATION] Virement programmée'; +$headers = 'From: "'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>'; +mail(''.$conf->global->MAIN_INFO_SOCIETE_MAIL.'', $subject, $body, $headers); + return 1; + } + else + { + $error++; + return -1; + } +} +elseif ($event->type == 'payout.paid') { +global $conf; + $error=0; + $result=dolibarr_set_const($db, $service."_NEXTPAYOUT",null,'chaine',0,'',$conf->entity); + if ($result) + { + $langs->load("errors"); - /** - * customerStripe - * - * @param int $id ??? - * @param string $key ??? - * @return \Stripe\StripeObject|\Stripe\ApiResource - */ - public function customerStripe($id,$key) - { - global $conf; - if (empty($conf->global->STRIPECONNECT_LIVE)) { - $mode = 0; - } else { - if (empty($conf->global->STRIPE_LIVE)) { - $mode = 0; - } else { - $mode = $conf->global->STRIPE_LIVE; - } - } - $sql = "SELECT rowid,fk_soc,fk_key,mode,entity"; - $sql .= " FROM " . MAIN_DB_PREFIX . "societe_stripe"; - $sql .= " WHERE fk_soc = " . $id . " "; - $sql .= " AND mode=" . $mode . " AND entity IN (" . getEntity('stripe') . ")"; + $dateo = dol_now(); + $label = $event->data->object->description; + $amount= $event->data->object->amount/100; + $amount_to= $event->data->object->amount/100; +require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; - dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); - $resql = $this->db->query($sql); - if ($resql) { - $soc = new Societe($this->db); - $soc->fetch($id); - $num = $this->db->num_rows($resql); - if ($num) { - $obj = $this->db->fetch_object($resql); - $tiers = $obj->fk_key; - if ($conf->entity == 1) { - $customer = \Stripe\Customer::retrieve("$tiers"); - } else { - $customer = \Stripe\Customer::retrieve("$tiers", array( - "stripe_account" => $key - )); - } - } else { - if ($conf->entity == 1) { - $customer = \Stripe\Customer::create(array( - "email" => $soc->email, - "description" => $soc->name - )); - } else { - $customer = \Stripe\Customer::create(array( - "email" => $soc->email, - "description" => $soc->name - ), array( - "stripe_account" => $key - )); - } - $customer_id = "" . $customer->id . ""; - $sql = "INSERT INTO " . MAIN_DB_PREFIX . "societe_stripe (fk_soc,fk_key,mode,entity)"; - $sql .= " VALUES ($id,'$customer_id'," . $mode . "," . $conf->entity . ")"; - dol_syslog(get_class($this) . "::create sql=" . $sql, LOG_DEBUG); - $resql = $this->db->query($sql); - } - } - return $customer; - } + $accountfrom=new Account($db); + $accountfrom->fetch($conf->global->STRIPE_BANK_ACCOUNT_FOR_PAYMENTS); - /** - * createPaymentStripe - * - * @param unknown $amount ??? - * @param unknown $currency ??? - * @param unknown $origin ??? - * @param unknown $item ??? - * @param unknown $source ??? - * @param unknown $customer ??? - * @param unknown $account ??? - * @return Stripe - */ - public function createPaymentStripe($amount, $currency, $origin, $item, $source, $customer, $account) - { - global $conf; - if (empty($conf->global->STRIPECONNECT_LIVE)) { - $mode = 0; - } else { - if (empty($conf->global->STRIPE_LIVE)) { - $mode = 0; - } else { - $mode = $conf->global->STRIPE_LIVE; - } - } - $sql = "SELECT fk_soc,fk_key,mode,entity"; - $sql .= " FROM " . MAIN_DB_PREFIX . "societe_stripe"; - $sql .= " WHERE fk_key = '$customer' "; - $sql .= " AND mode=" . $mode . " "; + $accountto=new Account($db); + $accountto->fetch($conf->global->STRIPE_BANK_ACCOUNT_FOR_BANKTRANFERS); - dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); - $result = $this->db->query($sql); - if ($result) { - if ($this->db->num_rows($result)) { - $obj = $this->db->fetch_object($result); - $entite = $obj->entity; - $fksoc = $obj->fk_soc; - } - } - $stripeamount = round($amount * 100); - $societe = new Societe($this->db); - $societe->fetch($fksoc); - - if ($origin == order) { - $order = new Commande($this->db); - $order->fetch($item); - $ref = $order->ref; - $description = "ORD=" . $ref . ".CUS=" . $societe->code_client; - } elseif ($origin == invoice) { - $invoice = new Facture($this->db); - $invoice->fetch($item); - $ref = $invoice->ref; - $description = "INV=" . $ref . ".CUS=" . $societe->code_client; + if ($accountto->currency_code != $accountfrom->currency_code) { + $error++; + setEventMessages($langs->trans("ErrorTransferBetweenDifferentCurrencyNotPossible"), null, 'errors'); } - $metadata = array( - "source" => "" . $origin . "", - "idsource" => "" . $item . "", - "idcustomer" => "" . $societe->id . "" - ); - $return = new Stripe($this->db); - try { - if ($stripeamount >= 100) { - if ($entite == '1' or empty($conf->stripeconnect->enabled)) { - if (preg_match('/acct_/i', $source)) { - $charge = \Stripe\Charge::create(array( - "amount" => "$stripeamount", - "currency" => "$currency", - // "statement_descriptor" => " ", - "metadata" => $metadata, - "source" => "$source" - )); - } else { - $charge = \Stripe\Charge::create(array( - "amount" => "$stripeamount", - "currency" => "$currency", - // "statement_descriptor" => " ", - "description" => "$description", - "metadata" => $metadata, - "receipt_email" => $societe->email, - "source" => "$source", - "customer" => "$customer" - ), array( - "idempotency_key" => "$ref" - )); - } - } else { - $fee = round(($amount * ($conf->global->STRIPE_APPLICATION_FEE_PERCENT / 100) + $conf->global->STRIPE_APPLICATION_FEE) * 100); - if ($fee < ($conf->global->STRIPE_APPLICATION_FEE_MINIMAL * 100)) { - $fee = round($conf->global->STRIPE_APPLICATION_FEE_MINIMAL * 100); - } - $charge = \Stripe\Charge::create(array( - "amount" => "$stripeamount", - "currency" => "$currency", - // "statement_descriptor" => " ", - "description" => "$description", - "metadata" => $metadata, - "source" => "$source", - "customer" => "$customer", - "application_fee" => "$fee" - ), array( - "idempotency_key" => "$ref", - "stripe_account" => "$account" - )); - } - if (isset($charge->id)) {} - } + if ($accountto->id != $accountfrom->id) + { - $return->statut = 'success'; - $return->id = $charge->id; - if ($charge->source->type == 'card') { - $return->message = $charge->source->card->brand . " ****" . $charge->source->card->last4; - } elseif ($charge->source->type == 'three_d_secure') { - $stripe = new Stripe($this->db); - $src = \Stripe\Source::retrieve("" . $charge->source->three_d_secure->card . "", array( - "stripe_account" => $stripe->getStripeAccount($conf->entity) - )); - $return->message = $src->card->brand . " ****" . $src->card->last4; - } else { - $return->message = $charge->id; - } - } catch (\Stripe\Error\Card $e) { - // Since it's a decline, \Stripe\Error\Card will be caught - $body = $e->getJsonBody(); - $err = $body['error']; + $bank_line_id_from=0; + $bank_line_id_to=0; + $result=0; - $return->statut = 'error'; - $return->id = $err['charge']; - $return->type = $err['type']; - $return->code = $err['code']; - $return->message = $err['message']; - $body = "Une erreur de paiement est survenue. Voici le code d'erreur:
" . $return->id . " " . $return->message . " "; - $subject = '[NOTIFICATION] Erreur de paiement'; - $headers = 'From: "ptibogxiv.net" <' . $conf->global->MAIN_INFO_SOCIETE_MAIL . '>'; - mail('' . $conf->global->MAIN_INFO_SOCIETE_MAIL . '', $subject, $body, $headers); - $error ++; - dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); - } catch (\Stripe\Error\RateLimit $e) { - // Too many requests made to the API too quickly - $error ++; - dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); - } catch (\Stripe\Error\InvalidRequest $e) { - // Invalid parameters were supplied to Stripe's API - $error ++; - dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); - } catch (\Stripe\Error\Authentication $e) { - // Authentication with Stripe's API failed - // (maybe you changed API keys recently) - $error ++; - dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); - } catch (\Stripe\Error\ApiConnection $e) { - // Network communication with Stripe failed - $error ++; - dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); - } catch (\Stripe\Error\Base $e) { - // Display a very generic error to the user, and maybe send - // yourself an email - $error ++; - dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); - } catch (Exception $e) { - // Something else happened, completely unrelated to Stripe - $error ++; - dol_syslog($e->getMessage(), LOG_WARNING, 0, '_stripe'); - } - return $return; - } + // By default, electronic transfert from bank to bank + $typefrom='PRE'; + $typeto='VIR'; + + if (! $error) $bank_line_id_from = $accountfrom->addline($dateo, $typefrom, $label, -1*price2num($amount), '', '', $user); + if (! ($bank_line_id_from > 0)) $error++; + if ((! $error) && ($accountto->currency_code == $accountfrom->currency_code)) $bank_line_id_to = $accountto->addline($dateo, $typeto, $label, price2num($amount), '', '', $user); + if ((! $error) && ($accountto->currency_code != $accountfrom->currency_code)) $bank_line_id_to = $accountto->addline($dateo, $typeto, $label, price2num($amount_to), '', '', $user); + if (! ($bank_line_id_to > 0)) $error++; + + if (! $error) $result=$accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.'/compta/bank/ligne.php?rowid=', '(banktransfert)', 'banktransfert'); + if (! ($result > 0)) $error++; + if (! $error) $result=$accountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.'/compta/bank/ligne.php?rowid=', '(banktransfert)', 'banktransfert'); + if (! ($result > 0)) $error++; + } + +$body = "Un virement de ".price2num($event->data->object->amount/100)." ".$event->data->object->currency." a ete effectue sur votre compte le ".date('d-m-Y H:i:s',$event->data->object->arrival_date); +$subject = '[NOTIFICATION] Virement effectué'; +$headers = 'From: "'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>'; +mail(''.$conf->global->MAIN_INFO_SOCIETE_MAIL.'', $subject, $body, $headers); + + return 1; + } + else + { + $error++; + return -1; + } +} +elseif ($event->type == 'charge.succeeded') { + +//TODO: create fees } +elseif ($event->type == 'charge.failed') { + +$subject = 'Your payment has been received: '.$event->data->object->id.''; +$headers = 'From: "test webhook" '; +//mail('ptibogxiv@msn.com', $subject, 'test', $headers); + +} +elseif (($event->type == 'source.chargeable') && ($event->data->object->type == 'three_d_secure') && ($event->data->object->three_d_secure->authenticated==true)) { + +$stripe=new Stripe($db); +$charge=$stripe->CreatePaymentStripe($event->data->object->amount/100,$event->data->object->currency,$event->data->object->metadata->source,$event->data->object->metadata->idsource,$event->data->object->id,$event->data->object->metadata->customer,$stripe->getStripeAccount($service)); + +if (isset($charge->id) && $charge->statut=='error'){ +$msg=$charge->message; +$code=$charge->code; +$error++; +} +elseif (isset($charge->id) && $charge->statut=='success' && $event->data->object->metadata->source=='order') { +$order=new Commande($db); +$order->fetch($event->data->object->metadata->idsource); +$invoice = new Facture($db); +$idinv=$invoice->createFromOrder($order); + +if ($idinv > 0) +{ +$result=$invoice->validate($user); +if ($result > 0) { +$invoice->fetch($idinv); +$paiement = $invoice->getSommePaiement(); +$creditnotes=$invoice->getSumCreditNotesUsed(); +$deposits=$invoice->getSumDepositsUsed(); +$ref=$invoice->ref; +$ifverif=$invoice->socid; +$currency=$invoice->multicurrency_code; +$total=price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,'MT'); +}else{ +$msg=$invoice->error; +$error++; +} +}else{ +$msg=$invoice->error; +$error++; +} +} + +if (!$error){ +$datepaye = dol_now(); +$paiementcode ="CB"; +$amounts=array(); +$amounts[$invoice->id] = $total; +$multicurrency_amounts=array(); +//$multicurrency_amounts[$item] = $total; + $paiement = new Paiement($db); + $paiement->datepaye = $datepaye; + $paiement->amounts = $amounts; // Array with all payments dispatching + $paiement->multicurrency_amounts = $multicurrency_amounts; // Array with all payments dispatching + $paiement->paiementid = dol_getIdFromCode($db,$paiementcode,'c_paiement'); + $paiement->num_paiement = $charge->message; + $paiement->note = ''; +} + +if (! $error){ +$paiement_id=$paiement->create($user, 0); + +if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE) && count($invoice->lines)){ + $outputlangs = $langs; + $newlang = ''; + if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) $newlang = GETPOST('lang_id','aZ09'); + if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $invoice->thirdparty->default_lang; + if (! empty($newlang)) { + $outputlangs = new Translate("", $conf); + $outputlangs->setDefaultLang($newlang); + } + $model=$invoice->modelpdf; + $ret = $invoice->fetch($invoice->id); // Reload to get new records + +$invoice->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);} +if ($paiement_id < 0){ + $msg=$paiement->errors; + $error++; +}else{ + if ($event->data->object->metadata->source=='order') { + $order->classifyBilled($user); + } + } + } + +if (! $error){ + $label='(CustomerInvoicePayment)'; + if (GETPOST('type') == 2) $label='(CustomerInvoicePaymentBack)'; + $paiement->addPaymentToBank($user,'payment',$label,$conf->global->STRIPE_BANK_ACCOUNT_FOR_PAYMENTS,'',''); + if ($result < 0) + { + $msg=$paiement->errors; + $error++; + } +$invoice->set_paid($user); +} + +$body = ""; +$subject = 'Facture '.$invoice->ref; +$headers = 'From: "test webhook" '; +//mail('ptibogxiv@msn.com', $subject, $body, $headers); +} +elseif ($event->type == 'customer.deleted') { + $db->begin(); + $sql = "DELETE FROM ".MAIN_DB_PREFIX."societe_stripe WHERE fk_key = '".$event->data->object->id."' "; + dol_syslog(get_class($this) . "::delete sql=" . $sql, LOG_DEBUG); + $db->query($sql); + $db->commit(); +} + + + +?> From dd15dc56dd692cda903de4fd76a1143674e408b9 Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Sat, 10 Mar 2018 14:34:10 +0100 Subject: [PATCH 08/12] Update and fix prepare integration for more callback support --- htdocs/public/stripe/ipn.php | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/htdocs/public/stripe/ipn.php b/htdocs/public/stripe/ipn.php index 32d874eafbe..15cd2d5cc69 100644 --- a/htdocs/public/stripe/ipn.php +++ b/htdocs/public/stripe/ipn.php @@ -90,7 +90,7 @@ if (! empty($conf->multicompany->enabled) && ! empty($conf->stripeconnect->enabl } else {$key=1;} $ret=$mc->switchEntity($key); -if (! $res && file_exists("../../main.inc.php")) $res=@include("../../main.inc.php"); // to work if your module directory is into a subdir of root htdocs directory +if (! $res && file_exists("../../main.inc.php")) $res=@include("../../main.inc.php"); if (! $res) die("Include of main fails"); } @@ -105,7 +105,7 @@ if ($event->type == 'payout.created') { { $body = "Un virement de ".price2num($event->data->object->amount/100)." ".$event->data->object->currency." est attendu sur votre compte le ".date('d-m-Y H:i:s',$event->data->object->arrival_date); $subject = '[NOTIFICATION] Virement programmée'; -$headers = 'From: "'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>'; +$headers = 'From: "'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>'; TODO convert in dolibarr standard mail(''.$conf->global->MAIN_INFO_SOCIETE_MAIL.'', $subject, $body, $headers); return 1; } @@ -165,7 +165,7 @@ require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; $body = "Un virement de ".price2num($event->data->object->amount/100)." ".$event->data->object->currency." a ete effectue sur votre compte le ".date('d-m-Y H:i:s',$event->data->object->arrival_date); $subject = '[NOTIFICATION] Virement effectué'; -$headers = 'From: "'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>'; +$headers = 'From: "'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>'; TODO convert in dolibarr standard mail(''.$conf->global->MAIN_INFO_SOCIETE_MAIL.'', $subject, $body, $headers); return 1; @@ -180,11 +180,26 @@ elseif ($event->type == 'charge.succeeded') { //TODO: create fees +} +elseif ($event->type == 'customer.source.created') { + +//TODO: save customer's source + +} +elseif ($event->type == 'customer.source.updated') { + +//TODO: update customer's source + +} +elseif ($event->type == 'customer.source.delete') { + +//TODO: delete customer's source + } elseif ($event->type == 'charge.failed') { $subject = 'Your payment has been received: '.$event->data->object->id.''; -$headers = 'From: "test webhook" '; +$headers = 'From: "'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>'; //mail('ptibogxiv@msn.com', $subject, 'test', $headers); } @@ -282,8 +297,8 @@ $invoice->set_paid($user); $body = ""; $subject = 'Facture '.$invoice->ref; -$headers = 'From: "test webhook" '; -//mail('ptibogxiv@msn.com', $subject, $body, $headers); +$headers = 'From: "'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>'; +//mail('ptibogxiv@msn.com', $subject, $body, $headers); TODO convert in dolibarr standard } elseif ($event->type == 'customer.deleted') { $db->begin(); From bbff580fb3d152cb9b7583af6907aaa38e6f3461 Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Sat, 10 Mar 2018 14:45:07 +0100 Subject: [PATCH 09/12] integration of fee's platform --- htdocs/stripe/admin/stripe.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/htdocs/stripe/admin/stripe.php b/htdocs/stripe/admin/stripe.php index 91d056aa094..d25831ee264 100644 --- a/htdocs/stripe/admin/stripe.php +++ b/htdocs/stripe/admin/stripe.php @@ -206,8 +206,14 @@ print 'trans("Example").': whsec_xxxxxxxxxxxxxxxxxxxxxxxx'; print ''; } else { -print ''.$langs->trans("StripeConnect").''; -print ''.$langs->trans("StripeConnect_Mode").''; + print ''.$langs->trans("StripeConnect").''; + print ''.$langs->trans("StripeConnect_Mode").'
'; + print $langs->trans("STRIPE_APPLICATION_FEE_PLATFORM"); + print price($conf->global->STRIPE_APPLICATION_FEE_PERCENT); + print '% + '; + print price($conf->global->STRIPE_APPLICATION_FEE); + print $langs->getCurrencySymbol($conf->currency).' avec un minimum de '.price($conf->global->STRIPE_APPLICATION_FEE_MINIMAL).' '.$langs->getCurrencySymbol($conf->currency).' '; + print ''; } if (! empty($conf->banque->enabled)) //deplace here for separate stripe setting of general and common online payment settings From 5403e7a34719d212911f1410d06f5b7ac749cc0a Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Sat, 10 Mar 2018 14:46:37 +0100 Subject: [PATCH 10/12] Update stripe.php --- htdocs/stripe/admin/stripe.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/stripe/admin/stripe.php b/htdocs/stripe/admin/stripe.php index d25831ee264..42306b5b866 100644 --- a/htdocs/stripe/admin/stripe.php +++ b/htdocs/stripe/admin/stripe.php @@ -208,7 +208,7 @@ print ''; } else { print ''.$langs->trans("StripeConnect").''; print ''.$langs->trans("StripeConnect_Mode").'
'; - print $langs->trans("STRIPE_APPLICATION_FEE_PLATFORM"); + print $langs->trans("STRIPE_APPLICATION_FEE_PLATFORM").' '; print price($conf->global->STRIPE_APPLICATION_FEE_PERCENT); print '% + '; print price($conf->global->STRIPE_APPLICATION_FEE); From 6b43477c3960979bb45931b2a4d1117615ca77b0 Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Sat, 10 Mar 2018 14:48:07 +0100 Subject: [PATCH 11/12] Update stripe.php --- htdocs/stripe/admin/stripe.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/stripe/admin/stripe.php b/htdocs/stripe/admin/stripe.php index 42306b5b866..5253002691f 100644 --- a/htdocs/stripe/admin/stripe.php +++ b/htdocs/stripe/admin/stripe.php @@ -212,7 +212,7 @@ print ''; print price($conf->global->STRIPE_APPLICATION_FEE_PERCENT); print '% + '; print price($conf->global->STRIPE_APPLICATION_FEE); - print $langs->getCurrencySymbol($conf->currency).' avec un minimum de '.price($conf->global->STRIPE_APPLICATION_FEE_MINIMAL).' '.$langs->getCurrencySymbol($conf->currency).' '; + print ' '.$langs->getCurrencySymbol($conf->currency).' avec un minimum de '.price($conf->global->STRIPE_APPLICATION_FEE_MINIMAL).' '.$langs->getCurrencySymbol($conf->currency).' '; print ''; } From 3ea6a9fa5f899d3d84ca58e68f843814cf2b5c7a Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Sat, 10 Mar 2018 15:05:04 +0100 Subject: [PATCH 12/12] Update stripe.php --- htdocs/stripe/admin/stripe.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/stripe/admin/stripe.php b/htdocs/stripe/admin/stripe.php index 5253002691f..70fad8082a8 100644 --- a/htdocs/stripe/admin/stripe.php +++ b/htdocs/stripe/admin/stripe.php @@ -212,7 +212,7 @@ print ''; print price($conf->global->STRIPE_APPLICATION_FEE_PERCENT); print '% + '; print price($conf->global->STRIPE_APPLICATION_FEE); - print ' '.$langs->getCurrencySymbol($conf->currency).' avec un minimum de '.price($conf->global->STRIPE_APPLICATION_FEE_MINIMAL).' '.$langs->getCurrencySymbol($conf->currency).' '; + print ' '.$langs->getCurrencySymbol($conf->currency).' '.$langs->trans("minimum").' '.price($conf->global->STRIPE_APPLICATION_FEE_MINIMAL).' '.$langs->getCurrencySymbol($conf->currency).' '; print ''; }