Skip to content

Commit

Permalink
Merge pull request #25 from OXID-eSales/STRIP-29_REUSE_CREDITCARDS
Browse files Browse the repository at this point in the history
Strip 29 reuse creditcards
  • Loading branch information
mariolorenz authored Jun 18, 2024
2 parents 162b090 + d3a521c commit 264dafb
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 25 deletions.
2 changes: 2 additions & 0 deletions Application/translations/de/stripe_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
'STRIPE_COUNTRY_ES' => 'Spanien',
'STRIPE_COUNTRY_IT' => 'Italien',
'STRIPE_COUNTRY_NL' => 'Niederlande',
'STRIPE_CARDS_USED' => 'genutzte Kreditkarten',
'STRIPE_NEW_CARD' => 'neue Karte',

'STRIPE_ERROR_ORDER_NOT_FOUND' => 'Bestellung konnte nicht gefunden werden',
'STRIPE_ERROR_TRANSACTIONID_NOT_FOUND' => 'Transaktions-Id konnte nicht gefunden werden',
Expand Down
2 changes: 2 additions & 0 deletions Application/translations/en/stripe_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
'STRIPE_COUNTRY_ES' => 'Spain',
'STRIPE_COUNTRY_IT' => 'Italy',
'STRIPE_COUNTRY_NL' => 'Netherlands',
'STRIPE_CARDS_USED' => 'credit cards used',
'STRIPE_NEW_CARD' => 'new credit card',

'STRIPE_ERROR_ORDER_NOT_FOUND' => 'Order not found',
'STRIPE_ERROR_TRANSACTIONID_NOT_FOUND' => 'Transaction id not found',
Expand Down
83 changes: 65 additions & 18 deletions Application/views/frontend/tpl/stripecreditcard.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,76 @@
</div>
</div>

<div class="form-group">
<label for="stripe_card_holder" class="req control-label col-lg-3">[{oxmultilang ident="BANK_ACCOUNT_HOLDER"}]</label>
<div class="col-lg-9">
<input type="text" id="stripe_card_holder" class="form-control" name="dynvalue[stripe_card_holder]" required="required" />
[{if $oView->stripeGetUsedCards()}]
<div class="form-group">
<label for="stripe_used_card" class="req control-label col-lg-3">[{oxmultilang ident="STRIPE_CARDS_USED"}]</label>
<div class="col-lg-9">
<select class="form-control" id="stripe_used_card" name="dynvalue[stripe_used_card]" required="required">
<option value="">[{oxmultilang ident="STRIPE_PLEASE_SELECT"}]</option>
[{foreach from=$oView->stripeGetUsedCards() item=card}]
<option value="[{$card.id}]">[{$card.title}] ([{$card.expire}])</option>
[{/foreach}]
<option value="new">[{oxmultilang ident="STRIPE_NEW_CARD"}]</option>
</select>
[{foreach from=$oView->stripeGetUsedCards() item=card}]
<input type="hidden" id="stripe_card_known_holder_[{$card.id}]" name="dynvalue[stripe_card_holder][]" value="[{$card.holder}]" />
[{/foreach}]
</div>
</div>
</div>
[{/if}]

<div class="form-group">
<label class="req control-label col-lg-3">[{oxmultilang ident="CREDITCARD"}]</label>
<div class="col-lg-9">
<div id="stripeCardElement" class="form-control"></div>
<div id="stripe_new_card"[{if $oView->stripeGetUsedCards()}] style="display:none;"[{/if}]>
<div class="form-group">
<label for="stripe_card_holder" class="req control-label col-lg-3">[{oxmultilang ident="BANK_ACCOUNT_HOLDER"}]</label>
<div class="col-lg-9">
<input type="text" id="stripe_card_holder" class="form-control" name="dynvalue[stripe_card_holder]" required="required" />
</div>
</div>

<div class="form-group">
<label class="req control-label col-lg-3">[{oxmultilang ident="CREDITCARD"}]</label>
<div class="col-lg-9">
<div id="stripeCardElement" class="form-control"></div>
</div>
</div>
</div>

