diff --git a/htdocs/langs/en_US/stripe.lang b/htdocs/langs/en_US/stripe.lang index 2e244858d28..3d1e3925309 100644 --- a/htdocs/langs/en_US/stripe.lang +++ b/htdocs/langs/en_US/stripe.lang @@ -12,6 +12,7 @@ YourEMail=Email to receive payment confirmation STRIPE_PAYONLINE_SENDEMAIL=Email notification after a payment attempt (success or fail) Creditor=Creditor PaymentCode=Payment code +StripeAutoRecordPayout=Enable the auto recording of payout (when Stripe do a payout and call the webhook payout.create/payout.paid) StripeDoPayment=Pay with Stripe YouWillBeRedirectedOnStripe=You will be redirected on secured Stripe page to input you credit card information Continue=Next diff --git a/htdocs/public/stripe/ipn.php b/htdocs/public/stripe/ipn.php index 0a2c982258d..e04053634d7 100644 --- a/htdocs/public/stripe/ipn.php +++ b/htdocs/public/stripe/ipn.php @@ -178,14 +178,14 @@ top_httphead(); dol_syslog("***** Stripe IPN was called with event->type=".$event->type." service=".$service); -if ($event->type == 'payout.created') { - // When a payout is create by Stripe to transfer money to your account +if ($event->type == 'payout.created' && getDolGlobalString('STRIPE_AUTO_RECORD_PAYOUT')) { + // When a payout is created by Stripe to transfer money to your account $error = 0; $result = dolibarr_set_const($db, $service."_NEXTPAYOUT", date('Y-m-d H:i:s', $event->data->object->arrival_date), 'chaine', 0, '', $conf->entity); if ($result > 0) { - $subject = $societeName.' - [NOTIFICATION] Stripe payout scheduled'; + $subject = '['.$societeName.'] Notification - Stripe payout scheduled'; if (!empty($user->email)) { $sendto = dolGetFirstLastname($user->firstname, $user->lastname)." <".$user->email.">"; } else { @@ -221,7 +221,7 @@ if ($event->type == 'payout.created') { http_response_code(500); return -1; } -} elseif ($event->type == 'payout.paid') { +} elseif ($event->type == 'payout.paid' && getDolGlobalString('STRIPE_AUTO_RECORD_PAYOUT')) { // When a payout to transfer money to your account is completely done $error = 0; $result = dolibarr_set_const($db, $service."_NEXTPAYOUT", 0, 'chaine', 0, '', $conf->entity); @@ -249,6 +249,8 @@ if ($event->type == 'payout.created') { $typefrom = 'PRE'; $typeto = 'VIR'; + $db->begin(); + if (!$error) { $bank_line_id_from = $accountfrom->addline($dateo, $typefrom, $label, -1 * (float) price2num($amount), '', '', $user); } @@ -274,38 +276,47 @@ if ($event->type == 'payout.created') { if (!($result > 0)) { $error++; } + + if (!$error) { + $db->commit(); + } else { + $db->rollback(); + } + + // Send email + if (!$error) { + $subject = '['.$societeName.'] - NotificationOTIFICATION] Stripe payout done'; + if (!empty($user->email)) { + $sendto = dolGetFirstLastname($user->firstname, $user->lastname)." <".$user->email.">"; + } else { + $sendto = getDolGlobalString('MAIN_INFO_SOCIETE_MAIL') . '" <' . getDolGlobalString('MAIN_INFO_SOCIETE_MAIL').'>'; + } + $replyto = $sendto; + $sendtocc = ''; + if (getDolGlobalString('ONLINE_PAYMENT_SENDEMAIL')) { + $sendtocc = getDolGlobalString('ONLINE_PAYMENT_SENDEMAIL') . '" <' . getDolGlobalString('ONLINE_PAYMENT_SENDEMAIL').'>'; + } + + $message = "A bank transfer of ".price2num($event->data->object->amount / 100)." ".$event->data->object->currency." has been done to your account the ".dol_print_date($event->data->object->arrival_date, 'dayhour'); + + $mailfile = new CMailFile( + $subject, + $sendto, + $replyto, + $message, + array(), + array(), + array(), + $sendtocc, + '', + 0, + -1 + ); + + $ret = $mailfile->sendfile(); + } } - $subject = $societeName.' - [NOTIFICATION] Stripe payout done'; - if (!empty($user->email)) { - $sendto = dolGetFirstLastname($user->firstname, $user->lastname)." <".$user->email.">"; - } else { - $sendto = getDolGlobalString('MAIN_INFO_SOCIETE_MAIL') . '" <' . getDolGlobalString('MAIN_INFO_SOCIETE_MAIL').'>'; - } - $replyto = $sendto; - $sendtocc = ''; - if (getDolGlobalString('ONLINE_PAYMENT_SENDEMAIL')) { - $sendtocc = getDolGlobalString('ONLINE_PAYMENT_SENDEMAIL') . '" <' . getDolGlobalString('ONLINE_PAYMENT_SENDEMAIL').'>'; - } - - $message = "A bank transfer of ".price2num($event->data->object->amount / 100)." ".$event->data->object->currency." has been done to your account the ".dol_print_date($event->data->object->arrival_date, 'dayhour'); - - $mailfile = new CMailFile( - $subject, - $sendto, - $replyto, - $message, - array(), - array(), - array(), - $sendtocc, - '', - 0, - -1 - ); - - $ret = $mailfile->sendfile(); - return 1; } else { $error++; diff --git a/htdocs/stripe/admin/stripe.php b/htdocs/stripe/admin/stripe.php index a017742d722..eb08aac811d 100644 --- a/htdocs/stripe/admin/stripe.php +++ b/htdocs/stripe/admin/stripe.php @@ -379,18 +379,30 @@ print ''.$langs->trans("Example").': '.$mysoc->name.''; print ''; -print ''; -print $langs->trans("StripeUserAccountForActions").''; -print img_picto('', 'user', 'class="pictofixedwidth"').$form->select_dolusers(getDolGlobalString('STRIPE_USER_ACCOUNT_FOR_ACTIONS'), 'STRIPE_USER_ACCOUNT_FOR_ACTIONS', 0); -print ''; - print ''; print $langs->trans("BankAccount").''; print img_picto('', 'bank_account', 'class="pictofixedwidth"'); $form->select_comptes(getDolGlobalString('STRIPE_BANK_ACCOUNT_FOR_PAYMENTS'), 'STRIPE_BANK_ACCOUNT_FOR_PAYMENTS', 0, '', 1); print ''; -if (getDolGlobalInt('MAIN_FEATURES_LEVEL') >= 2) { // What is this for ? + +// Param to record automatically payouts (received from IPN payout.payed and payout.created) +print ''; +print $langs->trans("StripeAutoRecordPayout").''; +if ($conf->use_javascript_ajax) { + print ajax_constantonoff('STRIPE_AUTO_RECORD_PAYOUT', array(), null, 0, 0, 1); +} else { + $arrval = array('0' => $langs->trans("No"), '1' => $langs->trans("Yes")); + print $form->selectarray("STRIPE_AUTO_RECORD_PAYOUT", $arrval, getDolGlobalInt('STRIPE_AUTO_RECORD_PAYOUT')); +} +print ''; + +if (getDolGlobalInt('STRIPE_AUTO_RECORD_PAYOUT')) { + print ''; + print $langs->trans("StripeUserAccountForActions").''; + print img_picto('', 'user', 'class="pictofixedwidth"').$form->select_dolusers(getDolGlobalString('STRIPE_USER_ACCOUNT_FOR_ACTIONS'), 'STRIPE_USER_ACCOUNT_FOR_ACTIONS', 0); + print ''; + print ''; print $langs->trans("BankAccountForBankTransfer").''; print img_picto('', 'bank_account', 'class="pictofixedwidth"');