Skip to content

Commit

Permalink
NEW enhancement management of webhooks
Browse files Browse the repository at this point in the history
active / inactive and sync only needed events
  • Loading branch information
ptibogxiv committed Apr 24, 2019
1 parent f8c381f commit 57ce70b
Showing 1 changed file with 70 additions and 12 deletions.
82 changes: 70 additions & 12 deletions htdocs/stripe/admin/stripe.php
Expand Up @@ -2,7 +2,7 @@
/* Copyright (C) 2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
* Copyright (C) 2017 Olivier Geffroy <jeff@jeffinfo.com>
* Copyright (C) 2017 Saasprov <saasprov@gmail.com>
* Copyright (C) 2018 ptibogxiv <support@ptibogxiv.net>
* Copyright (C) 2018 Thibault FOUCART <support@ptibogxiv.net>
* Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
*
* This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -30,6 +30,7 @@
require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php';
require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';

$servicename='Stripe';

Expand Down Expand Up @@ -150,6 +151,8 @@

dol_fiche_head($head, 'stripeaccount', '', -1);

$stripearrayofwebhookevents=array('payout.created','payout.paid','charge.pending','charge.refunded','charge.succeeded','charge.failed','payment_intent.succeeded','payment_intent.payment_failed','source.chargeable','customer.deleted');

print $langs->trans("StripeDesc")."<br>\n";

print '<br>';
Expand All @@ -158,6 +161,7 @@
print '<tr class="liste_titre">';
print '<td>'.$langs->trans("AccountParameter").'</td>';
print '<td>'.$langs->trans("Value").'</td>';
print '<td>'.$langs->trans("Status").'</td>';
print "</tr>\n";

print '<tr class="oddeven">';
Expand All @@ -177,24 +181,51 @@
print '<span class="fieldrequired">'.$langs->trans("STRIPE_TEST_PUBLISHABLE_KEY").'</span></td><td>';
print '<input class="minwidth300" type="text" name="STRIPE_TEST_PUBLISHABLE_KEY" value="'.$conf->global->STRIPE_TEST_PUBLISHABLE_KEY.'">';
print ' &nbsp; '.$langs->trans("Example").': pk_test_xxxxxxxxxxxxxxxxxxxxxxxx';
print '</td></tr>';
print '</td><td></td></tr>';

print '<tr class="oddeven"><td>';
print '<span class="titlefield fieldrequired">'.$langs->trans("STRIPE_TEST_SECRET_KEY").'</span></td><td>';
print '<input class="minwidth300" type="text" name="STRIPE_TEST_SECRET_KEY" value="'.$conf->global->STRIPE_TEST_SECRET_KEY.'">';
print ' &nbsp; '.$langs->trans("Example").': sk_test_xxxxxxxxxxxxxxxxxxxxxxxx';
print '</td></tr>';
print '</td><td></td></tr>';

print '<tr class="oddeven"><td>';
print '<span class="titlefield fieldrequired">'.$langs->trans("STRIPE_TEST_WEBHOOK_KEY").'</span></td><td>';
print '<span>'.$langs->trans("STRIPE_TEST_WEBHOOK_KEY").'</span></td><td>';
print '<input class="minwidth500" type="text" name="STRIPE_TEST_WEBHOOK_ID" value="'.$conf->global->STRIPE_TEST_WEBHOOK_ID.'">';
print ' &nbsp; '.$langs->trans("Example").': we_xxxxxxxxxxxxxxxxxxxxxxxx<br>';
print '<input class="minwidth500" type="text" name="STRIPE_TEST_WEBHOOK_KEY" value="'.$conf->global->STRIPE_TEST_WEBHOOK_KEY.'">';
print ' &nbsp; '.$langs->trans("Example").': whsec_xxxxxxxxxxxxxxxxxxxxxxxx';
$out = img_picto('', 'object_globe.png').' '.$langs->trans("ToOfferALinkForTestWebhook").'<br>';
$out = img_picto('', 'object_globe.png').' '.$langs->trans("ToOfferALinkForTestWebhook").'<br>';
$url = dol_buildpath('/public/stripe/ipn.php?test', 2);
$out.= '<input type="text" id="onlinetestwebhookurl" class="quatrevingtpercent" value="'.$url.'">';
$out.= ajax_autoselect("onlinetestwebhookurl", 0);
print '<br>'.$out;
print '</td></tr>';
print '<br />'.$out;
print '</td><td>';
if ( !empty($conf->global->STRIPE_TEST_WEBHOOK_KEY) && !empty($conf->global->STRIPE_TEST_SECRET_KEY) && !empty($conf->global->STRIPE_TEST_WEBHOOK_ID) ) {
\Stripe\Stripe::setApiKey($conf->global->STRIPE_TEST_SECRET_KEY);
$endpoint = \Stripe\WebhookEndpoint::retrieve($conf->global->STRIPE_TEST_WEBHOOK_ID);
$endpoint->enabled_events = $stripearrayofwebhookevents;
if ( GETPOST('webhook','alpha') == $conf->global->STRIPE_TEST_WEBHOOK_ID ) {
if ( empty( GETPOST('status','alpha') )) {
$endpoint->disabled = true;
} else {
$endpoint->disabled = false;
}}
$endpoint->url = dol_buildpath('/public/stripe/ipn.php?test', 2);
$endpoint->save();
if ($endpoint->status == 'enabled')
{
print '<a href="'.$_SERVER['PHP_SELF'].'?action=ipn&webhook='.$endpoint->id.'&status=0">';
print img_picto($langs->trans("Activated"),'switch_on');
}
else
{
print '<a href="'.$_SERVER['PHP_SELF'].'?action=ipn&webhook='.$endpoint->id.'&status=1">';
print img_picto($langs->trans("Disabled"),'switch_off');
}
//print $endpoint;
} else print img_picto($langs->trans("inactive"),'statut5');
print'</td></tr>';
} else {
print '<tr class="oddeven"><td>'.$langs->trans("StripeConnect").'</td>';
print '<td><b>'.$langs->trans("StripeConnect_Mode").'</b><br/>';
Expand All @@ -212,24 +243,51 @@
print '<span class="fieldrequired">'.$langs->trans("STRIPE_LIVE_PUBLISHABLE_KEY").'</span></td><td>';
print '<input class="minwidth300" type="text" name="STRIPE_LIVE_PUBLISHABLE_KEY" value="'.$conf->global->STRIPE_LIVE_PUBLISHABLE_KEY.'">';
print ' &nbsp; '.$langs->trans("Example").': pk_live_xxxxxxxxxxxxxxxxxxxxxxxx';
print '</td></tr>';
print '</td><td></td></tr>';

print '<tr class="oddeven"><td>';
print '<span class="fieldrequired">'.$langs->trans("STRIPE_LIVE_SECRET_KEY").'</span></td><td>';
print '<input class="minwidth300" type="text" name="STRIPE_LIVE_SECRET_KEY" value="'.$conf->global->STRIPE_LIVE_SECRET_KEY.'">';
print ' &nbsp; '.$langs->trans("Example").': sk_live_xxxxxxxxxxxxxxxxxxxxxxxx';
print '</td></tr>';
print '</td><td></td></tr>';

print '<tr class="oddeven"><td>';
print '<span class="titlefield fieldrequired">'.$langs->trans("STRIPE_LIVE_WEBHOOK_KEY").'</span></td><td>';
print '<span>'.$langs->trans("STRIPE_LIVE_WEBHOOK_KEY").'</span></td><td>';
print '<input class="minwidth500" type="text" name="STRIPE_LIVE_WEBHOOK_ID" value="'.$conf->global->STRIPE_LIVE_WEBHOOK_ID.'">';
print ' &nbsp; '.$langs->trans("Example").': we_xxxxxxxxxxxxxxxxxxxxxxxx<br>';
print '<input class="minwidth500" type="text" name="STRIPE_LIVE_WEBHOOK_KEY" value="'.$conf->global->STRIPE_LIVE_WEBHOOK_KEY.'">';
print ' &nbsp; '.$langs->trans("Example").': whsec_xxxxxxxxxxxxxxxxxxxxxxxx';
$out = img_picto('', 'object_globe.png').' '.$langs->trans("ToOfferALinkForLiveWebhook").'<br>';
$url = dol_buildpath('/public/stripe/ipn.php', 2);
$out.= '<input type="text" id="onlinelivewebhookurl" class="quatrevingtpercent" value="'.$url.'">';
$out.= ajax_autoselect("onlinelivewebhookurl", 0);
print '<br>'.$out;
print '</td></tr>';
print '<br />'.$out;
print '</td><td>';
if ( !empty($conf->global->STRIPE_LIVE_WEBHOOK_KEY) && !empty($conf->global->STRIPE_LIVE_SECRET_KEY) && !empty($conf->global->STRIPE_LIVE_WEBHOOK_ID) ) {
\Stripe\Stripe::setApiKey($conf->global->STRIPE_LIVE_SECRET_KEY);
$endpoint = \Stripe\WebhookEndpoint::retrieve($conf->global->STRIPE_LIVE_WEBHOOK_ID);
$endpoint->enabled_events = $stripearrayofwebhookevents;
if ( GETPOST('webhook','alpha') == $conf->global->STRIPE_LIVE_WEBHOOK_ID ) {
if ( empty( GETPOST('status','alpha') )) {
$endpoint->disabled = true;
} else {
$endpoint->disabled = false;
}}
$endpoint->url = dol_buildpath('/public/stripe/ipn.php', 2);
$endpoint->save();
if ($endpoint->status == 'enabled')
{
print '<a href="'.$_SERVER['PHP_SELF'].'?action=ipn&webhook='.$endpoint->id.'&status=0">';
print img_picto($langs->trans("Activated"),'switch_on');
}
else
{
print '<a href="'.$_SERVER['PHP_SELF'].'?action=ipn&webhook='.$endpoint->id.'&status=1">';
print img_picto($langs->trans("Disabled"),'switch_off');
}
//print $endpoint;
} else print img_picto($langs->trans("inactive"),'statut5');
print '</td></tr>';
}
else
{
Expand Down

0 comments on commit 57ce70b

Please sign in to comment.