[{oxscript include="https://js.stripe.com/v3/"}]
[{capture name="stripeComponentsLoad"}]
var pubKey = '[{$oPaymentModel->getPublishableKey()}]';
if (pubKey == '') {
[{if false}]<script>[{/if}]
const pubKey = '[{$oPaymentModel->getPublishableKey()}]';
if (pubKey === '') {
document.getElementById('[{$sInputName}]_error').innerHTML = '[{oxmultilang ident="STRIPE_ERROR_ORDER_CONFIG_PUBKEY"}]';
document.getElementById('[{$sInputName}]_error_box').style.display = '';
} else {
$("#stripe_used_card").change(function() {
const val = $(this).val();
if (val === "new") {
$("#stripe_new_card").show();
}
else {
$("#stripe_new_card").hide();
}
});
if (!stripe) {
var stripe = Stripe(pubKey);
let stripe = Stripe(pubKey);
}
var elements = stripe.elements();
var cardElement = elements.create('card', {hidePostalCode: true});
cardElement.mount('#stripeCardElement');
const elements = stripe.elements(),
cardElement = elements.create('card', {hidePostalCode: true}),
paymentForm = document.getElementById('payment');
let displayErrorBox = document.getElementById('stripe_creditcard_error_box');
let displayError = document.getElementById('stripe_creditcard_error');
cardElement.mount('#stripeCardElement');
cardElement.on('change', ({error}) => {
if (error) {
let stripeUsedCard = '';
if (paymentForm.elements['stripe_used_card']) {
stripeUsedCard = paymentForm.elements['stripe_used_card'].value;
}
if (error && (!stripeUsedCard || stripeUsedCard === 'new')) {
displayError.textContent = error.message;
displayErrorBox.style.display = 'block';
} else {
Expand All @@ -49,12 +86,21 @@
}
});
var paymentForm = document.getElementById('payment');
paymentForm.addEventListener('submit', function(event) {
let stripeUsedCard = '';
if (paymentForm.elements['stripe_used_card']) {
stripeUsedCard = paymentForm.elements['stripe_used_card'].value;
}
if (paymentForm.elements['payment_stripecreditcard'].checked === true) {
event.preventDefault();
stripe.createToken(cardElement, {name: document.getElementById('stripe_card_holder').value})
const holder = document.getElementById('stripe_card_holder').value;
if (!holder && stripeUsedCard && stripeUsedCard !== 'new') {
paymentForm.submit();
}
stripe.createToken(cardElement, {name: holder})
.then(function(result) {
if (result.error) {
displayError.textContent = result.error.message;
Expand All @@ -69,5 +115,6 @@
}
});
}
[{if false}]</script>[{/if}]
[{/capture}]
[{oxscript add=$smarty.capture.stripeComponentsLoad}]
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [1.0.3] - 2024-??-??

### NEW

- reuse of credit card data

## [1.0.2] - 2024-02-09

- Fix onBording in OXID-Enterprise-Environment
Expand Down
51 changes: 45 additions & 6 deletions extend/Application/Controller/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class PaymentController extends PaymentController_parent
{
protected ?array $aStripeUsedCards = null;

/**
* Delete sess_challenge from session to trigger the creation of a new order when needed
*/
Expand Down Expand Up @@ -120,13 +122,19 @@ public function validatepayment()
$oBasket = Registry::getSession()->getBasket();
$oStripePaymentModel = PaymentHelper::getInstance()->getStripePaymentModel($sPaymentId);

if ($sPaymentId == 'stripecreditcard') {
if ($sPaymentId === 'stripecreditcard') {
$sStripeTokenId = $this->getDynValue()['stripe_token_id'];
$oStripeCardRequest = $oStripePaymentModel->getCardRequest();
$oStripeCardRequest->addRequestParameters($sStripeTokenId, $oBasket->getUser());
$oCard = $oStripeCardRequest->execute();
if (!empty($oCard->id)) {
Registry::getSession()->setVariable('stripe_current_payment_method_id', $oCard->id);
$sStripeCard = $this->getDynValue()['stripe_used_card'];

if(!$sStripeCard || $sStripeCard === 'new') {
$oStripeCardRequest = $oStripePaymentModel->getCardRequest();
$oStripeCardRequest->addRequestParameters($sStripeTokenId, $oBasket->getUser());

$oCard = $oStripeCardRequest->execute();
$sStripeCard = $oCard->id ?? '';
}
if ($sStripeCard) {
Registry::getSession()->setVariable('stripe_current_payment_method_id', $sStripeCard);
}
} else {
$oStripePaymentMethodRequest = $oStripePaymentModel->getPaymentMethodRequest();
Expand All @@ -152,4 +160,35 @@ public function stripeGetSofortCountries()
{
return ['AT','BE','DE','ES','IT','NL'];
}

/**
* Template variable getter. Returns possible used cards
*
* @return null|array
*/
public function stripeGetUsedCards(): ?array
{
if (is_null($this->aStripeUsedCards)) {
$this->aStripeUsedCards = [];
$oUser = $this->getUser();
$sStripeCustomerId = $oUser->getFieldData('stripecustomerid');
if ($sStripeCustomerId) {
$oPaymentCollection = PaymentHelper::getInstance()->loadStripeApi()->paymentMethods->all([
'customer' => $sStripeCustomerId,
'type' => 'card'
]);
if (isset($oPaymentCollection->data) && count($oPaymentCollection->data)) {
foreach($oPaymentCollection->data as $oPaymentMethod) {
$this->aStripeUsedCards[] = [
'id' => $oPaymentMethod->id,
'title' => 'XXXX XXXX XXXX ' . $oPaymentMethod->card->last4,
'expire' => $oPaymentMethod->card->exp_month . '/' . $oPaymentMethod->card->exp_year,
'holder' => $oPaymentMethod->billing_details->name
];
}
}
}
}
return count($this->aStripeUsedCards) ? $this->aStripeUsedCards : null;
}
}
2 changes: 1 addition & 1 deletion metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
'en' => 'This module integrates STRIPE as payment provider in your OXID Shop.',
],
'thumbnail' => 'stripe_logo.png',
'version' => '1.0.2',
'version' => '1.0.3-rc.1',
'author' => 'OXID eSales AG',
'url' => 'https://www.oxid-esales.com',
'email' => '[email protected]',
Expand Down

0 comments on commit 264dafb

Please sign in to comment.