Total Complexity | 190 |
Total Lines | 1392 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Stripe often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Stripe, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class Stripe extends CommonObject |
||
38 | { |
||
39 | /** |
||
40 | * @var int ID |
||
41 | */ |
||
42 | public $rowid; |
||
43 | |||
44 | /** |
||
45 | * @var int Thirdparty ID |
||
46 | */ |
||
47 | public $fk_soc; |
||
48 | |||
49 | /** |
||
50 | * @var int ID |
||
51 | */ |
||
52 | public $fk_key; |
||
53 | |||
54 | /** |
||
55 | * @var int ID |
||
56 | */ |
||
57 | public $id; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | public $mode; |
||
63 | |||
64 | /** |
||
65 | * @var int Entity |
||
66 | */ |
||
67 | public $entity; |
||
68 | |||
69 | /** |
||
70 | * @var string |
||
71 | * @deprecated Was used by createPaymentStripe only that is deprecated |
||
72 | */ |
||
73 | public $result; |
||
74 | |||
75 | /** |
||
76 | * @var string |
||
77 | */ |
||
78 | public $type; |
||
79 | |||
80 | /** |
||
81 | * @var string |
||
82 | */ |
||
83 | public $code; |
||
84 | |||
85 | /** |
||
86 | * @var string |
||
87 | */ |
||
88 | public $declinecode; |
||
89 | |||
90 | /** |
||
91 | * @var string Message |
||
92 | */ |
||
93 | public $message; |
||
94 | |||
95 | /** |
||
96 | * Constructor |
||
97 | * |
||
98 | * @param DoliDB $db Database handler |
||
|
|||
99 | */ |
||
100 | public function __construct($db) |
||
101 | { |
||
102 | $this->db = $db; |
||
103 | } |
||
104 | |||
105 | |||
106 | /** |
||
107 | * Return main company OAuth Connect stripe account |
||
108 | * |
||
109 | * @param string $mode 'StripeTest' or 'StripeLive' |
||
110 | * @param int $fk_soc Id of thirdparty |
||
111 | * @param int $entity Id of entity (-1 = current environment) |
||
112 | * @return string Stripe account 'acc_....' or '' if no OAuth token found |
||
113 | */ |
||
114 | public function getStripeAccount($mode = 'StripeTest', $fk_soc = 0, $entity = -1) |
||
115 | { |
||
116 | global $conf; |
||
117 | |||
118 | $key = ''; |
||
119 | if ($entity < 0) { |
||
120 | $entity = $conf->entity; |
||
121 | } |
||
122 | |||
123 | $sql = "SELECT tokenstring"; |
||
124 | $sql .= " FROM " . MAIN_DB_PREFIX . "oauth_token"; |
||
125 | $sql .= " WHERE service = '" . $this->db->escape($mode) . "'"; |
||
126 | $sql .= " AND entity = " . ((int) $entity); |
||
127 | if ($fk_soc > 0) { |
||
128 | $sql .= " AND fk_soc = " . ((int) $fk_soc); |
||
129 | } else { |
||
130 | $sql .= " AND fk_soc IS NULL"; |
||
131 | } |
||
132 | $sql .= " AND fk_user IS NULL AND fk_adherent IS NULL"; |
||
133 | |||
134 | dol_syslog(get_class($this) . "::getStripeAccount", LOG_DEBUG); |
||
135 | |||
136 | $result = $this->db->query($sql); |
||
137 | if ($result) { |
||
138 | if ($this->db->num_rows($result)) { |
||
139 | $obj = $this->db->fetch_object($result); |
||
140 | $tokenstring = $obj->tokenstring; |
||
141 | |||
142 | if ($tokenstring) { |
||
143 | $tmparray = json_decode($tokenstring); |
||
144 | $key = empty($tmparray->stripe_user_id) ? '' : $tmparray->stripe_user_id; |
||
145 | } |
||
146 | } else { |
||
147 | $tokenstring = ''; |
||
148 | } |
||
149 | } else { |
||
150 | dol_print_error($this->db); |
||
151 | } |
||
152 | |||
153 | dol_syslog("No dedicated Stripe Connect account available for entity " . $conf->entity); |
||
154 | |||
155 | return $key; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * getStripeCustomerAccount |
||
160 | * |
||
161 | * @param int $id Id of third party |
||
162 | * @param int $status Status |
||
163 | * @param string $site_account Value to use to identify with account to use on site when site can offer several accounts. For example: 'pk_live_123456' when using Stripe service. |
||
164 | * @return string Stripe customer ref 'cu_xxxxxxxxxxxxx' or '' |
||
165 | */ |
||
166 | public function getStripeCustomerAccount($id, $status = 0, $site_account = '') |
||
167 | { |
||
168 | include_once DOL_DOCUMENT_ROOT . '/societe/class/societeaccount.class.php'; |
||
169 | $societeaccount = new SocieteAccount($this->db); |
||
170 | return $societeaccount->getCustomerAccount($id, 'stripe', $status, $site_account); // Get thirdparty cus_... |
||
171 | } |
||
172 | |||
173 | |||
174 | /** |
||
175 | * Get the Stripe customer of a thirdparty (with option to create it in Stripe if not linked yet). |
||
176 | * Search on site_account = 0 or = $stripearrayofkeysbyenv[$status]['publishable_key'] |
||
177 | * |
||
178 | * @param CommonObject $object Object thirdparty to check, or create on stripe (create on stripe also update the stripe_account table for current entity). Used for AdherentType and Societe. |
||
179 | * @param string $key ''=Use common API. If not '', it is the Stripe connect account 'acc_....' to use Stripe connect |
||
180 | * @param int $status Status (0=test, 1=live) |
||
181 | * @param int $createifnotlinkedtostripe 1=Create the stripe customer and the link if the thirdparty is not yet linked to a stripe customer |
||
182 | * @return \Stripe\Customer|null Stripe Customer or null if not found |
||
183 | */ |
||
184 | public function customerStripe(CommonObject $object, $key = '', $status = 0, $createifnotlinkedtostripe = 0) |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Get the Stripe payment method Object from its ID |
||
295 | * |
||
296 | * @param string $paymentmethod Payment Method ID |
||
297 | * @param string $key ''=Use common API. If not '', it is the Stripe connect account 'acc_....' to use Stripe connect |
||
298 | * @param int $status Status (0=test, 1=live) |
||
299 | * @return \Stripe\PaymentMethod|null Stripe PaymentMethod or null if not found |
||
300 | */ |
||
301 | public function getPaymentMethodStripe($paymentmethod, $key = '', $status = 0) |
||
302 | { |
||
303 | $stripepaymentmethod = null; |
||
304 | |||
305 | try { |
||
306 | // Force to use the correct API key |
||
307 | global $stripearrayofkeysbyenv; |
||
308 | \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status]['secret_key']); |
||
309 | if (empty($key)) { // If the Stripe connect account not set, we use common API usage |
||
310 | $stripepaymentmethod = \Stripe\PaymentMethod::retrieve((string) $paymentmethod->id); |
||
311 | } else { |
||
312 | $stripepaymentmethod = \Stripe\PaymentMethod::retrieve((string) $paymentmethod->id, array("stripe_account" => $key)); |
||
313 | } |
||
314 | } catch (Exception $e) { |
||
315 | $this->error = $e->getMessage(); |
||
316 | } |
||
317 | |||
318 | return $stripepaymentmethod; |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Get the Stripe reader Object from its ID |
||
323 | * |
||
324 | * @param string $reader Reader ID |
||
325 | * @param string $key ''=Use common API. If not '', it is the Stripe connect account 'acc_....' to use Stripe connect |
||
326 | * @param int $status Status (0=test, 1=live) |
||
327 | * @return \Stripe\Terminal\Reader|null Stripe Reader or null if not found |
||
328 | */ |
||
329 | public function getSelectedReader($reader, $key = '', $status = 0) |
||
330 | { |
||
331 | $selectedreader = null; |
||
332 | |||
333 | try { |
||
334 | // Force to use the correct API key |
||
335 | global $stripearrayofkeysbyenv; |
||
336 | \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status]['secret_key']); |
||
337 | if (empty($key)) { // If the Stripe connect account not set, we use common API usage |
||
338 | $selectedreader = \Stripe\Terminal\Reader::retrieve((string) $reader); |
||
339 | } else { |
||
340 | $stripepaymentmethod = \Stripe\Terminal\Reader::retrieve((string) $reader, array("stripe_account" => $key)); |
||
341 | } |
||
342 | } catch (Exception $e) { |
||
343 | $this->error = $e->getMessage(); |
||
344 | } |
||
345 | |||
346 | return $selectedreader; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Get the Stripe payment intent. Create it with confirmnow=false |
||
351 | * Warning. If a payment was tried and failed, a payment intent was created. |
||
352 | * But if we change something on object to pay (amount or other), reusing same payment intent is not allowed by Stripe. |
||
353 | * Recommended solution is to recreate a new payment intent each time we need one (old one will be automatically closed after a delay), |
||
354 | * that's why i comment the part of code to retrieve a payment intent with object id (never mind if we cumulate payment intent with old ones that will not be used) |
||
355 | * Note: This is used when option STRIPE_USE_INTENT_WITH_AUTOMATIC_CONFIRMATION is on when making a payment from the public/payment/newpayment.php page |
||
356 | * but not when using the STRIPE_USE_NEW_CHECKOUT. |
||
357 | * |
||
358 | * @param double $amount Amount |
||
359 | * @param string $currency_code Currency code |
||
360 | * @param string $tag Tag |
||
361 | * @param string $description Description |
||
362 | * @param mixed $object Object to pay with Stripe |
||
363 | * @param string $customer Stripe customer ref 'cus_xxxxxxxxxxxxx' via customerStripe() |
||
364 | * @param string $key ''=Use common API. If not '', it is the Stripe connect account 'acc_....' to use Stripe connect |
||
365 | * @param int $status Status (0=test, 1=live) |
||
366 | * @param int $usethirdpartyemailforreceiptemail 1=use thirdparty email for receipt |
||
367 | * @param string $mode automatic=automatic confirmation/payment when conditions are ok, manual=need to call confirm() on intent |
||
368 | * @param boolean $confirmnow false=default, true=try to confirm immediately after create (if conditions are ok) |
||
369 | * @param string $payment_method 'pm_....' (if known) |
||
370 | * @param int $off_session If we use an already known payment method to pay when customer is not available during the checkout flow. |
||
371 | * @param int $noidempotency_key Do not use the idempotency_key when creating the PaymentIntent |
||
372 | * @param int $did ID of an existing line into llx_prelevement_demande (Dolibarr intent). If provided, no new line will be created. |
||
373 | * @return \Stripe\PaymentIntent|null Stripe PaymentIntent or null if not found and failed to create |
||
374 | */ |
||
375 | 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) |
||
376 | { |
||
377 | global $conf, $user; |
||
378 | |||
379 | dol_syslog(get_class($this) . "::getPaymentIntent", LOG_INFO, 1); |
||
380 | |||
381 | $error = 0; |
||
382 | |||
383 | if (empty($status)) { |
||
384 | $service = 'StripeTest'; |
||
385 | } else { |
||
386 | $service = 'StripeLive'; |
||
387 | } |
||
388 | |||
389 | $arrayzerounitcurrency = array('BIF', 'CLP', 'DJF', 'GNF', 'JPY', 'KMF', 'KRW', 'MGA', 'PYG', 'RWF', 'VND', 'VUV', 'XAF', 'XOF', 'XPF'); |
||
390 | if (!in_array($currency_code, $arrayzerounitcurrency)) { |
||
391 | $stripeamount = $amount * 100; |
||
392 | } else { |
||
393 | $stripeamount = $amount; |
||
394 | } |
||
395 | |||
396 | $fee = 0; |
||
397 | if (getDolGlobalString("STRIPE_APPLICATION_FEE_PERCENT")) { |
||
398 | $fee = $amount * ((float) getDolGlobalString("STRIPE_APPLICATION_FEE_PERCENT", '0') / 100) + (float) getDolGlobalString("STRIPE_APPLICATION_FEE", '0'); |
||
399 | } |
||
400 | if ($fee >= (float) getDolGlobalString("STRIPE_APPLICATION_FEE_MAXIMAL", '0') && (float) getDolGlobalString("STRIPE_APPLICATION_FEE_MAXIMAL", '0') > (float) getDolGlobalString("STRIPE_APPLICATION_FEE_MINIMAL", '0')) { |
||
401 | $fee = (float) getDolGlobalString("STRIPE_APPLICATION_FEE_MAXIMAL", '0'); |
||
402 | } elseif ($fee < (float) getDolGlobalString("STRIPE_APPLICATION_FEE_MINIMAL", '0')) { |
||
403 | $fee = (float) getDolGlobalString("STRIPE_APPLICATION_FEE_MINIMAL", '0'); |
||
404 | } |
||
405 | if (!in_array($currency_code, $arrayzerounitcurrency)) { |
||
406 | $stripefee = round($fee * 100); |
||
407 | } else { |
||
408 | $stripefee = round($fee); |
||
409 | } |
||
410 | |||
411 | $paymentintent = null; |
||
412 | |||
413 | if (is_object($object) && getDolGlobalInt('STRIPE_REUSE_EXISTING_INTENT_IF_FOUND') && !getDolGlobalInt('STRIPE_CARD_PRESENT')) { |
||
414 | // Warning. If a payment was tried and failed, a payment intent was created. |
||
415 | // But if we change something on object to pay (amount or other that does not change the idempotency key), reusing same payment intent is not allowed by Stripe. |
||
416 | // Recommended solution is to recreate a new payment intent each time we need one (old one will be automatically closed by Stripe after a delay), Stripe will |
||
417 | // automatically return the existing payment intent if idempotency is provided when we try to create the new one. |
||
418 | // That's why we can comment the part of code to retrieve a payment intent with object id (never mind if we cumulate payment intent with old ones that will not be used) |
||
419 | |||
420 | $sql = "SELECT pi.ext_payment_id, pi.entity, pi.fk_facture, pi.sourcetype, pi.ext_payment_site"; |
||
421 | $sql .= " FROM " . MAIN_DB_PREFIX . "prelevement_demande as pi"; |
||
422 | $sql .= " WHERE pi.fk_facture = " . ((int) $object->id); |
||
423 | $sql .= " AND pi.sourcetype = '" . $this->db->escape($object->element) . "'"; |
||
424 | $sql .= " AND pi.entity IN (" . getEntity('societe') . ")"; |
||
425 | $sql .= " AND pi.ext_payment_site = '" . $this->db->escape($service) . "'"; |
||
426 | |||
427 | dol_syslog(get_class($this) . "::getPaymentIntent search stripe payment intent for object id = " . $object->id, LOG_DEBUG); |
||
428 | $resql = $this->db->query($sql); |
||
429 | if ($resql) { |
||
430 | $num = $this->db->num_rows($resql); |
||
431 | if ($num) { |
||
432 | $obj = $this->db->fetch_object($resql); |
||
433 | $intent = $obj->ext_payment_id; |
||
434 | |||
435 | dol_syslog(get_class($this) . "::getPaymentIntent found existing payment intent record"); |
||
436 | |||
437 | // Force to use the correct API key |
||
438 | global $stripearrayofkeysbyenv; |
||
439 | \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status]['secret_key']); |
||
440 | |||
441 | try { |
||
442 | if (empty($key)) { // If the Stripe connect account not set, we use common API usage |
||
443 | $paymentintent = \Stripe\PaymentIntent::retrieve($intent); |
||
444 | } else { |
||
445 | $paymentintent = \Stripe\PaymentIntent::retrieve($intent, array("stripe_account" => $key)); |
||
446 | } |
||
447 | } catch (Exception $e) { |
||
448 | $error++; |
||
449 | $this->error = $e->getMessage(); |
||
450 | } |
||
451 | } |
||
452 | } |
||
453 | } |
||
454 | |||
455 | if (empty($paymentintent)) { |
||
456 | // Try to create intent. See https://stripe.com/docs/api/payment_intents/create |
||
457 | $ipaddress = getUserRemoteIP(); |
||
458 | $metadata = array('dol_version' => DOL_VERSION, 'dol_entity' => $conf->entity, 'ipaddress' => $ipaddress); |
||
459 | if (is_object($object)) { |
||
460 | $metadata['dol_type'] = $object->element; |
||
461 | $metadata['dol_id'] = $object->id; |
||
462 | if (is_object($object->thirdparty) && $object->thirdparty->id > 0) { |
||
463 | $metadata['dol_thirdparty_id'] = $object->thirdparty->id; |
||
464 | } |
||
465 | } |
||
466 | |||
467 | // list of payment method types |
||
468 | $paymentmethodtypes = array("card"); |
||
469 | $descriptor = dol_trunc($tag, 10, 'right', 'UTF-8', 1); |
||
470 | if (getDolGlobalInt('STRIPE_SEPA_DIRECT_DEBIT')) { |
||
471 | $paymentmethodtypes[] = "sepa_debit"; //&& ($object->thirdparty->isInEEC()) |
||
472 | //$descriptor = preg_replace('/ref=[^:=]+/', '', $descriptor); // Clean ref |
||
473 | } |
||
474 | if (getDolGlobalInt('STRIPE_KLARNA')) { |
||
475 | $paymentmethodtypes[] = "klarna"; |
||
476 | } |
||
477 | if (getDolGlobalInt('STRIPE_BANCONTACT')) { |
||
478 | $paymentmethodtypes[] = "bancontact"; |
||
479 | } |
||
480 | if (getDolGlobalInt('STRIPE_IDEAL')) { |
||
481 | $paymentmethodtypes[] = "ideal"; |
||
482 | } |
||
483 | if (getDolGlobalInt('STRIPE_GIROPAY')) { |
||
484 | $paymentmethodtypes[] = "giropay"; |
||
485 | } |
||
486 | if (getDolGlobalInt('STRIPE_SOFORT')) { |
||
487 | $paymentmethodtypes[] = "sofort"; |
||
488 | } |
||
489 | if (getDolGlobalInt('STRIPE_CARD_PRESENT') && $mode == 'terminal') { |
||
490 | $paymentmethodtypes = array("card_present"); |
||
491 | } |
||
492 | |||
493 | global $dolibarr_main_url_root; |
||
494 | |||
495 | $dataforintent = array( |
||
496 | "confirm" => $confirmnow, // try to confirm immediately after create (if conditions are ok) |
||
497 | "confirmation_method" => $mode, |
||
498 | "amount" => $stripeamount, |
||
499 | "currency" => $currency_code, |
||
500 | "payment_method_types" => $paymentmethodtypes, // When payment_method_types is set, return_url is not required but payment mode can't be managed from dashboard |
||
501 | /* |
||
502 | 'return_url' => $dolibarr_main_url_root.'/public/payment/paymentok.php', |
||
503 | 'automatic_payment_methods' => array( |
||
504 | 'enabled' => true, |
||
505 | 'allow_redirects' => 'never', |
||
506 | ), |
||
507 | */ |
||
508 | "description" => $description, |
||
509 | //"save_payment_method" => true, |
||
510 | "setup_future_usage" => "on_session", |
||
511 | "metadata" => $metadata |
||
512 | ); |
||
513 | if ($descriptor) { |
||
514 | $dataforintent["statement_descriptor_suffix"] = $descriptor; // For card payment, 22 chars that appears on bank receipt (prefix into stripe setup + this suffix) |
||
515 | $dataforintent["statement_descriptor"] = $descriptor; // For SEPA, it will take only statement_descriptor, not statement_descriptor_suffix |
||
516 | } |
||
517 | if (!is_null($customer)) { |
||
518 | $dataforintent["customer"] = $customer; |
||
519 | } |
||
520 | // payment_method = |
||
521 | // payment_method_types = array('card') |
||
522 | //var_dump($dataforintent); |
||
523 | if ($off_session) { |
||
524 | unset($dataforintent['setup_future_usage']); |
||
525 | // We can't use both "setup_future_usage" = "off_session" and "off_session" = true. |
||
526 | // Because $off_session parameter is dedicated to create paymentintent off_line (and not future payment), we need to use "off_session" = true. |
||
527 | //$dataforintent["setup_future_usage"] = "off_session"; |
||
528 | $dataforintent["off_session"] = true; |
||
529 | } |
||
530 | if (getDolGlobalInt('STRIPE_GIROPAY')) { |
||
531 | unset($dataforintent['setup_future_usage']); |
||
532 | } |
||
533 | if (getDolGlobalInt('STRIPE_KLARNA')) { |
||
534 | unset($dataforintent['setup_future_usage']); |
||
535 | } |
||
536 | if (getDolGlobalInt('STRIPE_CARD_PRESENT') && $mode == 'terminal') { |
||
537 | unset($dataforintent['setup_future_usage']); |
||
538 | $dataforintent["capture_method"] = "manual"; |
||
539 | $dataforintent["confirmation_method"] = "manual"; |
||
540 | } |
||
541 | if (!is_null($payment_method)) { |
||
542 | $dataforintent["payment_method"] = $payment_method; |
||
543 | $description .= ' - ' . $payment_method; |
||
544 | } |
||
545 | |||
546 | if ($conf->entity != getDolGlobalInt('STRIPECONNECT_PRINCIPAL') && $stripefee > 0) { |
||
547 | $dataforintent["application_fee_amount"] = $stripefee; |
||
548 | } |
||
549 | if ($usethirdpartyemailforreceiptemail && is_object($object) && $object->thirdparty->email) { |
||
550 | $dataforintent["receipt_email"] = $object->thirdparty->email; |
||
551 | } |
||
552 | |||
553 | try { |
||
554 | // Force to use the correct API key |
||
555 | global $stripearrayofkeysbyenv; |
||
556 | \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status]['secret_key']); |
||
557 | |||
558 | $arrayofoptions = array(); |
||
559 | if (empty($noidempotency_key)) { |
||
560 | $arrayofoptions["idempotency_key"] = $description; |
||
561 | } |
||
562 | // Note: If all data for payment intent are same than a previous on, even if we use 'create', Stripe will return ID of the old existing payment intent. |
||
563 | if (!empty($key)) { // If the Stripe connect account not set, we use common API usage |
||
564 | $arrayofoptions["stripe_account"] = $key; |
||
565 | } |
||
566 | |||
567 | dol_syslog("dataforintent to create paymentintent = " . var_export($dataforintent, true)); |
||
568 | |||
569 | $paymentintent = \Stripe\PaymentIntent::create($dataforintent, $arrayofoptions); |
||
570 | |||
571 | // Store the payment intent |
||
572 | if (is_object($object)) { |
||
573 | $paymentintentalreadyexists = 0; |
||
574 | |||
575 | if ($did > 0) { |
||
576 | // If a payment request line provided, we do not need to recreate one, we just update it |
||
577 | dol_syslog(get_class($this) . "::getPaymentIntent search if payment intent already in prelevement_demande", LOG_DEBUG); |
||
578 | |||
579 | $sql = "UPDATE " . MAIN_DB_PREFIX . "prelevement_demande SET"; |
||
580 | $sql .= " ext_payment_site = '" . $this->db->escape($service) . "',"; |
||
581 | $sql .= " ext_payment_id = '" . $this->db->escape($paymentintent->id) . "'"; |
||
582 | $sql .= " WHERE rowid = " . ((int) $did); |
||
583 | |||
584 | $resql = $this->db->query($sql); |
||
585 | if ($resql) { |
||
586 | $paymentintentalreadyexists++; |
||
587 | } else { |
||
588 | $error++; |
||
589 | dol_print_error($this->db); |
||
590 | } |
||
591 | } else { |
||
592 | // Check that payment intent $paymentintent->id is not already recorded. |
||
593 | dol_syslog(get_class($this) . "::getPaymentIntent search if payment intent already in prelevement_demande", LOG_DEBUG); |
||
594 | |||
595 | $sql = "SELECT pi.rowid"; |
||
596 | $sql .= " FROM " . MAIN_DB_PREFIX . "prelevement_demande as pi"; |
||
597 | $sql .= " WHERE pi.entity IN (" . getEntity('societe') . ")"; |
||
598 | $sql .= " AND pi.ext_payment_site = '" . $this->db->escape($service) . "'"; |
||
599 | $sql .= " AND pi.ext_payment_id = '" . $this->db->escape($paymentintent->id) . "'"; |
||
600 | |||
601 | $resql = $this->db->query($sql); |
||
602 | if ($resql) { |
||
603 | $num = $this->db->num_rows($resql); |
||
604 | if ($num) { |
||
605 | $obj = $this->db->fetch_object($resql); |
||
606 | if ($obj) { |
||
607 | $paymentintentalreadyexists++; |
||
608 | } |
||
609 | } |
||
610 | } else { |
||
611 | $error++; |
||
612 | dol_print_error($this->db); |
||
613 | } |
||
614 | } |
||
615 | |||
616 | // If not, we create it. |
||
617 | if (!$error && !$paymentintentalreadyexists) { |
||
618 | $now = dol_now(); |
||
619 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "prelevement_demande (date_demande, fk_user_demande, ext_payment_id, fk_facture, sourcetype, entity, ext_payment_site, amount)"; |
||
620 | $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) . ")"; |
||
621 | $resql = $this->db->query($sql); |
||
622 | if (!$resql) { |
||
623 | $error++; |
||
624 | $this->error = $this->db->lasterror(); |
||
625 | dol_syslog(get_class($this) . "::PaymentIntent failed to insert paymentintent with id=" . $paymentintent->id . " into database.", LOG_ERR); |
||
626 | } |
||
627 | } |
||
628 | } else { |
||
629 | $_SESSION["stripe_payment_intent"] = $paymentintent; |
||
630 | } |
||
631 | } catch (Stripe\Exception\CardException $e) { |
||
632 | $error++; |
||
633 | $this->error = $e->getMessage(); |
||
634 | $this->code = $e->getStripeCode(); |
||
635 | $this->declinecode = $e->getDeclineCode(); |
||
636 | } catch (Exception $e) { |
||
637 | //var_dump($dataforintent); |
||
638 | //var_dump($description); |
||
639 | //var_dump($key); |
||
640 | //var_dump($paymentintent); |
||
641 | //var_dump($e->getMessage()); |
||
642 | //var_dump($e); |
||
643 | $error++; |
||
644 | $this->error = $e->getMessage(); |
||
645 | $this->code = ''; |
||
646 | $this->declinecode = ''; |
||
647 | } |
||
648 | } |
||
649 | |||
650 | dol_syslog(get_class($this) . "::getPaymentIntent return error=" . $error . " this->error=" . $this->error, LOG_INFO, -1); |
||
651 | |||
652 | if (!$error) { |
||
653 | return $paymentintent; |
||
654 | } else { |
||
655 | return null; |
||
656 | } |
||
657 | } |
||
658 | |||
659 | /** |
||
660 | * Get the Stripe payment intent. Create it with confirmnow=false |
||
661 | * Warning. If a payment was tried and failed, a payment intent was created. |
||
662 | * But if we change something on object to pay (amount or other), reusing same payment intent is not allowed. |
||
663 | * Recommended solution is to recreate a new payment intent each time we need one (old one will be automatically closed after a delay), |
||
664 | * that's why i comment the part of code to retrieve a payment intent with object id (never mind if we cumulate payment intent with old ones that will not be used) |
||
665 | * Note: This is used when option STRIPE_USE_INTENT_WITH_AUTOMATIC_CONFIRMATION is on when making a payment from the public/payment/newpayment.php page |
||
666 | * but not when using the STRIPE_USE_NEW_CHECKOUT. |
||
667 | * |
||
668 | * @param string $description Description |
||
669 | * @param Societe $object Object of company to link the Stripe payment mode with |
||
670 | * @param string $customer Stripe customer ref 'cus_xxxxxxxxxxxxx' via customerStripe() |
||
671 | * @param string $key ''=Use common API. If not '', it is the Stripe connect account 'acc_....' to use Stripe connect |
||
672 | * @param int $status Status (0=test, 1=live) |
||
673 | * @param int $usethirdpartyemailforreceiptemail 1=use thirdparty email for receipt |
||
674 | * @param boolean $confirmnow false=default, true=try to confirm immediately after create (if conditions are ok) |
||
675 | * @return \Stripe\SetupIntent|null Stripe SetupIntent or null if not found and failed to create |
||
676 | */ |
||
677 | public function getSetupIntent($description, $object, $customer, $key, $status, $usethirdpartyemailforreceiptemail = 0, $confirmnow = false) |
||
678 | { |
||
679 | global $conf; |
||
680 | |||
681 | dol_syslog("getSetupIntent description=" . $description . ' confirmnow=' . json_encode($confirmnow), LOG_INFO, 1); |
||
682 | |||
683 | $error = 0; |
||
684 | |||
685 | if (empty($status)) { |
||
686 | $service = 'StripeTest'; |
||
687 | } else { |
||
688 | $service = 'StripeLive'; |
||
689 | } |
||
690 | |||
691 | $setupintent = null; |
||
692 | |||
693 | if (empty($setupintent)) { |
||
694 | $ipaddress = getUserRemoteIP(); |
||
695 | $metadata = array('dol_version' => DOL_VERSION, 'dol_entity' => $conf->entity, 'ipaddress' => $ipaddress); |
||
696 | if (is_object($object)) { |
||
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; |
||
701 | } |
||
702 | } |
||
703 | |||
704 | // list of payment method types |
||
705 | $paymentmethodtypes = array("card"); |
||
706 | if (getDolGlobalString('STRIPE_SEPA_DIRECT_DEBIT')) { |
||
707 | $paymentmethodtypes[] = "sepa_debit"; //&& ($object->thirdparty->isInEEC()) |
||
708 | } |
||
709 | if (getDolGlobalString('STRIPE_BANCONTACT')) { |
||
710 | $paymentmethodtypes[] = "bancontact"; |
||
711 | } |
||
712 | if (getDolGlobalString('STRIPE_IDEAL')) { |
||
713 | $paymentmethodtypes[] = "ideal"; |
||
714 | } |
||
715 | // Giropay not possible for setup intent |
||
716 | if (getDolGlobalString('STRIPE_SOFORT')) { |
||
717 | $paymentmethodtypes[] = "sofort"; |
||
718 | } |
||
719 | |||
720 | global $dolibarr_main_url_root; |
||
721 | |||
722 | $dataforintent = array( |
||
723 | "confirm" => $confirmnow, // Do not confirm immediately during creation of intent |
||
724 | "payment_method_types" => $paymentmethodtypes, // When payment_method_types is set, return_url is not required but payment mode can't be managed from dashboard |
||
725 | /* |
||
726 | 'return_url' => $dolibarr_main_url_root.'/public/payment/paymentok.php', |
||
727 | 'automatic_payment_methods' => array( |
||
728 | 'enabled' => true, |
||
729 | 'allow_redirects' => 'never', |
||
730 | ), |
||
731 | */ |
||
732 | "usage" => "off_session", |
||
733 | "metadata" => $metadata |
||
734 | ); |
||
735 | if (!is_null($customer)) { |
||
736 | $dataforintent["customer"] = $customer; |
||
737 | } |
||
738 | if (!is_null($description)) { |
||
739 | $dataforintent["description"] = $description; |
||
740 | } |
||
741 | // payment_method = |
||
742 | // payment_method_types = array('card') |
||
743 | //var_dump($dataforintent); |
||
744 | |||
745 | if ($usethirdpartyemailforreceiptemail && is_object($object) && $object->thirdparty->email) { |
||
746 | $dataforintent["receipt_email"] = $object->thirdparty->email; |
||
747 | } |
||
748 | |||
749 | try { |
||
750 | // Force to use the correct API key |
||
751 | global $stripearrayofkeysbyenv; |
||
752 | \Stripe\Stripe::setApiKey($stripearrayofkeysbyenv[$status]['secret_key']); |
||
753 | |||
754 | dol_syslog("getSetupIntent " . $stripearrayofkeysbyenv[$status]['publishable_key'], LOG_DEBUG); |
||
755 | |||
756 | // Note: If all data for payment intent are same than a previous one, even if we use 'create', Stripe will return ID of the old existing payment intent. |
||
757 | if (empty($key)) { // If the Stripe connect account not set, we use common API usage |
||
758 | //$setupintent = \Stripe\SetupIntent::create($dataforintent, array("idempotency_key" => "$description")); |
||
759 | $setupintent = \Stripe\SetupIntent::create($dataforintent, array()); |
||
760 | } else { |
||
761 | //$setupintent = \Stripe\SetupIntent::create($dataforintent, array("idempotency_key" => "$description", "stripe_account" => $key)); |
||
762 | $setupintent = \Stripe\SetupIntent::create($dataforintent, array("stripe_account" => $key)); |
||
763 | } |
||
764 | //var_dump($setupintent->id); |
||
765 | |||
766 | // Store the setup intent |
||
767 | /*if (is_object($object)) |
||
768 | { |
||
769 | $setupintentalreadyexists = 0; |
||
770 | // Check that payment intent $setupintent->id is not already recorded. |
||
771 | $sql = "SELECT pi.rowid"; |
||
772 | $sql.= " FROM " . MAIN_DB_PREFIX . "prelevement_demande as pi"; |
||
773 | $sql.= " WHERE pi.entity IN (".getEntity('societe').")"; |
||
774 | $sql.= " AND pi.ext_payment_site = '" . $this->db->escape($service) . "'"; |
||
775 | $sql.= " AND pi.ext_payment_id = '".$this->db->escape($setupintent->id)."'"; |
||
776 | |||
777 | dol_syslog(get_class($this) . "::getPaymentIntent search if payment intent already in prelevement_demande", LOG_DEBUG); |
||
778 | $resql = $this->db->query($sql); |
||
779 | if ($resql) { |
||
780 | $num = $this->db->num_rows($resql); |
||
781 | if ($num) |
||
782 | { |
||
783 | $obj = $this->db->fetch_object($resql); |
||
784 | if ($obj) $setupintentalreadyexists++; |
||
785 | } |
||
786 | } |
||
787 | else dol_print_error($this->db); |
||
788 | |||
789 | // If not, we create it. |
||
790 | if (! $setupintentalreadyexists) |
||
791 | { |
||
792 | $now=dol_now(); |
||
793 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "prelevement_demande (date_demande, fk_user_demande, ext_payment_id, fk_facture, sourcetype, entity, ext_payment_site)"; |
||
794 | $sql .= " VALUES ('".$this->db->idate($now)."', ".((int) $user->id).", '".$this->db->escape($setupintent->id)."', ".((int) $object->id).", '".$this->db->escape($object->element)."', " . ((int) $conf->entity) . ", '" . $this->db->escape($service) . "', ".((float) $amount).")"; |
||
795 | $resql = $this->db->query($sql); |
||
796 | if (! $resql) |
||
797 | { |
||
798 | $error++; |
||
799 | $this->error = $this->db->lasterror(); |
||
800 | dol_syslog(get_class($this) . "::PaymentIntent failed to insert paymentintent with id=".$setupintent->id." into database."); |
||
801 | } |
||
802 | } |
||
803 | } |
||
804 | else |
||
805 | { |
||
806 | $_SESSION["stripe_setup_intent"] = $setupintent; |
||
807 | }*/ |
||
808 | } catch (Exception $e) { |
||
809 | //var_dump($dataforintent); |
||
810 | //var_dump($description); |
||
811 | //var_dump($key); |
||
812 | //var_dump($setupintent); |
||
813 | //var_dump($e->getMessage()); |
||
814 | $error++; |
||
815 | $this->error = $e->getMessage(); |
||
816 | } |
||
817 | } |
||
818 | |||
819 | if (!$error) { |
||
820 | dol_syslog("getSetupIntent " . (is_object($setupintent) ? $setupintent->id : ''), LOG_INFO, -1); |
||
821 | return $setupintent; |
||
822 | } else { |
||
823 | dol_syslog("getSetupIntent return error=" . $error, LOG_INFO, -1); |
||
824 | return null; |
||
825 | } |
||
826 | } |
||
827 | |||
828 | |||
829 | /** |
||
830 | * Get the Stripe card of a company payment mode (option to create it on Stripe if not linked yet is no more available on new Stripe API) |
||
831 | * |
||
832 | * @param \Stripe\Customer $cu Object stripe customer. |
||
833 | * @param CompanyPaymentMode $object Object companypaymentmode to check, or create on stripe (create on stripe also update the societe_rib table for current entity) |
||
834 | * @param string $stripeacc ''=Use common API. If not '', it is the Stripe connect account 'acc_....' to use Stripe connect |
||
835 | * @param int $status Status (0=test, 1=live) |
||
836 | * @param int $createifnotlinkedtostripe 1=Create the stripe card and the link if the card is not yet linked to a stripe card. Deprecated with new Stripe API and SCA. |
||
837 | * @return \Stripe\Card|\Stripe\PaymentMethod|null Stripe Card or null if not found |
||
838 | */ |
||
839 | public function cardStripe($cu, CompanyPaymentMode $object, $stripeacc = '', $status = 0, $createifnotlinkedtostripe = 0) |
||
978 | } |
||
979 | |||
980 | |||
981 | /** |
||
982 | * Get the Stripe SEPA of a company payment mode (create it if it doesn't exists and $createifnotlinkedtostripe is set) |
||
983 | * |
||
984 | * @param \Stripe\Customer $cu Object stripe customer. |
||
985 | * @param CompanyPaymentMode $object Object companypaymentmode to check, or create on stripe (create on stripe also update the societe_rib table for current entity) |
||
986 | * @param string $stripeacc ''=Use common API. If not '', it is the Stripe connect account 'acc_....' to use Stripe connect |
||
987 | * @param int $status Status (0=test, 1=live) |
||
988 | * @param int $createifnotlinkedtostripe 1=Create the stripe sepa and the link if the sepa is not yet linked to a stripe sepa. Used by the "Create bank to Stripe" feature. |
||
989 | * @return \Stripe\PaymentMethod|null Stripe SEPA or null if not found |
||
990 | */ |
||
991 | public function sepaStripe($cu, CompanyPaymentMode $object, $stripeacc = '', $status = 0, $createifnotlinkedtostripe = 0) |
||
1149 | } |
||
1150 | |||
1151 | |||
1152 | /** |
||
1153 | * Create charge. |
||
1154 | * This was called by page htdocs/stripe/payment.php and may be deprecated. |
||
1155 | * |
||
1156 | * @param int $amount Amount to pay |
||
1157 | * @param string $currency EUR, GPB... |
||
1158 | * @param string $origin Object type to pay (order, invoice, contract...) |
||
1159 | * @param int $item Object id to pay |
||
1160 | * @param string $source src_xxxxx or card_xxxxx or pm_xxxxx |
||
1161 | * @param string $customer Stripe customer ref 'cus_xxxxxxxxxxxxx' via customerStripe() |
||
1162 | * @param string $account Stripe account ref 'acc_xxxxxxxxxxxxx' via getStripeAccount() |
||
1163 | * @param int $status Status (0=test, 1=live) |
||
1164 | * @param int $usethirdpartyemailforreceiptemail Use thirdparty email as receipt email |
||
1165 | * @param boolean $capture Set capture flag to true (take payment) or false (wait) |
||
1166 | * @return Stripe |
||
1167 | * @deprecated |
||
1168 | */ |
||
1169 | public function createPaymentStripe($amount, $currency, $origin, $item, $source, $customer, $account, $status = 0, $usethirdpartyemailforreceiptemail = 0, $capture = true) |
||
1429 | } |
||
1430 | } |
||
1431 |