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 = $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;
318 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)
322 dol_syslog(get_class($this).
"::getPaymentIntent", LOG_INFO, 1);
326 if (empty($status)) {
327 $service =
'StripeTest';
329 $service =
'StripeLive';
332 $arrayzerounitcurrency = array(
'BIF',
'CLP',
'DJF',
'GNF',
'JPY',
'KMF',
'KRW',
'MGA',
'PYG',
'RWF',
'VND',
'VUV',
'XAF',
'XOF',
'XPF');
333 if (!in_array($currency_code, $arrayzerounitcurrency)) {
334 $stripeamount = $amount * 100;
336 $stripeamount = $amount;
339 $fee = $amount * ($conf->global->STRIPE_APPLICATION_FEE_PERCENT / 100) + $conf->global->STRIPE_APPLICATION_FEE;
340 if ($fee >= $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL && $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL > $conf->global->STRIPE_APPLICATION_FEE_MINIMAL) {
341 $fee = $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL;
342 } elseif ($fee < $conf->global->STRIPE_APPLICATION_FEE_MINIMAL) {
343 $fee = $conf->global->STRIPE_APPLICATION_FEE_MINIMAL;
345 if (!in_array($currency_code, $arrayzerounitcurrency)) {
346 $stripefee = round($fee * 100);
348 $stripefee = round($fee);
351 $paymentintent =
null;
353 if (is_object($object) && !empty($conf->global->STRIPE_REUSE_EXISTING_INTENT_IF_FOUND)) {
360 $sql =
"SELECT pi.ext_payment_id, pi.entity, pi.fk_facture, pi.sourcetype, pi.ext_payment_site";
361 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_facture_demande as pi";
362 $sql .=
" WHERE pi.fk_facture = ".((int) $object->id);
363 $sql .=
" AND pi.sourcetype = '".$this->db->escape($object->element).
"'";
364 $sql .=
" AND pi.entity IN (".getEntity(
'societe').
")";
365 $sql .=
" AND pi.ext_payment_site = '".$this->db->escape($service).
"'";
367 dol_syslog(get_class($this).
"::getPaymentIntent search stripe payment intent for object id = ".$object->id, LOG_DEBUG);
372 $obj = $this->
db->fetch_object(
$resql);
373 $intent = $obj->ext_payment_id;
375 dol_syslog(get_class($this).
"::getPaymentIntent found existing payment intent record");
378 global $stripearrayofkeysbyenv;
379 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
383 $paymentintent = \Stripe\PaymentIntent::retrieve($intent);
385 $paymentintent = \Stripe\PaymentIntent::retrieve($intent, array(
"stripe_account" => $key));
389 $this->error = $e->getMessage();
395 if (empty($paymentintent)) {
398 $metadata = array(
'dol_version'=>DOL_VERSION,
'dol_entity'=>$conf->entity,
'ipaddress'=>$ipaddress);
399 if (is_object($object)) {
400 $metadata[
'dol_type'] = $object->element;
401 $metadata[
'dol_id'] = $object->id;
402 if (is_object($object->thirdparty) && $object->thirdparty->id > 0) {
403 $metadata[
'dol_thirdparty_id'] = $object->thirdparty->id;
408 $paymentmethodtypes = array(
"card");
409 if (!empty($conf->global->STRIPE_SEPA_DIRECT_DEBIT)) {
410 $paymentmethodtypes[] =
"sepa_debit";
412 if (!empty($conf->global->STRIPE_KLARNA)) {
413 $paymentmethodtypes[] =
"klarna";
415 if (!empty($conf->global->STRIPE_BANCONTACT)) {
416 $paymentmethodtypes[] =
"bancontact";
418 if (!empty($conf->global->STRIPE_IDEAL)) {
419 $paymentmethodtypes[] =
"ideal";
421 if (!empty($conf->global->STRIPE_GIROPAY)) {
422 $paymentmethodtypes[] =
"giropay";
424 if (!empty($conf->global->STRIPE_SOFORT)) {
425 $paymentmethodtypes[] =
"sofort";
428 $dataforintent = array(
429 "confirm" => $confirmnow,
430 "confirmation_method" => $mode,
431 "amount" => $stripeamount,
432 "currency" => $currency_code,
433 "payment_method_types" => $paymentmethodtypes,
434 "description" => $description,
435 "statement_descriptor_suffix" =>
dol_trunc($tag, 10,
'right',
'UTF-8', 1),
437 "setup_future_usage" =>
"on_session",
438 "metadata" => $metadata
440 if (!is_null($customer)) {
441 $dataforintent[
"customer"] = $customer;
447 unset($dataforintent[
'setup_future_usage']);
451 $dataforintent[
"off_session"] =
true;
453 if (!empty($conf->global->STRIPE_GIROPAY)) {
454 unset($dataforintent[
'setup_future_usage']);
456 if (!empty($conf->global->STRIPE_KLARNA)) {
457 unset($dataforintent[
'setup_future_usage']);
459 if (!is_null($payment_method)) {
460 $dataforintent[
"payment_method"] = $payment_method;
461 $description .=
' - '.$payment_method;
464 if ($conf->entity != $conf->global->STRIPECONNECT_PRINCIPAL && $stripefee > 0) {
465 $dataforintent[
"application_fee_amount"] = $stripefee;
467 if ($usethirdpartyemailforreceiptemail && is_object($object) && $object->thirdparty->email) {
468 $dataforintent[
"receipt_email"] = $object->thirdparty->email;
473 global $stripearrayofkeysbyenv;
474 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
476 $arrayofoptions = array();
477 if (empty($noidempotency_key)) {
478 $arrayofoptions[
"idempotency_key"] = $description;
482 $arrayofoptions[
"stripe_account"] = $key;
485 dol_syslog(
"dataforintent to create paymentintent = ".var_export($dataforintent,
true));
487 $paymentintent = \Stripe\PaymentIntent::create($dataforintent, $arrayofoptions);
490 if (is_object($object)) {
491 $paymentintentalreadyexists = 0;
493 $sql =
"SELECT pi.rowid";
494 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_facture_demande as pi";
495 $sql .=
" WHERE pi.entity IN (".getEntity(
'societe').
")";
496 $sql .=
" AND pi.ext_payment_site = '".$this->db->escape($service).
"'";
497 $sql .=
" AND pi.ext_payment_id = '".$this->db->escape($paymentintent->id).
"'";
499 dol_syslog(get_class($this).
"::getPaymentIntent search if payment intent already in prelevement_facture_demande", LOG_DEBUG);
504 $obj = $this->
db->fetch_object(
$resql);
506 $paymentintentalreadyexists++;
514 if (!$paymentintentalreadyexists) {
516 $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)";
517 $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).
")";
521 $this->error = $this->
db->lasterror();
522 dol_syslog(get_class($this).
"::PaymentIntent failed to insert paymentintent with id=".$paymentintent->id.
" into database.", LOG_ERR);
526 $_SESSION[
"stripe_payment_intent"] = $paymentintent;
528 }
catch (
Stripe\Error\Card $e) {
530 $this->error = $e->getMessage();
531 $this->
code = $e->getStripeCode();
532 $this->declinecode = $e->getDeclineCode();
541 $this->error = $e->getMessage();
543 $this->declinecode =
'';
547 dol_syslog(get_class($this).
"::getPaymentIntent return error=".$error.
" this->error=".$this->error, LOG_INFO, -1);
550 return $paymentintent;
575 public function getSetupIntent($description, $object, $customer, $key, $status, $usethirdpartyemailforreceiptemail = 0, $confirmnow =
false)
579 dol_syslog(
"getSetupIntent description=".$description.
' confirmnow='.$confirmnow, LOG_INFO, 1);
583 if (empty($status)) {
584 $service =
'StripeTest';
586 $service =
'StripeLive';
591 if (empty($setupintent)) {
593 $metadata = array(
'dol_version'=>DOL_VERSION,
'dol_entity'=>$conf->entity,
'ipaddress'=>$ipaddress);
594 if (is_object($object)) {
595 $metadata[
'dol_type'] = $object->element;
596 $metadata[
'dol_id'] = $object->id;
597 if (is_object($object->thirdparty) && $object->thirdparty->id > 0) {
598 $metadata[
'dol_thirdparty_id'] = $object->thirdparty->id;
603 $paymentmethodtypes = array(
"card");
604 if (!empty($conf->global->STRIPE_SEPA_DIRECT_DEBIT)) {
605 $paymentmethodtypes[] =
"sepa_debit";
607 if (!empty($conf->global->STRIPE_BANCONTACT)) {
608 $paymentmethodtypes[] =
"bancontact";
610 if (!empty($conf->global->STRIPE_IDEAL)) {
611 $paymentmethodtypes[] =
"ideal";
614 if (!empty($conf->global->STRIPE_SOFORT)) {
615 $paymentmethodtypes[] =
"sofort";
618 $dataforintent = array(
619 "confirm" => $confirmnow,
620 "payment_method_types" => $paymentmethodtypes,
621 "usage" =>
"off_session",
622 "metadata" => $metadata
624 if (!is_null($customer)) {
625 $dataforintent[
"customer"] = $customer;
627 if (!is_null($description)) {
628 $dataforintent[
"description"] = $description;
634 if ($usethirdpartyemailforreceiptemail && is_object($object) && $object->thirdparty->email) {
635 $dataforintent[
"receipt_email"] = $object->thirdparty->email;
640 global $stripearrayofkeysbyenv;
641 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
643 dol_syslog(
"getSetupIntent ".$stripearrayofkeysbyenv[$status][
'publishable_key'], LOG_DEBUG);
648 $setupintent = \Stripe\SetupIntent::create($dataforintent, array());
651 $setupintent = \Stripe\SetupIntent::create($dataforintent, array(
"stripe_account" => $key));
704 $this->error = $e->getMessage();
709 dol_syslog(
"getSetupIntent ".(is_object($setupintent) ? $setupintent->id :
''), LOG_INFO, -1);
712 dol_syslog(
"getSetupIntent return error=".$error, LOG_INFO, -1);
730 global $conf, $user, $langs;
734 $sql =
"SELECT sa.stripe_card_ref, sa.proprio, sa.exp_date_month, sa.exp_date_year, sa.number, sa.cvn";
735 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib as sa";
736 $sql .=
" WHERE sa.rowid = ".((int) $object->id);
737 $sql .=
" AND sa.type = 'card'";
739 dol_syslog(get_class($this).
"::fetch search stripe card id for paymentmode id=".$object->id.
", stripeacc=".$stripeacc.
", status=".$status.
", createifnotlinkedtostripe=".$createifnotlinkedtostripe, LOG_DEBUG);
744 $obj = $this->
db->fetch_object(
$resql);
745 $cardref = $obj->stripe_card_ref;
746 dol_syslog(get_class($this).
"::cardStripe cardref=".$cardref);
749 if (empty($stripeacc)) {
750 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
751 $card = $cu->sources->retrieve($cardref);
753 $card = \Stripe\PaymentMethod::retrieve($cardref);
756 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
758 $card = $cu->sources->retrieve($cardref);
761 $card = \Stripe\PaymentMethod::retrieve($cardref);
765 $this->error = $e->getMessage();
768 } elseif ($createifnotlinkedtostripe) {
769 $exp_date_month = $obj->exp_date_month;
770 $exp_date_year = $obj->exp_date_year;
771 $number = $obj->number;
773 $cardholdername = $obj->proprio;
777 $dataforcard = array(
778 "source" => array(
'object'=>
'card',
'exp_month'=>$exp_date_month,
'exp_year'=>$exp_date_year,
'number'=>$number,
'cvc'=>$cvc,
'name'=>$cardholdername),
779 "metadata" => array(
'dol_id'=>$object->id,
'dol_version'=>DOL_VERSION,
'dol_entity'=>$conf->entity,
'ipaddress'=>$ipaddress)
786 if (empty($stripeacc)) {
787 if (empty($conf->global->STRIPE_USE_INTENT_WITH_AUTOMATIC_CONFIRMATION)) {
788 dol_syslog(
"Try to create card with dataforcard = ".json_encode($dataforcard));
789 $card = $cu->sources->create($dataforcard);
791 $this->error =
'Creation of card on Stripe has failed';
795 if (!empty($stripeacc)) {
796 $connect = $stripeacc.
'/';
798 $url =
'https://dashboard.stripe.com/'.$connect.
'test/customers/'.$cu->id;
800 $url =
'https://dashboard.stripe.com/'.$connect.
'customers/'.$cu->id;
802 $urtoswitchonstripe =
' <a href="'.$url.
'" target="_stripe">'.
img_picto($langs->trans(
'ShowInStripe'),
'globe').
'</a>';
805 $this->error = $langs->trans(
'CreationOfPaymentModeMustBeDoneFromStripeInterface', $urtoswitchonstripe);
808 if (empty($conf->global->STRIPE_USE_INTENT_WITH_AUTOMATIC_CONFIRMATION)) {
809 dol_syslog(
"Try to create card with dataforcard = ".json_encode($dataforcard));
810 $card = $cu->sources->create($dataforcard, array(
"stripe_account" => $stripeacc));
812 $this->error =
'Creation of card on Stripe has failed';
816 if (!empty($stripeacc)) {
817 $connect = $stripeacc.
'/';
819 $url =
'https://dashboard.stripe.com/'.$connect.
'test/customers/'.$cu->id;
821 $url =
'https://dashboard.stripe.com/'.$connect.
'customers/'.$cu->id;
823 $urtoswitchonstripe =
' <a href="'.$url.
'" target="_stripe">'.
img_picto($langs->trans(
'ShowInStripe'),
'globe').
'</a>';
826 $this->error = $langs->trans(
'CreationOfPaymentModeMustBeDoneFromStripeInterface', $urtoswitchonstripe);
831 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"societe_rib";
832 $sql .=
" SET stripe_card_ref = '".$this->db->escape($card->id).
"', card_type = '".$this->
db->escape($card->brand).
"',";
833 $sql .=
" country_code = '".$this->db->escape($card->country).
"',";
834 $sql .=
" approved = ".($card->cvc_check ==
'pass' ? 1 : 0);
835 $sql .=
" WHERE rowid = ".((int) $object->id);
836 $sql .=
" AND type = 'card'";
839 $this->error = $this->
db->lasterror();
843 $this->error = $e->getMessage();
871 public function createPaymentStripe($amount, $currency, $origin, $item, $source, $customer, $account, $status = 0, $usethirdpartyemailforreceiptemail = 0, $capture =
true)
877 if (empty($status)) {
878 $service =
'StripeTest';
880 $service =
'StripeLive';
883 $sql =
"SELECT sa.key_account as key_account, sa.fk_soc, sa.entity";
884 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account as sa";
885 $sql .=
" WHERE sa.key_account = '".$this->db->escape($customer).
"'";
887 $sql .=
" AND sa.site = 'stripe' AND sa.status = ".((int) $status);
889 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
890 $result = $this->
db->query($sql);
892 if ($this->
db->num_rows($result)) {
893 $obj = $this->
db->fetch_object($result);
902 $arrayzerounitcurrency = array(
'BIF',
'CLP',
'DJF',
'GNF',
'JPY',
'KMF',
'KRW',
'MGA',
'PYG',
'RWF',
'VND',
'VUV',
'XAF',
'XOF',
'XPF');
903 if (!in_array($currency, $arrayzerounitcurrency)) {
904 $stripeamount = $amount * 100;
906 $stripeamount = $amount;
911 $societe->fetch($key);
916 if ($origin ==
'order') {
918 $order->fetch($item);
920 $description =
"ORD=".$ref.
".CUS=".$societe->id.
".PM=stripe";
921 } elseif ($origin ==
'invoice') {
923 $invoice->fetch($item);
924 $ref = $invoice->ref;
925 $description =
"INV=".$ref.
".CUS=".$societe->id.
".PM=stripe";
931 "dol_id" =>
"".$item.
"",
932 "dol_type" =>
"".$origin.
"",
933 "dol_thirdparty_id" =>
"".$societe->id.
"",
934 'dol_thirdparty_name' => $societe->name,
935 'dol_version'=>DOL_VERSION,
936 'dol_entity'=>$conf->entity,
937 'ipaddress'=>$ipaddress
942 global $stripearrayofkeysbyenv;
943 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
945 if (empty($conf->stripeconnect->enabled)) {
946 if (preg_match(
'/pm_/i', $source)) {
947 $stripecard = $source;
948 $amountstripe = $stripeamount;
951 $amounttopay = $amount;
952 $servicestatus = $status;
954 dol_syslog(
"* createPaymentStripe get stripeacc", LOG_DEBUG);
955 $stripeacc = $stripe->getStripeAccount($service);
957 dol_syslog(
"* createPaymentStripe Create payment for customer ".$customer->id.
" on source card ".$stripecard->id.
", amounttopay=".$amounttopay.
", amountstripe=".$amountstripe.
", FULLTAG=".$FULLTAG, LOG_DEBUG);
960 $paymentintent = $stripe->getPaymentIntent($amounttopay, $currency, $FULLTAG, $description, $invoice, $customer->id, $stripeacc, $servicestatus, 0,
'automatic',
true, $stripecard->id, 1);
962 $charge =
new stdClass();
963 if ($paymentintent->status ==
'succeeded') {
964 $charge->status =
'ok';
966 $charge->status =
'failed';
967 $charge->failure_code = $stripe->code;
968 $charge->failure_message = $stripe->error;
969 $charge->failure_declinecode = $stripe->declinecode;
970 $stripefailurecode = $stripe->code;
971 $stripefailuremessage = $stripe->error;
972 $stripefailuredeclinecode = $stripe->declinecode;
974 } elseif (preg_match(
'/acct_/i', $source)) {
975 $charge = \Stripe\Charge::create(array(
976 "amount" =>
"$stripeamount",
977 "currency" =>
"$currency",
978 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
979 "description" =>
"Stripe payment: ".$description,
980 "capture" => $capture,
981 "metadata" => $metadata,
982 "source" =>
"$source"
985 $paymentarray = array(
986 "amount" =>
"$stripeamount",
987 "currency" =>
"$currency",
988 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
989 "description" =>
"Stripe payment: ".$description,
990 "capture" => $capture,
991 "metadata" => $metadata,
992 "source" =>
"$source",
993 "customer" =>
"$customer"
996 if ($societe->email && $usethirdpartyemailforreceiptemail) {
997 $paymentarray[
"receipt_email"] = $societe->email;
1000 $charge = \Stripe\Charge::create($paymentarray, array(
"idempotency_key" =>
"$description"));
1004 $fee = $amount * ($conf->global->STRIPE_APPLICATION_FEE_PERCENT / 100) + $conf->global->STRIPE_APPLICATION_FEE;
1005 if ($fee >= $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL && $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL > $conf->global->STRIPE_APPLICATION_FEE_MINIMAL) {
1006 $fee = $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL;
1007 } elseif ($fee < $conf->global->STRIPE_APPLICATION_FEE_MINIMAL) {
1008 $fee = $conf->global->STRIPE_APPLICATION_FEE_MINIMAL;
1011 if (!in_array($currency, $arrayzerounitcurrency)) {
1012 $stripefee = round($fee * 100);
1014 $stripefee = round($fee);
1017 $paymentarray = array(
1018 "amount" =>
"$stripeamount",
1019 "currency" =>
"$currency",
1020 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
1021 "description" =>
"Stripe payment: ".$description,
1022 "capture" => $capture,
1023 "metadata" => $metadata,
1024 "source" =>
"$source",
1025 "customer" =>
"$customer"
1027 if ($conf->entity != $conf->global->STRIPECONNECT_PRINCIPAL && $stripefee > 0) {
1028 $paymentarray[
"application_fee_amount"] = $stripefee;
1030 if ($societe->email && $usethirdpartyemailforreceiptemail) {
1031 $paymentarray[
"receipt_email"] = $societe->email;
1034 if (preg_match(
'/pm_/i', $source)) {
1035 $stripecard = $source;
1036 $amountstripe = $stripeamount;
1039 $amounttopay = $amount;
1040 $servicestatus = $status;
1042 dol_syslog(
"* createPaymentStripe get stripeacc", LOG_DEBUG);
1043 $stripeacc = $stripe->getStripeAccount($service);
1045 dol_syslog(
"* createPaymentStripe Create payment on card ".$stripecard->id.
", amounttopay=".$amounttopay.
", amountstripe=".$amountstripe.
", FULLTAG=".$FULLTAG, LOG_DEBUG);
1048 $paymentintent = $stripe->getPaymentIntent($amounttopay, $currency, $FULLTAG, $description, $invoice, $customer->id, $stripeacc, $servicestatus, 0,
'automatic',
true, $stripecard->id, 1);
1050 $charge =
new stdClass();
1051 if ($paymentintent->status ==
'succeeded') {
1052 $charge->status =
'ok';
1053 $charge->id = $paymentintent->id;
1055 $charge->status =
'failed';
1056 $charge->failure_code = $stripe->code;
1057 $charge->failure_message = $stripe->error;
1058 $charge->failure_declinecode = $stripe->declinecode;
1061 $charge = \Stripe\Charge::create($paymentarray, array(
"idempotency_key" =>
"$description",
"stripe_account" =>
"$account"));
1064 if (isset($charge->id)) {
1067 $return->statut =
'success';
1068 $return->id = $charge->id;
1070 if (preg_match(
'/pm_/i', $source)) {
1071 $return->message =
'Payment retrieved by card status = '.$charge->status;
1073 if ($charge->source->type ==
'card') {
1074 $return->message = $charge->source->card->brand.
" ....".$charge->source->card->last4;
1075 } elseif ($charge->source->type ==
'three_d_secure') {
1077 $src = \Stripe\Source::retrieve(
"".$charge->source->three_d_secure->card.
"", array(
1078 "stripe_account" => $stripe->getStripeAccount($service)
1080 $return->message = $src->card->brand.
" ....".$src->card->last4;
1082 $return->message = $charge->id;
1085 }
catch (\
Stripe\Error\Card $e) {
1086 include DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1088 $body = $e->getJsonBody();
1089 $err = $body[
'error'];
1091 $return->statut =
'error';
1092 $return->id = $err[
'charge'];
1093 $return->type = $err[
'type'];
1094 $return->code = $err[
'code'];
1095 $return->message = $err[
'message'];
1096 $body =
"Error: <br>".$return->id.
" ".$return->message.
" ";
1097 $subject =
'[Alert] Payment error using Stripe';
1098 $cmailfile =
new CMailFile($subject, $conf->global->ONLINE_PAYMENT_SENDEMAIL, $conf->global->MAIN_INFO_SOCIETE_MAIL, $body);
1099 $cmailfile->sendfile();
1102 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1103 }
catch (\
Stripe\Error\RateLimit $e) {
1106 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1107 }
catch (\
Stripe\Error\InvalidRequest $e) {
1110 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1111 }
catch (\
Stripe\Error\Authentication $e) {
1115 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1116 }
catch (\
Stripe\Error\ApiConnection $e) {
1119 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1120 }
catch (\
Stripe\Error\Base $e) {
1124 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1128 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');