19 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
20 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
21 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
22 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
23 require_once DOL_DOCUMENT_ROOT.
'/stripe/config.php';
95 $entity = $conf->entity;
98 $sql =
"SELECT tokenstring";
99 $sql .=
" FROM ".MAIN_DB_PREFIX.
"oauth_token";
100 $sql .=
" WHERE service = '".$this->db->escape($mode).
"'";
101 $sql .=
" AND entity = ".((int) $entity);
103 $sql .=
" AND fk_soc = ".((int) $fk_soc);
105 $sql .=
" AND fk_soc IS NULL";
107 $sql .=
" AND fk_user IS NULL AND fk_adherent IS NULL";
109 dol_syslog(get_class($this).
"::getStripeAccount", LOG_DEBUG);
111 $result = $this->
db->query($sql);
113 if ($this->
db->num_rows($result)) {
114 $obj = $this->
db->fetch_object($result);
115 $tokenstring = $obj->tokenstring;
117 $tmparray = json_decode($tokenstring);
118 $key = empty($tmparray->stripe_user_id) ?
'' : $tmparray->stripe_user_id;
126 dol_syslog(
"No dedicated Stripe Connect account available for entity ".$conf->entity);
140 include_once DOL_DOCUMENT_ROOT.
'/societe/class/societeaccount.class.php';
142 return $societeaccount->getCustomerAccount($id,
'stripe', $status, $site_account);
160 if (empty($object->id)) {
161 dol_syslog(
"customerStripe is called with the parameter object that is not loaded");
168 global $stripearrayofkeysbyenv;
169 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
171 $sql =
"SELECT sa.key_account as key_account, sa.entity";
172 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account as sa";
173 $sql .=
" WHERE sa.fk_soc = ".((int) $object->id);
174 $sql .=
" AND sa.entity IN (".getEntity(
'societe').
")";
175 $sql .=
" AND sa.site = 'stripe' AND sa.status = ".((int) $status);
176 $sql .=
" AND (sa.site_account IS NULL OR sa.site_account = '' OR sa.site_account = '".$this->db->escape($stripearrayofkeysbyenv[$status][
'publishable_key']).
"')";
177 $sql .=
" AND sa.key_account IS NOT NULL AND sa.key_account <> ''";
179 dol_syslog(get_class($this).
"::customerStripe search stripe customer id for thirdparty id=".$object->id, LOG_DEBUG);
184 $obj = $this->
db->fetch_object(
$resql);
185 $tiers = $obj->key_account;
187 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']);
192 $customer = \Stripe\Customer::retrieve(array(
'id'=>
"$tiers",
'expand[]'=>
'sources'));
195 $customer = \Stripe\Customer::retrieve(array(
'id'=>
"$tiers",
'expand[]'=>
'sources'), array(
"stripe_account" => $key));
199 $this->error = $e->getMessage();
201 } elseif ($createifnotlinkedtostripe) {
204 $dataforcustomer = array(
205 "email" => $object->email,
206 "description" => $object->name,
207 "metadata" => array(
'dol_id'=>$object->id,
'dol_version'=>DOL_VERSION,
'dol_entity'=>$conf->entity,
'ipaddress'=>$ipaddress)
210 $vatcleaned = $object->tva_intra ? $object->tva_intra :
null;
227 global $stripearrayofkeysbyenv;
228 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
231 $customer = \Stripe\Customer::create($dataforcustomer);
233 $customer = \Stripe\Customer::create($dataforcustomer, array(
"stripe_account" => $key));
237 if (!empty($conf->global->STRIPE_SAVE_TAX_IDS)) {
238 if (!empty($vatcleaned)) {
240 if ($object->country_code && $isineec) {
242 $customer->createTaxId($customer->id, array(
'type'=>
'eu_vat',
'value'=>$vatcleaned));
248 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"societe_account (fk_soc, login, key_account, site, site_account, status, entity, date_creation, fk_user_creat)";
249 $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).
")";
252 $this->error = $this->
db->lasterror();
255 $this->error = $e->getMessage();
275 $stripepaymentmethod =
null;
279 global $stripearrayofkeysbyenv;
280 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
282 $stripepaymentmethod = \Stripe\PaymentMethod::retrieve(
''.$paymentmethod->id.
'');
284 $stripepaymentmethod = \Stripe\PaymentMethod::retrieve(
''.$paymentmethod->id.
'', array(
"stripe_account" => $key));
287 $this->error = $e->getMessage();
290 return $stripepaymentmethod;
303 $selectedreader =
null;
307 global $stripearrayofkeysbyenv;
308 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
310 $selectedreader = \Stripe\Terminal\Reader::retrieve(
''.$reader.
'');
312 $stripepaymentmethod = \Stripe\Terminal\Reader::retrieve(
''.$reader.
'', array(
"stripe_account" => $key));
315 $this->error = $e->getMessage();
318 return $selectedreader;
346 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)
350 dol_syslog(get_class($this).
"::getPaymentIntent", LOG_INFO, 1);
354 if (empty($status)) {
355 $service =
'StripeTest';
357 $service =
'StripeLive';
360 $arrayzerounitcurrency = array(
'BIF',
'CLP',
'DJF',
'GNF',
'JPY',
'KMF',
'KRW',
'MGA',
'PYG',
'RWF',
'VND',
'VUV',
'XAF',
'XOF',
'XPF');
361 if (!in_array($currency_code, $arrayzerounitcurrency)) {
362 $stripeamount = $amount * 100;
364 $stripeamount = $amount;
367 $fee = $amount * ($conf->global->STRIPE_APPLICATION_FEE_PERCENT / 100) + $conf->global->STRIPE_APPLICATION_FEE;
368 if ($fee >= $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL && $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL > $conf->global->STRIPE_APPLICATION_FEE_MINIMAL) {
369 $fee = $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL;
370 } elseif ($fee < $conf->global->STRIPE_APPLICATION_FEE_MINIMAL) {
371 $fee = $conf->global->STRIPE_APPLICATION_FEE_MINIMAL;
373 if (!in_array($currency_code, $arrayzerounitcurrency)) {
374 $stripefee = round($fee * 100);
376 $stripefee = round($fee);
379 $paymentintent =
null;
388 $sql =
"SELECT pi.ext_payment_id, pi.entity, pi.fk_facture, pi.sourcetype, pi.ext_payment_site";
389 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_demande as pi";
390 $sql .=
" WHERE pi.fk_facture = ".((int) $object->id);
391 $sql .=
" AND pi.sourcetype = '".$this->db->escape($object->element).
"'";
392 $sql .=
" AND pi.entity IN (".getEntity(
'societe').
")";
393 $sql .=
" AND pi.ext_payment_site = '".$this->db->escape($service).
"'";
395 dol_syslog(get_class($this).
"::getPaymentIntent search stripe payment intent for object id = ".$object->id, LOG_DEBUG);
400 $obj = $this->
db->fetch_object(
$resql);
401 $intent = $obj->ext_payment_id;
403 dol_syslog(get_class($this).
"::getPaymentIntent found existing payment intent record");
406 global $stripearrayofkeysbyenv;
407 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
411 $paymentintent = \Stripe\PaymentIntent::retrieve($intent);
413 $paymentintent = \Stripe\PaymentIntent::retrieve($intent, array(
"stripe_account" => $key));
417 $this->error = $e->getMessage();
423 if (empty($paymentintent)) {
426 $metadata = array(
'dol_version'=>DOL_VERSION,
'dol_entity'=>$conf->entity,
'ipaddress'=>$ipaddress);
427 if (is_object($object)) {
428 $metadata[
'dol_type'] = $object->element;
429 $metadata[
'dol_id'] = $object->id;
430 if (is_object($object->thirdparty) && $object->thirdparty->id > 0) {
431 $metadata[
'dol_thirdparty_id'] = $object->thirdparty->id;
436 $paymentmethodtypes = array(
"card");
437 $descriptor =
dol_trunc($tag, 10,
'right',
'UTF-8', 1);
439 $paymentmethodtypes[] =
"sepa_debit";
443 $paymentmethodtypes[] =
"klarna";
446 $paymentmethodtypes[] =
"bancontact";
449 $paymentmethodtypes[] =
"ideal";
452 $paymentmethodtypes[] =
"giropay";
455 $paymentmethodtypes[] =
"sofort";
458 $paymentmethodtypes = array(
"card_present");
461 $dataforintent = array(
462 "confirm" => $confirmnow,
463 "confirmation_method" => $mode,
464 "amount" => $stripeamount,
465 "currency" => $currency_code,
466 "payment_method_types" => $paymentmethodtypes,
467 "description" => $description,
469 "setup_future_usage" =>
"on_session",
470 "metadata" => $metadata
473 $dataforintent[
"statement_descriptor_suffix"] = $descriptor;
474 $dataforintent[
"statement_descriptor"] = $descriptor;
476 if (!is_null($customer)) {
477 $dataforintent[
"customer"] = $customer;
483 unset($dataforintent[
'setup_future_usage']);
487 $dataforintent[
"off_session"] =
true;
490 unset($dataforintent[
'setup_future_usage']);
493 unset($dataforintent[
'setup_future_usage']);
496 unset($dataforintent[
'setup_future_usage']);
497 $dataforintent[
"capture_method"] =
"manual";
498 $dataforintent[
"confirmation_method"] =
"manual";
500 if (!is_null($payment_method)) {
501 $dataforintent[
"payment_method"] = $payment_method;
502 $description .=
' - '.$payment_method;
505 if ($conf->entity !=
getDolGlobalInt(
'STRIPECONNECT_PRINCIPAL') && $stripefee > 0) {
506 $dataforintent[
"application_fee_amount"] = $stripefee;
508 if ($usethirdpartyemailforreceiptemail && is_object($object) && $object->thirdparty->email) {
509 $dataforintent[
"receipt_email"] = $object->thirdparty->email;
514 global $stripearrayofkeysbyenv;
515 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
517 $arrayofoptions = array();
518 if (empty($noidempotency_key)) {
519 $arrayofoptions[
"idempotency_key"] = $description;
523 $arrayofoptions[
"stripe_account"] = $key;
526 dol_syslog(
"dataforintent to create paymentintent = ".var_export($dataforintent,
true));
528 $paymentintent = \Stripe\PaymentIntent::create($dataforintent, $arrayofoptions);
531 if (is_object($object)) {
532 $paymentintentalreadyexists = 0;
534 $sql =
"SELECT pi.rowid";
535 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_demande as pi";
536 $sql .=
" WHERE pi.entity IN (".getEntity(
'societe').
")";
537 $sql .=
" AND pi.ext_payment_site = '".$this->db->escape($service).
"'";
538 $sql .=
" AND pi.ext_payment_id = '".$this->db->escape($paymentintent->id).
"'";
540 dol_syslog(get_class($this).
"::getPaymentIntent search if payment intent already in prelevement_demande", LOG_DEBUG);
545 $obj = $this->
db->fetch_object(
$resql);
547 $paymentintentalreadyexists++;
555 if (!$paymentintentalreadyexists) {
557 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"prelevement_demande (date_demande, fk_user_demande, ext_payment_id, fk_facture, sourcetype, entity, ext_payment_site, amount)";
558 $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).
")";
562 $this->error = $this->
db->lasterror();
563 dol_syslog(get_class($this).
"::PaymentIntent failed to insert paymentintent with id=".$paymentintent->id.
" into database.", LOG_ERR);
567 $_SESSION[
"stripe_payment_intent"] = $paymentintent;
569 }
catch (
Stripe\Error\Card $e) {
571 $this->error = $e->getMessage();
572 $this->
code = $e->getStripeCode();
573 $this->declinecode = $e->getDeclineCode();
582 $this->error = $e->getMessage();
584 $this->declinecode =
'';
588 dol_syslog(get_class($this).
"::getPaymentIntent return error=".$error.
" this->error=".$this->error, LOG_INFO, -1);
591 return $paymentintent;
615 public function getSetupIntent($description, $object, $customer, $key, $status, $usethirdpartyemailforreceiptemail = 0, $confirmnow =
false)
619 dol_syslog(
"getSetupIntent description=".$description.
' confirmnow='.$confirmnow, LOG_INFO, 1);
623 if (empty($status)) {
624 $service =
'StripeTest';
626 $service =
'StripeLive';
631 if (empty($setupintent)) {
633 $metadata = array(
'dol_version'=>DOL_VERSION,
'dol_entity'=>$conf->entity,
'ipaddress'=>$ipaddress);
634 if (is_object($object)) {
635 $metadata[
'dol_type'] = $object->element;
636 $metadata[
'dol_id'] = $object->id;
637 if (is_object($object->thirdparty) && $object->thirdparty->id > 0) {
638 $metadata[
'dol_thirdparty_id'] = $object->thirdparty->id;
643 $paymentmethodtypes = array(
"card");
644 if (!empty($conf->global->STRIPE_SEPA_DIRECT_DEBIT)) {
645 $paymentmethodtypes[] =
"sepa_debit";
647 if (!empty($conf->global->STRIPE_BANCONTACT)) {
648 $paymentmethodtypes[] =
"bancontact";
650 if (!empty($conf->global->STRIPE_IDEAL)) {
651 $paymentmethodtypes[] =
"ideal";
654 if (!empty($conf->global->STRIPE_SOFORT)) {
655 $paymentmethodtypes[] =
"sofort";
658 $dataforintent = array(
659 "confirm" => $confirmnow,
660 "payment_method_types" => $paymentmethodtypes,
661 "usage" =>
"off_session",
662 "metadata" => $metadata
664 if (!is_null($customer)) {
665 $dataforintent[
"customer"] = $customer;
667 if (!is_null($description)) {
668 $dataforintent[
"description"] = $description;
674 if ($usethirdpartyemailforreceiptemail && is_object($object) && $object->thirdparty->email) {
675 $dataforintent[
"receipt_email"] = $object->thirdparty->email;
680 global $stripearrayofkeysbyenv;
681 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
683 dol_syslog(
"getSetupIntent ".$stripearrayofkeysbyenv[$status][
'publishable_key'], LOG_DEBUG);
688 $setupintent = \Stripe\SetupIntent::create($dataforintent, array());
691 $setupintent = \Stripe\SetupIntent::create($dataforintent, array(
"stripe_account" => $key));
744 $this->error = $e->getMessage();
749 dol_syslog(
"getSetupIntent ".(is_object($setupintent) ? $setupintent->id :
''), LOG_INFO, -1);
752 dol_syslog(
"getSetupIntent return error=".$error, LOG_INFO, -1);
770 global $conf, $user, $langs;
774 $sql =
"SELECT sa.stripe_card_ref, sa.proprio, sa.exp_date_month, sa.exp_date_year, sa.number, sa.cvn";
775 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib as sa";
776 $sql .=
" WHERE sa.rowid = ".((int) $object->id);
777 $sql .=
" AND sa.type = 'card'";
779 dol_syslog(get_class($this).
"::cardStripe search stripe card id for paymentmode id=".$object->id.
", stripeacc=".$stripeacc.
", status=".$status.
", createifnotlinkedtostripe=".$createifnotlinkedtostripe, LOG_DEBUG);
784 $obj = $this->
db->fetch_object(
$resql);
785 $cardref = $obj->stripe_card_ref;
786 dol_syslog(get_class($this).
"::cardStripe cardref=".$cardref);
789 if (empty($stripeacc)) {
790 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
791 $card = $cu->sources->retrieve($cardref);
793 $card = \Stripe\PaymentMethod::retrieve($cardref);
796 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
798 $card = $cu->sources->retrieve($cardref);
801 $card = \Stripe\PaymentMethod::retrieve($cardref);
805 $this->error = $e->getMessage();
808 } elseif ($createifnotlinkedtostripe) {
809 $exp_date_month = $obj->exp_date_month;
810 $exp_date_year = $obj->exp_date_year;
811 $number = $obj->number;
813 $cardholdername = $obj->proprio;
817 $dataforcard = array(
818 "source" => array(
'object'=>
'card',
'exp_month'=>$exp_date_month,
'exp_year'=>$exp_date_year,
'number'=>$number,
'cvc'=>$cvc,
'name'=>$cardholdername),
819 "metadata" => array(
'dol_id'=>$object->id,
'dol_version'=>DOL_VERSION,
'dol_entity'=>$conf->entity,
'ipaddress'=>$ipaddress)
826 if (empty($stripeacc)) {
827 if (empty($conf->global->STRIPE_USE_INTENT_WITH_AUTOMATIC_CONFIRMATION)) {
828 dol_syslog(
"Try to create card with dataforcard = ".json_encode($dataforcard));
829 $card = $cu->sources->create($dataforcard);
831 $this->error =
'Creation of card on Stripe has failed';
835 if (!empty($stripeacc)) {
836 $connect = $stripeacc.
'/';
838 $url =
'https://dashboard.stripe.com/'.$connect.
'test/customers/'.$cu->id;
840 $url =
'https://dashboard.stripe.com/'.$connect.
'customers/'.$cu->id;
842 $urtoswitchonstripe =
' <a href="'.$url.
'" target="_stripe">'.
img_picto($langs->trans(
'ShowInStripe'),
'globe').
'</a>';
845 $this->error = $langs->trans(
'CreationOfPaymentModeMustBeDoneFromStripeInterface', $urtoswitchonstripe);
848 if (empty($conf->global->STRIPE_USE_INTENT_WITH_AUTOMATIC_CONFIRMATION)) {
849 dol_syslog(
"Try to create card with dataforcard = ".json_encode($dataforcard));
850 $card = $cu->sources->create($dataforcard, array(
"stripe_account" => $stripeacc));
852 $this->error =
'Creation of card on Stripe has failed';
856 if (!empty($stripeacc)) {
857 $connect = $stripeacc.
'/';
859 $url =
'https://dashboard.stripe.com/'.$connect.
'test/customers/'.$cu->id;
861 $url =
'https://dashboard.stripe.com/'.$connect.
'customers/'.$cu->id;
863 $urtoswitchonstripe =
' <a href="'.$url.
'" target="_stripe">'.
img_picto($langs->trans(
'ShowInStripe'),
'globe').
'</a>';
866 $this->error = $langs->trans(
'CreationOfPaymentModeMustBeDoneFromStripeInterface', $urtoswitchonstripe);
871 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"societe_rib";
872 $sql .=
" SET stripe_card_ref = '".$this->db->escape($card->id).
"', card_type = '".$this->
db->escape($card->brand).
"',";
873 $sql .=
" country_code = '".$this->db->escape($card->country).
"',";
874 $sql .=
" approved = ".($card->cvc_check ==
'pass' ? 1 : 0);
875 $sql .=
" WHERE rowid = ".((int) $object->id);
876 $sql .=
" AND type = 'card'";
879 $this->error = $this->
db->lasterror();
883 $this->error = $e->getMessage();
908 global $conf, $user, $langs;
911 $sql =
"SELECT sa.stripe_card_ref, sa.proprio, sa.iban_prefix";
912 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib as sa";
913 $sql .=
" WHERE sa.rowid = ".((int) $object->id);
914 $sql .=
" AND sa.type = 'ban'";
917 $soc->fetch($object->fk_soc);
919 dol_syslog(get_class($this).
"::sepaStripe search stripe ban id for paymentmode id=".$object->id.
", stripeacc=".$stripeacc.
", status=".$status.
", createifnotlinkedtostripe=".$createifnotlinkedtostripe, LOG_DEBUG);
924 $obj = $this->
db->fetch_object(
$resql);
925 $cardref = $obj->stripe_card_ref;
926 dol_syslog(get_class($this).
"::sepaStripe cardref=".$cardref);
929 if (empty($stripeacc)) {
930 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
931 $sepa = $cu->sources->retrieve($cardref);
933 $sepa = \Stripe\PaymentMethod::retrieve($cardref);
936 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
938 $sepa = $cu->sources->retrieve($cardref);
941 $sepa = \Stripe\PaymentMethod::retrieve($cardref);
945 $this->error = $e->getMessage();
948 } elseif ($createifnotlinkedtostripe) {
949 $iban = $obj->iban_prefix;
952 $dataforcard = array(
953 'type'=>
'sepa_debit',
954 "sepa_debit" => array(
'iban' => $iban),
956 'usage' =>
'reusable',
958 'name' => $soc->name,
960 "metadata" => array(
'dol_id'=>$object->id,
'dol_version'=>DOL_VERSION,
'dol_entity'=>$conf->entity,
'ipaddress'=>$ipaddress)
968 $service =
'StripeTest';
970 if (!empty($conf->global->STRIPE_LIVE) && !
GETPOST(
'forcesandbox',
'alpha')) {
971 $service =
'StripeLive';
975 global $stripearrayofkeysbyenv;
976 $stripeacc = $stripearrayofkeysbyenv[$servicestatus][
'secret_key'];
978 dol_syslog(
"Try to create sepa_debit with data = ".json_encode($dataforcard));
979 $s = new \Stripe\StripeClient($stripeacc);
980 $sepa = $s->sources->create($dataforcard);
982 $this->error =
'Creation of sepa_debit on Stripe has failed';
985 $cs = $cu->createSource($cu->id, array(
'source' => $sepa->id));
987 $this->error =
'Link SEPA <-> Customer failed';
992 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"societe_rib";
993 $sql .=
" SET stripe_card_ref = '".$this->db->escape($sepa->id).
"', card_type = 'sepa_debit',";
994 $sql .=
" stripe_account= '" . $this->
db->escape($cu->id .
"@" . $stripeacc) .
"'";
995 $sql .=
" WHERE rowid = ".((int) $object->id);
996 $sql .=
" AND type = 'ban'";
999 $this->error = $this->
db->lasterror();
1004 $this->error = $e->getMessage();
1032 public function createPaymentStripe($amount, $currency, $origin, $item, $source, $customer, $account, $status = 0, $usethirdpartyemailforreceiptemail = 0, $capture =
true)
1038 if (empty($status)) {
1039 $service =
'StripeTest';
1041 $service =
'StripeLive';
1044 $sql =
"SELECT sa.key_account as key_account, sa.fk_soc, sa.entity";
1045 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account as sa";
1046 $sql .=
" WHERE sa.key_account = '".$this->db->escape($customer).
"'";
1048 $sql .=
" AND sa.site = 'stripe' AND sa.status = ".((int) $status);
1050 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1051 $result = $this->
db->query($sql);
1053 if ($this->
db->num_rows($result)) {
1054 $obj = $this->
db->fetch_object($result);
1055 $key = $obj->fk_soc;
1063 $arrayzerounitcurrency = array(
'BIF',
'CLP',
'DJF',
'GNF',
'JPY',
'KMF',
'KRW',
'MGA',
'PYG',
'RWF',
'VND',
'VUV',
'XAF',
'XOF',
'XPF');
1064 if (!in_array($currency, $arrayzerounitcurrency)) {
1065 $stripeamount = $amount * 100;
1067 $stripeamount = $amount;
1072 $societe->fetch($key);
1077 if ($origin ==
'order') {
1079 $order->fetch($item);
1081 $description =
"ORD=".$ref.
".CUS=".$societe->id.
".PM=stripe";
1082 } elseif ($origin ==
'invoice') {
1084 $invoice->fetch($item);
1085 $ref = $invoice->ref;
1086 $description =
"INV=".$ref.
".CUS=".$societe->id.
".PM=stripe";
1092 "dol_id" =>
"".$item.
"",
1093 "dol_type" =>
"".$origin.
"",
1094 "dol_thirdparty_id" =>
"".$societe->id.
"",
1095 'dol_thirdparty_name' => $societe->name,
1096 'dol_version'=>DOL_VERSION,
1097 'dol_entity'=>$conf->entity,
1098 'ipaddress'=>$ipaddress
1103 global $stripearrayofkeysbyenv;
1104 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
1106 if (empty($conf->stripeconnect->enabled)) {
1107 if (preg_match(
'/pm_/i', $source)) {
1108 $stripecard = $source;
1109 $amountstripe = $stripeamount;
1112 $amounttopay = $amount;
1113 $servicestatus = $status;
1115 dol_syslog(
"* createPaymentStripe get stripeacc", LOG_DEBUG);
1116 $stripeacc = $stripe->getStripeAccount($service);
1118 dol_syslog(
"* createPaymentStripe Create payment for customer ".$customer->id.
" on source card ".$stripecard->id.
", amounttopay=".$amounttopay.
", amountstripe=".$amountstripe.
", FULLTAG=".$FULLTAG, LOG_DEBUG);
1121 $paymentintent = $stripe->getPaymentIntent($amounttopay, $currency, $FULLTAG, $description, $invoice, $customer->id, $stripeacc, $servicestatus, 0,
'automatic',
true, $stripecard->id, 1);
1123 $charge =
new stdClass();
1124 if ($paymentintent->status ==
'succeeded') {
1125 $charge->status =
'ok';
1127 $charge->status =
'failed';
1128 $charge->failure_code = $stripe->code;
1129 $charge->failure_message = $stripe->error;
1130 $charge->failure_declinecode = $stripe->declinecode;
1131 $stripefailurecode = $stripe->code;
1132 $stripefailuremessage = $stripe->error;
1133 $stripefailuredeclinecode = $stripe->declinecode;
1135 } elseif (preg_match(
'/acct_/i', $source)) {
1136 $charge = \Stripe\Charge::create(array(
1137 "amount" =>
"$stripeamount",
1138 "currency" =>
"$currency",
1139 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
1140 "description" =>
"Stripe payment: ".$description,
1141 "capture" => $capture,
1142 "metadata" => $metadata,
1143 "source" =>
"$source"
1146 $paymentarray = array(
1147 "amount" =>
"$stripeamount",
1148 "currency" =>
"$currency",
1149 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
1150 "description" =>
"Stripe payment: ".$description,
1151 "capture" => $capture,
1152 "metadata" => $metadata,
1153 "source" =>
"$source",
1154 "customer" =>
"$customer"
1157 if ($societe->email && $usethirdpartyemailforreceiptemail) {
1158 $paymentarray[
"receipt_email"] = $societe->email;
1161 $charge = \Stripe\Charge::create($paymentarray, array(
"idempotency_key" =>
"$description"));
1165 $fee = $amount * ($conf->global->STRIPE_APPLICATION_FEE_PERCENT / 100) + $conf->global->STRIPE_APPLICATION_FEE;
1166 if ($fee >= $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL && $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL > $conf->global->STRIPE_APPLICATION_FEE_MINIMAL) {
1167 $fee = $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL;
1168 } elseif ($fee < $conf->global->STRIPE_APPLICATION_FEE_MINIMAL) {
1169 $fee = $conf->global->STRIPE_APPLICATION_FEE_MINIMAL;
1172 if (!in_array($currency, $arrayzerounitcurrency)) {
1173 $stripefee = round($fee * 100);
1175 $stripefee = round($fee);
1178 $paymentarray = array(
1179 "amount" =>
"$stripeamount",
1180 "currency" =>
"$currency",
1181 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
1182 "description" =>
"Stripe payment: ".$description,
1183 "capture" => $capture,
1184 "metadata" => $metadata,
1185 "source" =>
"$source",
1186 "customer" =>
"$customer"
1188 if ($conf->entity != $conf->global->STRIPECONNECT_PRINCIPAL && $stripefee > 0) {
1189 $paymentarray[
"application_fee_amount"] = $stripefee;
1191 if ($societe->email && $usethirdpartyemailforreceiptemail) {
1192 $paymentarray[
"receipt_email"] = $societe->email;
1195 if (preg_match(
'/pm_/i', $source)) {
1196 $stripecard = $source;
1197 $amountstripe = $stripeamount;
1200 $amounttopay = $amount;
1201 $servicestatus = $status;
1203 dol_syslog(
"* createPaymentStripe get stripeacc", LOG_DEBUG);
1204 $stripeacc = $stripe->getStripeAccount($service);
1206 dol_syslog(
"* createPaymentStripe Create payment on card ".$stripecard->id.
", amounttopay=".$amounttopay.
", amountstripe=".$amountstripe.
", FULLTAG=".$FULLTAG, LOG_DEBUG);
1209 $paymentintent = $stripe->getPaymentIntent($amounttopay, $currency, $FULLTAG, $description, $invoice, $customer->id, $stripeacc, $servicestatus, 0,
'automatic',
true, $stripecard->id, 1);
1211 $charge =
new stdClass();
1212 if ($paymentintent->status ==
'succeeded') {
1213 $charge->status =
'ok';
1214 $charge->id = $paymentintent->id;
1216 $charge->status =
'failed';
1217 $charge->failure_code = $stripe->code;
1218 $charge->failure_message = $stripe->error;
1219 $charge->failure_declinecode = $stripe->declinecode;
1222 $charge = \Stripe\Charge::create($paymentarray, array(
"idempotency_key" =>
"$description",
"stripe_account" =>
"$account"));
1225 if (isset($charge->id)) {
1228 $return->statut =
'success';
1229 $return->id = $charge->id;
1231 if (preg_match(
'/pm_/i', $source)) {
1232 $return->message =
'Payment retrieved by card status = '.$charge->status;
1234 if ($charge->source->type ==
'card') {
1235 $return->message = $charge->source->card->brand.
" ....".$charge->source->card->last4;
1236 } elseif ($charge->source->type ==
'three_d_secure') {
1238 $src = \Stripe\Source::retrieve(
"".$charge->source->three_d_secure->card.
"", array(
1239 "stripe_account" => $stripe->getStripeAccount($service)
1241 $return->message = $src->card->brand.
" ....".$src->card->last4;
1243 $return->message = $charge->id;
1246 }
catch (\
Stripe\Error\Card $e) {
1247 include DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1249 $body = $e->getJsonBody();
1250 $err = $body[
'error'];
1252 $return->statut =
'error';
1253 $return->id = $err[
'charge'];
1254 $return->type = $err[
'type'];
1255 $return->code = $err[
'code'];
1256 $return->message = $err[
'message'];
1257 $body =
"Error: <br>".$return->id.
" ".$return->message.
" ";
1258 $subject =
'[Alert] Payment error using Stripe';
1259 $cmailfile =
new CMailFile($subject, $conf->global->ONLINE_PAYMENT_SENDEMAIL, $conf->global->MAIN_INFO_SOCIETE_MAIL, $body);
1260 $cmailfile->sendfile();
1263 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1264 }
catch (\
Stripe\Error\RateLimit $e) {
1267 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1268 }
catch (\
Stripe\Error\InvalidRequest $e) {
1271 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1272 }
catch (\
Stripe\Error\Authentication $e) {
1276 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1277 }
catch (\
Stripe\Error\ApiConnection $e) {
1280 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1281 }
catch (\
Stripe\Error\Base $e) {
1285 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1289 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Class to manage customers orders.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Class for CompanyPaymentMode.
Class to manage invoices.
Class for SocieteAccount.
Class to manage third parties objects (customers, suppliers, prospects...)
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)
Get the Stripe payment intent.
getSetupIntent($description, $object, $customer, $key, $status, $usethirdpartyemailforreceiptemail=0, $confirmnow=false)
Get the Stripe payment intent.
getPaymentMethodStripe($paymentmethod, $key='', $status=0)
Get the Stripe payment method Object from its ID.
sepaStripe($cu, CompanyPaymentMode $object, $stripeacc='', $status=0, $createifnotlinkedtostripe=0)
Get the Stripe SEPA of a company payment mode.
getStripeCustomerAccount($id, $status=0, $site_account='')
getStripeCustomerAccount
cardStripe($cu, CompanyPaymentMode $object, $stripeacc='', $status=0, $createifnotlinkedtostripe=0)
Get the Stripe card of a company payment mode (option to create it on Stripe if not linked yet is no ...
createPaymentStripe($amount, $currency, $origin, $item, $source, $customer, $account, $status=0, $usethirdpartyemailforreceiptemail=0, $capture=true)
Create charge.
__construct($db)
Constructor.
getStripeAccount($mode='StripeTest', $fk_soc=0, $entity=-1)
Return main company OAuth Connect stripe account.
customerStripe(Societe $object, $key='', $status=0, $createifnotlinkedtostripe=0)
Get the Stripe customer of a thirdparty (with option to create it in Stripe if not linked yet).
getSelectedReader($reader, $key='', $status=0)
Get the Stripe reader Object from its ID.
isInEEC($object)
Return if a country of an object is inside the EEC (European Economic Community)
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
getUserRemoteIP()
Return the IP of remote user.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
if(!defined( 'CSRFCHECK_WITH_TOKEN'))
div float
Buy price without taxes.
$conf db
API class for accounts.
print *****$script_file(".$version.") pid code
! Closing after partial payment: discount_vat, badcustomer or badsupplier, bankcharge,...