Skip to content

Commit

Permalink
AD-49 [FE][Express checkout] Add ApplePayExpress button on cart page
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaneta committed Oct 24, 2023
1 parent 53920a4 commit 2b6e8ed
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
var AdyenExpressCheckoutHybris = (function () {
'use strict';


return {

initiateCheckout: async function (initConfig) {
const configuration = {
...initConfig,
analytics: {
enabled: false // Set to false to not send analytics data to Adyen.
},
risk: {
enabled: false
},
onPaymentCompleted: (result, component) => {
console.info(result, component);
},
onError: (error, component) => {
console.error(error.name, error.message, error.stack, component);
},
};

return await AdyenCheckout(configuration);
},

initiateApplePayExpress: async function (params, config) {
var checkoutPromise = this.initiateCheckout(config);
const {amount, countryCode, applePayMerchantIdentifier, applePayMerchantName, label} = params;
const applePayNode = document.getElementById('adyen-component-button-container-' + label);
const self = this;
const applePayConfiguration = {
//onValidateMerchant is required if you're using your own Apple Pay certificate
onValidateMerchant: (resolve, reject, validationURL) => {
resolve();

// Your server uses the validation URL to request a session from the Apple Pay server.
// Call resolve(MERCHANTSESSION) or reject() to complete merchant validation.
/*validateMerchant(validationURL)
.then(response => {
// Complete merchant validation with resolve(MERCHANTSESSION) after receiving an opaque merchant session object, MerchantSession
resolve(response);
})
.catch(error => {
// Complete merchant validation with reject() if any error occurs
reject();
});*/
}
};
checkoutPromise.then((checkout) => {
var applePayComponent = checkout.create("applepay", {
amount: {
currency: amount.currency,
value: amount.value
},
configuration: {
merchantName: applePayMerchantName,
merchantId: applePayMerchantIdentifier
},
// Button config
buttonType: "plain",
buttonColor: "black",
onChange: function(state, component) {
console.log("Apple pay on change, state: ")
console.log(state)
},
onSubmit: function(state, component) {
if (!state.isValid) {
return false;
}
//TODO: implement payment call
},
onShippingContactSelected: function(event){
var shippingAddress = event.shippingContact;
//TODO: implement call with address
console.log(shippingAddress);
},
onClick: function(resolve, reject) {
resolve();
}
});
applePayComponent.isAvailable()
.then(function () {
applePayComponent.mount(applePayNode);
})
.catch(function (e) {
// Apple Pay is not available
console.log('Something went wrong trying to mount the Apple Pay component: ' + e);
});
})
}
}
})();
8 changes: 4 additions & 4 deletions adyenv6b2ccheckoutaddon/project.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Wed, 28 Jun 2023 14:11:09 +0200
#Fri, 20 Oct 2023 13:13:06 +0200
# -----------------------------------------------------------------------
# [y] hybris Platform
#
Expand All @@ -23,11 +23,11 @@ adyenv6b2ccheckoutaddon.application-context=adyenv6b2ccheckoutaddon-spring.xml



adyenv6b2ccheckoutaddon.javascript.paths.responsive=/responsive/common/js/adyen.checkout.js;/responsive/common/js/adyen_billingaddress.js;/responsive/common/js/adyen_deliveryaddress.js
adyenv6b2ccheckoutaddon.javascript.paths.responsive=/responsive/common/js/adyen.checkout.js;/responsive/common/js/adyen_billingaddress.js;/responsive/common/js/adyen_deliveryaddress.js;/responsive/common/js/adyen_express_checkout.js
adyenv6b2ccheckoutaddon.css.paths.responsive=/responsive/common/css/adyenv6b2ccheckoutaddon.css;

csrf.allowed.url.patterns=/[^/]+(/[^?]*)+(sop-response)$,/[^/]+(/[^?]*)+(merchant_callback)$,/[^/]+(/[^?]*)+(hop-response)$,/[^/]+(/[^?]*)+(adyen-response)$,/adyen(/[^?]*)+$

yacceleratorstorefront.additionalWebSpringConfigs.adyenv6b2ccheckoutaddon=classpath\:/adyenv6b2ccheckoutaddon/web/spring/adyenv6b2ccheckoutaddon-web-spring.xml
yb2cacceleratorstorefront.additionalWebSpringConfigs.adyenv6b2ccheckoutaddon=classpath\:/adyenv6b2ccheckoutaddon/web/spring/adyenv6b2ccheckoutaddon-web-spring.xml

yacceleratorstorefront.wro4jconfigscan.adyenv6b2ccheckoutaddon=true
yb2cacceleratorstorefront.wro4jconfigscan.adyenv6b2ccheckoutaddon=true
2 changes: 1 addition & 1 deletion adyenv6b2ccheckoutaddon/project.properties.template
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ adyenv6b2ccheckoutaddon.application-context=adyenv6b2ccheckoutaddon-spring.xml

yacceleratorstorefront.additionalWebSpringConfigs.adyenv6b2ccheckoutaddon=classpath:/adyenv6b2ccheckoutaddon/web/spring/adyenv6b2ccheckoutaddon-web-spring.xml

adyenv6b2ccheckoutaddon.javascript.paths.responsive=/responsive/common/js/adyen.checkout.js;/responsive/common/js/adyen_billingaddress.js;/responsive/common/js/adyen_deliveryaddress.js
adyenv6b2ccheckoutaddon.javascript.paths.responsive=/responsive/common/js/adyen.checkout.js;/responsive/common/js/adyen_billingaddress.js;/responsive/common/js/adyen_deliveryaddress.js;/responsive/common/js/adyen_express_checkout.js
adyenv6b2ccheckoutaddon.css.paths.responsive=/responsive/common/css/adyenv6b2ccheckoutaddon.css;

