-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #464 from Adyen/feature/AD-301
AD-301: Add support for Enhanced Scheme Data (L2/L3 data)
- Loading branch information
Showing
19 changed files
with
257 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
adyenv6core/src/com/adyen/commerce/services/AdyenRequestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.adyen.commerce.services; | ||
|
||
import com.adyen.model.checkout.PaymentRequest; | ||
import de.hybris.platform.commercefacades.order.data.CartData; | ||
|
||
import java.util.Map; | ||
|
||
public interface AdyenRequestService { | ||
|
||
String TOTAL_TAX_AMOUNT = "enhancedSchemeData.totalTaxAmount"; | ||
String CUSTOMER_REFERENCE = "enhancedSchemeData.customerReference"; | ||
String FREIGHT_AMOUNT = "enhancedSchemeData.freightAmount"; | ||
String SHIP_FROM_POSTAL_CODE = "enhancedSchemeData.shipFromPostalCode"; | ||
String ORDER_DATE = "enhancedSchemeData.orderDate"; | ||
String DESTINATION_POSTAL_CODE = "enhancedSchemeData.destinationPostalCode"; | ||
String DESTINATION_STATE_PROVINCE_CODE = "enhancedSchemeData.destinationStateProvinceCode"; | ||
String DESTINATION_COUNTRY_CODE = "enhancedSchemeData.destinationCountryCode"; | ||
String DUTY_AMOUNT = "enhancedSchemeData.dutyAmount"; | ||
|
||
String ITEM_DETAIL_DESCRIPTION = "enhancedSchemeData.itemDetailLine%d.description"; | ||
String ITEM_DETAIL_PRODUCT_CODE = "enhancedSchemeData.itemDetailLine%d.productCode"; | ||
String ITEM_DETAIL_COMMODITY_CODE = "enhancedSchemeData.itemDetailLine%d.commodityCode"; | ||
String ITEM_DETAIL_QUANTITY = "enhancedSchemeData.itemDetailLine%d.quantity"; | ||
String ITEM_DETAIL_UNIT_OF_MEASURE = "enhancedSchemeData.itemDetailLine%d.unitOfMeasure"; | ||
String ITEM_DETAIL_UNIT_PRICE = "enhancedSchemeData.itemDetailLine%d.unitPrice"; | ||
String ITEM_DETAIL_DISCOUNT_AMOUNT = "enhancedSchemeData.itemDetailLine%d.discountAmount"; | ||
String ITEM_DETAIL_TOTAL_AMOUNT = "enhancedSchemeData.itemDetailLine%d.totalAmount"; | ||
|
||
|
||
void populateL2L3AdditionalData(final Map<String, String> additionalData, final CartData cartData); | ||
|
||
|
||
void applyAdditionalData(CartData cartData, PaymentRequest paymentsRequest); | ||
} |
132 changes: 132 additions & 0 deletions
132
adyenv6core/src/com/adyen/commerce/services/impl/DefaultAdyenRequestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package com.adyen.commerce.services.impl; | ||
|
||
import com.adyen.commerce.services.AdyenRequestService; | ||
import com.adyen.model.checkout.CardDetails; | ||
import com.adyen.model.checkout.PaymentRequest; | ||
import de.hybris.platform.commercefacades.order.data.CartData; | ||
import de.hybris.platform.commercefacades.order.data.OrderEntryData; | ||
import de.hybris.platform.core.model.order.CartModel; | ||
import de.hybris.platform.order.CartService; | ||
import de.hybris.platform.servicelayer.config.ConfigurationService; | ||
import de.hybris.platform.store.services.BaseStoreService; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.spockframework.util.CollectionUtil; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.IntStream; | ||
|
||
public class DefaultAdyenRequestService implements AdyenRequestService { | ||
|
||
|
||
private BaseStoreService baseStoreService; | ||
private CartService cartService; | ||
private ConfigurationService configurationService; | ||
|
||
private static final String L2L3_EDS_SUPPORTED_BRANDS = "adyen.l2l3eds.supported.brands"; | ||
private static final String L2L3_EDS_SUPPORTED_COUNTRIES = "adyen.l2l3eds.supported.countries"; | ||
|
||
public DefaultAdyenRequestService(BaseStoreService baseStoreService, CartService cartService, ConfigurationService configurationService) { | ||
this.baseStoreService = baseStoreService; | ||
this.cartService = cartService; | ||
this.configurationService = configurationService; | ||
} | ||
|
||
@Override | ||
public void populateL2L3AdditionalData(final Map<String, String> additionalData, final CartData cartData) { | ||
// required fields | ||
if (cartData.getTotalTax() != null) { | ||
additionalData.put(TOTAL_TAX_AMOUNT, String.valueOf(cartData.getTotalTax().getValue())); | ||
} | ||
if (StringUtils.isNotEmpty(cartData.getMerchantCustomerId())) { | ||
additionalData.put(CUSTOMER_REFERENCE, cartData.getMerchantCustomerId()); | ||
} | ||
// not required but available | ||
if (cartData.getDeliveryCost() != null) { | ||
additionalData.put(FREIGHT_AMOUNT, String.valueOf(cartData.getDeliveryCost().getValue())); | ||
} | ||
if (cartData.getDeliveryAddress() != null) { | ||
if (cartData.getDeliveryAddress().getPostalCode() != null) { | ||
additionalData.put(DESTINATION_POSTAL_CODE, cartData.getDeliveryAddress().getPostalCode()); | ||
} | ||
if (cartData.getDeliveryAddress().getCountry() != null && cartData.getDeliveryAddress().getCountry().getIsocode() != null) { | ||
additionalData.put(DESTINATION_COUNTRY_CODE, cartData.getDeliveryAddress().getCountry().getIsocode()); | ||
} | ||
} | ||
|
||
// Extract item details from cartData and populate additionalData using a stream | ||
if (cartData.getEntries() != null) { | ||
cartData.getEntries().forEach( | ||
entry -> { | ||
if (entry != null && entry.getProduct() != null) { | ||
additionalData.put(String.format(ITEM_DETAIL_PRODUCT_CODE, entry.getEntryNumber()), | ||
Optional.ofNullable(entry.getProduct().getCode()).orElse(StringUtils.EMPTY)); | ||
additionalData.put(String.format(ITEM_DETAIL_DESCRIPTION, entry.getEntryNumber()), | ||
Optional.ofNullable(entry.getProduct().getName()).orElse(StringUtils.EMPTY)); | ||
|
||
additionalData.put(String.format(ITEM_DETAIL_QUANTITY, entry.getEntryNumber()), String.valueOf(entry.getQuantity())); | ||
|
||
additionalData.put(String.format(ITEM_DETAIL_UNIT_OF_MEASURE, entry.getEntryNumber()), | ||
Optional.ofNullable(entry.getUnitOfMeasure()).orElse(StringUtils.EMPTY)); | ||
|
||
additionalData.put(String.format(ITEM_DETAIL_COMMODITY_CODE, entry.getEntryNumber()), | ||
Optional.ofNullable(entry.getProduct().getCommodityCode()).orElse(StringUtils.EMPTY)); | ||
|
||
if (entry.getTotalPrice() != null) { | ||
additionalData.put(String.format(ITEM_DETAIL_TOTAL_AMOUNT, entry.getEntryNumber()), | ||
String.valueOf(Optional.ofNullable(entry.getTotalPrice().getValue()).orElse(BigDecimal.ZERO))); | ||
} | ||
|
||
if (entry.getBasePrice() != null) { | ||
additionalData.put(String.format(ITEM_DETAIL_UNIT_PRICE, entry.getEntryNumber()), | ||
String.valueOf(Optional.ofNullable(entry.getBasePrice().getValue()).orElse(BigDecimal.ZERO))); | ||
} | ||
} | ||
} | ||
); | ||
} | ||
} | ||
|
||
@Override | ||
public void applyAdditionalData(CartData cartData, PaymentRequest paymentsRequest) { | ||
Map<String, String> additionalData = new HashMap<>(); | ||
CartModel sessionCart = cartService.getSessionCart(); | ||
if(canL23EdsBeSent(paymentsRequest, sessionCart)) { | ||
populateL2L3AdditionalData(additionalData, cartData); | ||
} | ||
|
||
paymentsRequest.setAdditionalData(additionalData); | ||
|
||
} | ||
|
||
protected boolean canL23EdsBeSent(PaymentRequest paymentsRequest, CartModel sessionCart) { | ||
return Optional.ofNullable(baseStoreService.getCurrentBaseStore()) | ||
.map(store -> store.getL2L3ESDEnabled()) | ||
.orElse(false) && | ||
Optional.ofNullable(paymentsRequest.getPaymentMethod()) | ||
.map(method -> method.getActualInstance()) | ||
.filter(instance -> instance instanceof CardDetails) | ||
.map(instance -> (CardDetails) instance) | ||
.map(cardDetails -> getL2L3SupportedBrands().contains(cardDetails.getBrand())) | ||
.orElse(false) && | ||
Optional.ofNullable(sessionCart.getDeliveryAddress()) | ||
.map(address -> address.getCountry()) | ||
.map(country -> getL2L3SupportedCountries().contains(country.getIsocode())) | ||
.orElse(false); | ||
} | ||
|
||
protected List<String> getL2L3SupportedBrands() { | ||
String property = configurationService.getConfiguration().getString(L2L3_EDS_SUPPORTED_BRANDS); | ||
return property != null ? List.of(property.split(",")) : new ArrayList<>(); | ||
} | ||
|
||
protected List<String> getL2L3SupportedCountries() { | ||
String property = configurationService.getConfiguration().getString(L2L3_EDS_SUPPORTED_COUNTRIES); | ||
return property != null ? List.of(property.split(",")) : new ArrayList<>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.