184 dol_syslog(
"customerStripe is called with the parameter object that is not loaded");
191 global $stripearrayofkeysbyenv;
192 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
194 $sql =
"SELECT sa.key_account as key_account, sa.entity";
195 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account as sa";
196 $sql .=
" WHERE sa.fk_soc = ".((int)
$object->id);
197 $sql .=
" AND sa.entity IN (".getEntity(
'societe').
")";
198 $sql .=
" AND sa.site = 'stripe' AND sa.status = ".((int) $status);
199 $sql .=
" AND (sa.site_account IS NULL OR sa.site_account = '' OR sa.site_account = '".$this->db->escape($stripearrayofkeysbyenv[$status][
'publishable_key']).
"')";
200 $sql .=
" AND sa.key_account IS NOT NULL AND sa.key_account <> ''";
202 dol_syslog(get_class($this).
"::customerStripe search stripe customer id for thirdparty id=".
$object->id, LOG_DEBUG);
203 $resql = $this->db->query($sql);
205 $num = $this->db->num_rows($resql);
207 $obj = $this->db->fetch_object($resql);
208 $tiers = $obj->key_account;
210 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']);
215 $customer = \Stripe\Customer::retrieve(array(
'id' =>
"$tiers",
'expand[]' =>
'sources'));
218 $customer = \Stripe\Customer::retrieve(array(
'id' =>
"$tiers",
'expand[]' =>
'sources'), array(
"stripe_account" => $key));
222 $this->error = $e->getMessage();
224 } elseif ($createifnotlinkedtostripe) {
227 $dataforcustomer = array(
229 "description" =>
$object->name,
230 "metadata" => array(
'dol_id' =>
$object->id,
'dol_version' => DOL_VERSION,
'dol_entity' => $conf->entity,
'ipaddress' => $ipaddress)
250 global $stripearrayofkeysbyenv;
251 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
254 $customer = \Stripe\Customer::create($dataforcustomer);
256 $customer = \Stripe\Customer::create($dataforcustomer, array(
"stripe_account" => $key));
261 if (!empty($vatcleaned)) {
263 if (
$object->country_code && $isineec) {
265 $customer->createTaxId($customer->id, array(
'type' =>
'eu_vat',
'value' => $vatcleaned));
271 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"societe_account (fk_soc, login, key_account, site, site_account, status, entity, date_creation, fk_user_creat)";
272 $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).
")";
273 $resql = $this->db->query($sql);
275 $this->error = $this->db->lasterror();
278 $this->error = $e->getMessage();
370 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)
374 dol_syslog(get_class($this).
"::getPaymentIntent description=".$description, LOG_INFO, 1);
378 if (empty($status)) {
379 $service =
'StripeTest';
381 $service =
'StripeLive';
384 $arrayzerounitcurrency = array(
'BIF',
'CLP',
'DJF',
'GNF',
'JPY',
'KMF',
'KRW',
'MGA',
'PYG',
'RWF',
'VND',
'VUV',
'XAF',
'XOF',
'XPF');
385 if (!in_array($currency_code, $arrayzerounitcurrency)) {
386 $stripeamount = $amount * 100;
388 $stripeamount = $amount;
400 if (!in_array($currency_code, $arrayzerounitcurrency)) {
401 $stripefee = round($fee * 100);
403 $stripefee = round($fee);
406 $paymentintent =
null;
415 $sql =
"SELECT pi.ext_payment_id, pi.entity, pi.fk_facture, pi.sourcetype, pi.ext_payment_site";
416 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_demande as pi";
417 $sql .=
" WHERE pi.fk_facture = ".((int)
$object->id);
418 $sql .=
" AND pi.sourcetype = '".$this->db->escape(
$object->element).
"'";
419 $sql .=
" AND pi.entity IN (".getEntity(
'societe').
")";
420 $sql .=
" AND pi.ext_payment_site = '".$this->db->escape($service).
"'";
422 dol_syslog(get_class($this).
"::getPaymentIntent search stripe payment intent for object id = ".
$object->id, LOG_DEBUG);
423 $resql = $this->db->query($sql);
425 $num = $this->db->num_rows($resql);
427 $obj = $this->db->fetch_object($resql);
428 $intent = $obj->ext_payment_id;
430 dol_syslog(get_class($this).
"::getPaymentIntent found existing payment intent record");
433 global $stripearrayofkeysbyenv;
434 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
438 $paymentintent = \Stripe\PaymentIntent::retrieve($intent);
440 $paymentintent = \Stripe\PaymentIntent::retrieve($intent, array(
"stripe_account" => $key));
444 $this->error = $e->getMessage();
450 if (empty($paymentintent)) {
453 $metadata = array(
'dol_version' => DOL_VERSION,
'dol_entity' => $conf->entity,
'ipaddress' => $ipaddress,
'dol_noidempotency' => (
int) $noidempotency_key);
455 $metadata[
'dol_type'] =
$object->element;
456 $metadata[
'dol_id'] =
$object->id;
457 if (is_object(
$object->thirdparty) &&
$object->thirdparty->id > 0) {
458 $metadata[
'dol_thirdparty_id'] =
$object->thirdparty->id;
463 $paymentmethodtypes = array(
"card");
464 $descriptor =
dol_trunc($tag, 10,
'right',
'UTF-8', 1);
466 $paymentmethodtypes[] =
"sepa_debit";
470 $paymentmethodtypes[] =
"klarna";
473 $paymentmethodtypes[] =
"bancontact";
476 $paymentmethodtypes[] =
"ideal";
479 $paymentmethodtypes[] =
"giropay";
482 $paymentmethodtypes[] =
"sofort";
485 $paymentmethodtypes = array(
"card_present");
488 global $dolibarr_main_url_root;
490 $descriptioninpaymentintent = $description;
492 $dataforintent = array(
493 "confirm" => $confirmnow,
494 "confirmation_method" => $mode,
495 "amount" => $stripeamount,
496 "currency" => $currency_code,
497 "payment_method_types" => $paymentmethodtypes,
505 "description" => $descriptioninpaymentintent,
507 "setup_future_usage" =>
"on_session",
508 "metadata" => $metadata
511 $dataforintent[
"statement_descriptor_suffix"] = $descriptor;
512 $dataforintent[
"statement_descriptor"] = $descriptor;
514 if (!is_null($customer)) {
515 $dataforintent[
"customer"] = $customer;
521 unset($dataforintent[
'setup_future_usage']);
525 $dataforintent[
"off_session"] =
true;
528 unset($dataforintent[
'setup_future_usage']);
531 unset($dataforintent[
'setup_future_usage']);
534 unset($dataforintent[
'setup_future_usage']);
535 $dataforintent[
"capture_method"] =
"manual";
536 $dataforintent[
"confirmation_method"] =
"manual";
538 if (!is_null($payment_method)) {
539 $dataforintent[
"payment_method"] = $payment_method;
540 $description .=
' - '.$payment_method;
543 if ($conf->entity !=
getDolGlobalInt(
'STRIPECONNECT_PRINCIPAL') && $stripefee > 0) {
544 $dataforintent[
"application_fee_amount"] = $stripefee;
546 if ($usethirdpartyemailforreceiptemail && is_object(
$object) &&
$object->thirdparty->email) {
547 $dataforintent[
"receipt_email"] =
$object->thirdparty->email;
552 global $stripearrayofkeysbyenv;
553 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
555 $arrayofoptions = array();
556 if (empty($noidempotency_key)) {
557 $arrayofoptions[
"idempotency_key"] = $descriptioninpaymentintent;
561 $arrayofoptions[
"stripe_account"] = $key;
564 dol_syslog(get_class($this).
"::getPaymentIntent ".$stripearrayofkeysbyenv[$status][
'publishable_key'], LOG_DEBUG);
565 dol_syslog(get_class($this).
"::getPaymentIntent dataforintent to create paymentintent = ".var_export($dataforintent,
true));
567 $paymentintent = \Stripe\PaymentIntent::create($dataforintent, $arrayofoptions);
571 $paymentintentalreadyexists = 0;
575 dol_syslog(get_class($this).
"::getPaymentIntent search if payment intent already in prelevement_demande", LOG_DEBUG);
577 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"prelevement_demande SET";
578 $sql .=
" ext_payment_site = '".$this->db->escape($service).
"',";
579 $sql .=
" ext_payment_id = '".$this->db->escape($paymentintent->id).
"'";
580 $sql .=
" WHERE rowid = ".((int) $did);
582 $resql = $this->db->query($sql);
584 $paymentintentalreadyexists++;
591 dol_syslog(get_class($this).
"::getPaymentIntent search if payment intent already in prelevement_demande", LOG_DEBUG);
593 $sql =
"SELECT pi.rowid";
594 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_demande as pi";
595 $sql .=
" WHERE pi.entity IN (".getEntity(
'societe').
")";
596 $sql .=
" AND pi.ext_payment_site = '".$this->db->escape($service).
"'";
597 $sql .=
" AND pi.ext_payment_id = '".$this->db->escape($paymentintent->id).
"'";
599 $resql = $this->db->query($sql);
601 $num = $this->db->num_rows($resql);
603 $obj = $this->db->fetch_object($resql);
605 $paymentintentalreadyexists++;
615 if (!$error && !$paymentintentalreadyexists) {
617 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"prelevement_demande (date_demande, fk_user_demande, ext_payment_id, fk_facture, sourcetype, entity, ext_payment_site, amount)";
618 $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).
")";
619 $resql = $this->db->query($sql);
622 $this->error = $this->db->lasterror();
623 dol_syslog(get_class($this).
"::PaymentIntent failed to insert paymentintent with id=".$paymentintent->id.
" into database.", LOG_ERR);
627 $_SESSION[
"stripe_payment_intent"] = $paymentintent;
631 $this->error = $e->getMessage();
632 $this->code = $e->getStripeCode();
633 $this->declinecode = $e->getDeclineCode();
642 $this->error = $e->getMessage();
644 $this->declinecode =
'';
648 dol_syslog(get_class($this).
"::getPaymentIntent return error=".$error.
" this->error=".$this->error, LOG_INFO, -1);
651 return $paymentintent;
675 public function getSetupIntent($description,
$object, $customer, $key, $status, $usethirdpartyemailforreceiptemail = 0, $confirmnow =
false)
679 $noidempotency_key = 1;
681 dol_syslog(
"getSetupIntent description=".$description.
' confirmnow='.json_encode($confirmnow), LOG_INFO, 1);
685 if (empty($status)) {
686 $service =
'StripeTest';
688 $service =
'StripeLive';
693 if (empty($setupintent)) {
695 $metadata = array(
'dol_version' => DOL_VERSION,
'dol_entity' => $conf->entity,
'ipaddress' => $ipaddress,
'dol_noidempotency' => (
int) $noidempotency_key);
697 $metadata[
'dol_type'] =
$object->element;
698 $metadata[
'dol_id'] =
$object->id;
699 if (is_object(
$object->thirdparty) &&
$object->thirdparty->id > 0) {
700 $metadata[
'dol_thirdparty_id'] =
$object->thirdparty->id;
705 $paymentmethodtypes = array(
"card");
707 $paymentmethodtypes[] =
"sepa_debit";
710 $paymentmethodtypes[] =
"bancontact";
713 $paymentmethodtypes[] =
"ideal";
717 $paymentmethodtypes[] =
"sofort";
720 global $dolibarr_main_url_root;
722 $descriptioninsetupintent = $description;
724 $dataforintent = array(
725 "confirm" => $confirmnow,
726 "payment_method_types" => $paymentmethodtypes,
734 "usage" =>
"off_session",
735 "metadata" => $metadata
737 if (!is_null($customer)) {
738 $dataforintent[
"customer"] = $customer;
740 if (!is_null($description)) {
741 $dataforintent[
"description"] = $descriptioninsetupintent;
747 if ($usethirdpartyemailforreceiptemail && is_object(
$object) &&
$object->thirdparty->email) {
748 $dataforintent[
"receipt_email"] =
$object->thirdparty->email;
753 global $stripearrayofkeysbyenv;
754 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
756 dol_syslog(get_class($this).
"::getSetupIntent ".$stripearrayofkeysbyenv[$status][
'publishable_key'], LOG_DEBUG);
757 dol_syslog(get_class($this).
"::getSetupIntent dataforintent to create setupintent = ".var_export($dataforintent,
true));
762 $setupintent = \Stripe\SetupIntent::create($dataforintent, array());
765 $setupintent = \Stripe\SetupIntent::create($dataforintent, array(
"stripe_account" => $key));
818 $this->error = $e->getMessage();
823 dol_syslog(
"getSetupIntent ".(is_object($setupintent) ? $setupintent->id :
''), LOG_INFO, -1);
826 dol_syslog(
"getSetupIntent return error=".$error, LOG_INFO, -1);
844 global $conf, $user, $langs;
848 $sql =
"SELECT sa.stripe_card_ref, sa.proprio as owner_name, sa.exp_date_month, sa.exp_date_year, sa.number, sa.cvn";
849 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib as sa";
850 $sql .=
" WHERE sa.rowid = ".((int)
$object->id);
851 $sql .=
" AND sa.type = 'card'";
853 dol_syslog(get_class($this).
"::cardStripe search stripe card id for paymentmode id=".
$object->id.
", stripeacc=".$stripeacc.
", status=".$status.
", createifnotlinkedtostripe=".$createifnotlinkedtostripe, LOG_DEBUG);
854 $resql = $this->db->query($sql);
856 $num = $this->db->num_rows($resql);
858 $obj = $this->db->fetch_object($resql);
859 $cardref = $obj->stripe_card_ref;
860 dol_syslog(get_class($this).
"::cardStripe cardref=".$cardref);
863 if (empty($stripeacc)) {
864 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
865 $card = $cu->sources->retrieve($cardref);
867 $card = \Stripe\PaymentMethod::retrieve($cardref);
870 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
872 $card = $cu->sources->retrieve($cardref);
875 $card = \Stripe\PaymentMethod::retrieve($cardref);
879 $this->error = $e->getMessage();
882 } elseif ($createifnotlinkedtostripe) {
884 $exp_date_month = $obj->exp_date_month;
885 $exp_date_year = $obj->exp_date_year;
886 $number = $obj->number;
888 $cardholdername = $obj->owner_name;
892 $dataforcard = array(
895 'exp_month' => $exp_date_month,
896 'exp_year' => $exp_date_year,
899 'name' => $cardholdername
902 'dol_type' =>
$object->element,
904 'dol_version' => DOL_VERSION,
905 'dol_entity' => $conf->entity,
906 'ipaddress' => $ipaddress
914 if (empty($stripeacc)) {
916 dol_syslog(
"Try to create card with dataforcard = ".json_encode($dataforcard));
917 $card = $cu->sources->create($dataforcard);
919 $this->error =
'Creation of card on Stripe has failed';
923 if (!empty($stripeacc)) {
924 $connect = $stripeacc.
'/';
926 $url =
'https://dashboard.stripe.com/'.$connect.
'test/customers/'.$cu->id;
928 $url =
'https://dashboard.stripe.com/'.$connect.
'customers/'.$cu->id;
930 $urtoswitchonstripe =
'<a href="'.$url.
'" target="_stripe">'.
img_picto($langs->trans(
'ShowInStripe'),
'globe').
'</a>';
933 $this->error = str_replace(
'{s1}', $urtoswitchonstripe, $langs->trans(
'CreationOfPaymentModeMustBeDoneFromStripeInterface',
'{s1}'));
937 dol_syslog(
"Try to create card with dataforcard = ".json_encode($dataforcard));
938 $card = $cu->sources->create($dataforcard, array(
"stripe_account" => $stripeacc));
940 $this->error =
'Creation of card on Stripe has failed';
944 if (!empty($stripeacc)) {
945 $connect = $stripeacc.
'/';
947 $url =
'https://dashboard.stripe.com/'.$connect.
'test/customers/'.$cu->id;
949 $url =
'https://dashboard.stripe.com/'.$connect.
'customers/'.$cu->id;
951 $urtoswitchonstripe =
'<a href="'.$url.
'" target="_stripe">'.
img_picto($langs->trans(
'ShowInStripe'),
'globe').
'</a>';
954 $this->error = str_replace(
'{s1}', $urtoswitchonstripe, $langs->trans(
'CreationOfPaymentModeMustBeDoneFromStripeInterface',
'{s1}'));
959 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"societe_rib";
960 $sql .=
" SET stripe_card_ref = '".$this->db->escape($card->id).
"', card_type = '".$this->db->escape($card->brand).
"',";
961 $sql .=
" country_code = '".$this->db->escape($card->country).
"',";
962 $sql .=
" approved = ".($card->cvc_check ==
'pass' ? 1 : 0);
963 $sql .=
" WHERE rowid = ".((int)
$object->id);
964 $sql .=
" AND type = 'card'";
965 $resql = $this->db->query($sql);
967 $this->error = $this->db->lasterror();
971 $this->error = $e->getMessage();
999 $sql =
"SELECT sa.stripe_card_ref, sa.proprio, sa.iban_prefix as iban, sa.rum";
1000 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib as sa";
1001 $sql .=
" WHERE sa.rowid = ".((int)
$object->id);
1002 $sql .=
" AND sa.type = 'ban'";
1004 $soc =
new Societe($this->db);
1007 dol_syslog(get_class($this).
"::sepaStripe search stripe ban id for paymentmode id=".
$object->id.
", stripeacc=".$stripeacc.
", status=".$status.
", createifnotlinkedtostripe=".$createifnotlinkedtostripe, LOG_DEBUG);
1008 $resql = $this->db->query($sql);
1010 $num = $this->db->num_rows($resql);
1012 $obj = $this->db->fetch_object($resql);
1013 $cardref = $obj->stripe_card_ref;
1014 dol_syslog(get_class($this).
"::sepaStripe paymentmode=".$cardref);
1017 if (empty($stripeacc)) {
1018 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
1019 $sepa = $cu->sources->retrieve($cardref);
1021 $sepa = \Stripe\PaymentMethod::retrieve($cardref);
1024 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
1026 $sepa = $cu->sources->retrieve($cardref);
1029 $sepa = \Stripe\PaymentMethod::retrieve($cardref);
1033 $this->error = $e->getMessage();
1036 } elseif ($createifnotlinkedtostripe) {
1039 $metadata = array(
'dol_version' => DOL_VERSION,
'dol_entity' => $conf->entity,
'ipaddress' => $ipaddress);
1041 $metadata[
'dol_type'] =
$object->element;
1042 $metadata[
'dol_id'] =
$object->id;
1043 $metadata[
'dol_thirdparty_id'] = $soc->id;
1046 $description =
'SEPA for IBAN '.$iban;
1048 $dataforcard = array(
1049 'type' =>
'sepa_debit',
1050 "sepa_debit" => array(
'iban' => $iban),
1051 'billing_details' => array(
1052 'name' => $soc->name,
1053 'email' => !empty($soc->email) ? $soc->email :
"",
1055 "metadata" => $metadata
1058 if (!empty($soc->town)) {
1059 $dataforcard[
'billing_details'][
'address'][
'city'] = $soc->town;
1061 if (!empty($soc->country_code)) {
1062 $dataforcard[
'billing_details'][
'address'][
'country'] = $soc->country_code;
1064 if (!empty($soc->address)) {
1065 $dataforcard[
'billing_details'][
'address'][
'line1'] = $soc->address;
1067 if (!empty($soc->zip)) {
1068 $dataforcard[
'billing_details'][
'address'][
'postal_code'] = $soc->zip;
1070 if (!empty($soc->state)) {
1071 $dataforcard[
'billing_details'][
'address'][
'state'] = $soc->state;
1079 $service =
'StripeTest';
1082 $service =
'StripeLive';
1086 global $stripearrayofkeysbyenv;
1087 $stripeacc = $stripearrayofkeysbyenv[$servicestatus][
'secret_key'];
1089 dol_syslog(
"Try to create sepa_debit with data = ".json_encode($dataforcard));
1091 $s = new \Stripe\StripeClient($stripeacc);
1095 $sepa = $s->paymentMethods->create($dataforcard);
1097 $this->error =
'Creation of payment method sepa_debit on Stripe has failed';
1101 $dataforintent = array(0 => [
'description' => $description,
'payment_method_types' => [
'sepa_debit'],
'customer' => $cu->id,
'payment_method' => $sepa->id],
'metadata' => $metadata);
1103 $cs = $s->setupIntents->create($dataforintent);
1105 $cs = $s->setupIntents->confirm($cs->id, [
'mandate_data' => [
'customer_acceptance' => [
'type' =>
'offline']]]);
1109 $this->error =
'Link SEPA <-> Customer failed';
1111 dol_syslog(
"Update the payment mode of the customer");
1116 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"societe_rib";
1117 $sql .=
" SET stripe_card_ref = '".$this->db->escape($sepa->id).
"',";
1118 $sql .=
" card_type = 'sepa_debit',";
1119 $sql .=
" stripe_account= '" . $this->db->escape($cu->id .
"@" . $stripeacc) .
"',";
1120 $sql .=
" ext_payment_site = '".$this->db->escape($service).
"'";
1121 if (!empty($cs->mandate)) {
1122 $mandateservice = new \Stripe\Mandate($stripeacc);
1123 $mandate = $mandateservice->retrieve($cs->mandate);
1124 if (is_object($mandate) && is_object($mandate->payment_method_details) && is_object($mandate->payment_method_details->sepa_debit)) {
1125 $refmandate = $mandate->payment_method_details->sepa_debit->reference;
1127 $sql .=
", rum = '".$this->db->escape($refmandate).
"'";
1129 $sql .=
", comment = '".$this->db->escape($cs->mandate).
"'";
1130 $sql .=
", date_rum = '".$this->db->idate(
dol_now()).
"'";
1132 $sql .=
" WHERE rowid = ".((int)
$object->id);
1133 $sql .=
" AND type = 'ban'";
1134 $resql = $this->db->query($sql);
1136 $this->error = $this->db->lasterror();
1142 $this->error =
'Stripe error: '.$e->getMessage().
'. Check the BAN information.';
1172 public function createPaymentStripe($amount, $currency, $origin, $item, $source, $customer, $account, $status = 0, $usethirdpartyemailforreceiptemail = 0, $capture =
true)
1178 if (empty($status)) {
1179 $service =
'StripeTest';
1181 $service =
'StripeLive';
1184 $sql =
"SELECT sa.key_account as key_account, sa.fk_soc, sa.entity";
1185 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account as sa";
1186 $sql .=
" WHERE sa.key_account = '".$this->db->escape($customer).
"'";
1188 $sql .=
" AND sa.site = 'stripe' AND sa.status = ".((int) $status);
1190 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1191 $result = $this->db->query($sql);
1193 if ($this->db->num_rows($result)) {
1194 $obj = $this->db->fetch_object($result);
1195 $key = $obj->fk_soc;
1203 $arrayzerounitcurrency = array(
'BIF',
'CLP',
'DJF',
'GNF',
'JPY',
'KMF',
'KRW',
'MGA',
'PYG',
'RWF',
'VND',
'VUV',
'XAF',
'XOF',
'XPF');
1204 if (!in_array($currency, $arrayzerounitcurrency)) {
1205 $stripeamount = $amount * 100;
1207 $stripeamount = $amount;
1210 $societe =
new Societe($this->db);
1212 $societe->fetch($key);
1217 if ($origin ==
'order') {
1219 $order->fetch($item);
1221 $description =
"ORD=".$ref.
".CUS=".$societe->id.
".PM=stripe";
1222 } elseif ($origin ==
'invoice') {
1223 $invoice =
new Facture($this->db);
1224 $invoice->fetch($item);
1225 $ref = $invoice->ref;
1226 $description =
"INV=".$ref.
".CUS=".$societe->id.
".PM=stripe";
1232 "dol_id" => (
string) $item,
1233 "dol_type" => (
string) $origin,
1234 "dol_thirdparty_id" => (
string) $societe->id,
1235 'dol_thirdparty_name' => $societe->name,
1236 'dol_version' => DOL_VERSION,
1237 'dol_entity' => $conf->entity,
1238 'ipaddress' => $ipaddress
1240 $return =
new Stripe($this->db);
1243 global $stripearrayofkeysbyenv;
1244 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
1246 if (empty($conf->stripeconnect->enabled)) {
1247 if (preg_match(
'/pm_/i', $source)) {
1248 $stripecard = $source;
1249 $amountstripe = $stripeamount;
1252 $amounttopay = $amount;
1253 $servicestatus = $status;
1255 dol_syslog(
"* createPaymentStripe get stripeacc", LOG_DEBUG);
1256 $stripeacc = $stripe->getStripeAccount($service);
1258 dol_syslog(
"* createPaymentStripe Create payment for customer ".$customer->id.
" on source card ".$stripecard->id.
", amounttopay=".$amounttopay.
", amountstripe=".$amountstripe.
", FULLTAG=".$FULLTAG, LOG_DEBUG);
1261 $paymentintent = $stripe->getPaymentIntent($amounttopay, $currency, $FULLTAG, $description, $invoice, $customer->id, $stripeacc, $servicestatus, 0,
'automatic',
true, $stripecard->id, 1);
1264 if ($paymentintent->status ==
'succeeded') {
1265 $charge->status =
'ok';
1267 $charge->status =
'failed';
1268 $charge->failure_code = $stripe->code;
1269 $charge->failure_message = $stripe->error;
1270 $charge->failure_declinecode = $stripe->declinecode;
1271 $stripefailurecode = $stripe->code;
1272 $stripefailuremessage = $stripe->error;
1273 $stripefailuredeclinecode = $stripe->declinecode;
1275 } elseif (preg_match(
'/acct_/i', $source)) {
1276 $charge = \Stripe\Charge::create(array(
1277 "amount" =>
"$stripeamount",
1278 "currency" =>
"$currency",
1279 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
1280 "description" =>
"Stripe payment: ".$description,
1281 "capture" => $capture,
1282 "metadata" => $metadata,
1283 "source" =>
"$source"
1286 $paymentarray = array(
1287 "amount" =>
"$stripeamount",
1288 "currency" =>
"$currency",
1289 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
1290 "description" =>
"Stripe payment: ".$description,
1291 "capture" => $capture,
1292 "metadata" => $metadata,
1293 "source" =>
"$source",
1294 "customer" =>
"$customer"
1297 if ($societe->email && $usethirdpartyemailforreceiptemail) {
1298 $paymentarray[
"receipt_email"] = $societe->email;
1301 $charge = \Stripe\Charge::create($paymentarray, array(
"idempotency_key" =>
"$description"));
1305 $fee = $amount * ($conf->global->STRIPE_APPLICATION_FEE_PERCENT / 100) + $conf->global->STRIPE_APPLICATION_FEE;
1306 if ($fee >= $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL && $conf->global->STRIPE_APPLICATION_FEE_MAXIMAL > $conf->global->STRIPE_APPLICATION_FEE_MINIMAL) {
1308 } elseif ($fee < $conf->global->STRIPE_APPLICATION_FEE_MINIMAL) {
1312 if (!in_array($currency, $arrayzerounitcurrency)) {
1313 $stripefee = round($fee * 100);
1315 $stripefee = round($fee);
1318 $paymentarray = array(
1319 "amount" =>
"$stripeamount",
1320 "currency" =>
"$currency",
1321 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
1322 "description" =>
"Stripe payment: ".$description,
1323 "capture" => $capture,
1324 "metadata" => $metadata,
1325 "source" =>
"$source",
1326 "customer" =>
"$customer"
1328 if ($conf->entity != $conf->global->STRIPECONNECT_PRINCIPAL && $stripefee > 0) {
1329 $paymentarray[
"application_fee_amount"] = $stripefee;
1331 if ($societe->email && $usethirdpartyemailforreceiptemail) {
1332 $paymentarray[
"receipt_email"] = $societe->email;
1335 if (preg_match(
'/pm_/i', $source)) {
1336 $stripecard = $source;
1337 $amountstripe = $stripeamount;
1340 $amounttopay = $amount;
1341 $servicestatus = $status;
1343 dol_syslog(
"* createPaymentStripe get stripeacc", LOG_DEBUG);
1344 $stripeacc = $stripe->getStripeAccount($service);
1346 dol_syslog(
"* createPaymentStripe Create payment on card ".$stripecard->id.
", amounttopay=".$amounttopay.
", amountstripe=".$amountstripe.
", FULLTAG=".$FULLTAG, LOG_DEBUG);
1349 $paymentintent = $stripe->getPaymentIntent($amounttopay, $currency, $FULLTAG, $description, $invoice, $customer->id, $stripeacc, $servicestatus, 0,
'automatic',
true, $stripecard->id, 1);
1352 if ($paymentintent->status ==
'succeeded') {
1353 $charge->status =
'ok';
1354 $charge->id = $paymentintent->id;
1356 $charge->status =
'failed';
1357 $charge->failure_code = $stripe->code;
1358 $charge->failure_message = $stripe->error;
1359 $charge->failure_declinecode = $stripe->declinecode;
1362 $charge = \Stripe\Charge::create($paymentarray, array(
"idempotency_key" =>
"$description",
"stripe_account" =>
"$account"));
1365 '@phan-var-force stdClass|\Stripe\Charge $charge';
1366 if (isset($charge->id)) {
1369 $return->result =
'success';
1370 $return->id = $charge->id;
1372 if (preg_match(
'/pm_/i', $source)) {
1373 $return->message =
'Payment retrieved by card status = '.$charge->status;
1375 if ($charge->source->type ==
'card') {
1376 $return->message = $charge->source->card->brand.
" ....".$charge->source->card->last4;
1377 } elseif ($charge->source->type ==
'three_d_secure') {
1378 $stripe =
new Stripe($this->db);
1379 $src = \Stripe\Source::retrieve(
"".$charge->source->three_d_secure->card, array(
1380 "stripe_account" => $stripe->getStripeAccount($service)
1382 $return->message = $src->card->brand.
" ....".$src->card->last4;
1384 $return->message = $charge->id;
1388 include DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1390 $body = $e->getJsonBody();
1391 $err = $body[
'error'];
1393 $return->result =
'error';
1394 $return->id = $err[
'charge'];
1395 $return->type = $err[
'type'];
1396 $return->code = $err[
'code'];
1397 $return->message = $err[
'message'];
1398 $body =
"Error: <br>".$return->id.
" ".$return->message.
" ";
1399 $subject =
'[Alert] Payment error using Stripe';
1400 $cmailfile =
new CMailFile($subject, $conf->global->ONLINE_PAYMENT_SENDEMAIL, $conf->global->MAIN_INFO_SOCIETE_MAIL, $body);
1401 $cmailfile->sendfile();
1404 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1408 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1412 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1417 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1421 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1426 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1430 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');