Skip to content

Commit

Permalink
Fix various fix cmail, payout, const, connect
Browse files Browse the repository at this point in the history
  • Loading branch information
ptibogxiv committed Nov 1, 2018
1 parent 970f501 commit d5a9108
Showing 1 changed file with 45 additions and 24 deletions.
69 changes: 45 additions & 24 deletions htdocs/public/stripe/ipn.php
Expand Up @@ -22,7 +22,9 @@
$entity=(! empty($_GET['entity']) ? (int) $_GET['entity'] : (! empty($_POST['entity']) ? (int) $_POST['entity'] : 1));
if (is_numeric($entity)) define("DOLENTITY", $entity);

require '../../main.inc.php';
$res=0;
if (! $res && file_exists("../../main.inc.php")) $res=@include("../../main.inc.php"); // to work if your module directory is into a subdir of root htdocs directory
if (! $res) die("Include of main fails");

if (empty($conf->stripe->enabled)) accessforbidden('',0,0,1);
require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
Expand All @@ -43,29 +45,30 @@
{
$endpoint_secret = $conf->global->STRIPE_TEST_WEBHOOK_CONNECT_KEY;
$service = 'StripeTest';
$servicestatus = 0;
$servicestatus = 0;
}
else
{
$endpoint_secret = $conf->global->STRIPE_LIVE_WEBHOOK_CONNECT_KEY;
$service = 'StripeLive';
$servicestatus = 1;
$servicestatus = 1;
}
}
else {
if (isset($_GET['test']))
{
$endpoint_secret = $conf->global->STRIPE_TEST_WEBHOOK_KEY;
$service = 'StripeTest';
$servicestatus = 0;
$servicestatus = 0;
}
else
{
$endpoint_secret = $conf->global->STRIPE_LIVE_WEBHOOK_KEY;
$service = 'StripeLive';
$servicestatus = 1;
$servicestatus = 1;
}
}

$payload = @file_get_contents("php://input");
$sig_header = $_SERVER["HTTP_STRIPE_SIGNATURE"];
$event = null;
Expand Down Expand Up @@ -127,7 +130,7 @@
if ($event->type == 'payout.created') {
$error=0;

$result=dolibarr_set_const($db, $servicestatus."_NEXTPAYOUT", date('Y-m-d H:i:s',$event->data->object->arrival_date), 'chaine', 0, '', $conf->entity);
$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)
{
Expand Down Expand Up @@ -172,7 +175,7 @@
elseif ($event->type == 'payout.paid') {
global $conf;
$error=0;
$result=dolibarr_set_const($db, $servicestatus."_NEXTPAYOUT",null,'chaine',0,'',$conf->entity);
$result=dolibarr_set_const($db, $service."_NEXTPAYOUT",null,'chaine',0,'',$conf->entity);
if ($result)
{
$langs->load("errors");
Expand All @@ -187,14 +190,9 @@
$accountfrom->fetch($conf->global->STRIPE_BANK_ACCOUNT_FOR_PAYMENTS);

$accountto=new Account($db);
$accountto->fetch($conf->global->STRIPE_BANK_ACCOUNT_FOR_BANKTRANFERS);

if ($accountto->currency_code != $accountfrom->currency_code) {
$error++;
setEventMessages($langs->trans("ErrorTransferBetweenDifferentCurrencyNotPossible"), null, 'errors');
}
$accountto->fetch($conf->global->STRIPE_BANK_ACCOUNT_FOR_BANKTRANSFERS);

if ($accountto->id != $accountfrom->id)
if (($accountto->id != $accountfrom->id) && empty($error))
{

$bank_line_id_from=0;
Expand All @@ -207,21 +205,44 @@

if (! $error) $bank_line_id_from = $accountfrom->addline($dateo, $typefrom, $label, -1*price2num($amount), '', '', $user);
if (! ($bank_line_id_from > 0)) $error++;
if ((! $error) && ($accountto->currency_code == $accountfrom->currency_code)) $bank_line_id_to = $accountto->addline($dateo, $typeto, $label, price2num($amount), '', '', $user);
if ((! $error) && ($accountto->currency_code != $accountfrom->currency_code)) $bank_line_id_to = $accountto->addline($dateo, $typeto, $label, price2num($amount_to), '', '', $user);
if (! $error) $bank_line_id_to = $accountto->addline($dateo, $typeto, $label, price2num($amount), '', '', $user);
if (! ($bank_line_id_to > 0)) $error++;

if (! $error) $result=$accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.'/compta/bank/ligne.php?rowid=', '(banktransfert)', 'banktransfert');
if (! ($result > 0)) $error++;
if (! $error) $result=$accountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.'/compta/bank/ligne.php?rowid=', '(banktransfert)', 'banktransfert');
if (! $error) $result=$accountfrom->add_url_line($bank_line_id_from, $bank_line_id_to, DOL_URL_ROOT.'/compta/bank/ligne.php?rowid=', '(banktransfert)', 'banktransfert');
if (! ($result > 0)) $error++;
if (! $error) $result=$accountto->add_url_line($bank_line_id_to, $bank_line_id_from, DOL_URL_ROOT.'/compta/bank/ligne.php?rowid=', '(banktransfert)', 'banktransfert');
if (! ($result > 0)) $error++;

}
$subject = '[NOTIFICATION] Stripe payout done';
if (!empty($user->email)) {
$sendto = dolGetFirstLastname($user->firstname, $user->lastname) . " <".$user->email.">";
} else {
$sendto = $conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>';
}
$replyto = $sendto;
$sendtocc = '';
if (!empty($conf->global->ONLINE_PAYMENT_SENDEMAIL)) {
$sendtocc = $conf->global->ONLINE_PAYMENT_SENDEMAIL.'" <'.$conf->global->ONLINE_PAYMENT_SENDEMAIL.'>';
}

// TODO Use CMail and translation
$body = "Un virement de ".price2num($event->data->object->amount/100)." ".$event->data->object->currency." a ete effectue sur votre compte le ".date('d-m-Y H:i:s',$event->data->object->arrival_date);
$subject = '[NOTIFICATION] Virement effectué';
$headers = 'From: "'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'" <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>';
mail(''.$conf->global->MAIN_INFO_SOCIETE_MAIL.'', $subject, $body, $headers);
$message = "A bank transfer of ".price2num($event->data->object->amount/100)." ".$event->data->object->currency." has been transfert in 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;
}
Expand Down

0 comments on commit d5a9108

Please sign in to comment.