Skip to content

Commit

Permalink
Tokenize payments (#927)
Browse files Browse the repository at this point in the history
* feat: set recurring rocessing model to get details reference(token)

* chore: renamed tokenization field

* fix: renamed function

* fix: check if token is already set

* fix: use constant for CardOnFile

* fix: use helper function to get first two numbers from year

* fix: use helper function to get first two numbers from year

* chore: added comment for checking token
  • Loading branch information
amihajlovski committed Sep 28, 2023
1 parent 07521ba commit 5b7d20d
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 25 deletions.
3 changes: 2 additions & 1 deletion jest/sfccCartridgeMocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ jest.mock('*/cartridge/scripts/util/adyenHelper', () => ({
getDonationAmounts: jest.fn(() => [10, 20, 30]),
getCardToken: jest.fn(() => 'mocked_token'),
getSFCCCardType: jest.fn(() => 'mocked_cardType'),
getFirstTwoNumbersFromYear: jest.fn(() => 20),
createAdyenCheckoutResponse: jest.fn(() => ({isFinal: true, isSuccessful: false})),
getCustomer: jest.fn(() => {}),
createSignature: jest.fn( () => 'mocked_signature'),
Expand Down Expand Up @@ -256,7 +257,7 @@ jest.mock('*/cartridge/scripts/util/adyenConfigs', () => {
return {
getAdyenEnvironment: jest.fn(() => 'TEST'),
getCreditCardInstallments: jest.fn(() => true),
getAdyenApplePayTokenisationEnabled: jest.fn(() => true),
getAdyenTokenisationEnabled: jest.fn(() => true),
getAdyenClientKey: jest.fn(() => 'mocked_client_key'),
getGoogleMerchantID: jest.fn(() => 'mocked_google_merchant_id'),
getAdyenCardholderNameEnabled: jest.fn(() => true),
Expand Down
8 changes: 4 additions & 4 deletions metadata/site_import/meta/system-objecttype-extensions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,9 @@
<externally-managed-flag>false</externally-managed-flag>
<default-value>false</default-value>
</attribute-definition>
<attribute-definition attribute-id="AdyenApplePayTokenisationEnabled">
<display-name xml:lang="x-default">Tokenize Apple Pay payments</display-name>
<description xml:lang="x-default">Tokenize payment details for Apple Pay</description>
<attribute-definition attribute-id="AdyenTokenisationEnabled">
<display-name xml:lang="x-default">Tokenize payments</display-name>
<description xml:lang="x-default">Tokenize payment details for all payment methods</description>
<type>boolean</type>
<mandatory-flag>false</mandatory-flag>
<externally-managed-flag>false</externally-managed-flag>
Expand Down Expand Up @@ -670,7 +670,7 @@
<attribute attribute-id="Adyen_IntegratorName"/>
<attribute attribute-id="Adyen_StoreId"/>
<attribute attribute-id="AdyenOneClickEnabled"/>
<attribute attribute-id="AdyenApplePayTokenisationEnabled"/>
<attribute attribute-id="AdyenTokenisationEnabled"/>
<attribute attribute-id="AdyenCreditCardInstallments"/>
<attribute attribute-id="AdyenBasketFieldsEnabled"/>
<attribute attribute-id="AdyenLevel23DataEnabled"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@
</div>
</div>
<div class="form-group border-top mt-4">
<label class="form-title mt-4" for="applePayTokenisation">Apple Pay Tokenisation</label>
<label class="form-title mt-4" for="adyenTokenisation">Tokenisation</label>
<small id="applePayHelp" class="form-text mb-1">
To tokenize payment details for Apple Pay, make sure you have enabled Apple Pay in Adyen Customer Area.
To tokenize payment details for all payment methods, make sure you have this feature enabled.
</small>
<div class="radio-buttons">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="AdyenApplePayTokenisationEnabled" id="ApplePayTokenisationYes" value=true ${AdyenConfigs.getAdyenApplePayTokenisationEnabled() ? 'checked': ''}>
<label class="form-check-label" for="ApplePayTokenisationYes">Enable</label>
<input class="form-check-input" type="radio" name="AdyenTokenisationEnabled" id="AdyenTokenisationYes" value=true ${AdyenConfigs.getAdyenTokenisationEnabled() ? 'checked': ''}>
<label class="form-check-label" for="AdyenTokenisationYes">Enable</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="AdyenApplePayTokenisationEnabled" id="ApplePayTokenisationNo" value=false ${!AdyenConfigs.getAdyenApplePayTokenisationEnabled() ? 'checked': ''}>
<label class="form-check-label" for="ApplePayTokenisationNo">Disable</label>
<input class="form-check-input" type="radio" name="AdyenTokenisationEnabled" id="AdyenTokenisationNo" value=false ${!AdyenConfigs.getAdyenTokenisationEnabled() ? 'checked': ''}>
<label class="form-check-label" for="AdyenTokenisationNo">Disable</label>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ function convertToSfccCardType(paymentInformation, paymentInstrument) {
sfccCardType;

if (paymentInformation.creditCardToken) {
const firstTwoDigitsFromCurrentYear =
AdyenHelper.getFirstTwoNumbersFromYear();
const expirationYear =
firstTwoDigitsFromCurrentYear * 100 + paymentInformation.expirationYear;
paymentInstrument.setCreditCardExpirationMonth(
paymentInformation.expirationMonth,
);
paymentInstrument.setCreditCardExpirationYear(
paymentInformation.expirationYear,
);
paymentInstrument.setCreditCardExpirationYear(expirationYear);
paymentInstrument.setCreditCardToken(paymentInformation.creditCardToken);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
window.googleMerchantID = '${pdict.adyen.googleMerchantID}';
window.cardholderNameBool = '${pdict.adyen.cardholderNameBool}';
window.merchantAccount = '${pdict.adyen.merchantAccount}';
window.customerEmail = '${customer?.profile?.email}';
window.customerEmail = '${customer && customer.profile && customer.profile.email ? customer.profile.email : ''}';
var showStoreDetails = ${customer.authenticated && adyenRecurringPaymentsEnabled};
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ function createPaymentRequest(args) {
}
}

//Set Apple Pay tokenisation
if (AdyenConfigs.getAdyenApplePayTokenisationEnabled() && AdyenHelper.isApplePay(paymentRequest.paymentMethod.type)) {
paymentRequest.storePaymentMethod = true;
paymentRequest.recurringProcessingModel = 'CardOnFile';
//Set tokenisation
if (AdyenConfigs.getAdyenTokenisationEnabled()) {
paymentRequest.storePaymentMethod = true;
paymentRequest.recurringProcessingModel = constants.RECURRING_PROCESSING_MODEL.CARD_ON_FILE;
}
setPaymentTransactionType(paymentInstrument, paymentRequest.paymentMethod);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ const adyenConfigsObj = {
return getCustomPreference('Adyen_Frontend_Region').value;
},

getAdyenApplePayTokenisationEnabled: function () {
return getCustomPreference('AdyenApplePayTokenisationEnabled');
getAdyenTokenisationEnabled: function () {
return getCustomPreference('AdyenTokenisationEnabled');
},

getAdyenSalePaymentMethods: function () {
Expand All @@ -109,10 +109,6 @@ const adyenConfigsObj = {
return getCustomPreference('AdyenBasketFieldsEnabled');
},

getAdyenApplePayTokenisationEnabled: function () {
return getCustomPreference('AdyenApplePayTokenisationEnabled');
},

getAdyenCardholderNameEnabled: function () {
return getCustomPreference('AdyenCardHolderName_enabled');
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,13 @@ var adyenHelperObj = {
);
}

// For authenticated shoppers we are setting the token on other place already
// SFRA throws an error if you try to set token again that's why this check is added
const tokenAlreadyExists = paymentInstrument.getCreditCardToken();
if (!tokenAlreadyExists && result.additionalData && result.additionalData['recurring.recurringDetailReference']) {
paymentInstrument.setCreditCardToken(result.additionalData['recurring.recurringDetailReference']);
}

paymentInstrument.paymentTransaction.custom.authCode = result.resultCode
? result.resultCode
: '';
Expand All @@ -683,6 +690,12 @@ var adyenHelperObj = {
return true;
},

getFirstTwoNumbersFromYear() {
return Math.floor(
new Date().getFullYear() / 100,
);
},

// converts the currency value for the Adyen Checkout API
getCurrencyValueForApi(amount) {
const currencyCode = dwutil.Currency.getCurrency(amount.currencyCode);
Expand Down

0 comments on commit 5b7d20d

Please sign in to comment.