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 <> ''";
201 $sql .=
" ORDER BY sa.site_account DESC, sa.rowid DESC";
203 dol_syslog(get_class($this).
"::customerStripe search stripe customer id for thirdparty id=".
$object->id, LOG_DEBUG);
204 $resql = $this->db->query($sql);
206 $num = $this->db->num_rows($resql);
208 $obj = $this->db->fetch_object($resql);
209 $tiers = $obj->key_account;
211 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']);
216 $customer = \Stripe\Customer::retrieve(array(
'id' =>
"$tiers",
'expand[]' =>
'sources'));
219 $customer = \Stripe\Customer::retrieve(array(
'id' =>
"$tiers",
'expand[]' =>
'sources'), array(
"stripe_account" => $key));
223 $this->error = $e->getMessage();
225 } elseif ($createifnotlinkedtostripe) {
228 $dataforcustomer = array(
230 "description" =>
$object->name,
231 "metadata" => array(
'dol_id' =>
$object->id,
'dol_version' => DOL_VERSION,
'dol_entity' =>
$conf->entity,
'ipaddress' => $ipaddress)
251 global $stripearrayofkeysbyenv;
252 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
255 $customer = \Stripe\Customer::create($dataforcustomer);
257 $customer = \Stripe\Customer::create($dataforcustomer, array(
"stripe_account" => $key));
262 if (!empty($vatcleaned)) {
264 if (
$object->country_code && $isineec) {
266 $customer->createTaxId($customer->id, array(
'type' =>
'eu_vat',
'value' => $vatcleaned));
272 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"societe_account (fk_soc, login, key_account, site, site_account, status, entity, date_creation, fk_user_creat)";
273 $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).
")";
274 $resql = $this->db->query($sql);
276 $this->error = $this->db->lasterror();
279 $this->error = $e->getMessage();
371 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)
375 dol_syslog(get_class($this).
"::getPaymentIntent description=".$description, LOG_INFO, 1);
379 if (empty($status)) {
380 $service =
'StripeTest';
382 $service =
'StripeLive';
385 $arrayzerounitcurrency = array(
'BIF',
'CLP',
'DJF',
'GNF',
'JPY',
'KMF',
'KRW',
'MGA',
'PYG',
'RWF',
'VND',
'VUV',
'XAF',
'XOF',
'XPF');
386 if (!in_array($currency_code, $arrayzerounitcurrency)) {
387 $stripeamount = $amount * 100;
389 $stripeamount = $amount;
401 if (!in_array($currency_code, $arrayzerounitcurrency)) {
402 $stripefee = round($fee * 100);
404 $stripefee = round($fee);
407 $paymentintent =
null;
416 $sql =
"SELECT pi.ext_payment_id, pi.entity, pi.fk_facture, pi.sourcetype, pi.ext_payment_site";
417 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_demande as pi";
418 $sql .=
" WHERE pi.fk_facture = ".((int)
$object->id);
419 $sql .=
" AND pi.sourcetype = '".$this->db->escape(
$object->element).
"'";
420 $sql .=
" AND pi.entity IN (".getEntity(
'societe').
")";
421 $sql .=
" AND pi.ext_payment_site = '".$this->db->escape($service).
"'";
423 dol_syslog(get_class($this).
"::getPaymentIntent search stripe payment intent for object id = ".
$object->id, LOG_DEBUG);
424 $resql = $this->db->query($sql);
426 $num = $this->db->num_rows($resql);
428 $obj = $this->db->fetch_object($resql);
429 $intent = $obj->ext_payment_id;
431 dol_syslog(get_class($this).
"::getPaymentIntent found existing payment intent record");
434 global $stripearrayofkeysbyenv;
435 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
439 $paymentintent = \Stripe\PaymentIntent::retrieve($intent);
441 $paymentintent = \Stripe\PaymentIntent::retrieve($intent, array(
"stripe_account" => $key));
445 $this->error = $e->getMessage();
451 if (empty($paymentintent)) {
454 $metadata = array(
'dol_version' => DOL_VERSION,
'dol_entity' =>
$conf->entity,
'ipaddress' => $ipaddress,
'dol_noidempotency' => (
int) $noidempotency_key);
456 $metadata[
'dol_type'] =
$object->element;
457 $metadata[
'dol_id'] =
$object->id;
458 if (is_object(
$object->thirdparty) &&
$object->thirdparty->id > 0) {
459 $metadata[
'dol_thirdparty_id'] =
$object->thirdparty->id;
464 $paymentmethodtypes = array(
"card");
465 $descriptor =
dol_trunc($tag, 10,
'right',
'UTF-8', 1);
467 $paymentmethodtypes[] =
"sepa_debit";
471 $paymentmethodtypes[] =
"klarna";
474 $paymentmethodtypes[] =
"bancontact";
477 $paymentmethodtypes[] =
"ideal";
480 $paymentmethodtypes[] =
"giropay";
483 $paymentmethodtypes[] =
"sofort";
486 $paymentmethodtypes = array(
"card_present");
489 global $dolibarr_main_url_root;
491 $descriptioninpaymentintent = $description;
493 $dataforintent = array(
494 "confirm" => $confirmnow,
495 "confirmation_method" => $mode,
496 "amount" => $stripeamount,
497 "currency" => $currency_code,
498 "payment_method_types" => $paymentmethodtypes,
506 "description" => $descriptioninpaymentintent,
508 "setup_future_usage" =>
"on_session",
509 "metadata" => $metadata
512 $dataforintent[
"statement_descriptor_suffix"] = $descriptor;
513 $dataforintent[
"statement_descriptor"] = $descriptor;
515 if (!is_null($customer)) {
516 $dataforintent[
"customer"] = $customer;
522 unset($dataforintent[
'setup_future_usage']);
526 $dataforintent[
"off_session"] =
true;
529 unset($dataforintent[
'setup_future_usage']);
532 unset($dataforintent[
'setup_future_usage']);
535 unset($dataforintent[
'setup_future_usage']);
536 $dataforintent[
"capture_method"] =
"manual";
537 $dataforintent[
"confirmation_method"] =
"manual";
539 if (!is_null($payment_method)) {
540 $dataforintent[
"payment_method"] = $payment_method;
541 $description .=
' - '.$payment_method;
545 $dataforintent[
"application_fee_amount"] = $stripefee;
547 if ($usethirdpartyemailforreceiptemail && is_object(
$object) &&
$object->thirdparty->email) {
548 $dataforintent[
"receipt_email"] =
$object->thirdparty->email;
553 global $stripearrayofkeysbyenv;
554 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
556 $arrayofoptions = array();
557 if (empty($noidempotency_key)) {
558 $arrayofoptions[
"idempotency_key"] = $descriptioninpaymentintent;
562 $arrayofoptions[
"stripe_account"] = $key;
565 dol_syslog(get_class($this).
"::getPaymentIntent ".$stripearrayofkeysbyenv[$status][
'publishable_key'], LOG_DEBUG);
566 dol_syslog(get_class($this).
"::getPaymentIntent dataforintent to create paymentintent = ".var_export($dataforintent,
true));
568 $paymentintent = \Stripe\PaymentIntent::create($dataforintent, $arrayofoptions);
572 $paymentintentalreadyexists = 0;
576 dol_syslog(get_class($this).
"::getPaymentIntent search if payment intent already in prelevement_demande", LOG_DEBUG);
578 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"prelevement_demande SET";
579 $sql .=
" ext_payment_site = '".$this->db->escape($service).
"',";
580 $sql .=
" ext_payment_id = '".$this->db->escape($paymentintent->id).
"'";
581 $sql .=
" WHERE rowid = ".((int) $did);
583 $resql = $this->db->query($sql);
585 $paymentintentalreadyexists++;
592 dol_syslog(get_class($this).
"::getPaymentIntent search if payment intent already in prelevement_demande", LOG_DEBUG);
594 $sql =
"SELECT pi.rowid";
595 $sql .=
" FROM ".MAIN_DB_PREFIX.
"prelevement_demande as pi";
596 $sql .=
" WHERE pi.entity IN (".getEntity(
'societe').
")";
597 $sql .=
" AND pi.ext_payment_site = '".$this->db->escape($service).
"'";
598 $sql .=
" AND pi.ext_payment_id = '".$this->db->escape($paymentintent->id).
"'";
600 $resql = $this->db->query($sql);
602 $num = $this->db->num_rows($resql);
604 $obj = $this->db->fetch_object($resql);
606 $paymentintentalreadyexists++;
616 if (!$error && !$paymentintentalreadyexists) {
618 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"prelevement_demande (date_demande, fk_user_demande, ext_payment_id, fk_facture, sourcetype, entity, ext_payment_site, amount)";
619 $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).
")";
620 $resql = $this->db->query($sql);
623 $this->error = $this->db->lasterror();
624 dol_syslog(get_class($this).
"::PaymentIntent failed to insert paymentintent with id=".$paymentintent->id.
" into database.", LOG_ERR);
628 $_SESSION[
"stripe_payment_intent"] = $paymentintent;
632 $this->error = $e->getMessage();
633 $this->code = $e->getStripeCode();
634 $this->declinecode = $e->getDeclineCode();
643 $this->error = $e->getMessage();
645 $this->declinecode =
'';
649 dol_syslog(get_class($this).
"::getPaymentIntent return error=".$error.
" this->error=".$this->error, LOG_INFO, -1);
652 return $paymentintent;
676 public function getSetupIntent($description,
$object, $customer, $key, $status, $usethirdpartyemailforreceiptemail = 0, $confirmnow =
false)
680 $noidempotency_key = 1;
682 dol_syslog(
"getSetupIntent description=".$description.
' confirmnow='.json_encode($confirmnow), LOG_INFO, 1);
686 if (empty($status)) {
687 $service =
'StripeTest';
689 $service =
'StripeLive';
694 if (empty($setupintent)) {
696 $metadata = array(
'dol_version' => DOL_VERSION,
'dol_entity' =>
$conf->entity,
'ipaddress' => $ipaddress,
'dol_noidempotency' => (
int) $noidempotency_key);
698 $metadata[
'dol_type'] =
$object->element;
699 $metadata[
'dol_id'] =
$object->id;
700 if (is_object(
$object->thirdparty) &&
$object->thirdparty->id > 0) {
701 $metadata[
'dol_thirdparty_id'] =
$object->thirdparty->id;
706 $paymentmethodtypes = array(
"card");
708 $paymentmethodtypes[] =
"sepa_debit";
711 $paymentmethodtypes[] =
"bancontact";
714 $paymentmethodtypes[] =
"ideal";
718 $paymentmethodtypes[] =
"sofort";
721 global $dolibarr_main_url_root;
723 $descriptioninsetupintent = $description;
725 $dataforintent = array(
726 "confirm" => $confirmnow,
727 "payment_method_types" => $paymentmethodtypes,
735 "usage" =>
"off_session",
736 "metadata" => $metadata
738 if (!is_null($customer)) {
739 $dataforintent[
"customer"] = $customer;
741 if (!is_null($description)) {
742 $dataforintent[
"description"] = $descriptioninsetupintent;
748 if ($usethirdpartyemailforreceiptemail && is_object(
$object) &&
$object->thirdparty->email) {
749 $dataforintent[
"receipt_email"] =
$object->thirdparty->email;
754 global $stripearrayofkeysbyenv;
755 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
757 dol_syslog(get_class($this).
"::getSetupIntent ".$stripearrayofkeysbyenv[$status][
'publishable_key'], LOG_DEBUG);
758 dol_syslog(get_class($this).
"::getSetupIntent dataforintent to create setupintent = ".var_export($dataforintent,
true));
763 $setupintent = \Stripe\SetupIntent::create($dataforintent, array());
766 $setupintent = \Stripe\SetupIntent::create($dataforintent, array(
"stripe_account" => $key));
819 $this->error = $e->getMessage();
824 dol_syslog(
"getSetupIntent ".(is_object($setupintent) ? $setupintent->id :
''), LOG_INFO, -1);
827 dol_syslog(
"getSetupIntent return error=".$error, LOG_INFO, -1);
845 global
$conf, $user, $langs;
849 $sql =
"SELECT sa.stripe_card_ref, sa.proprio as owner_name, sa.exp_date_month, sa.exp_date_year, sa.number, sa.cvn";
850 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib as sa";
851 $sql .=
" WHERE sa.rowid = ".((int)
$object->id);
852 $sql .=
" AND sa.type = 'card'";
854 dol_syslog(get_class($this).
"::cardStripe search stripe card id for paymentmode id=".
$object->id.
", stripeacc=".$stripeacc.
", status=".$status.
", createifnotlinkedtostripe=".$createifnotlinkedtostripe, LOG_DEBUG);
855 $resql = $this->db->query($sql);
857 $num = $this->db->num_rows($resql);
859 $obj = $this->db->fetch_object($resql);
860 $cardref = $obj->stripe_card_ref;
861 dol_syslog(get_class($this).
"::cardStripe cardref=".$cardref);
864 if (empty($stripeacc)) {
865 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
866 $card = $cu->sources->retrieve($cardref);
868 $card = \Stripe\PaymentMethod::retrieve($cardref);
871 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
873 $card = $cu->sources->retrieve($cardref);
876 $card = \Stripe\PaymentMethod::retrieve($cardref);
880 $this->error = $e->getMessage();
883 } elseif ($createifnotlinkedtostripe) {
885 $exp_date_month = $obj->exp_date_month;
886 $exp_date_year = $obj->exp_date_year;
887 $number = $obj->number;
889 $cardholdername = $obj->owner_name;
893 $dataforcard = array(
896 'exp_month' => $exp_date_month,
897 'exp_year' => $exp_date_year,
900 'name' => $cardholdername
903 'dol_type' =>
$object->element,
905 'dol_version' => DOL_VERSION,
906 'dol_entity' =>
$conf->entity,
907 'ipaddress' => $ipaddress
915 if (empty($stripeacc)) {
917 dol_syslog(
"Try to create card with dataforcard = ".json_encode($dataforcard));
918 $card = $cu->sources->create($dataforcard);
920 $this->error =
'Creation of card on Stripe has failed';
924 if (!empty($stripeacc)) {
925 $connect = $stripeacc.
'/';
927 $url =
'https://dashboard.stripe.com/'.$connect.
'test/customers/'.$cu->id;
929 $url =
'https://dashboard.stripe.com/'.$connect.
'customers/'.$cu->id;
931 $urtoswitchonstripe =
'<a href="'.$url.
'" target="_stripe">'.
img_picto($langs->trans(
'ShowInStripe'),
'globe').
'</a>';
934 $this->error = str_replace(
'{s1}', $urtoswitchonstripe, $langs->trans(
'CreationOfPaymentModeMustBeDoneFromStripeInterface',
'{s1}'));
938 dol_syslog(
"Try to create card with dataforcard = ".json_encode($dataforcard));
939 $card = $cu->sources->create($dataforcard, array(
"stripe_account" => $stripeacc));
941 $this->error =
'Creation of card on Stripe has failed';
945 if (!empty($stripeacc)) {
946 $connect = $stripeacc.
'/';
948 $url =
'https://dashboard.stripe.com/'.$connect.
'test/customers/'.$cu->id;
950 $url =
'https://dashboard.stripe.com/'.$connect.
'customers/'.$cu->id;
952 $urtoswitchonstripe =
'<a href="'.$url.
'" target="_stripe">'.
img_picto($langs->trans(
'ShowInStripe'),
'globe').
'</a>';
955 $this->error = str_replace(
'{s1}', $urtoswitchonstripe, $langs->trans(
'CreationOfPaymentModeMustBeDoneFromStripeInterface',
'{s1}'));
960 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"societe_rib";
961 $sql .=
" SET stripe_card_ref = '".$this->db->escape($card->id).
"', card_type = '".$this->db->escape($card->brand).
"',";
962 $sql .=
" country_code = '".$this->db->escape($card->country).
"',";
963 $sql .=
" approved = ".($card->cvc_check ==
'pass' ? 1 : 0);
964 $sql .=
" WHERE rowid = ".((int)
$object->id);
965 $sql .=
" AND type = 'card'";
966 $resql = $this->db->query($sql);
968 $this->error = $this->db->lasterror();
972 $this->error = $e->getMessage();
1000 $sql =
"SELECT sa.stripe_card_ref, sa.proprio, sa.iban_prefix as iban, sa.rum";
1001 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_rib as sa";
1002 $sql .=
" WHERE sa.rowid = ".((int)
$object->id);
1003 $sql .=
" AND sa.type = 'ban'";
1005 $soc =
new Societe($this->db);
1008 dol_syslog(get_class($this).
"::sepaStripe search stripe ban id for paymentmode id=".
$object->id.
", stripeacc=".$stripeacc.
", status=".$status.
", createifnotlinkedtostripe=".$createifnotlinkedtostripe, LOG_DEBUG);
1009 $resql = $this->db->query($sql);
1011 $num = $this->db->num_rows($resql);
1013 $obj = $this->db->fetch_object($resql);
1014 $cardref = $obj->stripe_card_ref;
1016 dol_syslog(get_class($this).
"::sepaStripe paymentmode=".$cardref);
1020 if (empty($stripeacc)) {
1021 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
1022 $sepa = $cu->sources->retrieve($cardref);
1024 $sepa = \Stripe\PaymentMethod::retrieve($cardref);
1027 if (!preg_match(
'/^pm_/', $cardref) && !empty($cu->sources)) {
1029 $sepa = $cu->sources->retrieve($cardref);
1032 $sepa = \Stripe\PaymentMethod::retrieve($cardref);
1036 $this->error = $e->getMessage();
1039 } elseif ($createifnotlinkedtostripe) {
1042 $metadata = array(
'dol_version' => DOL_VERSION,
'dol_entity' =>
$conf->entity,
'ipaddress' => $ipaddress);
1044 $metadata[
'dol_type'] =
$object->element;
1045 $metadata[
'dol_id'] =
$object->id;
1046 $metadata[
'dol_thirdparty_id'] = $soc->id;
1049 $description =
'SEPA for IBAN '.$iban;
1051 $dataforcard = array(
1052 'type' =>
'sepa_debit',
1053 "sepa_debit" => array(
'iban' => $iban),
1054 'billing_details' => array(
1055 'name' => $soc->name,
1056 'email' => !empty($soc->email) ? $soc->email :
"",
1058 "metadata" => $metadata
1061 if (!empty($soc->town)) {
1062 $dataforcard[
'billing_details'][
'address'][
'city'] = $soc->town;
1064 if (!empty($soc->country_code)) {
1065 $dataforcard[
'billing_details'][
'address'][
'country'] = $soc->country_code;
1067 if (!empty($soc->address)) {
1068 $dataforcard[
'billing_details'][
'address'][
'line1'] = $soc->address;
1070 if (!empty($soc->zip)) {
1071 $dataforcard[
'billing_details'][
'address'][
'postal_code'] = $soc->zip;
1073 if (!empty($soc->state)) {
1074 $dataforcard[
'billing_details'][
'address'][
'state'] = $soc->state;
1082 $service =
'StripeTest';
1085 $service =
'StripeLive';
1089 global $stripearrayofkeysbyenv;
1090 $stripeacc = $stripearrayofkeysbyenv[$servicestatus][
'secret_key'];
1092 dol_syslog(
"Try to create sepa_debit with data = ".json_encode($dataforcard));
1094 $s = new \Stripe\StripeClient($stripeacc);
1098 $sepa = $s->paymentMethods->create($dataforcard);
1100 $this->error =
'Creation of payment method sepa_debit on Stripe has failed';
1105 $dataforintent = array(0 => [
'description' => $description,
'payment_method_types' => [
'sepa_debit'],
'customer' => $cu->id,
'payment_method' => $sepa->id],
'metadata' => $metadata);
1107 $cs = $s->setupIntents->create($dataforintent);
1109 $cs = $s->setupIntents->confirm($cs->id, [
'mandate_data' => [
'customer_acceptance' => [
'type' =>
'offline']]]);
1113 $this->error =
'Link SEPA <-> Customer failed';
1116 dol_syslog(
"Update the payment mode of the customer");
1121 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"societe_rib";
1122 $sql .=
" SET stripe_card_ref = '".$this->db->escape($sepa->id).
"',";
1123 $sql .=
" card_type = 'sepa_debit',";
1124 $sql .=
" stripe_account= '" . $this->db->escape($cu->id .
"@" . $stripeacc) .
"',";
1125 $sql .=
" ext_payment_site = '".$this->db->escape($service).
"'";
1126 if (!empty($cs->mandate)) {
1127 $mandateservice = new \Stripe\Mandate($stripeacc);
1128 $mandate = $mandateservice->retrieve($cs->mandate);
1129 if (is_object($mandate) && is_object($mandate->payment_method_details) && is_object($mandate->payment_method_details->sepa_debit)) {
1130 $refmandate = $mandate->payment_method_details->sepa_debit->reference;
1132 $sql .=
", rum = '".$this->db->escape($refmandate).
"'";
1134 $sql .=
", comment = '".$this->db->escape($cs->mandate).
"'";
1135 $sql .=
", date_rum = '".$this->db->idate(
dol_now()).
"'";
1137 $sql .=
" WHERE rowid = ".((int)
$object->id);
1138 $sql .=
" AND type = 'ban'";
1139 $resql = $this->db->query($sql);
1141 $this->error = $this->db->lasterror();
1147 $this->error =
'Stripe error: '.$e->getMessage().
'. Check the BAN information.';
1177 public function createPaymentStripe($amount, $currency, $origin, $item, $source, $customer, $account, $status = 0, $usethirdpartyemailforreceiptemail = 0, $capture =
true)
1183 if (empty($status)) {
1184 $service =
'StripeTest';
1186 $service =
'StripeLive';
1189 $sql =
"SELECT sa.key_account as key_account, sa.fk_soc, sa.entity";
1190 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe_account as sa";
1191 $sql .=
" WHERE sa.key_account = '".$this->db->escape($customer).
"'";
1193 $sql .=
" AND sa.site = 'stripe' AND sa.status = ".((int) $status);
1194 $sql .=
" ORDER BY sa.site_account DESC, sa.rowid DESC";
1196 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1197 $result = $this->db->query($sql);
1199 if ($this->db->num_rows($result)) {
1200 $obj = $this->db->fetch_object($result);
1201 $key = $obj->fk_soc;
1209 $arrayzerounitcurrency = array(
'BIF',
'CLP',
'DJF',
'GNF',
'JPY',
'KMF',
'KRW',
'MGA',
'PYG',
'RWF',
'VND',
'VUV',
'XAF',
'XOF',
'XPF');
1210 if (!in_array($currency, $arrayzerounitcurrency)) {
1211 $stripeamount = $amount * 100;
1213 $stripeamount = $amount;
1216 $societe =
new Societe($this->db);
1218 $societe->fetch($key);
1223 if ($origin ==
'order') {
1225 $order->fetch($item);
1227 $description =
"ORD=".$ref.
".CUS=".$societe->id.
".PM=stripe";
1228 } elseif ($origin ==
'invoice') {
1229 $invoice =
new Facture($this->db);
1230 $invoice->fetch($item);
1231 $ref = $invoice->ref;
1232 $description =
"INV=".$ref.
".CUS=".$societe->id.
".PM=stripe";
1238 "dol_id" => (
string) $item,
1239 "dol_type" => (
string) $origin,
1240 "dol_thirdparty_id" => (
string) $societe->id,
1241 'dol_thirdparty_name' => $societe->name,
1242 'dol_version' => DOL_VERSION,
1243 'dol_entity' =>
$conf->entity,
1244 'ipaddress' => $ipaddress
1246 $return =
new Stripe($this->db);
1249 global $stripearrayofkeysbyenv;
1250 \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status][
'secret_key']);
1252 if (empty(
$conf->stripeconnect->enabled)) {
1253 if (preg_match(
'/pm_/i', $source)) {
1254 $stripecard = $source;
1255 $amountstripe = $stripeamount;
1258 $amounttopay = $amount;
1259 $servicestatus = $status;
1261 dol_syslog(
"* createPaymentStripe get stripeacc", LOG_DEBUG);
1262 $stripeacc = $stripe->getStripeAccount($service);
1264 dol_syslog(
"* createPaymentStripe Create payment for customer ".$customer->id.
" on source card ".$stripecard->id.
", amounttopay=".$amounttopay.
", amountstripe=".$amountstripe.
", FULLTAG=".$FULLTAG, LOG_DEBUG);
1267 $paymentintent = $stripe->getPaymentIntent($amounttopay, $currency, $FULLTAG, $description, $invoice, $customer->id, $stripeacc, $servicestatus, 0,
'automatic',
true, $stripecard->id, 1);
1270 if ($paymentintent->status ==
'succeeded') {
1271 $charge->status =
'ok';
1273 $charge->status =
'failed';
1274 $charge->failure_code = $stripe->code;
1275 $charge->failure_message = $stripe->error;
1276 $charge->failure_declinecode = $stripe->declinecode;
1277 $stripefailurecode = $stripe->code;
1278 $stripefailuremessage = $stripe->error;
1279 $stripefailuredeclinecode = $stripe->declinecode;
1281 } elseif (preg_match(
'/acct_/i', $source)) {
1282 $charge = \Stripe\Charge::create(array(
1283 "amount" =>
"$stripeamount",
1284 "currency" =>
"$currency",
1285 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
1286 "description" =>
"Stripe payment: ".$description,
1287 "capture" => $capture,
1288 "metadata" => $metadata,
1289 "source" =>
"$source"
1292 $paymentarray = array(
1293 "amount" =>
"$stripeamount",
1294 "currency" =>
"$currency",
1295 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
1296 "description" =>
"Stripe payment: ".$description,
1297 "capture" => $capture,
1298 "metadata" => $metadata,
1299 "source" =>
"$source",
1300 "customer" =>
"$customer"
1303 if ($societe->email && $usethirdpartyemailforreceiptemail) {
1304 $paymentarray[
"receipt_email"] = $societe->email;
1307 $charge = \Stripe\Charge::create($paymentarray, array(
"idempotency_key" =>
"$description"));
1311 $fee = $amount * (
$conf->global->STRIPE_APPLICATION_FEE_PERCENT / 100) +
$conf->global->STRIPE_APPLICATION_FEE;
1312 if ($fee >=
$conf->global->STRIPE_APPLICATION_FEE_MAXIMAL &&
$conf->global->STRIPE_APPLICATION_FEE_MAXIMAL >
$conf->global->STRIPE_APPLICATION_FEE_MINIMAL) {
1314 } elseif ($fee < $conf->global->STRIPE_APPLICATION_FEE_MINIMAL) {
1318 if (!in_array($currency, $arrayzerounitcurrency)) {
1319 $stripefee = round($fee * 100);
1321 $stripefee = round($fee);
1324 $paymentarray = array(
1325 "amount" =>
"$stripeamount",
1326 "currency" =>
"$currency",
1327 "statement_descriptor_suffix" =>
dol_trunc($description, 10,
'right',
'UTF-8', 1),
1328 "description" =>
"Stripe payment: ".$description,
1329 "capture" => $capture,
1330 "metadata" => $metadata,
1331 "source" =>
"$source",
1332 "customer" =>
"$customer"
1334 if (
$conf->entity !=
$conf->global->STRIPECONNECT_PRINCIPAL && $stripefee > 0) {
1335 $paymentarray[
"application_fee_amount"] = $stripefee;
1337 if ($societe->email && $usethirdpartyemailforreceiptemail) {
1338 $paymentarray[
"receipt_email"] = $societe->email;
1341 if (preg_match(
'/pm_/i', $source)) {
1342 $stripecard = $source;
1343 $amountstripe = $stripeamount;
1346 $amounttopay = $amount;
1347 $servicestatus = $status;
1349 dol_syslog(
"* createPaymentStripe get stripeacc", LOG_DEBUG);
1350 $stripeacc = $stripe->getStripeAccount($service);
1352 dol_syslog(
"* createPaymentStripe Create payment on card ".$stripecard->id.
", amounttopay=".$amounttopay.
", amountstripe=".$amountstripe.
", FULLTAG=".$FULLTAG, LOG_DEBUG);
1355 $paymentintent = $stripe->getPaymentIntent($amounttopay, $currency, $FULLTAG, $description, $invoice, $customer->id, $stripeacc, $servicestatus, 0,
'automatic',
true, $stripecard->id, 1);
1358 if ($paymentintent->status ==
'succeeded') {
1359 $charge->status =
'ok';
1360 $charge->id = $paymentintent->id;
1362 $charge->status =
'failed';
1363 $charge->failure_code = $stripe->code;
1364 $charge->failure_message = $stripe->error;
1365 $charge->failure_declinecode = $stripe->declinecode;
1368 $charge = \Stripe\Charge::create($paymentarray, array(
"idempotency_key" =>
"$description",
"stripe_account" =>
"$account"));
1371 '@phan-var-force stdClass|\Stripe\Charge $charge';
1377 $return->result =
'success';
1378 $return->id = $charge->id;
1380 if (preg_match(
'/pm_/i', $source)) {
1381 $return->message =
'Payment retrieved by card status = '.$charge->status;
1383 if ($charge->source->type ==
'card') {
1384 $return->message = $charge->source->card->brand.
" ....".$charge->source->card->last4;
1385 } elseif ($charge->source->type ==
'three_d_secure') {
1386 $stripe =
new Stripe($this->db);
1387 $src = \Stripe\Source::retrieve((
string) $charge->source->three_d_secure->card, array(
1388 "stripe_account" => $stripe->getStripeAccount($service)
1390 $return->message = $src->card->brand.
" ....".$src->card->last4;
1392 $return->message = $charge->id;
1396 include DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
1398 $body = $e->getJsonBody();
1399 $err = $body[
'error'];
1401 $return->result =
'error';
1402 $return->id = $err[
'charge'];
1403 $return->type = $err[
'type'];
1404 $return->code = $err[
'code'];
1405 $return->message = $err[
'message'];
1406 $body =
"Error: <br>".$return->id.
" ".$return->message.
" ";
1407 $subject =
'[Alert] Payment error using Stripe';
1408 $cmailfile =
new CMailFile($subject,
$conf->global->ONLINE_PAYMENT_SENDEMAIL,
$conf->global->MAIN_INFO_SOCIETE_MAIL, $body);
1409 $cmailfile->sendfile();
1412 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1416 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1420 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1425 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1429 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1434 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');
1438 dol_syslog($e->getMessage(), LOG_WARNING, 0,
'_stripe');