2018-03-07 20:04:13 +01:00
< ? php
2021-01-24 13:38:03 +01:00
/* Copyright ( C ) 2018 - 2021 Thibault FOUCART < support @ ptibogxiv . net >
2018-03-07 20:04:13 +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-07 20:04:13 +01:00
*/
// 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' ;
2019-11-08 15:51:54 +01:00
require_once DOL_DOCUMENT_ROOT . '/stripe/config.php' ; // This set stripe global env
2018-03-08 19:50:41 +01:00
/**
* Stripe class
2018-03-07 20:04:13 +01:00
*/
class Stripe extends CommonObject
{
2018-09-01 22:55:10 +02:00
/**
* @ var int ID
*/
2018-03-07 20:04:13 +01:00
public $rowid ;
2018-09-01 22:55:10 +02:00
2018-09-04 13:37:00 +02:00
/**
* @ var int Thirdparty ID
*/
2020-09-08 21:27:28 +02:00
public $fk_soc ;
2018-09-01 23:04:46 +02:00
2020-09-08 21:27:28 +02:00
/**
* @ var int ID
*/
2018-03-08 19:50:41 +01:00
public $fk_key ;
2018-09-01 23:04:46 +02:00
/**
* @ var int ID
*/
2018-03-08 19:50:41 +01:00
public $id ;
2018-09-01 23:04:46 +02:00
2018-03-08 19:50:41 +01:00
public $mode ;
2018-08-31 18:27:16 +02:00
/**
* @ var int Entity
*/
2018-03-08 19:50:41 +01:00
public $entity ;
2018-08-31 18:27:16 +02:00
2018-03-08 19:50:41 +01:00
public $statut ;
2018-09-01 23:04:46 +02:00
2018-03-08 19:50:41 +01:00
public $type ;
2018-09-01 23:04:46 +02:00
2018-03-08 19:50:41 +01:00
public $code ;
2019-09-23 16:50:32 +02:00
public $declinecode ;
2018-09-01 23:04:46 +02:00
2020-09-08 21:27:28 +02:00
/**
* @ var string Message
*/
2018-03-08 19:50:41 +01:00
public $message ;
2018-03-07 20:04:13 +01:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
public function __construct ( $db )
{
$this -> db = $db ;
}
2018-03-08 19:50:41 +01:00
/**
2018-03-12 15:59:03 +01:00
* Return main company OAuth Connect stripe account
2018-03-08 19:50:41 +01:00
*
* @ param string $mode 'StripeTest' or 'StripeLive'
2019-08-21 17:09:26 +02:00
* @ param int $fk_soc Id of thirdparty
2020-10-01 16:57:49 +02:00
* @ param int $entity Id of entity ( - 1 = current environment )
2018-03-16 16:10:04 +01:00
* @ return string Stripe account 'acc_....' or '' if no OAuth token found
2018-03-08 19:50:41 +01:00
*/
2020-10-01 16:57:49 +02:00
public function getStripeAccount ( $mode = 'StripeTest' , $fk_soc = 0 , $entity = - 1 )
2018-03-07 20:04:13 +01:00
{
global $conf ;
2020-12-17 17:04:42 +01:00
$key = '' ;
2021-02-26 21:17:52 +01:00
if ( $entity < 0 ) {
$entity = $conf -> entity ;
}
2020-10-01 16:57:49 +02:00
2018-03-08 19:50:41 +01:00
$sql = " SELECT tokenstring " ;
2019-11-08 15:51:54 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " oauth_token " ;
2020-10-01 16:43:56 +02:00
$sql .= " WHERE service = ' " . $this -> db -> escape ( $mode ) . " ' " ;
2020-10-01 16:49:27 +02:00
$sql .= " AND entity = " . (( int ) $entity );
2019-08-22 12:52:30 +02:00
if ( $fk_soc > 0 ) {
2021-03-22 13:31:06 +01:00
$sql .= " AND fk_soc = " . (( int ) $fk_soc );
2020-05-21 15:05:19 +02:00
} else {
2019-11-08 15:51:54 +01:00
$sql .= " AND fk_soc IS NULL " ;
2019-08-22 12:52:30 +02:00
}
2019-11-08 15:51:54 +01:00
$sql .= " AND fk_user IS NULL AND fk_adherent IS NULL " ;
2018-03-07 20:04:13 +01:00
2020-12-17 17:04:42 +01:00
dol_syslog ( get_class ( $this ) . " ::getStripeAccount " , LOG_DEBUG );
2018-03-07 20:04:13 +01:00
$result = $this -> db -> query ( $sql );
2020-09-08 21:27:28 +02:00
if ( $result ) {
2019-03-01 23:08:57 +01:00
if ( $this -> db -> num_rows ( $result )) {
2018-03-07 20:04:13 +01:00
$obj = $this -> db -> fetch_object ( $result );
2020-09-08 21:27:28 +02:00
$tokenstring = $obj -> tokenstring ;
2020-11-11 15:55:04 +01:00
$tmparray = json_decode ( $tokenstring );
2020-09-08 21:27:28 +02:00
$key = $tmparray -> stripe_user_id ;
} else {
$tokenstring = '' ;
}
} else {
dol_print_error ( $this -> db );
}
dol_syslog ( " No dedicated Stripe Connect account available for entity " . $conf -> entity );
2018-03-08 19:50:41 +01:00
return $key ;
}
/**
2018-03-08 20:59:23 +01:00
* getStripeCustomerAccount
2018-03-08 19:50:41 +01:00
*
2019-12-15 17:18:09 +01:00
* @ param int $id Id of third party
* @ param int $status Status
* @ param string $site_account Value to use to identify with account to use on site when site can offer several accounts . For example : 'pk_live_123456' when using Stripe service .
* @ return string Stripe customer ref 'cu_xxxxxxxxxxxxx' or ''
2018-03-08 19:50:41 +01:00
*/
2019-12-15 17:18:09 +01:00
public function getStripeCustomerAccount ( $id , $status = 0 , $site_account = '' )
2018-03-07 20:04:13 +01:00
{
2018-03-13 17:47:43 +01:00
include_once DOL_DOCUMENT_ROOT . '/societe/class/societeaccount.class.php' ;
$societeaccount = new SocieteAccount ( $this -> db );
2019-12-15 17:18:09 +01:00
return $societeaccount -> getCustomerAccount ( $id , 'stripe' , $status , $site_account ); // Get thirdparty cus_...
2018-03-08 19:50:41 +01:00
}
/**
2020-12-18 19:02:36 +01:00
* Get the Stripe customer of a thirdparty ( with option to create it in Stripe if not linked yet ) .
2020-01-31 15:27:17 +01:00
* Search on site_account = 0 or = $stripearrayofkeysbyenv [ $status ][ 'publishable_key' ]
2018-03-08 19:50:41 +01:00
*
2018-03-14 21:07:45 +01:00
* @ param Societe $object Object thirdparty to check , or create on stripe ( create on stripe also update the stripe_account table for current entity )
2018-03-14 16:53:16 +01:00
* @ param string $key '' = Use common API . If not '' , it is the Stripe connect account 'acc_....' to use Stripe connect
2018-03-13 14:44:35 +01:00
* @ param int $status Status ( 0 = test , 1 = live )
* @ param int $createifnotlinkedtostripe 1 = Create the stripe customer and the link if the thirdparty is not yet linked to a stripe customer
2018-03-14 16:53:16 +01:00
* @ return \Stripe\StripeCustomer | null Stripe Customer or null if not found
2018-03-08 19:50:41 +01:00
*/
2019-01-27 15:20:16 +01:00
public function customerStripe ( Societe $object , $key = '' , $status = 0 , $createifnotlinkedtostripe = 0 )
2018-03-07 20:04:13 +01:00
{
2018-03-14 16:53:16 +01:00
global $conf , $user ;
2021-02-26 21:17:52 +01:00
if ( empty ( $object -> id )) {
2019-05-02 21:54:28 +02:00
dol_syslog ( " customerStripe is called with the parameter object that is not loaded " );
2018-03-31 16:45:36 +02:00
return null ;
}
2018-03-14 16:53:16 +01:00
$customer = null ;
2018-03-13 14:44:35 +01:00
2019-12-15 17:44:05 +01:00
// Force to use the correct API key
global $stripearrayofkeysbyenv ;
\Stripe\Stripe :: setApiKey ( $stripearrayofkeysbyenv [ $status ][ 'secret_key' ]);
2019-11-08 15:51:54 +01:00
$sql = " SELECT sa.key_account as key_account, sa.entity " ; // key_account is cus_....
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_account as sa " ;
2021-08-27 18:18:50 +02:00
$sql .= " WHERE sa.fk_soc = " . (( int ) $object -> id );
2019-11-08 15:51:54 +01:00
$sql .= " AND sa.entity IN ( " . getEntity ( 'societe' ) . " ) " ;
$sql .= " AND sa.site = 'stripe' AND sa.status = " . (( int ) $status );
2019-12-15 17:46:56 +01:00
$sql .= " AND (sa.site_account IS NULL OR sa.site_account = '' OR sa.site_account = ' " . $this -> db -> escape ( $stripearrayofkeysbyenv [ $status ][ 'publishable_key' ]) . " ') " ;
2019-12-15 17:44:05 +01:00
$sql .= " AND sa.key_account IS NOT NULL AND sa.key_account <> '' " ;
2018-03-07 20:04:13 +01:00
2019-11-08 15:51:54 +01:00
dol_syslog ( get_class ( $this ) . " ::customerStripe search stripe customer id for thirdparty id= " . $object -> id , LOG_DEBUG );
2018-03-07 20:04:13 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
2018-03-08 19:50:41 +01:00
$num = $this -> db -> num_rows ( $resql );
2020-12-18 19:02:36 +01:00
if ( $num ) {
2018-03-08 19:50:41 +01:00
$obj = $this -> db -> fetch_object ( $resql );
2018-03-13 11:37:40 +01:00
$tiers = $obj -> key_account ;
2018-05-17 16:07:44 +02:00
2019-11-13 02:05:11 +01:00
dol_syslog ( get_class ( $this ) . " ::customerStripe found stripe customer key_account = " . $tiers . " . We will try to read it on Stripe with publishable_key = " . $stripearrayofkeysbyenv [ $status ][ 'publishable_key' ]);
2019-11-11 00:34:05 +01:00
2018-03-14 16:53:16 +01:00
try {
if ( empty ( $key )) { // If the Stripe connect account not set, we use common API usage
2020-11-02 15:07:59 +01:00
//$customer = \Stripe\Customer::retrieve("$tiers");
$customer = \Stripe\Customer :: retrieve ( array ( 'id' => " $tiers " , 'expand[]' => 'sources' ));
2018-03-14 16:53:16 +01:00
} else {
2020-11-02 15:07:59 +01:00
//$customer = \Stripe\Customer::retrieve("$tiers", array("stripe_account" => $key));
$customer = \Stripe\Customer :: retrieve ( array ( 'id' => " $tiers " , 'expand[]' => 'sources' ), array ( " stripe_account " => $key ));
2018-03-14 16:53:16 +01:00
}
2021-02-26 21:17:52 +01:00
} catch ( Exception $e ) {
2019-07-29 01:12:03 +02:00
// For exemple, we may have error: 'No such customer: cus_XXXXX; a similar object exists in live mode, but a test mode key was used to make this request.'
2018-03-15 00:40:29 +01:00
$this -> error = $e -> getMessage ();
2018-03-08 19:50:41 +01:00
}
2020-12-18 19:02:36 +01:00
} elseif ( $createifnotlinkedtostripe ) {
2020-09-08 21:27:28 +02:00
$ipaddress = getUserRemoteIP ();
2019-02-15 15:17:24 +01:00
2018-03-14 16:53:16 +01:00
$dataforcustomer = array (
" email " => $object -> email ,
" description " => $object -> name ,
2019-02-15 15:17:24 +01:00
" metadata " => array ( 'dol_id' => $object -> id , 'dol_version' => DOL_VERSION , 'dol_entity' => $conf -> entity , 'ipaddress' => $ipaddress )
2018-03-14 16:53:16 +01:00
);
2018-10-12 21:00:33 +02:00
$vatcleaned = $object -> tva_intra ? $object -> tva_intra : null ;
2019-06-21 19:58:28 +02:00
/*
2018-10-12 21:00:33 +02:00
$taxinfo = array ( 'type' => 'vat' );
if ( $vatcleaned )
{
$taxinfo [ " tax_id " ] = $vatcleaned ;
}
// We force data to "null" if not defined as expected by Stripe
if ( empty ( $vatcleaned )) $taxinfo = null ;
$dataforcustomer [ " tax_info " ] = $taxinfo ;
2019-06-21 19:58:28 +02:00
*/
2018-03-14 16:53:16 +01:00
//$a = \Stripe\Stripe::getApiKey();
//var_dump($a);var_dump($key);exit;
try {
2018-05-17 16:07:44 +02:00
// Force to use the correct API key
global $stripearrayofkeysbyenv ;
\Stripe\Stripe :: setApiKey ( $stripearrayofkeysbyenv [ $status ][ 'secret_key' ]);
2018-03-14 16:53:16 +01:00
if ( empty ( $key )) { // If the Stripe connect account not set, we use common API usage
$customer = \Stripe\Customer :: create ( $dataforcustomer );
} else {
$customer = \Stripe\Customer :: create ( $dataforcustomer , array ( " stripe_account " => $key ));
}
2019-06-21 19:58:28 +02:00
// Create the VAT record in Stripe
2021-02-26 21:17:52 +01:00
if ( ! empty ( $conf -> global -> STRIPE_SAVE_TAX_IDS )) { // We setup to save Tax info on Stripe side. Warning: This may result in error when saving customer
if ( ! empty ( $vatcleaned )) {
2019-11-08 15:51:54 +01:00
$isineec = isInEEC ( $object );
2021-02-26 21:17:52 +01:00
if ( $object -> country_code && $isineec ) {
2019-06-21 19:58:28 +02:00
//$taxids = $customer->allTaxIds($customer->id);
$customer -> createTaxId ( $customer -> id , array ( 'type' => 'eu_vat' , 'value' => $vatcleaned ));
}
}
}
// Create customer in Dolibarr
2019-12-23 22:41:34 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " societe_account (fk_soc, login, key_account, site, site_account, status, entity, date_creation, fk_user_creat) " ;
2021-08-28 03:09:18 +02:00
$sql .= " VALUES ( " . (( int ) $object -> id ) . " , '', ' " . $this -> db -> escape ( $customer -> id ) . " ', 'stripe', ' " . $this -> db -> escape ( $stripearrayofkeysbyenv [ $status ][ 'publishable_key' ]) . " ', " . (( int ) $status ) . " , " . (( int ) $conf -> entity ) . " , ' " . $this -> db -> idate ( dol_now ()) . " ', " . (( int ) $user -> id ) . " ) " ;
2018-03-14 16:53:16 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-26 21:17:52 +01:00
if ( ! $resql ) {
2018-03-14 16:53:16 +01:00
$this -> error = $this -> db -> lasterror ();
}
2021-02-26 21:17:52 +01:00
} catch ( Exception $e ) {
2018-03-14 21:07:45 +01:00
$this -> error = $e -> getMessage ();
2018-03-08 19:50:41 +01:00
}
}
2020-05-21 15:05:19 +02:00
} else {
2018-03-14 21:07:45 +01:00
dol_print_error ( $this -> db );
}
2018-03-14 16:53:16 +01:00
2018-03-08 19:50:41 +01:00
return $customer ;
2018-03-07 20:04:13 +01:00
}
2019-02-15 15:17:24 +01:00
2019-06-16 17:08:23 +02:00
/**
* Get the Stripe payment method Object from its ID
*
* @ param string $paymentmethod Payment Method ID
* @ param string $key '' = Use common API . If not '' , it is the Stripe connect account 'acc_....' to use Stripe connect
* @ param int $status Status ( 0 = test , 1 = live )
* @ return \Stripe\PaymentMethod | null Stripe PaymentMethod or null if not found
*/
public function getPaymentMethodStripe ( $paymentmethod , $key = '' , $status = 0 )
{
2019-06-17 18:24:45 +02:00
$stripepaymentmethod = null ;
2019-06-16 17:08:23 +02:00
try {
// Force to use the correct API key
global $stripearrayofkeysbyenv ;
\Stripe\Stripe :: setApiKey ( $stripearrayofkeysbyenv [ $status ][ 'secret_key' ]);
if ( empty ( $key )) { // If the Stripe connect account not set, we use common API usage
$stripepaymentmethod = \Stripe\PaymentMethod :: retrieve ( '' . $paymentmethod -> id . '' );
} else {
$stripepaymentmethod = \Stripe\PaymentMethod :: retrieve ( '' . $paymentmethod -> id . '' , array ( " stripe_account " => $key ));
}
2021-02-26 21:17:52 +01:00
} catch ( Exception $e ) {
2019-06-16 17:08:23 +02:00
$this -> error = $e -> getMessage ();
}
2019-06-17 18:24:45 +02:00
2019-06-16 17:08:23 +02:00
return $stripepaymentmethod ;
}
2020-09-08 21:27:28 +02:00
/**
2019-09-13 18:52:35 +02:00
* Get the Stripe payment intent . Create it with confirmnow = false
2020-09-08 21:27:28 +02:00
* Warning . If a payment was tried and failed , a payment intent was created .
2020-09-05 14:14:56 +02:00
* But if we change something on object to pay ( amount or other ), reusing same payment intent is not allowed by Stripe .
* Recommended solution is to recreate a new payment intent each time we need one ( old one will be automatically closed after a delay ),
2020-10-23 20:08:35 +02:00
* that ' s why i comment the part of code to retrieve a payment intent with object id ( never mind if we cumulate payment intent with old ones that will not be used )
2019-07-19 03:22:52 +02:00
* Note : This is used when option STRIPE_USE_INTENT_WITH_AUTOMATIC_CONFIRMATION is on when making a payment from the public / payment / newpayment . php page
* but not when using the STRIPE_USE_NEW_CHECKOUT .
2019-02-12 18:47:35 +01:00
*
2019-05-02 21:54:28 +02:00
* @ param double $amount Amount
* @ param string $currency_code Currency code
* @ param string $tag Tag
2019-05-03 02:22:27 +02:00
* @ param string $description Description
2019-09-23 11:35:11 +02:00
* @ param mixed $object Object to pay with Stripe
2019-02-25 15:15:17 +01:00
* @ param string $customer Stripe customer ref 'cus_xxxxxxxxxxxxx' via customerStripe ()
* @ param string $key '' = Use common API . If not '' , it is the Stripe connect account 'acc_....' to use Stripe connect
* @ param int $status Status ( 0 = test , 1 = live )
* @ param int $usethirdpartyemailforreceiptemail 1 = use thirdparty email for receipt
2019-04-26 05:19:11 +02:00
* @ param int $mode automatic = automatic confirmation / payment when conditions are ok , manual = need to call confirm () on intent
* @ param boolean $confirmnow false = default , true = try to confirm immediatly after create ( if conditions are ok )
2019-09-23 11:35:11 +02:00
* @ param string $payment_method 'pm_....' ( if known )
2021-02-02 10:25:57 +01:00
* @ param string $off_session If we use an already known payment method to pay when customer is not available during the checkout flow .
2019-10-31 16:47:27 +01:00
* @ param string $noidempotency_key Do not use the idempotency_key when creating the PaymentIntent
2019-09-13 18:52:35 +02:00
* @ return \Stripe\PaymentIntent | null Stripe PaymentIntent or null if not found and failed to create
2019-02-12 18:47:35 +01:00
*/
2021-01-28 23:18:10 +01:00
public function getPaymentIntent ( $amount , $currency_code , $tag , $description = '' , $object = null , $customer = null , $key = null , $status = 0 , $usethirdpartyemailforreceiptemail = 0 , $mode = 'automatic' , $confirmnow = false , $payment_method = null , $off_session = 0 , $noidempotency_key = 1 )
2019-02-12 18:47:35 +01:00
{
2020-02-12 23:17:46 +01:00
global $conf , $user ;
2018-03-08 19:50:41 +01:00
2021-02-03 17:20:05 +01:00
dol_syslog ( get_class ( $this ) . " ::getPaymentIntent " , LOG_INFO , 1 );
2019-05-03 02:22:27 +02:00
$error = 0 ;
2021-02-26 21:17:52 +01:00
if ( empty ( $status )) {
$service = 'StripeTest' ;
} else {
$service = 'StripeLive' ;
}
2019-02-12 18:47:35 +01:00
2019-11-08 15:51:54 +01:00
$arrayzerounitcurrency = array ( 'BIF' , 'CLP' , 'DJF' , 'GNF' , 'JPY' , 'KMF' , 'KRW' , 'MGA' , 'PYG' , 'RWF' , 'VND' , 'VUV' , 'XAF' , 'XOF' , 'XPF' );
2021-02-26 21:17:52 +01:00
if ( ! in_array ( $currency_code , $arrayzerounitcurrency )) {
$stripeamount = $amount * 100 ;
} else {
$stripeamount = $amount ;
}
2019-02-12 18:47:35 +01:00
2019-08-03 12:31:13 +02:00
$fee = $amount * ( $conf -> global -> STRIPE_APPLICATION_FEE_PERCENT / 100 ) + $conf -> global -> STRIPE_APPLICATION_FEE ;
2019-06-16 17:08:23 +02:00
if ( $fee >= $conf -> global -> STRIPE_APPLICATION_FEE_MAXIMAL && $conf -> global -> STRIPE_APPLICATION_FEE_MAXIMAL > $conf -> global -> STRIPE_APPLICATION_FEE_MINIMAL ) {
2020-09-08 21:27:28 +02:00
$fee = $conf -> global -> STRIPE_APPLICATION_FEE_MAXIMAL ;
2019-06-16 17:08:23 +02:00
} elseif ( $fee < $conf -> global -> STRIPE_APPLICATION_FEE_MINIMAL ) {
2020-09-08 21:27:28 +02:00
$fee = $conf -> global -> STRIPE_APPLICATION_FEE_MINIMAL ;
2019-05-02 21:54:28 +02:00
}
2019-11-08 15:51:54 +01:00
if ( ! in_array ( $currency_code , $arrayzerounitcurrency )) {
2019-10-27 17:01:23 +01:00
$stripefee = round ( $fee * 100 );
} else {
$stripefee = round ( $fee );
}
2019-02-12 18:47:35 +01:00
2019-05-02 21:54:28 +02:00
$paymentintent = null ;
2019-02-12 18:47:35 +01:00
2021-02-26 21:17:52 +01:00
if ( is_object ( $object ) && ! empty ( $conf -> global -> STRIPE_REUSE_EXISTING_INTENT_IF_FOUND )) {
2019-05-16 23:11:49 +02:00
// Warning. If a payment was tried and failed, a payment intent was created.
2020-09-05 14:14:56 +02:00
// But if we change something on object to pay (amount or other that does not change the idempotency key), reusing same payment intent is not allowed by Stripe.
// Recommended solution is to recreate a new payment intent each time we need one (old one will be automatically closed by Stripe after a delay), Stripe will
2019-10-31 16:47:27 +01:00
// automatically return the existing payment intent if idempotency is provided when we try to create the new one.
2020-10-23 20:08:35 +02:00
// That's why we can comment the part of code to retrieve a payment intent with object id (never mind if we cumulate payment intent with old ones that will not be used)
2020-09-05 14:14:56 +02:00
2019-05-16 23:11:49 +02:00
$sql = " SELECT pi.ext_payment_id, pi.entity, pi.fk_facture, pi.sourcetype, pi.ext_payment_site " ;
2020-09-14 04:30:04 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " prelevement_facture_demande as pi " ;
2021-08-27 18:18:50 +02:00
$sql .= " WHERE pi.fk_facture = " . (( int ) $object -> id );
2020-09-20 03:32:43 +02:00
$sql .= " AND pi.sourcetype = ' " . $this -> db -> escape ( $object -> element ) . " ' " ;
2020-09-14 04:30:04 +02:00
$sql .= " AND pi.entity IN ( " . getEntity ( 'societe' ) . " ) " ;
2020-09-20 03:32:43 +02:00
$sql .= " AND pi.ext_payment_site = ' " . $this -> db -> escape ( $service ) . " ' " ;
2020-09-08 21:27:28 +02:00
2020-09-14 04:30:04 +02:00
dol_syslog ( get_class ( $this ) . " ::getPaymentIntent search stripe payment intent for object id = " . $object -> id , LOG_DEBUG );
2020-09-08 21:27:28 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$num = $this -> db -> num_rows ( $resql );
2021-02-26 21:17:52 +01:00
if ( $num ) {
2020-09-08 21:27:28 +02:00
$obj = $this -> db -> fetch_object ( $resql );
$intent = $obj -> ext_payment_id ;
2020-09-14 04:30:04 +02:00
dol_syslog ( get_class ( $this ) . " ::getPaymentIntent found existing payment intent record " );
2020-09-08 21:27:28 +02:00
// Force to use the correct API key
global $stripearrayofkeysbyenv ;
\Stripe\Stripe :: setApiKey ( $stripearrayofkeysbyenv [ $status ][ 'secret_key' ]);
try {
if ( empty ( $key )) { // If the Stripe connect account not set, we use common API usage
$paymentintent = \Stripe\PaymentIntent :: retrieve ( $intent );
} else {
$paymentintent = \Stripe\PaymentIntent :: retrieve ( $intent , array ( " stripe_account " => $key ));
}
2021-02-26 21:17:52 +01:00
} catch ( Exception $e ) {
2020-09-08 21:27:28 +02:00
$error ++ ;
$this -> error = $e -> getMessage ();
}
}
}
2019-05-02 21:54:28 +02:00
}
2019-02-15 15:17:24 +01:00
2021-02-26 21:17:52 +01:00
if ( empty ( $paymentintent )) {
2021-01-28 22:06:13 +01:00
// Try to create intent. See https://stripe.com/docs/api/payment_intents/create
2020-09-08 21:27:28 +02:00
$ipaddress = getUserRemoteIP ();
$metadata = array ( 'dol_version' => DOL_VERSION , 'dol_entity' => $conf -> entity , 'ipaddress' => $ipaddress );
2021-02-26 21:17:52 +01:00
if ( is_object ( $object )) {
2020-09-08 21:27:28 +02:00
$metadata [ 'dol_type' ] = $object -> element ;
$metadata [ 'dol_id' ] = $object -> id ;
2021-02-26 21:17:52 +01:00
if ( is_object ( $object -> thirdparty ) && $object -> thirdparty -> id > 0 ) {
$metadata [ 'dol_thirdparty_id' ] = $object -> thirdparty -> id ;
}
2020-09-08 21:27:28 +02:00
}
// list of payment method types
$paymentmethodtypes = array ( " card " );
2021-02-26 21:17:52 +01:00
if ( ! empty ( $conf -> global -> STRIPE_SEPA_DIRECT_DEBIT )) {
$paymentmethodtypes [] = " sepa_debit " ; //&& ($object->thirdparty->isInEEC())
}
2021-11-12 13:20:58 +01:00
if ( ! empty ( $conf -> global -> STRIPE_KLARNA )) {
$paymentmethodtypes [] = " klarna " ;
2021-11-12 13:23:40 +01:00
}
2021-02-26 21:17:52 +01:00
if ( ! empty ( $conf -> global -> STRIPE_BANCONTACT )) {
$paymentmethodtypes [] = " bancontact " ;
}
if ( ! empty ( $conf -> global -> STRIPE_IDEAL )) {
$paymentmethodtypes [] = " ideal " ;
}
if ( ! empty ( $conf -> global -> STRIPE_GIROPAY )) {
$paymentmethodtypes [] = " giropay " ;
}
if ( ! empty ( $conf -> global -> STRIPE_SOFORT )) {
$paymentmethodtypes [] = " sofort " ;
}
2020-09-08 21:27:28 +02:00
$dataforintent = array (
" confirm " => $confirmnow , // Do not confirm immediatly during creation of intent
" confirmation_method " => $mode ,
" amount " => $stripeamount ,
" currency " => $currency_code ,
" payment_method_types " => $paymentmethodtypes ,
" description " => $description ,
" statement_descriptor_suffix " => dol_trunc ( $tag , 10 , 'right' , 'UTF-8' , 1 ), // 22 chars that appears on bank receipt (company + description)
//"save_payment_method" => true,
2019-07-19 03:18:53 +02:00
" setup_future_usage " => " on_session " ,
2020-09-08 21:27:28 +02:00
" metadata " => $metadata
);
2021-02-26 21:17:52 +01:00
if ( ! is_null ( $customer )) {
$dataforintent [ " customer " ] = $customer ;
}
2020-09-08 21:27:28 +02:00
// payment_method =
// payment_method_types = array('card')
//var_dump($dataforintent);
2021-02-26 21:17:52 +01:00
if ( $off_session ) {
2020-09-08 21:27:28 +02:00
unset ( $dataforintent [ 'setup_future_usage' ]);
2021-01-28 23:22:35 +01:00
// We can't use both "setup_future_usage" = "off_session" and "off_session" = true.
// Because $off_session parameter is dedicated to create paymentintent off_line (and not future payment), we need to use "off_session" = true.
2021-01-28 22:32:00 +01:00
//$dataforintent["setup_future_usage"] = "off_session";
2021-01-28 23:13:43 +01:00
$dataforintent [ " off_session " ] = true ;
2020-09-08 21:27:28 +02:00
}
2021-02-26 21:17:52 +01:00
if ( ! empty ( $conf -> global -> STRIPE_GIROPAY )) {
unset ( $dataforintent [ 'setup_future_usage' ]);
}
2021-11-12 13:20:58 +01:00
if ( ! empty ( $conf -> global -> STRIPE_KLARNA )) {
unset ( $dataforintent [ 'setup_future_usage' ]);
}
2021-02-26 21:17:52 +01:00
if ( ! is_null ( $payment_method )) {
2020-09-08 21:27:28 +02:00
$dataforintent [ " payment_method " ] = $payment_method ;
$description .= ' - ' . $payment_method ;
}
2021-02-26 21:17:52 +01:00
if ( $conf -> entity != $conf -> global -> STRIPECONNECT_PRINCIPAL && $stripefee > 0 ) {
2020-09-08 21:27:28 +02:00
$dataforintent [ " application_fee_amount " ] = $stripefee ;
}
2021-02-26 21:17:52 +01:00
if ( $usethirdpartyemailforreceiptemail && is_object ( $object ) && $object -> thirdparty -> email ) {
2020-09-08 21:27:28 +02:00
$dataforintent [ " receipt_email " ] = $object -> thirdparty -> email ;
}
try {
// Force to use the correct API key
global $stripearrayofkeysbyenv ;
\Stripe\Stripe :: setApiKey ( $stripearrayofkeysbyenv [ $status ][ 'secret_key' ]);
$arrayofoptions = array ();
if ( empty ( $noidempotency_key )) {
$arrayofoptions [ " idempotency_key " ] = $description ;
}
// Note: If all data for payment intent are same than a previous on, even if we use 'create', Stripe will return ID of the old existing payment intent.
if ( ! empty ( $key )) { // If the Stripe connect account not set, we use common API usage
$arrayofoptions [ " stripe_account " ] = $key ;
}
2021-01-28 22:10:38 +01:00
dol_syslog ( " dataforintent to create paymentintent = " . var_export ( $dataforintent , true ));
2020-09-08 21:27:28 +02:00
$paymentintent = \Stripe\PaymentIntent :: create ( $dataforintent , $arrayofoptions );
// Store the payment intent
2021-02-26 21:17:52 +01:00
if ( is_object ( $object )) {
2020-09-08 21:27:28 +02:00
$paymentintentalreadyexists = 0 ;
// Check that payment intent $paymentintent->id is not already recorded.
$sql = " SELECT pi.rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " prelevement_facture_demande as pi " ;
$sql .= " WHERE pi.entity IN ( " . getEntity ( 'societe' ) . " ) " ;
2020-09-20 03:32:43 +02:00
$sql .= " AND pi.ext_payment_site = ' " . $this -> db -> escape ( $service ) . " ' " ;
2020-09-08 21:27:28 +02:00
$sql .= " AND pi.ext_payment_id = ' " . $this -> db -> escape ( $paymentintent -> id ) . " ' " ;
dol_syslog ( get_class ( $this ) . " ::getPaymentIntent search if payment intent already in prelevement_facture_demande " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$num = $this -> db -> num_rows ( $resql );
2021-02-26 21:17:52 +01:00
if ( $num ) {
2020-09-08 21:27:28 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2021-02-26 21:17:52 +01:00
if ( $obj ) {
$paymentintentalreadyexists ++ ;
}
2020-09-08 21:27:28 +02:00
}
2021-02-26 21:17:52 +01:00
} else {
dol_print_error ( $this -> db );
}
2020-09-08 21:27:28 +02:00
// If not, we create it.
2021-02-26 21:17:52 +01:00
if ( ! $paymentintentalreadyexists ) {
2020-09-08 21:27:28 +02:00
$now = dol_now ();
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " prelevement_facture_demande (date_demande, fk_user_demande, ext_payment_id, fk_facture, sourcetype, entity, ext_payment_site, amount) " ;
2021-08-28 03:09:18 +02:00
$sql .= " VALUES (' " . $this -> db -> idate ( $now ) . " ', " . (( int ) $user -> id ) . " , ' " . $this -> db -> escape ( $paymentintent -> id ) . " ', " . (( int ) $object -> id ) . " , ' " . $this -> db -> escape ( $object -> element ) . " ', " . (( int ) $conf -> entity ) . " , ' " . $this -> db -> escape ( $service ) . " ', " . (( float ) $amount ) . " ) " ;
2020-09-08 21:27:28 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-26 21:17:52 +01:00
if ( ! $resql ) {
2020-09-08 21:27:28 +02:00
$error ++ ;
$this -> error = $this -> db -> lasterror ();
2021-05-27 11:57:17 +02:00
dol_syslog ( get_class ( $this ) . " ::PaymentIntent failed to insert paymentintent with id= " . $paymentintent -> id . " into database. " , LOG_ERR );
2020-09-08 21:27:28 +02:00
}
}
} else {
$_SESSION [ " stripe_payment_intent " ] = $paymentintent ;
}
2021-02-26 21:17:52 +01:00
} catch ( Stripe\Error\Card $e ) {
2020-09-08 21:27:28 +02:00
$error ++ ;
$this -> error = $e -> getMessage ();
$this -> code = $e -> getStripeCode ();
$this -> declinecode = $e -> getDeclineCode ();
2021-02-26 21:17:52 +01:00
} catch ( Exception $e ) {
2022-05-17 14:55:38 +02:00
//var_dump($dataforintent);
//var_dump($description);
//var_dump($key);
//var_dump($paymentintent);
//var_dump($e->getMessage());
//var_dump($e);
2020-09-08 21:27:28 +02:00
$error ++ ;
$this -> error = $e -> getMessage ();
$this -> code = '' ;
$this -> declinecode = '' ;
}
2019-02-12 18:47:35 +01:00
}
2021-02-03 17:20:05 +01:00
dol_syslog ( get_class ( $this ) . " ::getPaymentIntent return error= " . $error . " this->error= " . $this -> error , LOG_INFO , - 1 );
2019-05-03 02:22:27 +02:00
2021-02-03 17:20:05 +01:00
if ( ! $error ) {
2019-05-16 23:11:49 +02:00
return $paymentintent ;
2020-05-21 15:05:19 +02:00
} else {
2019-05-16 23:11:49 +02:00
return null ;
}
2019-02-12 18:47:35 +01:00
}
2019-02-15 15:17:24 +01:00
2019-09-13 18:52:35 +02:00
/**
* Get the Stripe payment intent . Create it with confirmnow = false
* Warning . If a payment was tried and failed , a payment intent was created .
* But if we change something on object to pay ( amount or other ), reusing same payment intent is not allowed .
* Recommanded solution is to recreate a new payment intent each time we need one ( old one will be automatically closed after a delay ),
2020-10-23 20:08:35 +02:00
* that ' s why i comment the part of code to retrieve a payment intent with object id ( never mind if we cumulate payment intent with old ones that will not be used )
2019-09-13 18:52:35 +02:00
* Note : This is used when option STRIPE_USE_INTENT_WITH_AUTOMATIC_CONFIRMATION is on when making a payment from the public / payment / newpayment . php page
* but not when using the STRIPE_USE_NEW_CHECKOUT .
*
* @ param string $description Description
* @ param Societe $object Object to pay with Stripe
* @ param string $customer Stripe customer ref 'cus_xxxxxxxxxxxxx' via customerStripe ()
* @ param string $key '' = Use common API . If not '' , it is the Stripe connect account 'acc_....' to use Stripe connect
* @ param int $status Status ( 0 = test , 1 = live )
* @ param int $usethirdpartyemailforreceiptemail 1 = use thirdparty email for receipt
* @ param boolean $confirmnow false = default , true = try to confirm immediatly after create ( if conditions are ok )
* @ return \Stripe\SetupIntent | null Stripe SetupIntent or null if not found and failed to create
*/
public function getSetupIntent ( $description , $object , $customer , $key , $status , $usethirdpartyemailforreceiptemail = 0 , $confirmnow = false )
{
global $conf ;
2020-02-04 18:59:49 +01:00
dol_syslog ( " getSetupIntent description= " . $description . ' confirmnow=' . $confirmnow , LOG_INFO , 1 );
2019-09-13 18:52:35 +02:00
$error = 0 ;
2021-02-26 21:17:52 +01:00
if ( empty ( $status )) {
$service = 'StripeTest' ;
} else {
$service = 'StripeLive' ;
}
2019-09-13 18:52:35 +02:00
$setupintent = null ;
2021-02-26 21:17:52 +01:00
if ( empty ( $setupintent )) {
2019-11-08 15:51:54 +01:00
$ipaddress = getUserRemoteIP ();
2019-09-13 18:52:35 +02:00
$metadata = array ( 'dol_version' => DOL_VERSION , 'dol_entity' => $conf -> entity , 'ipaddress' => $ipaddress );
2021-02-26 21:17:52 +01:00
if ( is_object ( $object )) {
2019-09-13 18:52:35 +02:00
$metadata [ 'dol_type' ] = $object -> element ;
$metadata [ 'dol_id' ] = $object -> id ;
2021-02-26 21:17:52 +01:00
if ( is_object ( $object -> thirdparty ) && $object -> thirdparty -> id > 0 ) {
$metadata [ 'dol_thirdparty_id' ] = $object -> thirdparty -> id ;
}
2019-09-13 18:52:35 +02:00
}
2020-09-05 14:14:56 +02:00
// list of payment method types
$paymentmethodtypes = array ( " card " );
2021-02-26 21:17:52 +01:00
if ( ! empty ( $conf -> global -> STRIPE_SEPA_DIRECT_DEBIT )) {
$paymentmethodtypes [] = " sepa_debit " ; //&& ($object->thirdparty->isInEEC())
}
if ( ! empty ( $conf -> global -> STRIPE_BANCONTACT )) {
$paymentmethodtypes [] = " bancontact " ;
}
if ( ! empty ( $conf -> global -> STRIPE_IDEAL )) {
$paymentmethodtypes [] = " ideal " ;
}
2021-01-24 13:40:36 +01:00
// Giropay not possible for setup intent
2021-02-26 21:17:52 +01:00
if ( ! empty ( $conf -> global -> STRIPE_SOFORT )) {
$paymentmethodtypes [] = " sofort " ;
}
2020-09-05 14:14:56 +02:00
2019-09-13 18:52:35 +02:00
$dataforintent = array (
2019-11-08 15:51:54 +01:00
" confirm " => $confirmnow , // Do not confirm immediatly during creation of intent
2020-09-05 14:14:56 +02:00
" payment_method_types " => $paymentmethodtypes ,
2019-09-13 18:52:35 +02:00
" usage " => " off_session " ,
" metadata " => $metadata
);
2021-02-26 21:17:52 +01:00
if ( ! is_null ( $customer )) {
$dataforintent [ " customer " ] = $customer ;
}
if ( ! is_null ( $description )) {
$dataforintent [ " description " ] = $description ;
}
2019-09-13 18:52:35 +02:00
// payment_method =
// payment_method_types = array('card')
//var_dump($dataforintent);
2021-02-26 21:17:52 +01:00
if ( $usethirdpartyemailforreceiptemail && is_object ( $object ) && $object -> thirdparty -> email ) {
2019-09-13 18:52:35 +02:00
$dataforintent [ " receipt_email " ] = $object -> thirdparty -> email ;
}
try {
// Force to use the correct API key
global $stripearrayofkeysbyenv ;
\Stripe\Stripe :: setApiKey ( $stripearrayofkeysbyenv [ $status ][ 'secret_key' ]);
2020-02-04 18:59:49 +01:00
dol_syslog ( " getSetupIntent " . $stripearrayofkeysbyenv [ $status ][ 'publishable_key' ], LOG_DEBUG );
2019-09-13 18:52:35 +02:00
// Note: If all data for payment intent are same than a previous on, even if we use 'create', Stripe will return ID of the old existing payment intent.
if ( empty ( $key )) { // If the Stripe connect account not set, we use common API usage
//$setupintent = \Stripe\SetupIntent::create($dataforintent, array("idempotency_key" => "$description"));
$setupintent = \Stripe\SetupIntent :: create ( $dataforintent , array ());
} else {
//$setupintent = \Stripe\SetupIntent::create($dataforintent, array("idempotency_key" => "$description", "stripe_account" => $key));
$setupintent = \Stripe\SetupIntent :: create ( $dataforintent , array ( " stripe_account " => $key ));
}
//var_dump($setupintent->id);
// Store the setup intent
/* if ( is_object ( $object ))
{
$setupintentalreadyexists = 0 ;
// Check that payment intent $setupintent->id is not already recorded.
$sql = " SELECT pi.rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " prelevement_facture_demande as pi " ;
$sql .= " WHERE pi.entity IN ( " . getEntity ( 'societe' ) . " ) " ;
2020-09-20 03:32:43 +02:00
$sql .= " AND pi.ext_payment_site = ' " . $this -> db -> escape ( $service ) . " ' " ;
2019-09-13 18:52:35 +02:00
$sql .= " AND pi.ext_payment_id = ' " . $this -> db -> escape ( $setupintent -> id ) . " ' " ;
dol_syslog ( get_class ( $this ) . " ::getPaymentIntent search if payment intent already in prelevement_facture_demande " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$num = $this -> db -> num_rows ( $resql );
if ( $num )
{
$obj = $this -> db -> fetch_object ( $resql );
if ( $obj ) $setupintentalreadyexists ++ ;
}
}
else dol_print_error ( $this -> db );
// If not, we create it.
if ( ! $setupintentalreadyexists )
{
$now = dol_now ();
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " prelevement_facture_demande (date_demande, fk_user_demande, ext_payment_id, fk_facture, sourcetype, entity, ext_payment_site) " ;
2021-08-28 03:09:18 +02:00
$sql .= " VALUES (' " . $this -> db -> idate ( $now ) . " ', " . (( int ) $user -> id ) . " , ' " . $this -> db -> escape ( $setupintent -> id ) . " ', " . (( int ) $object -> id ) . " , ' " . $this -> db -> escape ( $object -> element ) . " ', " . (( int ) $conf -> entity ) . " , ' " . $this -> db -> escape ( $service ) . " ', " . (( float ) $amount ) . " ) " ;
2019-09-13 18:52:35 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$error ++ ;
$this -> error = $this -> db -> lasterror ();
dol_syslog ( get_class ( $this ) . " ::PaymentIntent failed to insert paymentintent with id= " . $setupintent -> id . " into database. " );
}
}
}
else
{
$_SESSION [ " stripe_setup_intent " ] = $setupintent ;
} */
2021-02-26 21:17:52 +01:00
} catch ( Exception $e ) {
2022-05-17 14:55:38 +02:00
//var_dump($dataforintent);
//var_dump($description);
//var_dump($key);
//var_dump($setupintent);
//var_dump($e->getMessage());
2019-09-13 18:52:35 +02:00
$error ++ ;
$this -> error = $e -> getMessage ();
}
}
2021-02-26 21:17:52 +01:00
if ( ! $error ) {
2020-02-04 18:59:49 +01:00
dol_syslog ( " getSetupIntent " . ( is_object ( $setupintent ) ? $setupintent -> id : '' ), LOG_INFO , - 1 );
2019-09-13 18:52:35 +02:00
return $setupintent ;
2020-05-21 15:05:19 +02:00
} else {
2020-02-04 18:59:49 +01:00
dol_syslog ( " getSetupIntent return error= " . $error , LOG_INFO , - 1 );
2019-09-13 18:52:35 +02:00
return null ;
}
}
2018-03-14 21:07:45 +01:00
/**
2020-02-03 14:54:35 +01:00
* Get the Stripe card of a company payment mode ( option to create it on Stripe if not linked yet is no more available on new Stripe API )
2018-03-14 21:07:45 +01:00
*
2020-11-02 15:07:59 +01:00
* @ param \Stripe\StripeCustomer $cu Object stripe customer .
2018-03-14 21:07:45 +01:00
* @ param CompanyPaymentMode $object Object companypaymentmode to check , or create on stripe ( create on stripe also update the societe_rib table for current entity )
2018-03-29 19:44:38 +02:00
* @ param string $stripeacc '' = Use common API . If not '' , it is the Stripe connect account 'acc_....' to use Stripe connect
2018-03-14 21:07:45 +01:00
* @ param int $status Status ( 0 = test , 1 = live )
2020-02-03 14:54:35 +01:00
* @ param int $createifnotlinkedtostripe 1 = Create the stripe card and the link if the card is not yet linked to a stripe card . Deprecated with new Stripe API and SCA .
2019-09-22 21:09:01 +02:00
* @ return \Stripe\StripeCard | \Stripe\PaymentMethod | null Stripe Card or null if not found
2018-03-14 21:07:45 +01:00
*/
2019-01-27 15:20:16 +01:00
public function cardStripe ( $cu , CompanyPaymentMode $object , $stripeacc = '' , $status = 0 , $createifnotlinkedtostripe = 0 )
2018-03-14 21:07:45 +01:00
{
2020-02-03 14:54:35 +01:00
global $conf , $user , $langs ;
2018-03-14 21:07:45 +01:00
2018-03-16 00:30:03 +01:00
$card = null ;
2018-03-14 21:07:45 +01:00
2019-11-08 15:51:54 +01:00
$sql = " SELECT sa.stripe_card_ref, sa.proprio, sa.exp_date_month, sa.exp_date_year, sa.number, sa.cvn " ; // stripe_card_ref is card_....
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_rib as sa " ;
2021-08-27 18:18:50 +02:00
$sql .= " WHERE sa.rowid = " . (( int ) $object -> id ); // We get record from ID, no need for filter on entity
2019-11-08 15:51:54 +01:00
$sql .= " AND sa.type = 'card' " ;
2018-03-14 21:07:45 +01:00
2019-11-08 15:51:54 +01:00
dol_syslog ( get_class ( $this ) . " ::fetch search stripe card id for paymentmode id= " . $object -> id . " , stripeacc= " . $stripeacc . " , status= " . $status . " , createifnotlinkedtostripe= " . $createifnotlinkedtostripe , LOG_DEBUG );
2018-03-14 21:07:45 +01:00
$resql = $this -> db -> query ( $sql );
if ( $resql ) {
$num = $this -> db -> num_rows ( $resql );
2021-02-26 21:17:52 +01:00
if ( $num ) {
2018-03-14 21:07:45 +01:00
$obj = $this -> db -> fetch_object ( $resql );
$cardref = $obj -> stripe_card_ref ;
2019-11-08 15:51:54 +01:00
dol_syslog ( get_class ( $this ) . " ::cardStripe cardref= " . $cardref );
2021-02-26 21:17:52 +01:00
if ( $cardref ) {
2018-03-14 21:07:45 +01:00
try {
2018-03-29 19:44:38 +02:00
if ( empty ( $stripeacc )) { // If the Stripe connect account not set, we use common API usage
2021-02-26 21:17:52 +01:00
if ( ! preg_match ( '/^pm_/' , $cardref ) && ! empty ( $cu -> sources )) {
2019-09-22 21:09:01 +02:00
$card = $cu -> sources -> retrieve ( $cardref );
2020-05-21 15:05:19 +02:00
} else {
2019-09-22 21:09:01 +02:00
$card = \Stripe\PaymentMethod :: retrieve ( $cardref );
}
2018-03-14 21:07:45 +01:00
} else {
2021-02-26 21:17:52 +01:00
if ( ! preg_match ( '/^pm_/' , $cardref ) && ! empty ( $cu -> sources )) {
2019-09-22 21:09:01 +02:00
//$card = $cu->sources->retrieve($cardref, array("stripe_account" => $stripeacc)); // this API fails when array stripe_account is provided
$card = $cu -> sources -> retrieve ( $cardref );
2020-05-21 15:05:19 +02:00
} else {
2019-09-22 21:09:01 +02:00
//$card = \Stripe\PaymentMethod::retrieve($cardref, array("stripe_account" => $stripeacc)); // Don't know if this works
$card = \Stripe\PaymentMethod :: retrieve ( $cardref );
}
2018-03-14 21:07:45 +01:00
}
2021-02-26 21:17:52 +01:00
} catch ( Exception $e ) {
2018-03-15 00:40:29 +01:00
$this -> error = $e -> getMessage ();
2018-03-16 16:42:26 +01:00
dol_syslog ( $this -> error , LOG_WARNING );
2018-03-14 21:07:45 +01:00
}
2021-02-26 21:17:52 +01:00
} elseif ( $createifnotlinkedtostripe ) {
2019-11-08 15:51:54 +01:00
$exp_date_month = $obj -> exp_date_month ;
$exp_date_year = $obj -> exp_date_year ;
$number = $obj -> number ;
$cvc = $obj -> cvn ; // cvn in database, cvc for stripe
$cardholdername = $obj -> proprio ;
2018-03-15 00:18:43 +01:00
2020-11-03 01:25:58 +01:00
$ipaddress = getUserRemoteIP ();
2020-11-02 15:11:42 +01:00
2018-03-14 21:07:45 +01:00
$dataforcard = array (
2018-03-15 00:18:43 +01:00
" source " => array ( 'object' => 'card' , 'exp_month' => $exp_date_month , 'exp_year' => $exp_date_year , 'number' => $number , 'cvc' => $cvc , 'name' => $cardholdername ),
2020-11-03 01:25:58 +01:00
" metadata " => array ( 'dol_id' => $object -> id , 'dol_version' => DOL_VERSION , 'dol_entity' => $conf -> entity , 'ipaddress' => $ipaddress )
2018-03-14 21:07:45 +01:00
);
//$a = \Stripe\Stripe::getApiKey();
2022-05-17 14:55:38 +02:00
//var_dump($a);
//var_dump($stripeacc);exit;
2018-03-14 21:07:45 +01:00
try {
2018-03-29 19:44:38 +02:00
if ( empty ( $stripeacc )) { // If the Stripe connect account not set, we use common API usage
2021-02-26 21:17:52 +01:00
if ( empty ( $conf -> global -> STRIPE_USE_INTENT_WITH_AUTOMATIC_CONFIRMATION )) {
2020-02-03 14:54:35 +01:00
dol_syslog ( " Try to create card with dataforcard = " . json_encode ( $dataforcard ));
2019-09-22 21:09:01 +02:00
$card = $cu -> sources -> create ( $dataforcard );
2021-02-26 21:17:52 +01:00
if ( ! $card ) {
2020-02-03 14:54:35 +01:00
$this -> error = 'Creation of card on Stripe has failed' ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-02-03 14:54:35 +01:00
$connect = '' ;
2021-02-26 21:17:52 +01:00
if ( ! empty ( $stripeacc )) {
$connect = $stripeacc . '/' ;
}
2020-02-03 14:54:35 +01:00
$url = 'https://dashboard.stripe.com/' . $connect . 'test/customers/' . $cu -> id ;
2021-02-26 21:17:52 +01:00
if ( $status ) {
2020-02-03 14:54:35 +01:00
$url = 'https://dashboard.stripe.com/' . $connect . 'customers/' . $cu -> id ;
}
$urtoswitchonstripe = ' <a href="' . $url . '" target="_stripe">' . img_picto ( $langs -> trans ( 'ShowInStripe' ), 'globe' ) . '</a>' ;
//dol_syslog("Error: This case is not supported", LOG_ERR);
$this -> error = $langs -> trans ( 'CreationOfPaymentModeMustBeDoneFromStripeInterface' , $urtoswitchonstripe );
2019-09-22 21:09:01 +02:00
}
2018-03-14 21:07:45 +01:00
} else {
2021-02-26 21:17:52 +01:00
if ( empty ( $conf -> global -> STRIPE_USE_INTENT_WITH_AUTOMATIC_CONFIRMATION )) {
2020-02-03 14:54:35 +01:00
dol_syslog ( " Try to create card with dataforcard = " . json_encode ( $dataforcard ));
2019-09-22 21:09:01 +02:00
$card = $cu -> sources -> create ( $dataforcard , array ( " stripe_account " => $stripeacc ));
2021-02-26 21:17:52 +01:00
if ( ! $card ) {
2020-02-03 14:54:35 +01:00
$this -> error = 'Creation of card on Stripe has failed' ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-02-03 14:54:35 +01:00
$connect = '' ;
2021-02-26 21:17:52 +01:00
if ( ! empty ( $stripeacc )) {
$connect = $stripeacc . '/' ;
}
2020-02-03 14:54:35 +01:00
$url = 'https://dashboard.stripe.com/' . $connect . 'test/customers/' . $cu -> id ;
2021-02-26 21:17:52 +01:00
if ( $status ) {
2020-02-03 14:54:35 +01:00
$url = 'https://dashboard.stripe.com/' . $connect . 'customers/' . $cu -> id ;
}
$urtoswitchonstripe = ' <a href="' . $url . '" target="_stripe">' . img_picto ( $langs -> trans ( 'ShowInStripe' ), 'globe' ) . '</a>' ;
//dol_syslog("Error: This case is not supported", LOG_ERR);
$this -> error = $langs -> trans ( 'CreationOfPaymentModeMustBeDoneFromStripeInterface' , $urtoswitchonstripe );
2019-09-22 21:09:01 +02:00
}
2018-03-14 21:07:45 +01:00
}
2021-02-26 21:17:52 +01:00
if ( $card ) {
2019-11-08 15:51:54 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " societe_rib " ;
$sql .= " SET stripe_card_ref = ' " . $this -> db -> escape ( $card -> id ) . " ', card_type = ' " . $this -> db -> escape ( $card -> brand ) . " ', " ;
$sql .= " country_code = ' " . $this -> db -> escape ( $card -> country ) . " ', " ;
$sql .= " approved = " . ( $card -> cvc_check == 'pass' ? 1 : 0 );
2021-08-27 18:18:50 +02:00
$sql .= " WHERE rowid = " . (( int ) $object -> id );
2019-11-08 15:51:54 +01:00
$sql .= " AND type = 'card' " ;
2018-03-15 00:18:43 +01:00
$resql = $this -> db -> query ( $sql );
2021-02-26 21:17:52 +01:00
if ( ! $resql ) {
2018-03-15 00:18:43 +01:00
$this -> error = $this -> db -> lasterror ();
}
}
2021-02-26 21:17:52 +01:00
} catch ( Exception $e ) {
2018-03-14 21:07:45 +01:00
$this -> error = $e -> getMessage ();
2018-03-16 16:42:26 +01:00
dol_syslog ( $this -> error , LOG_WARNING );
2018-03-14 21:07:45 +01:00
}
}
}
2020-05-21 15:05:19 +02:00
} else {
2018-03-14 21:07:45 +01:00
dol_print_error ( $this -> db );
}
return $card ;
}
2018-03-08 19:50:41 +01:00
/**
2019-10-12 04:51:41 +02:00
* Create charge .
* This is called by page htdocs / stripe / payment . php and may be deprecated .
2018-03-08 19:50:41 +01:00
*
2018-09-11 10:59:25 +02:00
* @ param int $amount Amount to pay
* @ param string $currency EUR , GPB ...
* @ param string $origin Object type to pay ( order , invoice , contract ... )
* @ param int $item Object id to pay
2019-09-23 16:50:32 +02:00
* @ param string $source src_xxxxx or card_xxxxx or pm_xxxxx
2018-09-11 10:59:25 +02:00
* @ param string $customer Stripe customer ref 'cus_xxxxxxxxxxxxx' via customerStripe ()
* @ param string $account Stripe account ref 'acc_xxxxxxxxxxxxx' via getStripeAccount ()
2018-03-16 00:43:47 +01:00
* @ param int $status Status ( 0 = test , 1 = live )
* @ param int $usethirdpartyemailforreceiptemail Use thirdparty email as receipt email
2018-09-11 10:59:25 +02:00
* @ param boolean $capture Set capture flag to true ( take payment ) or false ( wait )
2018-03-08 19:50:41 +01:00
* @ return Stripe
*/
2019-01-27 15:20:16 +01:00
public function createPaymentStripe ( $amount , $currency , $origin , $item , $source , $customer , $account , $status = 0 , $usethirdpartyemailforreceiptemail = 0 , $capture = true )
2018-03-08 19:50:41 +01:00
{
global $conf ;
2018-03-08 22:26:42 +01:00
2018-03-16 00:43:47 +01:00
$error = 0 ;
2021-02-26 21:17:52 +01:00
if ( empty ( $status )) {
$service = 'StripeTest' ;
} else {
$service = 'StripeLive' ;
}
2018-03-13 14:44:35 +01:00
2018-03-16 00:30:03 +01:00
$sql = " SELECT sa.key_account as key_account, sa.fk_soc, sa.entity " ;
2019-11-08 15:51:54 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_account as sa " ;
$sql .= " WHERE sa.key_account = ' " . $this -> db -> escape ( $customer ) . " ' " ;
2018-03-13 12:07:53 +01:00
//$sql.= " AND sa.entity IN (".getEntity('societe').")";
2019-11-08 15:51:54 +01:00
$sql .= " AND sa.site = 'stripe' AND sa.status = " . (( int ) $status );
2018-03-07 20:04:13 +01:00
2019-11-08 15:51:54 +01:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2018-03-07 20:04:13 +01:00
$result = $this -> db -> query ( $sql );
2018-03-08 19:50:41 +01:00
if ( $result ) {
if ( $this -> db -> num_rows ( $result )) {
2018-03-07 20:04:13 +01:00
$obj = $this -> db -> fetch_object ( $result );
2018-03-13 12:07:53 +01:00
$key = $obj -> fk_soc ;
} else {
2018-04-16 13:51:16 +02:00
$key = null ;
2018-03-08 19:50:41 +01:00
}
2018-03-13 12:07:53 +01:00
} else {
2018-04-16 13:51:16 +02:00
$key = null ;
2018-03-08 19:50:41 +01:00
}
2018-03-13 14:44:35 +01:00
2019-11-08 15:51:54 +01:00
$arrayzerounitcurrency = array ( 'BIF' , 'CLP' , 'DJF' , 'GNF' , 'JPY' , 'KMF' , 'KRW' , 'MGA' , 'PYG' , 'RWF' , 'VND' , 'VUV' , 'XAF' , 'XOF' , 'XPF' );
2021-02-26 21:17:52 +01:00
if ( ! in_array ( $currency , $arrayzerounitcurrency )) {
$stripeamount = $amount * 100 ;
} else {
$stripeamount = $amount ;
}
2018-03-16 00:30:03 +01:00
2018-03-08 19:50:41 +01:00
$societe = new Societe ( $this -> db );
2021-02-26 21:17:52 +01:00
if ( $key > 0 ) {
$societe -> fetch ( $key );
}
2018-03-07 20:04:13 +01:00
2018-03-16 00:43:47 +01:00
$description = " " ;
$ref = " " ;
2019-10-12 04:51:41 +02:00
if ( $origin == 'order' ) {
2018-03-08 19:50:41 +01:00
$order = new Commande ( $this -> db );
$order -> fetch ( $item );
$ref = $order -> ref ;
2019-11-08 15:51:54 +01:00
$description = " ORD= " . $ref . " .CUS= " . $societe -> id . " .PM=stripe " ;
2019-10-12 04:51:41 +02:00
} elseif ( $origin == 'invoice' ) {
2018-03-08 19:50:41 +01:00
$invoice = new Facture ( $this -> db );
$invoice -> fetch ( $item );
$ref = $invoice -> ref ;
2019-11-08 15:51:54 +01:00
$description = " INV= " . $ref . " .CUS= " . $societe -> id . " .PM=stripe " ;
2018-03-08 19:50:41 +01:00
}
2018-03-07 20:04:13 +01:00
2020-11-03 01:25:58 +01:00
$ipaddress = getUserRemoteIP ();
2020-11-02 15:11:42 +01:00
2018-03-08 19:50:41 +01:00
$metadata = array (
2019-11-08 15:51:54 +01:00
" dol_id " => " " . $item . " " ,
" dol_type " => " " . $origin . " " ,
" dol_thirdparty_id " => " " . $societe -> id . " " ,
2018-09-02 17:53:31 +02:00
'dol_thirdparty_name' => $societe -> name ,
2018-03-16 00:30:03 +01:00
'dol_version' => DOL_VERSION ,
'dol_entity' => $conf -> entity ,
2020-11-03 01:25:58 +01:00
'ipaddress' => $ipaddress
2018-03-08 19:50:41 +01:00
);
$return = new Stripe ( $this -> db );
try {
2018-05-17 16:07:44 +02:00
// Force to use the correct API key
global $stripearrayofkeysbyenv ;
\Stripe\Stripe :: setApiKey ( $stripearrayofkeysbyenv [ $status ][ 'secret_key' ]);
2021-02-26 21:17:52 +01:00
if ( empty ( $conf -> stripeconnect -> enabled )) { // With a common Stripe account
if ( preg_match ( '/pm_/i' , $source )) {
2019-10-12 04:51:41 +02:00
$stripecard = $source ;
$amountstripe = $stripeamount ;
2019-11-08 15:51:54 +01:00
$FULLTAG = 'PFBO' ; // Payment From Back Office
2019-10-12 04:51:41 +02:00
$stripe = $return ;
$amounttopay = $amount ;
$servicestatus = $status ;
dol_syslog ( " * createPaymentStripe get stripeacc " , LOG_DEBUG );
2019-11-08 15:51:54 +01:00
$stripeacc = $stripe -> getStripeAccount ( $service ); // Get Stripe OAuth connect account if it exists (no network access here)
2019-10-12 04:51:41 +02:00
2021-02-03 12:04:28 +01:00
dol_syslog ( " * createPaymentStripe Create payment for customer " . $customer -> id . " on source card " . $stripecard -> id . " , amounttopay= " . $amounttopay . " , amountstripe= " . $amountstripe . " , FULLTAG= " . $FULLTAG , LOG_DEBUG );
2019-10-12 04:51:41 +02:00
// Create payment intent and charge payment (confirmnow = true)
$paymentintent = $stripe -> getPaymentIntent ( $amounttopay , $currency , $FULLTAG , $description , $invoice , $customer -> id , $stripeacc , $servicestatus , 0 , 'automatic' , true , $stripecard -> id , 1 );
$charge = new stdClass ();
2021-02-26 21:17:52 +01:00
if ( $paymentintent -> status == 'succeeded' ) {
2019-10-12 04:51:41 +02:00
$charge -> status = 'ok' ;
2020-05-21 15:05:19 +02:00
} else {
2019-10-12 04:51:41 +02:00
$charge -> status = 'failed' ;
$charge -> failure_code = $stripe -> code ;
$charge -> failure_message = $stripe -> error ;
$charge -> failure_declinecode = $stripe -> declinecode ;
$stripefailurecode = $stripe -> code ;
$stripefailuremessage = $stripe -> error ;
$stripefailuredeclinecode = $stripe -> declinecode ;
}
2021-02-26 21:17:52 +01:00
} elseif ( preg_match ( '/acct_/i' , $source )) {
2020-09-08 21:27:28 +02:00
$charge = \Stripe\Charge :: create ( array (
2018-03-08 19:50:41 +01:00
" amount " => " $stripeamount " ,
" currency " => " $currency " ,
2020-09-08 21:27:28 +02:00
" statement_descriptor_suffix " => dol_trunc ( $description , 10 , 'right' , 'UTF-8' , 1 ), // 22 chars that appears on bank receipt (company + description)
2018-09-11 10:59:25 +02:00
" description " => " Stripe payment: " . $description ,
" capture " => $capture ,
2018-03-08 19:50:41 +01:00
" metadata " => $metadata ,
" source " => " $source "
2018-03-16 00:30:03 +01:00
));
} else {
2018-03-16 00:43:47 +01:00
$paymentarray = array (
2018-03-08 19:50:41 +01:00
" amount " => " $stripeamount " ,
" currency " => " $currency " ,
2020-09-08 21:27:28 +02:00
" statement_descriptor_suffix " => dol_trunc ( $description , 10 , 'right' , 'UTF-8' , 1 ), // 22 chars that appears on bank receipt (company + description)
2018-09-11 10:59:25 +02:00
" description " => " Stripe payment: " . $description ,
" capture " => $capture ,
2018-03-08 19:50:41 +01:00
" metadata " => $metadata ,
" source " => " $source " ,
" customer " => " $customer "
2018-03-16 00:43:47 +01:00
);
2021-02-26 21:17:52 +01:00
if ( $societe -> email && $usethirdpartyemailforreceiptemail ) {
2018-03-16 00:43:47 +01:00
$paymentarray [ " receipt_email " ] = $societe -> email ;
}
2019-04-30 03:07:20 +02:00
$charge = \Stripe\Charge :: create ( $paymentarray , array ( " idempotency_key " => " $description " ));
2018-03-08 19:50:41 +01:00
}
2018-03-16 00:30:03 +01:00
} else {
2019-10-12 04:51:41 +02:00
// With Stripe Connect
$fee = $amount * ( $conf -> global -> STRIPE_APPLICATION_FEE_PERCENT / 100 ) + $conf -> global -> STRIPE_APPLICATION_FEE ;
if ( $fee >= $conf -> global -> STRIPE_APPLICATION_FEE_MAXIMAL && $conf -> global -> STRIPE_APPLICATION_FEE_MAXIMAL > $conf -> global -> STRIPE_APPLICATION_FEE_MINIMAL ) {
2020-09-08 21:27:28 +02:00
$fee = $conf -> global -> STRIPE_APPLICATION_FEE_MAXIMAL ;
2019-10-12 04:51:41 +02:00
} elseif ( $fee < $conf -> global -> STRIPE_APPLICATION_FEE_MINIMAL ) {
2020-09-08 21:27:28 +02:00
$fee = $conf -> global -> STRIPE_APPLICATION_FEE_MINIMAL ;
2019-10-12 04:51:41 +02:00
}
2021-02-26 21:17:52 +01:00
if ( ! in_array ( $currency , $arrayzerounitcurrency )) {
$stripefee = round ( $fee * 100 );
} else {
$stripefee = round ( $fee );
}
2018-03-16 00:30:03 +01:00
2019-12-01 11:03:29 +01:00
$paymentarray = array (
2019-02-14 20:32:11 +01:00
" amount " => " $stripeamount " ,
" currency " => " $currency " ,
2020-04-10 10:59:32 +02:00
" statement_descriptor_suffix " => dol_trunc ( $description , 10 , 'right' , 'UTF-8' , 1 ), // 22 chars that appears on bank receipt (company + description)
2019-02-14 20:32:11 +01:00
" description " => " Stripe payment: " . $description ,
" capture " => $capture ,
" metadata " => $metadata ,
" source " => " $source " ,
" customer " => " $customer "
);
2021-02-26 21:17:52 +01:00
if ( $conf -> entity != $conf -> global -> STRIPECONNECT_PRINCIPAL && $stripefee > 0 ) {
2019-07-26 14:16:28 +02:00
$paymentarray [ " application_fee_amount " ] = $stripefee ;
2019-02-14 20:32:11 +01:00
}
2021-02-26 21:17:52 +01:00
if ( $societe -> email && $usethirdpartyemailforreceiptemail ) {
2019-02-14 20:32:11 +01:00
$paymentarray [ " receipt_email " ] = $societe -> email ;
}
2018-08-04 14:30:52 +02:00
2021-02-26 21:17:52 +01:00
if ( preg_match ( '/pm_/i' , $source )) {
2019-10-12 04:51:41 +02:00
$stripecard = $source ;
$amountstripe = $stripeamount ;
2019-11-08 15:51:54 +01:00
$FULLTAG = 'PFBO' ; // Payment From Back Office
2019-10-12 04:51:41 +02:00
$stripe = $return ;
$amounttopay = $amount ;
$servicestatus = $status ;
dol_syslog ( " * createPaymentStripe get stripeacc " , LOG_DEBUG );
2019-11-08 15:51:54 +01:00
$stripeacc = $stripe -> getStripeAccount ( $service ); // Get Stripe OAuth connect account if it exists (no network access here)
2019-10-12 04:51:41 +02:00
dol_syslog ( " * createPaymentStripe Create payment on card " . $stripecard -> id . " , amounttopay= " . $amounttopay . " , amountstripe= " . $amountstripe . " , FULLTAG= " . $FULLTAG , LOG_DEBUG );
// Create payment intent and charge payment (confirmnow = true)
$paymentintent = $stripe -> getPaymentIntent ( $amounttopay , $currency , $FULLTAG , $description , $invoice , $customer -> id , $stripeacc , $servicestatus , 0 , 'automatic' , true , $stripecard -> id , 1 );
$charge = new stdClass ();
2021-02-26 21:17:52 +01:00
if ( $paymentintent -> status == 'succeeded' ) {
2019-10-12 04:51:41 +02:00
$charge -> status = 'ok' ;
$charge -> id = $paymentintent -> id ;
2020-05-21 15:05:19 +02:00
} else {
2019-10-12 04:51:41 +02:00
$charge -> status = 'failed' ;
$charge -> failure_code = $stripe -> code ;
$charge -> failure_message = $stripe -> error ;
$charge -> failure_declinecode = $stripe -> declinecode ;
}
2020-05-21 15:05:19 +02:00
} else {
2019-10-12 04:51:41 +02:00
$charge = \Stripe\Charge :: create ( $paymentarray , array ( " idempotency_key " => " $description " , " stripe_account " => " $account " ));
}
2018-03-08 19:50:41 +01:00
}
2021-02-26 21:17:52 +01:00
if ( isset ( $charge -> id )) {
}
2018-03-07 20:04:13 +01:00
2018-03-08 19:50:41 +01:00
$return -> statut = 'success' ;
$return -> id = $charge -> id ;
2019-10-12 04:51:41 +02:00
2021-02-26 21:17:52 +01:00
if ( preg_match ( '/pm_/i' , $source )) {
2020-10-23 20:08:35 +02:00
$return -> message = 'Payment retrieved by card status = ' . $charge -> status ;
2020-05-21 15:05:19 +02:00
} else {
2019-10-12 04:51:41 +02:00
if ( $charge -> source -> type == 'card' ) {
2019-11-08 15:51:54 +01:00
$return -> message = $charge -> source -> card -> brand . " .... " . $charge -> source -> card -> last4 ;
2019-10-12 04:51:41 +02:00
} elseif ( $charge -> source -> type == 'three_d_secure' ) {
$stripe = new Stripe ( $this -> db );
2019-11-08 15:51:54 +01:00
$src = \Stripe\Source :: retrieve ( " " . $charge -> source -> three_d_secure -> card . " " , array (
2019-10-12 04:51:41 +02:00
" stripe_account " => $stripe -> getStripeAccount ( $service )
));
2019-11-08 15:51:54 +01:00
$return -> message = $src -> card -> brand . " .... " . $src -> card -> last4 ;
2019-10-12 04:51:41 +02:00
} else {
$return -> message = $charge -> id ;
}
2018-03-08 19:50:41 +01:00
}
} catch ( \Stripe\Error\Card $e ) {
2018-03-16 00:43:47 +01:00
include DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php' ;
2018-03-08 19:50:41 +01:00
// Since it's a decline, \Stripe\Error\Card will be caught
$body = $e -> getJsonBody ();
$err = $body [ 'error' ];
2018-03-07 20:04:13 +01:00
2018-03-08 19:50:41 +01:00
$return -> statut = 'error' ;
$return -> id = $err [ 'charge' ];
$return -> type = $err [ 'type' ];
$return -> code = $err [ 'code' ];
$return -> message = $err [ 'message' ];
2019-11-08 15:51:54 +01:00
$body = " Error: <br> " . $return -> id . " " . $return -> message . " " ;
2018-03-16 00:43:47 +01:00
$subject = '[Alert] Payment error using Stripe' ;
$cmailfile = new CMailFile ( $subject , $conf -> global -> ONLINE_PAYMENT_SENDEMAIL , $conf -> global -> MAIN_INFO_SOCIETE_MAIL , $body );
$cmailfile -> sendfile ();
$error ++ ;
2018-03-08 19:50:41 +01:00
dol_syslog ( $e -> getMessage (), LOG_WARNING , 0 , '_stripe' );
} catch ( \Stripe\Error\RateLimit $e ) {
// Too many requests made to the API too quickly
2018-03-16 00:43:47 +01:00
$error ++ ;
2018-03-08 19:50:41 +01:00
dol_syslog ( $e -> getMessage (), LOG_WARNING , 0 , '_stripe' );
} catch ( \Stripe\Error\InvalidRequest $e ) {
// Invalid parameters were supplied to Stripe's API
2018-03-16 00:43:47 +01:00
$error ++ ;
2018-03-08 19:50:41 +01:00
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)
2018-03-16 00:43:47 +01:00
$error ++ ;
2018-03-08 19:50:41 +01:00
dol_syslog ( $e -> getMessage (), LOG_WARNING , 0 , '_stripe' );
} catch ( \Stripe\Error\ApiConnection $e ) {
// Network communication with Stripe failed
2018-03-16 00:43:47 +01:00
$error ++ ;
2018-03-08 19:50:41 +01:00
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
2018-03-16 00:43:47 +01:00
$error ++ ;
2018-03-08 19:50:41 +01:00
dol_syslog ( $e -> getMessage (), LOG_WARNING , 0 , '_stripe' );
} catch ( Exception $e ) {
// Something else happened, completely unrelated to Stripe
2018-03-16 00:43:47 +01:00
$error ++ ;
2018-03-08 19:50:41 +01:00
dol_syslog ( $e -> getMessage (), LOG_WARNING , 0 , '_stripe' );
}
return $return ;
}
2018-03-07 20:04:13 +01:00
}