163 if (empty($object->id)) {
164 dol_syslog(
"customerStripe is called with the parameter object that is not loaded");
171 global $stripearrayofkeysbyenv;
172 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
174 $sql =
"SELECT sa.key_account as key_account, sa.entity";
175 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account as sa";
176 $sql .=
" WHERE sa.fk_soc = ".((int) $object->id);
177 $sql .=
" AND sa.entity IN (".getEntity(
'societe').
")";
178 $sql .=
" AND sa.site = 'stripe' AND sa.status = ".((int) $status);
179 $sql .=
" AND (sa.site_account IS NULL OR sa.site_account = '' OR sa.site_account = '".$this->db->escape($stripearrayofkeysbyenv[$status][
'publishable_key']).
"')";
180 $sql .=
" AND sa.key_account IS NOT NULL AND sa.key_account <> ''";
182 dol_syslog(get_class($this).
"::customerStripe search stripe customer id for thirdparty id=".$object->id, LOG_DEBUG);
183 $resql = $this->db->query($sql);
185 $num = $this->db->num_rows($resql);
187 $obj = $this->db->fetch_object($resql);
188 $tiers = $obj->key_account;
190 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']);
195 $customer = \Stripe\Customer::retrieve(array(
'id'=>
"$tiers",
'expand[]'=>
'sources'));
198 $customer = \Stripe\Customer::retrieve(array(
'id'=>
"$tiers",
'expand[]'=>
'sources'), array(
"stripe_account" => $key));
202 $this->error = $e->getMessage();
204 } elseif ($createifnotlinkedtostripe) {
207 $dataforcustomer = array(
208 "email" => $object->email,
209 "description" => $object->name,
210 "metadata" => array(
'dol_id'=>$object->id,
'dol_version'=>DOL_VERSION,
'dol_entity'=>$conf->entity,
'ipaddress'=>$ipaddress)
213 $vatcleaned = $object->tva_intra ? $object->tva_intra :
null;
230 global $stripearrayofkeysbyenv;
231 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
234 $customer = \Stripe\Customer::create($dataforcustomer);
236 $customer = \Stripe\Customer::create($dataforcustomer, array(
"stripe_account" => $key));
241 if (!empty($vatcleaned)) {
243 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 (".((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).
")";
253 $resql = $this->db->query($sql);
255 $this->error = $this->db->lasterror();
258 $this->error = $e->getMessage();
350 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, $did = 0)
354 dol_syslog(get_class($this).
"::getPaymentIntent", LOG_INFO, 1);
358 if (empty($status)) {
359 $service =
'StripeTest';
361 $service =
'StripeLive';
364 $arrayzerounitcurrency = array(
'BIF',
'CLP',
'DJF',
'GNF',
'JPY',
'KMF',
'KRW',
'MGA',
'PYG',
'RWF',
'VND',
'VUV',
'XAF',
'XOF',
'XPF');
365 if (!in_array($currency_code, $arrayzerounitcurrency)) {
366 $stripeamount = $amount * 100;
368 $stripeamount = $amount;
380 if (!in_array($currency_code, $arrayzerounitcurrency)) {
381 $stripefee = round($fee * 100);
383 $stripefee = round($fee);
386 $paymentintent =
null;
395 $sql =
"SELECT pi.ext_payment_id, pi.entity, pi.fk_facture, pi.sourcetype, pi.ext_payment_site";
396 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_demande as pi";
397 $sql .=
" WHERE pi.fk_facture = ".((int) $object->id);
398 $sql .=
" AND pi.sourcetype = '".$this->db->escape($object->element).
"'";
399 $sql .=
" AND pi.entity IN (".getEntity(
'societe').
")";
400 $sql .=
" AND pi.ext_payment_site = '".$this->db->escape($service).
"'";
402 dol_syslog(get_class($this).
"::getPaymentIntent search stripe payment intent for object id = ".$object->id, LOG_DEBUG);
403 $resql = $this->db->query($sql);
405 $num = $this->db->num_rows($resql);
407 $obj = $this->db->fetch_object($resql);
408 $intent = $obj->ext_payment_id;
410 dol_syslog(get_class($this).
"::getPaymentIntent found existing payment intent record");
413 global $stripearrayofkeysbyenv;
414 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
418 $paymentintent = \Stripe\PaymentIntent::retrieve($intent);
420 $paymentintent = \Stripe\PaymentIntent::retrieve($intent, array(
"stripe_account" => $key));
424 $this->error = $e->getMessage();
430 if (empty($paymentintent)) {
433 $metadata = array(
'dol_version'=>DOL_VERSION,
'dol_entity'=>$conf->entity,
'ipaddress'=>$ipaddress);
434 if (is_object($object)) {
435 $metadata[
'dol_type'] = $object->element;
436 $metadata[
'dol_id'] = $object->id;
437 if (is_object($object->thirdparty) && $object->thirdparty->id > 0) {
438 $metadata[
'dol_thirdparty_id'] = $object->thirdparty->id;
443 $paymentmethodtypes = array(
"card");
444 $descriptor =
dol_trunc($tag, 10,
'right',
'UTF-8', 1);
446 $paymentmethodtypes[] =
"sepa_debit";
450 $paymentmethodtypes[] =
"klarna";
453 $paymentmethodtypes[] =
"bancontact";
456 $paymentmethodtypes[] =
"ideal";
459 $paymentmethodtypes[] =
"giropay";
462 $paymentmethodtypes[] =
"sofort";
465 $paymentmethodtypes = array(
"card_present");
468 global $dolibarr_main_url_root;
470 $dataforintent = array(
471 "confirm" => $confirmnow,
472 "confirmation_method" => $mode,
473 "amount" => $stripeamount,
474 "currency" => $currency_code,
475 "payment_method_types" => $paymentmethodtypes,
483 "description" => $description,
485 "setup_future_usage" =>
"on_session",
486 "metadata" => $metadata
489 $dataforintent[
"statement_descriptor_suffix"] = $descriptor;
490 $dataforintent[
"statement_descriptor"] = $descriptor;
492 if (!is_null($customer)) {
493 $dataforintent[
"customer"] = $customer;
499 unset($dataforintent[
'setup_future_usage']);
503 $dataforintent[
"off_session"] =
true;
506 unset($dataforintent[
'setup_future_usage']);
509 unset($dataforintent[
'setup_future_usage']);
512 unset($dataforintent[
'setup_future_usage']);
513 $dataforintent[
"capture_method"] =
"manual";
514 $dataforintent[
"confirmation_method"] =
"manual";
516 if (!is_null($payment_method)) {
517 $dataforintent[
"payment_method"] = $payment_method;
518 $description .=
' - '.$payment_method;
521 if ($conf->entity !=
getDolGlobalInt(
'STRIPECONNECT_PRINCIPAL') && $stripefee > 0) {
522 $dataforintent[
"application_fee_amount"] = $stripefee;
524 if ($usethirdpartyemailforreceiptemail && is_object($object) && $object->thirdparty->email) {
525 $dataforintent[
"receipt_email"] = $object->thirdparty->email;
530 global $stripearrayofkeysbyenv;
531 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
533 $arrayofoptions = array();
534 if (empty($noidempotency_key)) {
535 $arrayofoptions[
"idempotency_key"] = $description;
539 $arrayofoptions[
"stripe_account"] = $key;
542 dol_syslog(
"dataforintent to create paymentintent = ".var_export($dataforintent,
true));
544 $paymentintent = \Stripe\PaymentIntent::create($dataforintent, $arrayofoptions);
547 if (is_object($object)) {
548 $paymentintentalreadyexists = 0;
552 dol_syslog(get_class($this).
"::getPaymentIntent search if payment intent already in prelevement_demande", LOG_DEBUG);
554 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"prelevement_demande SET";
555 $sql .=
" ext_payment_site = '".$this->db->escape($service).
"',";
556 $sql .=
" ext_payment_id = '".$this->db->escape($paymentintent->id).
"'";
557 $sql .=
" WHERE rowid = ".((int) $did);
559 $resql = $this->db->query($sql);
561 $paymentintentalreadyexists++;
568 dol_syslog(get_class($this).
"::getPaymentIntent search if payment intent already in prelevement_demande", LOG_DEBUG);
570 $sql =
"SELECT pi.rowid";
571 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_demande as pi";
572 $sql .=
" WHERE pi.entity IN (".getEntity(
'societe').
")";
573 $sql .=
" AND pi.ext_payment_site = '".$this->db->escape($service).
"'";
574 $sql .=
" AND pi.ext_payment_id = '".$this->db->escape($paymentintent->id).
"'";
576 $resql = $this->db->query($sql);
578 $num = $this->db->num_rows($resql);
580 $obj = $this->db->fetch_object($resql);
582 $paymentintentalreadyexists++;
592 if (!$error && !$paymentintentalreadyexists) {
594 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"prelevement_demande (date_demande, fk_user_demande, ext_payment_id, fk_facture, sourcetype, entity, ext_payment_site, amount)";
595 $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).
")";
596 $resql = $this->db->query($sql);
599 $this->error = $this->db->lasterror();
600 dol_syslog(get_class($this).
"::PaymentIntent failed to insert paymentintent with id=".$paymentintent->id.
" into database.", LOG_ERR);
604 $_SESSION[
"stripe_payment_intent"] = $paymentintent;
606 }
catch (
Stripe\Error\Card $e) {
608 $this->error = $e->getMessage();
609 $this->code = $e->getStripeCode();
610 $this->declinecode = $e->getDeclineCode();
619 $this->error = $e->getMessage();
621 $this->declinecode =
'';
625 dol_syslog(get_class($this).
"::getPaymentIntent return error=".$error.
" this->error=".$this->error, LOG_INFO, -1);
628 return $paymentintent;
652 public function getSetupIntent($description, $object, $customer, $key, $status, $usethirdpartyemailforreceiptemail = 0, $confirmnow =
false)
656 dol_syslog(
"getSetupIntent description=".$description.
' confirmnow='.$confirmnow, LOG_INFO, 1);
660 if (empty($status)) {
661 $service =
'StripeTest';
663 $service =
'StripeLive';
668 if (empty($setupintent)) {
670 $metadata = array(
'dol_version'=>DOL_VERSION,
'dol_entity'=>$conf->entity,
'ipaddress'=>$ipaddress);
671 if (is_object($object)) {
672 $metadata[
'dol_type'] = $object->element;
673 $metadata[
'dol_id'] = $object->id;
674 if (is_object($object->thirdparty) && $object->thirdparty->id > 0) {
675 $metadata[
'dol_thirdparty_id'] = $object->thirdparty->id;
680 $paymentmethodtypes = array(
"card");
682 $paymentmethodtypes[] =
"sepa_debit";
685 $paymentmethodtypes[] =
"bancontact";
688 $paymentmethodtypes[] =
"ideal";
692 $paymentmethodtypes[] =
"sofort";
695 global $dolibarr_main_url_root;
697 $dataforintent = array(
698 "confirm" => $confirmnow,
699 "payment_method_types" => $paymentmethodtypes,
707 "usage" =>
"off_session",
708 "metadata" => $metadata
710 if (!is_null($customer)) {
711 $dataforintent[
"customer"] = $customer;
713 if (!is_null($description)) {
714 $dataforintent[
"description"] = $description;
720 if ($usethirdpartyemailforreceiptemail && is_object($object) && $object->thirdparty->email) {
721 $dataforintent[
"receipt_email"] = $object->thirdparty->email;
726 global $stripearrayofkeysbyenv;
727 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
729 dol_syslog(
"getSetupIntent ".$stripearrayofkeysbyenv[$status][
'publishable_key'], LOG_DEBUG);
734 $setupintent = \Stripe\SetupIntent::create($dataforintent, array());
737 $setupintent = \Stripe\SetupIntent::create($dataforintent, array(
"stripe_account" => $key));
790 $this->error = $e->getMessage();
795 dol_syslog(
"getSetupIntent ".(is_object($setupintent) ? $setupintent->id :
''), LOG_INFO, -1);
798 dol_syslog(
"getSetupIntent return error=".$error, LOG_INFO, -1);
816 global $conf, $user, $langs;
820 $sql =
"SELECT sa.stripe_card_ref, sa.proprio, sa.exp_date_month, sa.exp_date_year, sa.number, sa.cvn";
821 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib as sa";
822 $sql .=
" WHERE sa.rowid = ".((int) $object->id);
823 $sql .=
" AND sa.type = 'card'";
825 dol_syslog(get_class($this).
"::cardStripe search stripe card id for paymentmode id=".$object->id.
", stripeacc=".$stripeacc.
", status=".$status.
", createifnotlinkedtostripe=".$createifnotlinkedtostripe, LOG_DEBUG);
826 $resql = $this->db->query($sql);
828 $num = $this->db->num_rows($resql);
830 $obj = $this->db->fetch_object($resql);
831 $cardref = $obj->stripe_card_ref;
832 dol_syslog(get_class($this).
"::cardStripe cardref=".$cardref);
835 if (empty($stripeacc)) {
836 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
837 $card = $cu->sources->retrieve($cardref);
839 $card = \Stripe\PaymentMethod::retrieve($cardref);
842 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
844 $card = $cu->sources->retrieve($cardref);
847 $card = \Stripe\PaymentMethod::retrieve($cardref);
851 $this->error = $e->getMessage();
854 } elseif ($createifnotlinkedtostripe) {
856 $exp_date_month = $obj->exp_date_month;
857 $exp_date_year = $obj->exp_date_year;
858 $number = $obj->number;
860 $cardholdername = $obj->proprio;
864 $dataforcard = array(
867 'exp_month'=>$exp_date_month,
868 'exp_year'=>$exp_date_year,
871 'name'=>$cardholdername
874 'dol_type'=>$object->element,
875 'dol_id'=>$object->id,
876 'dol_version'=>DOL_VERSION,
877 'dol_entity'=>$conf->entity,
878 'ipaddress'=>$ipaddress
886 if (empty($stripeacc)) {
888 dol_syslog(
"Try to create card with dataforcard = ".json_encode($dataforcard));
889 $card = $cu->sources->create($dataforcard);
891 $this->error =
'Creation of card on Stripe has failed';
895 if (!empty($stripeacc)) {
896 $connect = $stripeacc.
'/';
898 $url =
'https://dashboard.stripe.com/'.$connect.
'test/customers/'.$cu->id;
900 $url =
'https://dashboard.stripe.com/'.$connect.
'customers/'.$cu->id;
902 $urtoswitchonstripe =
'<a href="'.$url.
'" target="_stripe">'.
img_picto($langs->trans(
'ShowInStripe'),
'globe').
'</a>';
905 $this->error = str_replace(
'{s1}', $urtoswitchonstripe, $langs->trans(
'CreationOfPaymentModeMustBeDoneFromStripeInterface',
'{s1}'));
909 dol_syslog(
"Try to create card with dataforcard = ".json_encode($dataforcard));
910 $card = $cu->sources->create($dataforcard, array(
"stripe_account" => $stripeacc));
912 $this->error =
'Creation of card on Stripe has failed';
916 if (!empty($stripeacc)) {
917 $connect = $stripeacc.
'/';
919 $url =
'https://dashboard.stripe.com/'.$connect.
'test/customers/'.$cu->id;
921 $url =
'https://dashboard.stripe.com/'.$connect.
'customers/'.$cu->id;
923 $urtoswitchonstripe =
'<a href="'.$url.
'" target="_stripe">'.
img_picto($langs->trans(
'ShowInStripe'),
'globe').
'</a>';
926 $this->error = str_replace(
'{s1}', $urtoswitchonstripe, $langs->trans(
'CreationOfPaymentModeMustBeDoneFromStripeInterface',
'{s1}'));
931 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"societe_rib";
932 $sql .=
" SET stripe_card_ref = '".$this->db->escape($card->id).
"', card_type = '".$this->db->escape($card->brand).
"',";
933 $sql .=
" country_code = '".$this->db->escape($card->country).
"',";
934 $sql .=
" approved = ".($card->cvc_check ==
'pass' ? 1 : 0);
935 $sql .=
" WHERE rowid = ".((int) $object->id);
936 $sql .=
" AND type = 'card'";
937 $resql = $this->db->query($sql);
939 $this->error = $this->db->lasterror();
943 $this->error = $e->getMessage();
971 $sql =
"SELECT sa.stripe_card_ref, sa.proprio, sa.iban_prefix as iban, sa.rum";
972 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib as sa";
973 $sql .=
" WHERE sa.rowid = ".((int) $object->id);
974 $sql .=
" AND sa.type = 'ban'";
977 $soc->fetch($object->fk_soc);
979 dol_syslog(get_class($this).
"::sepaStripe search stripe ban id for paymentmode id=".$object->id.
", stripeacc=".$stripeacc.
", status=".$status.
", createifnotlinkedtostripe=".$createifnotlinkedtostripe, LOG_DEBUG);
980 $resql = $this->db->query($sql);
982 $num = $this->db->num_rows($resql);
984 $obj = $this->db->fetch_object($resql);
985 $cardref = $obj->stripe_card_ref;
986 dol_syslog(get_class($this).
"::sepaStripe paymentmode=".$cardref);
989 if (empty($stripeacc)) {
990 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
991 $sepa = $cu->sources->retrieve($cardref);
993 $sepa = \Stripe\PaymentMethod::retrieve($cardref);
996 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
998 $sepa = $cu->sources->retrieve($cardref);
1001 $sepa = \Stripe\PaymentMethod::retrieve($cardref);
1005 $this->error = $e->getMessage();
1008 } elseif ($createifnotlinkedtostripe) {
1011 $metadata = array(
'dol_version'=>DOL_VERSION,
'dol_entity'=>$conf->entity,
'ipaddress'=>$ipaddress);
1012 if (is_object($object)) {
1013 $metadata[
'dol_type'] = $object->element;
1014 $metadata[
'dol_id'] = $object->id;
1015 $metadata[
'dol_thirdparty_id'] = $soc->id;
1018 $description =
'SEPA for IBAN '.$iban;
1020 $dataforcard = array(
1021 'type'=>
'sepa_debit',
1022 "sepa_debit" => array(
'iban' => $iban),
1023 'billing_details' => array(
1024 'name' => $soc->name,
1025 'email' => !empty($soc->email) ? $soc->email :
"",
1027 "metadata" => $metadata
1030 if (!empty($soc->town)) {
1031 $dataforcard[
'billing_details'][
'address'][
'city']=$soc->town;
1033 if (!empty($soc->country_code)) {
1034 $dataforcard[
'billing_details'][
'address'][
'country']=$soc->country_code;
1036 if (!empty($soc->address)) {
1037 $dataforcard[
'billing_details'][
'address'][
'line1']=$soc->address;
1039 if (!empty($soc->zip)) {
1040 $dataforcard[
'billing_details'][
'address'][
'postal_code']=$soc->zip;
1042 if (!empty($soc->state)) {
1043 $dataforcard[
'billing_details'][
'address'][
'state']=$soc->state;
1051 $service =
'StripeTest';
1054 $service =
'StripeLive';
1058 global $stripearrayofkeysbyenv;
1059 $stripeacc = $stripearrayofkeysbyenv[$servicestatus][
'secret_key'];
1061 dol_syslog(
"Try to create sepa_debit with data = ".json_encode($dataforcard));
1063 $s = new \Stripe\StripeClient($stripeacc);
1067 $sepa = $s->paymentMethods->create($dataforcard);
1069 $this->error =
'Creation of payment method sepa_debit on Stripe has failed';
1073 $dataforintent = array([
'description'=> $description,
'payment_method_types' => [
'sepa_debit'],
'customer' => $cu->id,
'payment_method' => $sepa->id],
'metadata'=>$metadata);
1075 $cs = $s->setupIntents->create($dataforintent);
1077 $cs = $s->setupIntents->confirm($cs->id, [
'mandate_data' => [
'customer_acceptance' => [
'type' =>
'offline']]]);
1081 $this->error =
'Link SEPA <-> Customer failed';
1083 dol_syslog(
"Update the payment mode of the customer");
1088 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"societe_rib";
1089 $sql .=
" SET stripe_card_ref = '".$this->db->escape($sepa->id).
"',";
1090 $sql .=
" card_type = 'sepa_debit',";
1091 $sql .=
" stripe_account= '" . $this->db->escape($cu->id .
"@" . $stripeacc) .
"',";
1092 $sql .=
" ext_payment_site = '".$this->db->escape($service).
"'";
1093 if (!empty($cs->mandate)) {
1094 $mandateservice = new \Stripe\Mandate($stripeacc);
1095 $mandate = $mandateservice->retrieve($cs->mandate);
1096 if (is_object($mandate) && is_object($mandate->payment_method_details) && is_object($mandate->payment_method_details->sepa_debit)) {
1097 $refmandate = $mandate->payment_method_details->sepa_debit->reference;
1099 $sql .=
", rum = '".$this->db->escape($refmandate).
"'";
1101 $sql .=
", comment = '".$this->db->escape($cs->mandate).
"'";
1102 $sql .=
", date_rum = '".$this->db->idate(
dol_now()).
"'";
1104 $sql .=
" WHERE rowid = ".((int) $object->id);
1105 $sql .=
" AND type = 'ban'";
1106 $resql = $this->db->query($sql);
1108 $this->error = $this->db->lasterror();
1114 $this->error =
'Stripe error: '.$e->getMessage().
'. Check the BAN information.';
1143 public function createPaymentStripe($amount, $currency, $origin, $item, $source, $customer, $account, $status = 0, $usethirdpartyemailforreceiptemail = 0, $capture =
true)
1149 if (empty($status)) {
1150 $service =
'StripeTest';
1152 $service =
'StripeLive';
1155 $sql =
"SELECT sa.key_account as key_account, sa.fk_soc, sa.entity";
1156 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account as sa";
1157 $sql .=
" WHERE sa.key_account = '".$this->db->escape($customer).
"'";
1159 $sql .=
" AND sa.site = 'stripe' AND sa.status = ".((int) $status);
1161 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1162 $result = $this->db->query($sql);
1164 if ($this->db->num_rows($result)) {
1165 $obj = $this->db->fetch_object($result);
1166 $key = $obj->fk_soc;
1174 $arrayzerounitcurrency = array(
'BIF',
'CLP',
'DJF',
'GNF',
'JPY',
'KMF',
'KRW',
'MGA',
'PYG',
'RWF',
'VND',
'VUV',
'XAF',
'XOF',
'XPF');
1175 if (!in_array($currency, $arrayzerounitcurrency)) {
1176 $stripeamount = $amount * 100;
1178 $stripeamount = $amount;
1181 $societe =
new Societe($this->db);
1183 $societe->fetch($key);
1188 if ($origin ==
'order') {
1190 $order->fetch($item);
1192 $description =
"ORD=".$ref.
".CUS=".$societe->id.
".PM=stripe";
1193 } elseif ($origin ==
'invoice') {
1194 $invoice =
new Facture($this->db);
1195 $invoice->fetch($item);
1196 $ref = $invoice->ref;
1197 $description =
"INV=".$ref.
".CUS=".$societe->id.
".PM=stripe";
1203 "dol_id" => (
string) $item,
1204 "dol_type" => (
string) $origin,
1205 "dol_thirdparty_id" => (
string) $societe->id,
1206 'dol_thirdparty_name' => $societe->name,
1207 'dol_version' => DOL_VERSION,
1208 'dol_entity' => $conf->entity,
1209 'ipaddress' => $ipaddress
1211 $return =
new Stripe($this->db);
1214 global $stripearrayofkeysbyenv;
1215 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
1217 if (empty($conf->stripeconnect->enabled)) {
1218 if (preg_match(
'/pm_/i', $source)) {
1219 $stripecard = $source;
1220 $amountstripe = $stripeamount;
1223 $amounttopay = $amount;
1224 $servicestatus = $status;
1226 dol_syslog(
"* createPaymentStripe get stripeacc", LOG_DEBUG);
1227 $stripeacc = $stripe->getStripeAccount($service);
1229 dol_syslog(
"* createPaymentStripe Create payment for customer ".$customer->id.
" on source card ".$stripecard->id.
", amounttopay=".$amounttopay.
", amountstripe=".$amountstripe.
", FULLTAG=".$FULLTAG, LOG_DEBUG);
1232 $paymentintent = $stripe->getPaymentIntent($amounttopay, $currency, $FULLTAG, $description, $invoice, $customer->id, $stripeacc, $servicestatus, 0,
'automatic',
true, $stripecard->id, 1);
1235 if ($paymentintent->status ==
'succeeded') {
1236 $charge->status =
'ok';
1238 $charge->status =
'failed';
1239 $charge->failure_code = $stripe->code;
1240 $charge->failure_message = $stripe->error;
1241 $charge->failure_declinecode = $stripe->declinecode;
1242 $stripefailurecode = $stripe->code;
1243 $stripefailuremessage = $stripe->error;
1244 $stripefailuredeclinecode = $stripe->declinecode;
1246 } elseif (preg_match(
'/acct_/i', $source)) {
1247 $charge = \Stripe\Charge::create(array(
1248 "amount" =>
"$stripeamount",
1249 "currency" =>
"$currency",
1250 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
1251 "description" =>
"Stripe payment: ".$description,
1252 "capture" => $capture,
1253 "metadata" => $metadata,
1254 "source" =>
"$source"
1257 $paymentarray = array(
1258 "amount" =>
"$stripeamount",
1259 "currency" =>
"$currency",
1260 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
1261 "description" =>
"Stripe payment: ".$description,
1262 "capture" => $capture,
1263 "metadata" => $metadata,
1264 "source" =>
"$source",
1265 "customer" =>
"$customer"
1268 if ($societe->email && $usethirdpartyemailforreceiptemail) {
1269 $paymentarray[
"receipt_email"] = $societe->email;
1272 $charge = \Stripe\Charge::create($paymentarray, array(
"idempotency_key" =>
"$description"));
1276 $fee = $amount * ($conf->global->STRIPE_APPLICATION_FEE_PERCENT / 100) + $conf->global->STRIPE_APPLICATION_FEE;
1277 if ($fee >= $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL && $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL > $conf->global->STRIPE_APPLICATION_FEE_MINIMAL) {
1278 $fee = $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL;
1279 } elseif ($fee < $conf->global->STRIPE_APPLICATION_FEE_MINIMAL) {
1280 $fee = $conf->global->STRIPE_APPLICATION_FEE_MINIMAL;
1283 if (!in_array($currency, $arrayzerounitcurrency)) {
1284 $stripefee = round($fee * 100);
1286 $stripefee = round($fee);
1289 $paymentarray = array(
1290 "amount" =>
"$stripeamount",
1291 "currency" =>
"$currency",
1292 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
1293 "description" =>
"Stripe payment: ".$description,
1294 "capture" => $capture,
1295 "metadata" => $metadata,
1296 "source" =>
"$source",
1297 "customer" =>
"$customer"
1299 if ($conf->entity != $conf->global->STRIPECONNECT_PRINCIPAL && $stripefee > 0) {
1300 $paymentarray[
"application_fee_amount"] = $stripefee;
1302 if ($societe->email && $usethirdpartyemailforreceiptemail) {
1303 $paymentarray[
"receipt_email"] = $societe->email;
1306 if (preg_match(
'/pm_/i', $source)) {
1307 $stripecard = $source;
1308 $amountstripe = $stripeamount;
1311 $amounttopay = $amount;
1312 $servicestatus = $status;
1314 dol_syslog(
"* createPaymentStripe get stripeacc", LOG_DEBUG);
1315 $stripeacc = $stripe->getStripeAccount($service);
1317 dol_syslog(
"* createPaymentStripe Create payment on card ".$stripecard->id.
", amounttopay=".$amounttopay.
", amountstripe=".$amountstripe.
", FULLTAG=".$FULLTAG, LOG_DEBUG);
1320 $paymentintent = $stripe->getPaymentIntent($amounttopay, $currency, $FULLTAG, $description, $invoice, $customer->id, $stripeacc, $servicestatus, 0,
'automatic',
true, $stripecard->id, 1);
1323 if ($paymentintent->status ==
'succeeded') {
1324 $charge->status =
'ok';
1325 $charge->id = $paymentintent->id;
1327 $charge->status =
'failed';
1328 $charge->failure_code = $stripe->code;
1329 $charge->failure_message = $stripe->error;
1330 $charge->failure_declinecode = $stripe->declinecode;
1333 $charge = \Stripe\Charge::create($paymentarray, array(
"idempotency_key" =>
"$description",
"stripe_account" =>
"$account"));
1336 if (isset($charge->id)) {
1339 $return->statut =
'success';
1340 $return->id = $charge->id;
1342 if (preg_match(
'/pm_/i', $source)) {
1343 $return->message =
'Payment retrieved by card status = '.$charge->status;
1345 if ($charge->source->type ==
'card') {
1346 $return->message = $charge->source->card->brand.
" ....".$charge->source->card->last4;
1347 } elseif ($charge->source->type ==
'three_d_secure') {
1348 $stripe =
new Stripe($this->db);
1349 $src = \Stripe\Source::retrieve(
"".$charge->source->three_d_secure->card, array(
1350 "stripe_account" => $stripe->getStripeAccount($service)
1352 $return->message = $src->card->brand.
" ....".$src->card->last4;
1354 $return->message = $charge->id;
1357 }
catch (\
Stripe\Error\Card $e) {
1358 include DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1360 $body = $e->getJsonBody();
1361 $err = $body[
'error'];
1363 $return->statut =
'error';
1364 $return->id = $err[
'charge'];
1365 $return->type = $err[
'type'];
1366 $return->code = $err[
'code'];
1367 $return->message = $err[
'message'];
1368 $body =
"Error: <br>".$return->id.
" ".$return->message.
" ";
1369 $subject =
'[Alert] Payment error using Stripe';
1370 $cmailfile =
new CMailFile($subject, $conf->global->ONLINE_PAYMENT_SENDEMAIL, $conf->global->MAIN_INFO_SOCIETE_MAIL, $body);
1371 $cmailfile->sendfile();
1374 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1375 }
catch (\
Stripe\Error\RateLimit $e) {
1378 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1379 }
catch (\
Stripe\Error\InvalidRequest $e) {
1382 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1383 }
catch (\
Stripe\Error\Authentication $e) {
1387 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1388 }
catch (\
Stripe\Error\ApiConnection $e) {
1391 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1392 }
catch (\
Stripe\Error\Base $e) {
1396 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1400 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');