Skip to content

Commit

Permalink
Integrate the new Stripe checkout with STRIPE_USE_NEW_CHECKOUT=1
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Jun 7, 2019
1 parent dc479a5 commit 9511a66
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 26 deletions.
76 changes: 52 additions & 24 deletions htdocs/public/payment/newpayment.php
Expand Up @@ -313,8 +313,6 @@

// Other
$PAYPAL_API_DEVISE="USD";
//if ($currency == 'EUR') $PAYPAL_API_DEVISE="EUR";
//if ($currency == 'USD') $PAYPAL_API_DEVISE="USD";
if (! empty($currency)) $PAYPAL_API_DEVISE=$currency;

// Show var initialized by include fo paypal lib at begin of this file
Expand All @@ -340,9 +338,6 @@
dol_syslog("desc: $desc", LOG_DEBUG);

dol_syslog("SCRIPT_URI: ".(empty($_SERVER["SCRIPT_URI"])?'':$_SERVER["SCRIPT_URI"]), LOG_DEBUG); // If defined script uri must match domain of PAYPAL_API_OK and PAYPAL_API_KO
//$_SESSION["PaymentType"]=$PAYPAL_PAYMENT_TYPE;
//$_SESSION["currencyCodeType"]=$PAYPAL_API_DEVISE;
//$_SESSION["FinalPaymentAmt"]=$PAYPAL_API_PRICE;

// A redirect is added if API call successfull
$mesg = print_paypal_redirect($PAYPAL_API_PRICE, $PAYPAL_API_DEVISE, $PAYPAL_PAYMENT_TYPE, $PAYPAL_API_OK, $PAYPAL_API_KO, $FULLTAG);
Expand Down Expand Up @@ -777,16 +772,20 @@
//print '<!-- Show logo (logosmall='.$logosmall.' logo='.$logo.') -->'."\n";
// Define urllogo
$urllogo='';
$urllogofull='';
if (! empty($logosmall) && is_readable($conf->mycompany->dir_output.'/logos/thumbs/'.$logosmall))
{
$urllogo=DOL_URL_ROOT.'/viewimage.php?modulepart=mycompany&amp;entity='.$conf->entity.'&amp;file='.urlencode('logos/thumbs/'.$logosmall);
$urllogofull=$dolibarr_main_url_root.'/viewimage.php?modulepart=mycompany&entity='.$conf->entity.'&file='.urlencode('logos/thumbs/'.$logosmall);
$width=150;
}
elseif (! empty($logo) && is_readable($conf->mycompany->dir_output.'/logos/'.$logo))
{
$urllogo=DOL_URL_ROOT.'/viewimage.php?modulepart=mycompany&amp;entity='.$conf->entity.'&amp;file='.urlencode('logos/'.$logo);
$urllogofull=$dolibarr_main_url_root.'/viewimage.php?modulepart=mycompany&entity='.$conf->entity.'&file='.urlencode('logos/'.$logo);
$width=150;
}