csrf.allowed.url.patterns=/[^/]+(/[^?]*)+(sop-response)$,/[^/]+(/[^?]*)+(merchant_callback)$,/[^/]+(/[^?]*)+(hop-response)$,/[^/]+(/[^?]*)+(adyen-response)$,/adyen(/[^?]*)+$
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$contentCatalog=electronicsContentCatalog
$contentCVS=catalogVersion(CatalogVersion.catalog(Catalog.id[default=$contentCatalog]),CatalogVersion.version[default=Staged])[default=$contentCatalog:Staged]
$contentCVO=catalogVersion(CatalogVersion.catalog(Catalog.id[default=$contentCatalog]),CatalogVersion.version[default=Online])[default=$contentCatalog:Online]

INSERT_UPDATE JspIncludeComponent;uid[unique=true];page;$contentCVS[unique=true]
;CartComponent;/WEB-INF/views/responsive/pages/cart/cartDisplay.jsp


INSERT_UPDATE JspIncludeComponent;uid[unique=true];page;$contentCVO[unique=true]
;CartComponent;/WEB-INF/views/responsive/pages/cart/cartDisplay.jsp
2 changes: 2 additions & 0 deletions adyenv6core/src/com/adyen/v6/facades/AdyenCheckoutFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ public interface AdyenCheckoutFacade {

void initializeSummaryData(Model model) throws ApiException;

void initializeApplePayExpressData(Model model) throws ApiException;

/**
* Returns whether Boleto should be shown as an available payment method on the checkout page
* Relevant for Brasil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public class DefaultAdyenCheckoutFacade implements AdyenCheckoutFacade {

public static final String DETAILS = "details";
private static final String LOCALE = "locale";
private static final String SESSION_DATA = "sessionData";
public static final String SESSION_DATA = "sessionData";
private static final String REGION = "region";
private static final String US_LOCALE = "en_US";
private static final String GB_LOCALE = "en_GB";
Expand Down Expand Up @@ -832,6 +832,23 @@ public void initializeSummaryData(Model model) throws ApiException {
model.addAttribute(LOCALE, gson.toJson(setLocale(cartData.getAdyenAmazonPayConfiguration(), shopperLocale)));
}

public void initializeApplePayExpressData(Model model) throws ApiException {
final BaseStoreModel baseStore = baseStoreService.getCurrentBaseStore();
final CartData cartData = getCheckoutFacade().getCheckoutCart();
final Amount amount = Util.createAmount(cartData.getTotalPriceWithTax().getValue(), cartData.getTotalPriceWithTax().getCurrencyIso());

model.addAttribute(SHOPPER_LOCALE, getShopperLocale());
model.addAttribute(MODEL_ENVIRONMENT_MODE, getEnvironmentMode());
model.addAttribute(MODEL_CLIENT_KEY, baseStore.getAdyenClientKey());
model.addAttribute(MODEL_MERCHANT_ACCOUNT, baseStore.getAdyenMerchantAccount());
model.addAttribute(MODEL_APPLEPAY_MERCHANT_IDENTIFIER, cartData.getAdyenApplePayMerchantIdentifier());
model.addAttribute(MODEL_APPLEPAY_MERCHANT_NAME, cartData.getAdyenApplePayMerchantName());
model.addAttribute(SESSION_DATA, getAdyenSessionData());
model.addAttribute(MODEL_AMOUNT, amount);
model.addAttribute(MODEL_DF_URL, getAdyenPaymentService().getDeviceFingerprintUrl());
model.addAttribute(MODEL_CHECKOUT_SHOPPER_HOST, getCheckoutShopperHost());
}

private String setLocale(final Map<String, String> map, final String shopperLocale) {
if (Objects.nonNull(map) && !map.get(REGION).isBlank() && map.get(REGION).equals(US)) {
return US_LOCALE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import com.adyen.service.exception.ApiException;
import com.adyen.terminal.serialization.TerminalAPIGsonBuilder;
import com.adyen.util.Util;
import com.adyen.v6.enums.AdyenRegions;
import com.adyen.v6.enums.RecurringContractMode;
import com.adyen.v6.factory.AdyenRequestFactory;
import com.adyen.v6.model.RequestInfo;
Expand Down Expand Up @@ -450,7 +449,9 @@ public CreateCheckoutSessionResponse getPaymentSessionData(final CartData cartDa
final CreateCheckoutSessionRequest createCheckoutSessionRequest = new CreateCheckoutSessionRequest();
createCheckoutSessionRequest.amount(Util.createAmount(totalPriceWithTax.getValue(), totalPriceWithTax.getCurrencyIso()));
createCheckoutSessionRequest.merchantAccount(getBaseStore().getAdyenMerchantAccount());
createCheckoutSessionRequest.countryCode(cartData.getDeliveryAddress().getCountry().getIsocode());
if (cartData.getDeliveryAddress() != null) {
createCheckoutSessionRequest.countryCode(cartData.getDeliveryAddress().getCountry().getIsocode());
}
createCheckoutSessionRequest.returnUrl(Optional.ofNullable(cartData.getAdyenReturnUrl()).orElse("returnUrl"));
createCheckoutSessionRequest.reference(cartData.getCode());

Expand Down

0 comments on commit 2b6e8ed

Please sign in to comment.