Debug sepa payment

This commit is contained in:
Laurent Destailleur 2022-10-13 05:31:36 +02:00
parent 9b1a01bbd6
commit eabf01a422
3 changed files with 657 additions and 651 deletions

View File

@ -114,11 +114,7 @@ if (empty($reshook)) {
} }
$paymentservice = GETPOST('paymentservice'); $paymentservice = GETPOST('paymentservice');
if (preg_match('/stripesepa/', $paymentservice)) {
$result = $object->demande_prelevement_stripe($user, price2num(GETPOST('withdraw_request_amount', 'alpha')), $newtype, $sourcetype);
} else {
$result = $object->demande_prelevement($user, price2num(GETPOST('withdraw_request_amount', 'alpha')), $newtype, $sourcetype); $result = $object->demande_prelevement($user, price2num(GETPOST('withdraw_request_amount', 'alpha')), $newtype, $sourcetype);
}
if ($result > 0) { if ($result > 0) {
$db->commit(); $db->commit();
@ -142,6 +138,14 @@ if (empty($reshook)) {
} }
} }
// Payment with Direct Debit Stripe
if ($action == 'sepastripepayment' && $usercancreate) {
$result = $object->makeStripeSepaRequest($user, GETPOST('did', 'int'), 'direct-debit', 'facture');
if ($result < 0) {
setEventMessages($object->error, $object->errors, 'errors');
}
}
// payments conditions // payments conditions
if ($action == 'setconditions' && $usercancreate) { if ($action == 'setconditions' && $usercancreate) {
$object->fetch($id); $object->fetch($id);
@ -871,7 +875,8 @@ if ($object->id > 0) {
print '<td>'; print '<td>';
if (!empty($conf->global->STRIPE_SEPA_DIRECT_DEBIT)) { if (!empty($conf->global->STRIPE_SEPA_DIRECT_DEBIT)) {
print '<a href="'.$_SERVER["PHP_SELF"].'?action=new&paymentservice=stripesepa&token='.newToken().'&did='.$obj->rowid.'&id='.$object->id.'&type='.urlencode($type).'">'.img_picto('', 'stripe', 'class="pictofixedwidth"').$langs->trans("SendToStripe").'</a>'; $langs->load("stripe");
print '<a href="'.$_SERVER["PHP_SELF"].'?action=sepastripepayment&paymentservice=stripesepa&token='.newToken().'&did='.$obj->rowid.'&id='.$object->id.'&type='.urlencode($type).'">'.img_picto('', 'stripe', 'class="pictofixedwidth"').$langs->trans("RequestDirectDebitWithStripe").'</a>';
} }
print '</td>'; print '</td>';

View File

@ -848,20 +848,18 @@ abstract class CommonInvoice extends CommonObject
} }
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/** /**
* Create a withdrawal request for a direct debit order or a credit transfer order. * Create a withdrawal request for a direct debit order or a credit transfer order.
* Use the remain to pay excluding all existing open direct debit requests. * Use the remain to pay excluding all existing open direct debit requests.
* *
* @param User $fuser User asking the direct debit transfer * @param User $fuser User asking the direct debit transfer
* @param float $amount Amount we request direct debit for * @param int $did ID of payment request
* @param string $type 'direct-debit' or 'bank-transfer' * @param string $type 'direct-debit' or 'bank-transfer'
* @param string $sourcetype Source ('facture' or 'supplier_invoice') * @param string $sourcetype Source ('facture' or 'supplier_invoice')
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
public function demande_prelevement_stripe($fuser, $amount = 0, $type = 'direct-debit', $sourcetype = 'facture') public function makeStripeSepaRequest($fuser, $did = 0, $type = 'direct-debit', $sourcetype = 'facture')
{ {
// phpcs:enable
global $conf, $mysoc, $user, $langs; global $conf, $mysoc, $user, $langs;
if (empty($conf->global->STRIPE_SEPA_DIRECT_DEBIT)) { if (empty($conf->global->STRIPE_SEPA_DIRECT_DEBIT)) {
@ -871,25 +869,35 @@ abstract class CommonInvoice extends CommonObject
$error = 0; $error = 0;
dol_syslog(get_class($this)."::demande_prelevement_stripe 0", LOG_DEBUG); dol_syslog(get_class($this)."::makeStripeSepaRequest 0", LOG_DEBUG);
if ($this->statut > self::STATUS_DRAFT && $this->paye == 0) { if ($this->statut > self::STATUS_DRAFT && $this->paye == 0) {
require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php'; require_once DOL_DOCUMENT_ROOT.'/societe/class/companybankaccount.class.php';
$bac = new CompanyBankAccount($this->db); $bac = new CompanyBankAccount($this->db);
$bac->fetch(0, $this->socid); $result = $bac->fetch(0, $this->socid, 1, 'ban');
if ($result <= 0 || empty($bac->id)) {
$this->error = $langs->trans("ThirdpartyHasNoDefaultBanAccount");
$this->errors[] = $this->error;
dol_syslog(get_class($this)."::makeStripeSepaRequest ".$this->error);
return -1;
}
$sql = "SELECT count(*)"; $sql = "SELECT rowid, date_demande, amount, fk_facture, fk_facture_fourn";
$sql .= " FROM ".$this->db->prefix()."prelevement_facture_demande"; $sql .= " FROM ".$this->db->prefix()."prelevement_facture_demande";
$sql .= " WHERE fk_facture = ".((int) $this->id); $sql .= " WHERE rowid = ".((int) $did);
$sql .= " AND ext_payment_id IS NULL"; // To exclude record done for some online payments
$sql .= " AND traite = 0";
dol_syslog(get_class($this)."::demande_prelevement_stripe 1", LOG_DEBUG); dol_syslog(get_class($this)."::makeStripeSepaRequest 1", LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) { if ($resql) {
$row = $this->db->fetch_row($resql); $obj = $this->db->fetch_object($resql);
if (!$obj) {
dol_print_error($this->db, 'CantFindRequestWithId');
return -2;
}
//
$amount = $obj->amount;
if ($row[0] == 0) {
$now = dol_now(); $now = dol_now();
$totalpaye = $this->getSommePaiement(); $totalpaye = $this->getSommePaiement();
@ -901,18 +909,16 @@ abstract class CommonInvoice extends CommonObject
// For example print 239.2 - 229.3 - 9.9; does not return 0. // For example print 239.2 - 229.3 - 9.9; does not return 0.
//$resteapayer=bcadd($this->total_ttc,$totalpaye,$conf->global->MAIN_MAX_DECIMALS_TOT); //$resteapayer=bcadd($this->total_ttc,$totalpaye,$conf->global->MAIN_MAX_DECIMALS_TOT);
//$resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT); //$resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT);
if (empty($amount)) { $amounttocheck = price2num($this->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits, 'MT');
$amount = price2num($this->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits, 'MT');
} // TODO We can compare $amount and $amounttocheck
if (is_numeric($amount) && $amount != 0) { if (is_numeric($amount) && $amount != 0) {
require_once DOL_DOCUMENT_ROOT.'/societe/class/companypaymentmode.class.php'; require_once DOL_DOCUMENT_ROOT.'/societe/class/companypaymentmode.class.php';
$companypaymentmode = new CompanyPaymentMode($this->db); $companypaymentmode = new CompanyPaymentMode($this->db);
$companypaymentmode->fetch($bac->id); $companypaymentmode->fetch($bac->id);
dol_syslog(get_class($this)."::demande_prelevement_stripe amount=$amount, companypaymentmode = " . $companypaymentmode->id, LOG_DEBUG); // Start code for Stripe
//Start code from sellyoursaas
$service = 'StripeTest'; $service = 'StripeTest';
$servicestatus = 0; $servicestatus = 0;
if (!empty($conf->global->STRIPE_LIVE) && !GETPOST('forcesandbox', 'alpha')) { if (!empty($conf->global->STRIPE_LIVE) && !GETPOST('forcesandbox', 'alpha')) {
@ -920,8 +926,7 @@ abstract class CommonInvoice extends CommonObject
$servicestatus = 1; $servicestatus = 1;
} }
$langs->load("agenda"); dol_syslog("makeStripeSepaRequest amount = ".$amount." service=" . $service . " servicestatus=" . $servicestatus . " thirdparty_id=" . $this->socid . " companypaymentmode=" . $companypaymentmode->id);
dol_syslog("doTakePaymentStripeForThirdparty service=" . $service . " servicestatus=" . $servicestatus . " thirdparty_id=" . $this->socid . " companypaymentmode=" . $companypaymentmode->id . " noemailtocustomeriferror=" . $noemailtocustomeriferror . " nocancelifpaymenterror=" . $nocancelifpaymenterror . " calledinmyaccountcontext=" . $calledinmyaccountcontext);
$this->stripechargedone = 0; $this->stripechargedone = 0;
$this->stripechargeerror = 0; $this->stripechargeerror = 0;
@ -965,14 +970,14 @@ abstract class CommonInvoice extends CommonObject
if (empty($savstripearrayofkeysbyenv)) { if (empty($savstripearrayofkeysbyenv)) {
$savstripearrayofkeysbyenv = $stripearrayofkeysbyenv; $savstripearrayofkeysbyenv = $stripearrayofkeysbyenv;
} }
dol_syslog("Current Stripe environment is " . $stripearrayofkeysbyenv[$servicestatus]['publishable_key']); dol_syslog("makeStripeSepaRequest Current Stripe environment is " . $stripearrayofkeysbyenv[$servicestatus]['publishable_key']);
dol_syslog("Current Saved Stripe environment is " . $savstripearrayofkeysbyenv[$servicestatus]['publishable_key']); dol_syslog("makeStripeSepaRequest Current Saved Stripe environment is " . $savstripearrayofkeysbyenv[$servicestatus]['publishable_key']);
$foundalternativestripeaccount = ''; $foundalternativestripeaccount = '';
// Force stripe to another value (by default this value is empty) // Force stripe to another value (by default this value is empty)
if (!empty($thirdparty->array_options['options_stripeaccount'])) { if (!empty($thirdparty->array_options['options_stripeaccount'])) {
dol_syslog("The thirdparty id=" . $thirdparty->id . " has a dedicated Stripe Account, so we switch to it."); dol_syslog("makeStripeSepaRequest The thirdparty id=" . $thirdparty->id . " has a dedicated Stripe Account, so we switch to it.");
$tmparray = explode('@', $thirdparty->array_options['options_stripeaccount']); $tmparray = explode('@', $thirdparty->array_options['options_stripeaccount']);
if (!empty($tmparray[1])) { if (!empty($tmparray[1])) {
@ -1014,9 +1019,9 @@ abstract class CommonInvoice extends CommonObject
} }
dol_syslog("get stripe account", LOG_DEBUG); dol_syslog("makeStripeSepaRequest get stripe account", LOG_DEBUG);
$stripeacc = $stripe->getStripeAccount($service, $this->socid); // Get Stripe OAuth connect account if it exists (no network access here) $stripeacc = $stripe->getStripeAccount($service, $this->socid); // Get Stripe OAuth connect account if it exists (no network access here)
dol_syslog("get stripe account return " . json_encode($stripeacc), LOG_DEBUG); dol_syslog("makeStripeSepaRequest get stripe account return " . json_encode($stripeacc), LOG_DEBUG);
if ($foundalternativestripeaccount) { if ($foundalternativestripeaccount) {
if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage if (empty($stripeacc)) { // If the Stripe connect account not set, we use common API usage
@ -1492,7 +1497,7 @@ abstract class CommonInvoice extends CommonObject
$from = "";//$conf->global->SELLYOURSAAS_NOREPLY_EMAIL; $from = "";//$conf->global->SELLYOURSAAS_NOREPLY_EMAIL;
$trackid = 'inv' . $this->id; $trackid = 'inv' . $this->id;
$moreinheader = 'X-Dolibarr-Info: doTakeStripePaymentForThirdParty' . "\r\n"; $moreinheader = 'X-Dolibarr-Info: makeStripeSepaRequest' . "\r\n";
// Send email (substitutionarray must be done just before this) // Send email (substitutionarray must be done just before this)
include_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php'; include_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php';
@ -1580,16 +1585,16 @@ abstract class CommonInvoice extends CommonObject
$sql .= ",".$conf->entity; $sql .= ",".$conf->entity;
$sql .= ")"; $sql .= ")";
dol_syslog(get_class($this)."::demande_prelevement_stripe", LOG_DEBUG); dol_syslog(get_class($this)."::makeStripeSepaRequest", LOG_DEBUG);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if (!$resql) { if (!$resql) {
$this->error = $this->db->lasterror(); $this->error = $this->db->lasterror();
dol_syslog(get_class($this).'::demande_prelevement_stripe Erreur'); dol_syslog(get_class($this).'::makeStripeSepaRequest Erreur');
$error++; $error++;
} }
} else { } else {
$this->error = 'WithdrawRequestErrorNilAmount'; $this->error = 'WithdrawRequestErrorNilAmount';
dol_syslog(get_class($this).'::demande_prelevement_stripe WithdrawRequestErrorNilAmount'); dol_syslog(get_class($this).'::makeStripeSepaRequest WithdrawRequestErrorNilAmount');
$error++; $error++;
} }
@ -1605,19 +1610,14 @@ abstract class CommonInvoice extends CommonObject
return -1; return -1;
} }
return 1; return 1;
} else {
$this->error = "A request already exists";
dol_syslog(get_class($this).'::demande_prelevement_stripe Impossible de creer une demande, demande deja en cours');
return 0;
}
} else { } else {
$this->error = $this->db->error(); $this->error = $this->db->error();
dol_syslog(get_class($this).'::demande_prelevement_stripe Erreur -2'); dol_syslog(get_class($this).'::makeStripeSepaRequest Erreur -2');
return -2; return -2;
} }
} else { } else {
$this->error = "Status of invoice does not allow this"; $this->error = "Status of invoice does not allow this";
dol_syslog(get_class($this)."::demande_prelevement_stripe ".$this->error." $this->statut, $this->paye, $this->mode_reglement_id"); dol_syslog(get_class($this)."::makeStripeSepaRequest ".$this->error." $this->statut, $this->paye, $this->mode_reglement_id");
return -3; return -3;
} }
} }

View File

@ -71,3 +71,4 @@ PaymentWillBeRecordedForNextPeriod=Payment will be recorded for the next period.
ClickHereToTryAgain=<a href="%s">Click here to try again...</a> ClickHereToTryAgain=<a href="%s">Click here to try again...</a>
CreationOfPaymentModeMustBeDoneFromStripeInterface=Due to Strong Customer Authentication rules, creation of a card must be done from Stripe backoffice. You can click here to switch on Stripe customer record: %s CreationOfPaymentModeMustBeDoneFromStripeInterface=Due to Strong Customer Authentication rules, creation of a card must be done from Stripe backoffice. You can click here to switch on Stripe customer record: %s
TERMINAL_LOCATION=Location (address) for terminals TERMINAL_LOCATION=Location (address) for terminals
RequestDirectDebitWithStripe=Request Direct Debit with Stripe