// Output html code for logo
if ($urllogo)
{
Expand Down Expand Up @@ -1771,7 +1770,7 @@
print '<input type="hidden" name="email" value="'.GETPOST('email', 'alpha').'" />';
print '<input type="hidden" name="thirdparty_id" value="'.GETPOST('thirdparty_id', 'int').'" />';

if (! empty($conf->global->STRIPE_USE_INTENT_WITH_AUTOMATIC_CONFIRMATION))
if (! empty($conf->global->STRIPE_USE_INTENT_WITH_AUTOMATIC_CONFIRMATION) || ! empty($conf->global->STRIPE_USE_NEW_CHECKOUT))
{
require_once DOL_DOCUMENT_ROOT.'/stripe/class/stripe.class.php';

Expand All @@ -1788,8 +1787,11 @@
$stripecu = null;
if (is_object($object) && is_object($object->thirdparty)) $stripecu = $stripe->customerStripe($object->thirdparty, $stripeacc, $servicestatus, 1);

$paymentintent=$stripe->getPaymentIntent($amount, $currency, $tag, 'Stripe payment: '.$fulltag.(is_object($object)?' ref='.$object->ref:''), $object, $stripecu, $stripeacc, $servicestatus);
if ($stripe->error) setEventMessages($stripe->error, null, 'errors');
if (! empty($conf->global->STRIPE_USE_INTENT_WITH_AUTOMATIC_CONFIRMATION))
{
$paymentintent=$stripe->getPaymentIntent($amount, $currency, $tag, 'Stripe payment: '.$fulltag.(is_object($object)?' ref='.$object->ref:''), $object, $stripecu, $stripeacc, $servicestatus);
if ($stripe->error) setEventMessages($stripe->error, null, 'errors');
}
}

if (empty($conf->global->STRIPE_USE_INTENT_WITH_AUTOMATIC_CONFIRMATION) || ! empty($paymentintent))
Expand Down Expand Up @@ -1847,13 +1849,14 @@

print '</form>'."\n";


// JS Code for Stripe
if (empty($stripearrayofkeys['publishable_key']))
{
print info_admin($langs->trans("ErrorModuleSetupNotComplete", "stripe"), 0, 0, 'error');
}
else
{
// JS Code for Stripe
print '<!-- JS Code for Stripe components -->';
print '<script src="https://js.stripe.com/v3/"></script>'."\n";

Expand All @@ -1862,21 +1865,46 @@

if (! empty($conf->global->STRIPE_USE_NEW_CHECKOUT))
{
$sessionstripe = \Stripe\Checkout\Session::create([
'customer_email' => (GETPOST('email', 'alpha')?GETPOST('email', 'alpha'):''),
'payment_method_types' => ['card'],
'line_items' => [[
'name' => 'T-shirt',
'description' => 'Stripe payment: '.$FULLTAG.' ref='.$ref,
//'images' => ['https://example.com/t-shirt.png'],
'amount' => $amount,
'currency' => $currency,
'quantity' => 1,
]],
'success_url' => $urlok,
'cancel_url' => $urlko,
]);

$amountstripe = $amount;

// Correct the amount according to unit of currency
// See https://support.stripe.com/questions/which-zero-decimal-currencies-does-stripe-support
$arrayzerounitcurrency=array('BIF', 'CLP', 'DJF', 'GNF', 'JPY', 'KMF', 'KRW', 'MGA', 'PYG', 'RWF', 'VND', 'VUV', 'XAF', 'XOF', 'XPF');
if (! in_array($currency, $arrayzerounitcurrency)) $amountstripe=$amountstripe * 100;

try {
$arrayforcheckout = array(
'payment_method_types' => array('card'),
'line_items' => array(array(
'name' => $langs->transnoentitiesnoconv("Payment").' '.$FULLTAG.' ref='.$ref,
'description' => 'Stripe payment: '.$FULLTAG.' ref='.$ref,
'amount' => $amountstripe,
'currency' => $currency,
'images' => array($urllogofull),
'quantity' => 1,
)),
'client_reference_id' => $FULLTAG,
'success_url' => $urlok,
'cancel_url' => $urlko,
);
if ($stripecu) $arrayforcheckout['customer'] = $stripecu;
elseif (GETPOST('email', 'alpha') && isValidEmail(GETPOST('email', 'alpha'))) $arrayforcheckout['customer_email'] = GETPOST('email', 'alpha');
$sessionstripe = \Stripe\Checkout\Session::create($arrayforcheckout);

$remoteip = getUserRemoteIP();

// Save some data for the paymentok
$_SESSION["currencyCodeType"] = $currency;
$_SESSION["paymentType"] = '';
$_SESSION["FinalPaymentAmt"] = $amount;
$_SESSION['ipaddress'] = ($remoteip?$remoteip:'unknown'); // Payer ip
$_SESSION['payerID'] = is_object($stripecu)?$stripecu->id:'';
$_SESSION['TRANSACTIONID'] = $sessionstripe->id;
}
catch(Exception $e)
{
print $e->getMessage();
}
?>
// Code for payment with option STRIPE_USE_NEW_CHECKOUT set

Expand Down
10 changes: 8 additions & 2 deletions htdocs/public/stripe/ipn.php
Expand Up @@ -297,12 +297,18 @@
$db->query($sql);
$db->commit();
}
elseif ($event->type == 'payment_intent.succeeded') {
// TODO: Redirect to paymentok.php
elseif ($event->type == 'payment_intent.succeeded') { // Called when making payment with PaymentIntent method ($conf->global->STRIPE_USE_NEW_CHECKOUT is on).
// TODO: create fees
// TODO: Redirect to paymentok.php
}
elseif ($event->type == 'payment_intent.payment_failed') {
// TODO: Redirect to paymentko.php
}
elseif ($event->type == 'checkout.session.completed') // Called when making payment with new Checkout method ($conf->global->STRIPE_USE_NEW_CHECKOUT is on).
{
// TODO: create fees
// TODO: Redirect to paymentok.php
}
elseif ($event->type == 'charge.succeeded') {
// TODO: create fees
// TODO: Redirect to paymentok.php
Expand Down

0 comments on commit 9511a66

Please sign in to comment.