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';
94 if ($entity < 0) $entity = $conf->entity;
96 $sql =
"SELECT tokenstring";
97 $sql .=
" FROM ".MAIN_DB_PREFIX.
"oauth_token";
98 $sql .=
" WHERE service = '".$this->db->escape($mode).
"'";
99 $sql .=
" AND entity = ".((int) $entity);
101 $sql .=
" AND fk_soc = ".$fk_soc;
103 $sql .=
" AND fk_soc IS NULL";
105 $sql .=
" AND fk_user IS NULL AND fk_adherent IS NULL";
107 dol_syslog(get_class($this).
"::getStripeAccount", LOG_DEBUG);
109 $result = $this->
db->query($sql);
111 if ($this->
db->num_rows($result)) {
112 $obj = $this->
db->fetch_object($result);
113 $tokenstring = $obj->tokenstring;
115 $tmparray = json_decode($tokenstring);
116 $key = $tmparray->stripe_user_id;
124 dol_syslog(
"No dedicated Stripe Connect account available for entity ".$conf->entity);
138 include_once DOL_DOCUMENT_ROOT.
'/societe/class/societeaccount.class.php';
140 return $societeaccount->getCustomerAccount($id,
'stripe', $status, $site_account);
158 if (empty($object->id))
160 dol_syslog(
"customerStripe is called with the parameter object that is not loaded");
167 global $stripearrayofkeysbyenv;
168 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
170 $sql =
"SELECT sa.key_account as key_account, sa.entity";
171 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account as sa";
172 $sql .=
" WHERE sa.fk_soc = ".$object->id;
173 $sql .=
" AND sa.entity IN (".getEntity(
'societe').
")";
174 $sql .=
" AND sa.site = 'stripe' AND sa.status = ".((int) $status);
175 $sql .=
" AND (sa.site_account IS NULL OR sa.site_account = '' OR sa.site_account = '".$this->db->escape($stripearrayofkeysbyenv[$status][
'publishable_key']).
"')";
176 $sql .=
" AND sa.key_account IS NOT NULL AND sa.key_account <> ''";
178 dol_syslog(get_class($this).
"::customerStripe search stripe customer id for thirdparty id=".$object->id, LOG_DEBUG);
183 $obj = $this->
db->fetch_object(
$resql);
184 $tiers = $obj->key_account;
186 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']);
191 $customer = \Stripe\Customer::retrieve(array(
'id'=>
"$tiers",
'expand[]'=>
'sources'));
194 $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))
239 if (!empty($vatcleaned))
242 if ($object->country_code && $isineec)
245 $customer->createTaxId($customer->id, array(
'type'=>
'eu_vat',
'value'=>$vatcleaned));
251 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"societe_account (fk_soc, login, key_account, site, site_account, status, entity, date_creation, fk_user_creat)";
252 $sql .=
" VALUES (".$object->id.
", '', '".$this->
db->escape($customer->id).
"', 'stripe', '".$this->
db->escape($stripearrayofkeysbyenv[$status][
'publishable_key']).
"', ".$status.
", ".$conf->entity.
", '".$this->
db->idate(
dol_now()).
"', ".$user->id.
")";
256 $this->error = $this->
db->lasterror();
260 $this->error = $e->getMessage();
280 $stripepaymentmethod = null;
284 global $stripearrayofkeysbyenv;
285 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
287 $stripepaymentmethod = \Stripe\PaymentMethod::retrieve(
''.$paymentmethod->id.
'');
289 $stripepaymentmethod = \Stripe\PaymentMethod::retrieve(
''.$paymentmethod->id.
'', array(
"stripe_account" => $key));
293 $this->error = $e->getMessage();
296 return $stripepaymentmethod;
324 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)
332 if (empty($status)) $service =
'StripeTest';
333 else $service =
'StripeLive';
335 $arrayzerounitcurrency = array(
'BIF',
'CLP',
'DJF',
'GNF',
'JPY',
'KMF',
'KRW',
'MGA',
'PYG',
'RWF',
'VND',
'VUV',
'XAF',
'XOF',
'XPF');
336 if (!in_array($currency_code, $arrayzerounitcurrency)) $stripeamount = $amount * 100;
337 else $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))
361 $sql =
"SELECT pi.ext_payment_id, pi.entity, pi.fk_facture, pi.sourcetype, pi.ext_payment_site";
362 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_facture_demande as pi";
363 $sql .=
" WHERE pi.fk_facture = ".$object->id;
364 $sql .=
" AND pi.sourcetype = '".$this->db->escape($object->element).
"'";
365 $sql .=
" AND pi.entity IN (".getEntity(
'societe').
")";
366 $sql .=
" AND pi.ext_payment_site = '".$this->db->escape($service).
"'";
368 dol_syslog(get_class($this).
"::getPaymentIntent search stripe payment intent for object id = ".$object->id, LOG_DEBUG);
374 $obj = $this->
db->fetch_object(
$resql);
375 $intent = $obj->ext_payment_id;
377 dol_syslog(get_class($this).
"::getPaymentIntent found existing payment intent record");
380 global $stripearrayofkeysbyenv;
381 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
385 $paymentintent = \Stripe\PaymentIntent::retrieve($intent);
387 $paymentintent = \Stripe\PaymentIntent::retrieve($intent, array(
"stripe_account" => $key));
392 $this->error = $e->getMessage();
398 if (empty($paymentintent))
401 $metadata = array(
'dol_version'=>DOL_VERSION,
'dol_entity'=>$conf->entity,
'ipaddress'=>$ipaddress);
402 if (is_object($object))
404 $metadata[
'dol_type'] = $object->element;
405 $metadata[
'dol_id'] = $object->id;
406 if (is_object($object->thirdparty) && $object->thirdparty->id > 0) $metadata[
'dol_thirdparty_id'] = $object->thirdparty->id;
410 $paymentmethodtypes = array(
"card");
411 if (!empty($conf->global->STRIPE_SEPA_DIRECT_DEBIT)) $paymentmethodtypes[] =
"sepa_debit";
412 if (!empty($conf->global->STRIPE_IDEAL)) $paymentmethodtypes[] =
"ideal";
414 $dataforintent = array(
415 "confirm" => $confirmnow,
416 "confirmation_method" => $mode,
417 "amount" => $stripeamount,
418 "currency" => $currency_code,
419 "payment_method_types" => $paymentmethodtypes,
420 "description" => $description,
421 "statement_descriptor_suffix" =>
dol_trunc($tag, 10,
'right',
'UTF-8', 1),
423 "setup_future_usage" =>
"on_session",
424 "metadata" => $metadata
426 if (!is_null($customer)) $dataforintent[
"customer"] = $customer;
432 unset($dataforintent[
'setup_future_usage']);
434 $dataforintent[
"off_session"] =
true;
436 if (!is_null($payment_method))
438 $dataforintent[
"payment_method"] = $payment_method;
439 $description .=
' - '.$payment_method;
442 if ($conf->entity != $conf->global->STRIPECONNECT_PRINCIPAL && $stripefee > 0)
444 $dataforintent[
"application_fee_amount"] = $stripefee;
446 if ($usethirdpartyemailforreceiptemail && is_object($object) && $object->thirdparty->email)
448 $dataforintent[
"receipt_email"] = $object->thirdparty->email;
453 global $stripearrayofkeysbyenv;
454 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
456 $arrayofoptions = array();
457 if (empty($noidempotency_key)) {
458 $arrayofoptions[
"idempotency_key"] = $description;
462 $arrayofoptions[
"stripe_account"] = $key;
464 $paymentintent = \Stripe\PaymentIntent::create($dataforintent, $arrayofoptions);
467 if (is_object($object))
469 $paymentintentalreadyexists = 0;
471 $sql =
"SELECT pi.rowid";
472 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_facture_demande as pi";
473 $sql .=
" WHERE pi.entity IN (".getEntity(
'societe').
")";
474 $sql .=
" AND pi.ext_payment_site = '".$this->db->escape($service).
"'";
475 $sql .=
" AND pi.ext_payment_id = '".$this->db->escape($paymentintent->id).
"'";
477 dol_syslog(get_class($this).
"::getPaymentIntent search if payment intent already in prelevement_facture_demande", LOG_DEBUG);
483 $obj = $this->
db->fetch_object(
$resql);
484 if ($obj) $paymentintentalreadyexists++;
489 if (!$paymentintentalreadyexists)
492 $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)";
493 $sql .=
" VALUES ('".$this->db->idate($now).
"', ".$user->id.
", '".$this->
db->escape($paymentintent->id).
"', ".$object->id.
", '".$this->
db->escape($object->element).
"', ".$conf->entity.
", '".$this->
db->escape($service).
"', ".$amount.
")";
498 $this->error = $this->
db->lasterror();
499 dol_syslog(get_class($this).
"::PaymentIntent failed to insert paymentintent with id=".$paymentintent->id.
" into database.");
503 $_SESSION[
"stripe_payment_intent"] = $paymentintent;
505 }
catch (
Stripe\Error\Card $e)
508 $this->error = $e->getMessage();
509 $this->
code = $e->getStripeCode();
510 $this->declinecode = $e->getDeclineCode();
520 $this->error = $e->getMessage();
522 $this->declinecode =
'';
526 dol_syslog(
"getPaymentIntent return error=".$error.
" this->error=".$this->error, LOG_INFO, -1);
530 return $paymentintent;
555 public function getSetupIntent($description, $object, $customer, $key, $status, $usethirdpartyemailforreceiptemail = 0, $confirmnow =
false)
559 dol_syslog(
"getSetupIntent description=".$description.
' confirmnow='.$confirmnow, LOG_INFO, 1);
563 if (empty($status)) $service =
'StripeTest';
564 else $service =
'StripeLive';
568 if (empty($setupintent))
571 $metadata = array(
'dol_version'=>DOL_VERSION,
'dol_entity'=>$conf->entity,
'ipaddress'=>$ipaddress);
572 if (is_object($object))
574 $metadata[
'dol_type'] = $object->element;
575 $metadata[
'dol_id'] = $object->id;
576 if (is_object($object->thirdparty) && $object->thirdparty->id > 0) $metadata[
'dol_thirdparty_id'] = $object->thirdparty->id;
580 $paymentmethodtypes = array(
"card");
581 if (!empty($conf->global->STRIPE_SEPA_DIRECT_DEBIT)) $paymentmethodtypes[] =
"sepa_debit";
584 $dataforintent = array(
585 "confirm" => $confirmnow,
586 "payment_method_types" => $paymentmethodtypes,
587 "usage" =>
"off_session",
588 "metadata" => $metadata
590 if (!is_null($customer)) $dataforintent[
"customer"] = $customer;
591 if (!is_null($description)) $dataforintent[
"description"] = $description;
596 if ($usethirdpartyemailforreceiptemail && is_object($object) && $object->thirdparty->email)
598 $dataforintent[
"receipt_email"] = $object->thirdparty->email;
603 global $stripearrayofkeysbyenv;
604 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
606 dol_syslog(
"getSetupIntent ".$stripearrayofkeysbyenv[$status][
'publishable_key'], LOG_DEBUG);
611 $setupintent = \Stripe\SetupIntent::create($dataforintent, array());
614 $setupintent = \Stripe\SetupIntent::create($dataforintent, array(
"stripe_account" => $key));
668 $this->error = $e->getMessage();
674 dol_syslog(
"getSetupIntent ".(is_object($setupintent) ? $setupintent->id :
''), LOG_INFO, -1);
677 dol_syslog(
"getSetupIntent return error=".$error, LOG_INFO, -1);
695 global $conf, $user, $langs;
699 $sql =
"SELECT sa.stripe_card_ref, sa.proprio, sa.exp_date_month, sa.exp_date_year, sa.number, sa.cvn";
700 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib as sa";
701 $sql .=
" WHERE sa.rowid = ".$object->id;
702 $sql .=
" AND sa.type = 'card'";
704 dol_syslog(get_class($this).
"::fetch search stripe card id for paymentmode id=".$object->id.
", stripeacc=".$stripeacc.
", status=".$status.
", createifnotlinkedtostripe=".$createifnotlinkedtostripe, LOG_DEBUG);
710 $obj = $this->
db->fetch_object(
$resql);
711 $cardref = $obj->stripe_card_ref;
712 dol_syslog(get_class($this).
"::cardStripe cardref=".$cardref);
716 if (empty($stripeacc)) {
717 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources))
719 $card = $cu->sources->retrieve($cardref);
721 $card = \Stripe\PaymentMethod::retrieve($cardref);
724 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources))
727 $card = $cu->sources->retrieve($cardref);
730 $card = \Stripe\PaymentMethod::retrieve($cardref);
735 $this->error = $e->getMessage();
738 } elseif ($createifnotlinkedtostripe)
740 $exp_date_month = $obj->exp_date_month;
741 $exp_date_year = $obj->exp_date_year;
742 $number = $obj->number;
744 $cardholdername = $obj->proprio;
748 $dataforcard = array(
749 "source" => array(
'object'=>
'card',
'exp_month'=>$exp_date_month,
'exp_year'=>$exp_date_year,
'number'=>$number,
'cvc'=>$cvc,
'name'=>$cardholdername),
750 "metadata" => array(
'dol_id'=>$object->id,
'dol_version'=>DOL_VERSION,
'dol_entity'=>$conf->entity,
'ipaddress'=>$ipaddress)
756 if (empty($stripeacc)) {
757 if (empty($conf->global->STRIPE_USE_INTENT_WITH_AUTOMATIC_CONFIRMATION))
759 dol_syslog(
"Try to create card with dataforcard = ".json_encode($dataforcard));
760 $card = $cu->sources->create($dataforcard);
763 $this->error =
'Creation of card on Stripe has failed';
767 if (!empty($stripeacc)) $connect = $stripeacc.
'/';
768 $url =
'https://dashboard.stripe.com/'.$connect.
'test/customers/'.$cu->id;
771 $url =
'https://dashboard.stripe.com/'.$connect.
'customers/'.$cu->id;
773 $urtoswitchonstripe =
' <a href="'.$url.
'" target="_stripe">'.
img_picto($langs->trans(
'ShowInStripe'),
'globe').
'</a>';
776 $this->error = $langs->trans(
'CreationOfPaymentModeMustBeDoneFromStripeInterface', $urtoswitchonstripe);
779 if (empty($conf->global->STRIPE_USE_INTENT_WITH_AUTOMATIC_CONFIRMATION))
781 dol_syslog(
"Try to create card with dataforcard = ".json_encode($dataforcard));
782 $card = $cu->sources->create($dataforcard, array(
"stripe_account" => $stripeacc));
785 $this->error =
'Creation of card on Stripe has failed';
789 if (!empty($stripeacc)) $connect = $stripeacc.
'/';
790 $url =
'https://dashboard.stripe.com/'.$connect.
'test/customers/'.$cu->id;
793 $url =
'https://dashboard.stripe.com/'.$connect.
'customers/'.$cu->id;
795 $urtoswitchonstripe =
' <a href="'.$url.
'" target="_stripe">'.
img_picto($langs->trans(
'ShowInStripe'),
'globe').
'</a>';
798 $this->error = $langs->trans(
'CreationOfPaymentModeMustBeDoneFromStripeInterface', $urtoswitchonstripe);
804 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"societe_rib";
805 $sql .=
" SET stripe_card_ref = '".$this->db->escape($card->id).
"', card_type = '".$this->
db->escape($card->brand).
"',";
806 $sql .=
" country_code = '".$this->db->escape($card->country).
"',";
807 $sql .=
" approved = ".($card->cvc_check ==
'pass' ? 1 : 0);
808 $sql .=
" WHERE rowid = ".$object->id;
809 $sql .=
" AND type = 'card'";
813 $this->error = $this->
db->lasterror();
818 $this->error = $e->getMessage();
846 public function createPaymentStripe($amount, $currency, $origin, $item, $source, $customer, $account, $status = 0, $usethirdpartyemailforreceiptemail = 0, $capture =
true)
852 if (empty($status)) $service =
'StripeTest';
853 else $service =
'StripeLive';
855 $sql =
"SELECT sa.key_account as key_account, sa.fk_soc, sa.entity";
856 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account as sa";
857 $sql .=
" WHERE sa.key_account = '".$this->db->escape($customer).
"'";
859 $sql .=
" AND sa.site = 'stripe' AND sa.status = ".((int) $status);
861 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
862 $result = $this->
db->query($sql);
864 if ($this->
db->num_rows($result)) {
865 $obj = $this->
db->fetch_object($result);
874 $arrayzerounitcurrency = array(
'BIF',
'CLP',
'DJF',
'GNF',
'JPY',
'KMF',
'KRW',
'MGA',
'PYG',
'RWF',
'VND',
'VUV',
'XAF',
'XOF',
'XPF');
875 if (!in_array($currency, $arrayzerounitcurrency)) $stripeamount = $amount * 100;
876 else $stripeamount = $amount;
879 if ($key > 0) $societe->fetch($key);
883 if ($origin ==
'order') {
885 $order->fetch($item);
887 $description =
"ORD=".$ref.
".CUS=".$societe->id.
".PM=stripe";
888 } elseif ($origin ==
'invoice') {
890 $invoice->fetch($item);
891 $ref = $invoice->ref;
892 $description =
"INV=".$ref.
".CUS=".$societe->id.
".PM=stripe";
898 "dol_id" =>
"".$item.
"",
899 "dol_type" =>
"".$origin.
"",
900 "dol_thirdparty_id" =>
"".$societe->id.
"",
901 'dol_thirdparty_name' => $societe->name,
902 'dol_version'=>DOL_VERSION,
903 'dol_entity'=>$conf->entity,
904 'ipaddress'=>$ipaddress
909 global $stripearrayofkeysbyenv;
910 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
912 if (empty($conf->stripeconnect->enabled))
914 if (preg_match(
'/pm_/i', $source))
916 $stripecard = $source;
917 $amountstripe = $stripeamount;
920 $amounttopay = $amount;
921 $servicestatus = $status;
923 dol_syslog(
"* createPaymentStripe get stripeacc", LOG_DEBUG);
924 $stripeacc = $stripe->getStripeAccount($service);
926 dol_syslog(
"* createPaymentStripe Create payment for customer ".$customer->id.
" on source card ".$stripecard->id.
", amounttopay=".$amounttopay.
", amountstripe=".$amountstripe.
", FULLTAG=".$FULLTAG, LOG_DEBUG);
929 $paymentintent = $stripe->getPaymentIntent($amounttopay, $currency, $FULLTAG, $description, $invoice, $customer->id, $stripeacc, $servicestatus, 0,
'automatic',
true, $stripecard->id, 1);
931 $charge =
new stdClass();
932 if ($paymentintent->status ==
'succeeded')
934 $charge->status =
'ok';
936 $charge->status =
'failed';
937 $charge->failure_code = $stripe->code;
938 $charge->failure_message = $stripe->error;
939 $charge->failure_declinecode = $stripe->declinecode;
940 $stripefailurecode = $stripe->code;
941 $stripefailuremessage = $stripe->error;
942 $stripefailuredeclinecode = $stripe->declinecode;
944 } elseif (preg_match(
'/acct_/i', $source))
946 $charge = \Stripe\Charge::create(array(
947 "amount" =>
"$stripeamount",
948 "currency" =>
"$currency",
949 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
950 "description" =>
"Stripe payment: ".$description,
951 "capture" => $capture,
952 "metadata" => $metadata,
953 "source" =>
"$source"
956 $paymentarray = array(
957 "amount" =>
"$stripeamount",
958 "currency" =>
"$currency",
959 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
960 "description" =>
"Stripe payment: ".$description,
961 "capture" => $capture,
962 "metadata" => $metadata,
963 "source" =>
"$source",
964 "customer" =>
"$customer"
967 if ($societe->email && $usethirdpartyemailforreceiptemail)
969 $paymentarray[
"receipt_email"] = $societe->email;
972 $charge = \Stripe\Charge::create($paymentarray, array(
"idempotency_key" =>
"$description"));
976 $fee = $amount * ($conf->global->STRIPE_APPLICATION_FEE_PERCENT / 100) + $conf->global->STRIPE_APPLICATION_FEE;
977 if ($fee >= $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL && $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL > $conf->global->STRIPE_APPLICATION_FEE_MINIMAL) {
978 $fee = $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL;
979 } elseif ($fee < $conf->global->STRIPE_APPLICATION_FEE_MINIMAL) {
980 $fee = $conf->global->STRIPE_APPLICATION_FEE_MINIMAL;
983 if (!in_array($currency, $arrayzerounitcurrency)) $stripefee = round($fee * 100);
984 else $stripefee = round($fee);
986 $paymentarray = array(
987 "amount" =>
"$stripeamount",
988 "currency" =>
"$currency",
989 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
990 "description" =>
"Stripe payment: ".$description,
991 "capture" => $capture,
992 "metadata" => $metadata,
993 "source" =>
"$source",
994 "customer" =>
"$customer"
996 if ($conf->entity != $conf->global->STRIPECONNECT_PRINCIPAL && $stripefee > 0)
998 $paymentarray[
"application_fee_amount"] = $stripefee;
1000 if ($societe->email && $usethirdpartyemailforreceiptemail)
1002 $paymentarray[
"receipt_email"] = $societe->email;
1005 if (preg_match(
'/pm_/i', $source))
1007 $stripecard = $source;
1008 $amountstripe = $stripeamount;
1011 $amounttopay = $amount;
1012 $servicestatus = $status;
1014 dol_syslog(
"* createPaymentStripe get stripeacc", LOG_DEBUG);
1015 $stripeacc = $stripe->getStripeAccount($service);
1017 dol_syslog(
"* createPaymentStripe Create payment on card ".$stripecard->id.
", amounttopay=".$amounttopay.
", amountstripe=".$amountstripe.
", FULLTAG=".$FULLTAG, LOG_DEBUG);
1020 $paymentintent = $stripe->getPaymentIntent($amounttopay, $currency, $FULLTAG, $description, $invoice, $customer->id, $stripeacc, $servicestatus, 0,
'automatic',
true, $stripecard->id, 1);
1022 $charge =
new stdClass();
1023 if ($paymentintent->status ==
'succeeded')
1025 $charge->status =
'ok';
1026 $charge->id = $paymentintent->id;
1028 $charge->status =
'failed';
1029 $charge->failure_code = $stripe->code;
1030 $charge->failure_message = $stripe->error;
1031 $charge->failure_declinecode = $stripe->declinecode;
1034 $charge = \Stripe\Charge::create($paymentarray, array(
"idempotency_key" =>
"$description",
"stripe_account" =>
"$account"));
1037 if (isset($charge->id)) {}
1039 $return->statut =
'success';
1040 $return->id = $charge->id;
1042 if (preg_match(
'/pm_/i', $source))
1044 $return->message =
'Payment retrieved by card status = '.$charge->status;
1046 if ($charge->source->type ==
'card') {
1047 $return->message = $charge->source->card->brand.
" ....".$charge->source->card->last4;
1048 } elseif ($charge->source->type ==
'three_d_secure') {
1050 $src = \Stripe\Source::retrieve(
"".$charge->source->three_d_secure->card.
"", array(
1051 "stripe_account" => $stripe->getStripeAccount($service)
1053 $return->message = $src->card->brand.
" ....".$src->card->last4;
1055 $return->message = $charge->id;
1058 }
catch (\
Stripe\Error\Card $e) {
1059 include DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1061 $body = $e->getJsonBody();
1062 $err = $body[
'error'];
1064 $return->statut =
'error';
1065 $return->id = $err[
'charge'];
1066 $return->type = $err[
'type'];
1067 $return->code = $err[
'code'];
1068 $return->message = $err[
'message'];
1069 $body =
"Error: <br>".$return->id.
" ".$return->message.
" ";
1070 $subject =
'[Alert] Payment error using Stripe';
1071 $cmailfile =
new CMailFile($subject, $conf->global->ONLINE_PAYMENT_SENDEMAIL, $conf->global->MAIN_INFO_SOCIETE_MAIL, $body);
1072 $cmailfile->sendfile();
1075 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1076 }
catch (\
Stripe\Error\RateLimit $e) {
1079 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1080 }
catch (\
Stripe\Error\InvalidRequest $e) {
1083 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1084 }
catch (\
Stripe\Error\Authentication $e) {
1088 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1089 }
catch (\
Stripe\Error\ApiConnection $e) {
1092 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1093 }
catch (\
Stripe\Error\Base $e) {
1097 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1101 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
getUserRemoteIP()
Return the IP of remote user.
getStripeCustomerAccount($id, $status=0, $site_account= '')
getStripeCustomerAccount
getSetupIntent($description, $object, $customer, $key, $status, $usethirdpartyemailforreceiptemail=0, $confirmnow=false)
Get the Stripe payment intent.
if(!empty($arrayfields['country.code_iso']['checked'])) print_liste_field_titre($arrayfields['country.code_iso']['label'] country if(!empty($arrayfields['typent.code']['checked'])) print_liste_field_titre($arrayfields['typent.code']['label'] typent code
dol_now($mode= 'auto')
Return date for now.
__construct($db)
Constructor.
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.
Class for SocieteAccount.
$conf db
API class for accounts.
createPaymentStripe($amount, $currency, $origin, $item, $source, $customer, $account, $status=0, $usethirdpartyemailforreceiptemail=0, $capture=true)
Create charge.
Class to manage third parties objects (customers, suppliers, prospects...)
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)...
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,$sendto,$replyto,$message,$filepath,$mimetype,$filename,$cc,$ccc,$deliveryreceipt,$msgishtml,$errors_to,$css,$trackid,$moreinheader,$sendcontext,$replyto); $mailfile->sendfile();.
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Class to manage customers orders.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
Class for CompanyPaymentMode.
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 ...
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $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_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.
Class to manage invoices.
if(!defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN'
Draft customers invoices.
getPaymentMethodStripe($paymentmethod, $key= '', $status=0)
Get the Stripe payment method Object from its ID.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
isInEEC($object)
Return if a country of an object is inside the EEC (European Economic Community)