Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-648 Upgrade stripe to support SCA #657

Open
wants to merge 13 commits into
base: 17.x
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -303,17 +303,20 @@ private function stripeHandler()
$sAmount = $this->httpRequest->post('amount');

try {
$oCharge = \Stripe\Charge::create(
[
'amount' => Stripe::getAmount($sAmount),
'currency' => $this->config->values['module.setting']['currency_code'],
'source' => $this->httpRequest->post('stripeToken'),
'description' => t('Membership charged for %0%', $this->httpRequest->post('stripeEmail'))
]
);
$oIntent = \Stripe\PaymentIntent::create([
'payment_method_data' => [
'type' => 'card',
'card' => ['token' => $this->httpRequest->post('stripeToken'),],
],
'amount' => Stripe::getAmount($sAmount),
'currency' => $this->config->values['module.setting']['currency_code'],
'confirmation_method' => 'manual',
'confirm' => true,
'description' => t('Membership charged for %0%', $this->httpRequest->post('stripeEmail'))
]);

// Make sure the item has been paid
if ($oCharge->paid === true) {
if (in_array($oIntent->status, ['requires_payment_method', 'requires_action'], true) === false) {
$iItemNumber = $this->httpRequest->post('item_number');
if ($this->oUserModel->updateMembership(
$iItemNumber,
Expand All @@ -325,10 +328,10 @@ private function stripeHandler()
$this->notification(Stripe::class, $iItemNumber);
}
}
} catch (\Stripe\Error\Card $oE) {
} catch (\Stripe\Exception\CardException $oE) {
// The card has been declined
// Do nothing here as "$this->bStatus" is by default FALSE and so it will display "Error occurred" msg later
} catch (\Stripe\Error\Base $oE) {
} catch (\Stripe\Exception\ApiErrorException $oE) {
$this->design->setMessage($this->str->escape($oE->getMessage(), true));
}
}
Expand Down
8 changes: 3 additions & 5 deletions _protected/app/system/modules/payment/inc/class/Stripe.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
<?php
/**
* @author Pierre-Henry Soria <ph7software@gmail.com>
* @author Pierre-Henry Soria <hello@ph7cms.com>
* @copyright (c) 2015-2019, Pierre-Henry Soria. All Rights Reserved.
* @license GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
* @package PH7 / App / System / Module / Payment / Inc / Class
*/

namespace PH7;

use PH7\Framework\Payment\Gateway\Api\Stripe as StripeGateway;

class Stripe extends StripeGateway
class Stripe
{
use Api; // Import the Api trait

const JS_LIBRARY_URL = 'https://checkout.stripe.com/checkout.js';
const JS_LIBRARY_URL = 'https://js.stripe.com/v3';

/**
* @param string $sPrice Normal price format (e.g., 19.95).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function buttonPayPal(stdClass $oMembership)
->param('return', Uri::get('payment', 'main', 'process', 'paypal'))
->param('rm', 2)// Auto redirection in POST data
->param('notify_url', Uri::get('payment', 'main', 'notification', 'PH7\PayPal,' . $oMembership->groupId))
->param('cancel_return', Uri::get('payment', 'main', 'membership', '?msg=' . t('The payment was aborted. No charge has been taken from your account.'), false));
->param('cancel_return', $this->getCancelPaymentUrl());

$this->displayGatewayForm($oPayPal, $oMembership->name, 'PayPal');

Expand All @@ -59,27 +59,32 @@ public function buttonPayPal(stdClass $oMembership)
*/
public function buttonStripe(stdClass $oMembership)
{
$oStripe = new Stripe;

$oStripe
->param('item_number', $oMembership->groupId)
->param('amount', $oMembership->price);

echo
'<form action="', $oStripe->getUrl(), '" method="post">',
$oStripe->generate(),
'<script
src="', Stripe::JS_LIBRARY_URL, '" class="stripe-button"
data-key="', $this->config->values['module.setting']['stripe.publishable_key'], '"
data-name="', $this->registry->site_name, '"
data-description="', $oMembership->name, '"
data-amount="', Stripe::getAmount($oMembership->price), '"
data-currency="', $this->config->values['module.setting']['currency_code'], '"
data-allow-remember-me="true">
</script>
</form>';

unset($oStripe);
$iAmount = Stripe::getAmount($oMembership->price);
$sMembershipName = $oMembership->name;
$sSuccessUrl = Uri::get('payment', 'main', 'process', 'stripe');
$sCancelUrl = $this->getCancelPaymentUrl();

echo '<script src="', Stripe::JS_LIBRARY_URL, '"></script>';
echo '<button id="checkout-button" class="btn btn-primary btn-md">', $this->buyTxt($sMembershipName, 'Stripe'), '</button>';

echo <<<JS
<script>
var stripe = Stripe('{$this->config->values['module.setting']['stripe.publishable_key']}');
var checkoutButton = document.querySelector('#checkout-button');
checkoutButton.addEventListener('click', function () {
stripe.redirectToCheckout({
items: [{
name: "{$this->registry->site_name}",
plan: "$sMembershipName",
amount: {$iAmount},
currency: "{$this->config->values['module.setting']['currency_code']}",
quantity: 1
}],
successUrl: '$sSuccessUrl',
cancelUrl: '$sCancelUrl'
});
});</script>
JS;
}

/**
Expand Down Expand Up @@ -227,6 +232,20 @@ private function getDivFormContainer()
return '<div id="' . self::DIV_CONTAINER_NAME . '"></div>';
}

/**
* @return string
*/
private function getCancelPaymentUrl()
{
return Uri::get(
'payment',
'main',
'membership',
'?msg=' . t('The payment was aborted. No charge has been taken from your account.'),
false
);
}

/**
* @param string $sValue
*
Expand Down
51 changes: 0 additions & 51 deletions _protected/framework/Payment/Gateway/Api/Stripe.class.php

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"robthree/twofactorauth": "^1.6",
"shrikeh/teapot": "^2.3",
"facebook/graph-sdk": "^5.6",
"stripe/stripe-php": "^5.9",
"stripe/stripe-php": "^7.5",
"braintree/braintree_php": "^3.27",
"arcansecurity/skeerel-php": "^2.3",
"twilio/sdk": "^5.28",
Expand Down