2018-03-07 20:04:13 +01:00
< ? php
2018-03-08 02:26:57 +01:00
/* Copyright ( C ) 2018 PtibogXIV < 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
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*/
// 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' ;
2018-03-14 16:53:16 +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
*/
2018-10-05 15:55:47 +02:00
public $fk_soc ;
2018-09-01 23:04:46 +02:00
2018-10-12 12:20:05 +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 ;
2018-09-01 23:04:46 +02:00
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'
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
*/
2018-03-08 20:59:23 +01:00
public function getStripeAccount ( $mode = 'StripeTest' )
2018-03-07 20:04:13 +01:00
{
global $conf ;
2018-03-08 19:50:41 +01:00
$sql = " SELECT tokenstring " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " oauth_token " ;
$sql .= " WHERE entity = " . $conf -> entity ;
$sql .= " AND service = ' " . $mode . " ' " ;
2018-03-07 20:04:13 +01:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
2018-03-08 19:50:41 +01:00
if ( $result )
2018-03-07 20:04:13 +01:00
{
if ( $this -> db -> num_rows ( $result ))
{
$obj = $this -> db -> fetch_object ( $result );
2018-03-08 19:50:41 +01:00
$tokenstring = $obj -> tokenstring ;
$tmparray = dol_json_decode ( $tokenstring );
$key = $tmparray -> stripe_user_id ;
}
else {
$tokenstring = '' ;
}
}
else {
dol_print_error ( $this -> db );
}
2018-03-31 16:45:36 +02:00
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
*
2018-03-12 15:59:03 +01:00
* @ param int $id Id of third party
2018-03-13 09:37:59 +01:00
* @ param int $status Status
2018-03-13 17:47:43 +01:00
* @ return string Stripe customer ref 'cu_xxxxxxxxxxxxx' or ''
2018-03-08 19:50:41 +01:00
*/
2018-03-13 09:37:59 +01:00
public function getStripeCustomerAccount ( $id , $status = 0 )
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 );
2018-03-14 16:53:16 +01:00
return $societeaccount -> getCustomerAccount ( $id , 'stripe' , $status ); // Get thirdparty cus_...
2018-03-08 19:50:41 +01:00
}
/**
2018-03-13 14:44:35 +01:00
* Get the Stripe customer of a thirdparty ( with option to create it if not linked yet )
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
*/
2018-03-14 21:07:45 +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 ;
2018-03-31 16:45:36 +02:00
if ( empty ( $object -> id ))
{
dol_syslog ( " customerStripe is called with param object not loaded " );
return null ;
}
2018-03-14 16:53:16 +01:00
$customer = null ;
2018-03-13 14:44:35 +01:00
2018-03-14 16:53:16 +01:00
$sql = " SELECT sa.key_account as key_account, sa.entity " ; // key_account is cus_....
2018-03-13 11:37:40 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_account as sa " ;
2018-03-14 16:53:16 +01:00
$sql .= " WHERE sa.fk_soc = " . $object -> id ;
2018-03-13 11:37:40 +01:00
$sql .= " AND sa.entity IN ( " . getEntity ( 'societe' ) . " ) " ;
2018-03-13 12:07:53 +01:00
$sql .= " AND sa.site = 'stripe' AND sa.status = " . (( int ) $status );
2018-03-14 16:53:16 +01:00
$sql .= " AND key_account IS NOT NULL AND key_account <> '' " ;
2018-03-07 20:04:13 +01:00
2018-05-17 16:07:44 +02: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 );
2018-03-13 15:19:31 +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
dol_syslog ( get_class ( $this ) . " ::customerStripe found stripe customer key_account = " . $tiers );
// Force to use the correct API key
global $stripearrayofkeysbyenv ;
\Stripe\Stripe :: setApiKey ( $stripearrayofkeysbyenv [ $status ][ 'secret_key' ]);
2018-03-14 16:53:16 +01:00
try {
if ( empty ( $key )) { // If the Stripe connect account not set, we use common API usage
$customer = \Stripe\Customer :: retrieve ( " $tiers " );
} else {
$customer = \Stripe\Customer :: retrieve ( " $tiers " , array ( " stripe_account " => $key ));
}
}
catch ( Exception $e )
{
2018-03-15 00:40:29 +01:00
$this -> error = $e -> getMessage ();
2018-03-08 19:50:41 +01:00
}
2018-03-13 14:44:35 +01:00
}
2018-03-13 15:19:31 +01:00
elseif ( $createifnotlinkedtostripe )
2018-03-13 14:44:35 +01:00
{
2018-03-14 16:53:16 +01:00
$dataforcustomer = array (
" email " => $object -> email ,
" description " => $object -> name ,
2018-03-15 00:18:43 +01:00
" metadata " => array ( 'dol_id' => $object -> id , 'dol_version' => DOL_VERSION , 'dol_entity' => $conf -> entity , 'ipaddress' => ( empty ( $_SERVER [ 'REMOTE_ADDR' ]) ? '' : $_SERVER [ 'REMOTE_ADDR' ]))
2018-03-14 16:53:16 +01:00
);
2018-10-12 21:00:33 +02:00
$vatcleaned = $object -> tva_intra ? $object -> tva_intra : null ;
$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 ;
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 ));
}
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " societe_account (fk_soc, login, key_account, site, status, entity, date_creation, fk_user_creat) " ;
2018-03-14 21:07:45 +01:00
$sql .= " VALUES ( " . $object -> id . " , '', ' " . $this -> db -> escape ( $customer -> id ) . " ', 'stripe', " . $status . " , " . $conf -> entity . " , ' " . $this -> db -> idate ( dol_now ()) . " ', " . $user -> id . " ) " ;
2018-03-14 16:53:16 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$this -> error = $this -> db -> lasterror ();
}
}
catch ( Exception $e )
{
2018-03-14 21:07:45 +01:00
$this -> error = $e -> getMessage ();
2018-03-08 19:50:41 +01:00
}
}
}
2018-03-14 21:07:45 +01:00
else
{
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
}
2018-03-08 19:50:41 +01:00
2018-03-14 21:07:45 +01:00
/**
2018-03-15 00:18:43 +01:00
* Get the Stripe card of a company payment mode ( with option to create it on Stripe if not linked yet )
2018-03-14 21:07:45 +01:00
*
* @ param \Stripe\StripeCustomer $cu Object stripe customer
* @ 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 )
* @ param int $createifnotlinkedtostripe 1 = Create the stripe card and the link if the card is not yet linked to a stripe card
* @ return \Stripe\StripeCard | null Stripe Card or null if not found
*/
2018-03-29 19:44:38 +02:00
public function cardStripe ( $cu , CompanyPaymentMode $object , $stripeacc = '' , $status = 0 , $createifnotlinkedtostripe = 0 )
2018-03-14 21:07:45 +01:00
{
global $conf , $user ;
2018-03-16 00:30:03 +01:00
$card = null ;
2018-03-14 21:07:45 +01:00
2018-03-15 00:18:43 +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_....
2018-03-14 21:07:45 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_rib as sa " ;
$sql .= " WHERE sa.rowid = " . $object -> id ;
//$sql.= " AND sa.entity IN (".getEntity('societe').")";
$sql .= " AND sa.type = 'card' " ;
2018-06-01 16:23:42 +02: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 );
if ( $num )
{
$obj = $this -> db -> fetch_object ( $resql );
$cardref = $obj -> stripe_card_ref ;
2018-06-01 16:23:42 +02:00
dol_syslog ( " ************* cardref= " . $cardref );
2018-03-14 21:07:45 +01:00
if ( $cardref )
{
try {
2018-03-29 19:44:38 +02:00
if ( empty ( $stripeacc )) { // If the Stripe connect account not set, we use common API usage
2018-03-16 16:42:26 +01:00
$card = $cu -> sources -> retrieve ( $cardref );
2018-03-14 21:07:45 +01:00
} else {
2018-04-20 23:46:56 +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 );
2018-03-14 21:07:45 +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
}
}
elseif ( $createifnotlinkedtostripe )
{
2018-03-15 00:18:43 +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-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 ),
" metadata " => array ( 'dol_id' => $object -> id , 'dol_version' => DOL_VERSION , 'dol_entity' => $conf -> entity , 'ipaddress' => ( empty ( $_SERVER [ 'REMOTE_ADDR' ]) ? '' : $_SERVER [ 'REMOTE_ADDR' ]))
2018-03-14 21:07:45 +01:00
);
//$a = \Stripe\Stripe::getApiKey();
2018-03-29 19:44:38 +02:00
//var_dump($a);var_dump($stripeacc);exit;
2018-06-01 17:20:49 +02:00
dol_syslog ( " Try to create card dataforcard = " . dol_json_encode ( $dataforcard ));
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
2018-03-14 21:07:45 +01:00
$card = $cu -> sources -> create ( $dataforcard );
} else {
2018-03-29 19:44:38 +02:00
$card = $cu -> sources -> create ( $dataforcard , array ( " stripe_account " => $stripeacc ));
2018-03-14 21:07:45 +01:00
}
2018-03-15 00:18:43 +01:00
if ( $card )
{
$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 );
$sql .= " WHERE rowid = " . $object -> id ;
$sql .= " AND type = 'card' " ;
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$this -> error = $this -> db -> lasterror ();
}
}
else
2018-03-14 21:07:45 +01:00
{
2018-03-15 00:18:43 +01:00
$this -> error = 'Call to cu->source->create return empty card' ;
2018-03-14 21:07:45 +01:00
}
}
catch ( Exception $e )
{
$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
}
}
}
}
else
{
dol_print_error ( $this -> db );
}
return $card ;
}
2018-03-08 19:50:41 +01:00
/**
2018-03-13 14:44:35 +01:00
* Create charge with public / payment / newpayment . php , stripe / card . php , cronjobs or REST API
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
* @ param string $source src_xxxxx or card_xxxxx
* @ 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
*/
2018-09-11 10:59:25 +02: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 ;
2018-03-13 14:44:35 +01:00
if ( empty ( $status )) $service = 'StripeTest' ;
else $service = 'StripeLive' ;
2018-03-16 00:30:03 +01:00
$sql = " SELECT sa.key_account as key_account, sa.fk_soc, sa.entity " ;
2018-03-13 12:07:53 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " societe_account as sa " ;
2018-03-13 14:44:35 +01:00
$sql .= " WHERE sa.key_account = ' " . $this -> db -> escape ( $customer ) . " ' " ;
2018-03-13 12:07:53 +01:00
//$sql.= " AND sa.entity IN (".getEntity('societe').")";
$sql .= " AND sa.site = 'stripe' AND sa.status = " . (( int ) $status );
2018-03-07 20:04:13 +01:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
$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
2018-03-16 00:30:03 +01:00
$arrayzerounitcurrency = array ( 'BIF' , 'CLP' , 'DJF' , 'GNF' , 'JPY' , 'KMF' , 'KRW' , 'MGA' , 'PYG' , 'RWF' , 'VND' , 'VUV' , 'XAF' , 'XOF' , 'XPF' );
if ( ! in_array ( $currency , $arrayzerounitcurrency )) $stripeamount = $amount * 100 ;
else $stripeamount = $amount ;
2018-03-08 19:50:41 +01:00
$societe = new Societe ( $this -> db );
2018-03-16 00:30:03 +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 = " " ;
2018-03-08 19:50:41 +01:00
if ( $origin == order ) {
$order = new Commande ( $this -> db );
$order -> fetch ( $item );
$ref = $order -> ref ;
2018-08-06 12:29:38 +02:00
$description = " ORD= " . $ref . " .CUS= " . $societe -> id . " .PM=stripe " ;
2018-03-08 19:50:41 +01:00
} elseif ( $origin == invoice ) {
$invoice = new Facture ( $this -> db );
$invoice -> fetch ( $item );
$ref = $invoice -> ref ;
2018-08-06 12:29:38 +02: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
2018-03-08 19:50:41 +01:00
$metadata = array (
2018-03-16 00:30:03 +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 ,
'ipaddress' => ( empty ( $_SERVER [ 'REMOTE_ADDR' ]) ? '' : $_SERVER [ 'REMOTE_ADDR' ])
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' ]);
2018-03-16 00:30:03 +01:00
if ( empty ( $conf -> stripeconnect -> enabled ))
{
if ( preg_match ( '/acct_/i' , $source ))
{
$charge = \Stripe\Charge :: create ( array (
2018-03-08 19:50:41 +01:00
" amount " => " $stripeamount " ,
" currency " => " $currency " ,
2018-09-02 17:22:45 +02:00
" statement_descriptor " => dol_trunc ( dol_trunc ( dol_string_unaccent ( $mysoc -> name ), 8 , 'right' , 'UTF-8' , 1 ) . ' ' . $description , 22 , 'right' , 'UTF-8' , 1 ), // 22 chars that appears on bank receipt
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 " ,
2018-09-11 10:59:25 +02:00
" statement_descriptor " => dol_trunc ( dol_trunc ( dol_string_unaccent ( $mysoc -> name ), 8 , 'right' , 'UTF-8' , 1 ) . ' ' . $description , 22 , 'right' , 'UTF-8' , 1 ), // 22 chars that appears on bank receipt
" 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
);
if ( $societe -> email && $usethirdpartyemailforreceiptemail )
{
$paymentarray [ " receipt_email " ] = $societe -> email ;
}
$charge = \Stripe\Charge :: create ( $paymentarray , array ( " idempotency_key " => " $ref " ));
2018-03-08 19:50:41 +01:00
}
2018-03-16 00:30:03 +01:00
} 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 );
}
2018-08-04 14:30:52 +02:00
$paymentarray = array (
" amount " => " $stripeamount " ,
" currency " => " $currency " ,
2018-09-02 17:23:38 +02:00
" statement_descriptor " => dol_trunc ( dol_trunc ( dol_string_unaccent ( $mysoc -> name ), 8 , 'right' , 'UTF-8' , 1 ) . ' ' . $description , 22 , 'right' , 'UTF-8' , 1 ), // 22 chars that appears on bank receipt
2018-09-11 10:59:25 +02:00
" description " => " Stripe payment: " . $description ,
" capture " => $capture ,
2018-08-04 14:30:52 +02:00
" metadata " => $metadata ,
" source " => " $source " ,
" customer " => " $customer "
);
2018-08-12 19:42:07 +02:00
if ( $conf -> entity != $conf -> global -> STRIPECONNECT_PRINCIPAL && $fee > 0 )
2018-08-04 14:30:52 +02:00
{
$paymentarray [ " application_fee " ] = $fee ;
}
if ( $societe -> email && $usethirdpartyemailforreceiptemail )
{
$paymentarray [ " receipt_email " ] = $societe -> email ;
}
$charge = \Stripe\Charge :: create ( $paymentarray , array ( " idempotency_key " => " $ref " , " stripe_account " => " $account " ));
2018-03-08 19:50:41 +01:00
}
2018-03-16 00:30:03 +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 ;
if ( $charge -> source -> type == 'card' ) {
2018-03-16 00:43:47 +01:00
$return -> message = $charge -> source -> card -> brand . " .... " . $charge -> source -> card -> last4 ;
2018-03-08 19:50:41 +01:00
} elseif ( $charge -> source -> type == 'three_d_secure' ) {
$stripe = new Stripe ( $this -> db );
$src = \Stripe\Source :: retrieve ( " " . $charge -> source -> three_d_secure -> card . " " , array (
2018-03-08 22:26:42 +01:00
" stripe_account " => $stripe -> getStripeAccount ( $service )
2018-03-08 19:50:41 +01:00
));
2018-03-16 00:43:47 +01:00
$return -> message = $src -> card -> brand . " .... " . $src -> card -> last4 ;
2018-03-08 19:50:41 +01:00
} else {
$return -> message = $charge -> id ;
}
} 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' ];
2018-03-08 23:16:55 +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
}