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