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

@W-14886263 - Update Apex for Delivery Estimation #301

Merged
merged 31 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d1de810
Update: Heroku Deprecation
gurpreetsainisalesforce Aug 14, 2023
90ca54b
Update: Use example.com as the hostname
gurpreetsainisalesforce Aug 17, 2023
d3ab189
Update: PR Feedback
gurpreetsainisalesforce Aug 17, 2023
5ed07bf
Update: heroku url remote site
gurpreetsainisalesforce Aug 17, 2023
2e45de9
Update: B2B Samples
gurpreetsainisalesforce Aug 21, 2023
1a46d77
Update: Fix Compilation Issues
gurpreetsainisalesforce Aug 21, 2023
060e425
Update: Revert with sharing and security enforced
gurpreetsainisalesforce Aug 22, 2023
323a1a7
Update: Cleanup
gurpreetsainisalesforce Aug 23, 2023
ccb9c64
Update: Cleanup
gurpreetsainisalesforce Aug 23, 2023
336f98c
Update: Cleanup
gurpreetsainisalesforce Aug 23, 2023
ecd1258
Update: Sync Integrations
gurpreetsainisalesforce Aug 23, 2023
571fd7a
Update: After SFDX Runs
gurpreetsainisalesforce Aug 25, 2023
5d5c511
Merge branch 'forcedotcom:develop' into develop
gurpreetsainisalesforce Sep 5, 2023
b9d2184
Update: Remove References to Custom Labels
gurpreetsainisalesforce Sep 5, 2023
947d678
Update: Revert B2B Aura Sample
gurpreetsainisalesforce Sep 8, 2023
b787ab9
Merge branch 'forcedotcom:develop' into develop
gurpreetsainisalesforce Sep 8, 2023
d57daab
Update: Revert B2B Aura Sample
gurpreetsainisalesforce Sep 8, 2023
7a57dec
Update: Revert B2B Aura Sample
gurpreetsainisalesforce Sep 8, 2023
b811311
Update: Revert B2B Aura Sample
gurpreetsainisalesforce Sep 8, 2023
9706330
Update: Fix B2BTaxSample
gurpreetsainisalesforce Sep 8, 2023
7ae2f27
Merge branch 'forcedotcom:develop' into develop
gurpreetsainisalesforce Sep 8, 2023
2be08a6
Merge branch 'forcedotcom:develop' into develop
gurpreetsainisalesforce Sep 12, 2023
2a6fb5b
Update: B2BSyncTax to consider United States
gurpreetsainisalesforce Sep 12, 2023
05010f4
Merge branch 'forcedotcom:develop' into develop
gurpreetsainisalesforce Sep 22, 2023
71e690b
Merge branch 'forcedotcom:develop' into develop
gurpreetsainisalesforce Jan 23, 2024
6530644
@W-14886263 - Update Apex for Delivery Estimation
gurpreetsainisalesforce Jan 25, 2024
94ecc07
Update API version to 61
gurpreetsainisalesforce Jan 26, 2024
7cf0d9e
Update: Remvoe hardcoding Class Name
gurpreetsainisalesforce Jan 29, 2024
f93fe99
Merge branch 'forcedotcom:develop' into develop
gurpreetsainisalesforce Jan 29, 2024
f202b98
Update: Remvoe hardcoding Class Name
gurpreetsainisalesforce Jan 29, 2024
6e3d59a
Update: Compilation Failure
gurpreetsainisalesforce Jan 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 77 additions & 21 deletions examples/b2c/checkout/integrations/classes/B2CDeliverySample.cls
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ global class B2CDeliverySample implements sfdc_checkout.CartShippingCharges {
// and add the host in Setup | Security | Remote site settings.
private static String httpHost = 'https://example.com';
private static Boolean useHTTPService = false;
private String className = String.valueOf(this).split(':')[0];
ApexClass apexClass = [SELECT ApiVersion FROM ApexClass WHERE Name = :className];
global sfdc_checkout.IntegrationStatus startCartProcessAsync(sfdc_checkout.IntegrationInfo jobInfo, Id cartId) {
sfdc_checkout.IntegrationStatus integStatus = new sfdc_checkout.IntegrationStatus();
try {
Expand Down Expand Up @@ -34,13 +36,17 @@ global class B2CDeliverySample implements sfdc_checkout.CartShippingCharges {

// Create a CartDeliveryGroupMethod record for every shipping option returned from the external service
Integer cdgmToBeCreated = 0;
CartDeliveryGroupMethod[] cdgmsToInsert = new CartDeliveryGroupMethod[]{};
for (ShippingOptionsAndRatesFromExternalService shippingOption: shippingOptionsAndRatesFromExternalService) {
for(CartDeliveryGroup curCartDeliveryGroup : cartDeliveryGroups){
populateCartDeliveryGroupMethodWithShippingOptions(shippingOption, curCartDeliveryGroup.Id, cartId);
CartDeliveryGroupMethod cdgm = populateCartDeliveryGroupMethodWithShippingOptions(shippingOption, curCartDeliveryGroup.Id, cartId);
cdgmsToInsert.add(cdgm);
cdgmToBeCreated += 1;
}
}

insert(cdgmsToInsert);

List<CartDeliveryGroupMethod> cdgms = new List<CartDeliveryGroupMethod>([SELECT Id FROM CartDeliveryGroupMethod WHERE WebCartId = :cartId]);
System.assertEquals(cdgmToBeCreated, cdgms.size(),'The number of created CDGMs is not matching'); // It's important to fail the example integration early

Expand Down Expand Up @@ -93,8 +99,8 @@ global class B2CDeliverySample implements sfdc_checkout.CartShippingCharges {

// If the request is successful, parse the JSON response.
// The response looks like this:
// [{"status":"calculated","rate":{"name":"Delivery Method 1","serviceName":"Test Carrier 1","serviceCode":"SNC9600","shipmentCost":11.99,"otherCost":5.99}},
// {"status":"calculated","rate":{"name":"Delivery Method 2","serviceName":"Test Carrier 2","serviceCode":"SNC9600","shipmentCost":15.99,"otherCost":6.99}}]
// [{"status":"calculated","rate":{"name":"'+name1+'","serviceName":"'+serviceName1+'","serviceCode":"SNC9600","shipmentCost":11.99,"otherCost":5.99,"transitTimeMin":1,"transitTimeMax":2,"transitTimeUnit":"Days","processTime":1,"processTimeUnit":"Days"}},
// {"status":"calculated","rate":{"name":"'+name2+'","serviceName":"'+serviceName2+'","serviceCode":"SNC9600","shipmentCost":15.99,"otherCost":6.99,"transitTimeMin":2,"transitTimeMax":3,"transitTimeUnit":"Days","processTime":1,"processTimeUnit":"Days"}}]
if (response.getStatusCode() == successfulHttpRequest) {
List<Object> results = (List<Object>) JSON.deserializeUntyped(response.getBody());
for (Object result: results) {
Expand All @@ -109,7 +115,12 @@ global class B2CDeliverySample implements sfdc_checkout.CartShippingCharges {
(String) providerAndRate.get('serviceName'),
(String) providerAndRate.get('serviceCode'),
generateRandomString(10),
true
true,
(Integer) providerAndRate.get('transitTimeMin'),
(Integer) providerAndRate.get('transitTimeMax'),
(String) providerAndRate.get('transitTimeUnit'),
(Integer) providerAndRate.get('processTime'),
(String) providerAndRate.get('processTimeUnit')
));
}
return shippingOptions;
Expand Down Expand Up @@ -140,7 +151,12 @@ global class B2CDeliverySample implements sfdc_checkout.CartShippingCharges {
(String) providerAndRate.get('serviceName'),
(String) providerAndRate.get('serviceCode'),
generateRandomString(10),
true
true,
(Integer) providerAndRate.get('transitTimeMin'),
(Integer) providerAndRate.get('transitTimeMax'),
(String) providerAndRate.get('transitTimeUnit'),
(Integer) providerAndRate.get('processTime'),
(String) providerAndRate.get('processTimeUnit')
));
}
return shippingOptions;
Expand All @@ -158,6 +174,8 @@ global class B2CDeliverySample implements sfdc_checkout.CartShippingCharges {

private String getShippingOptionsResponse(String siteLanguage) {
String name1, name2, serviceName1, serviceName2;
Integer transitTimeMin, transitTimeMax, processTime;
String transitTimeUnit, processTimeUnit;
if(siteLanguage == 'de') {
name1 = 'Liefermethode 1';
name2 = 'Liefermethode 2';
Expand All @@ -174,7 +192,8 @@ global class B2CDeliverySample implements sfdc_checkout.CartShippingCharges {
serviceName1 = 'Test Carrier 1';
serviceName2 = 'Test Carrier 2';
}
return '[{"status":"calculated","rate":{"name":"'+name1+'","serviceName":"'+serviceName1+'","serviceCode":"SNC9600","shipmentCost":11.99,"otherCost":5.99}},{"status":"calculated","rate":{"name":"'+name2+'","serviceName":"'+serviceName2+'","serviceCode":"SNC9600","shipmentCost":15.99,"otherCost":6.99}}]';

return '[{"status":"calculated","rate":{"name":"'+name1+'","serviceName":"'+serviceName1+'","serviceCode":"SNC9600","shipmentCost":11.99,"otherCost":5.99,"transitTimeMin":1,"transitTimeMax":2,"transitTimeUnit":"Days","processTime":1,"processTimeUnit":"Days"}},{"status":"calculated","rate":{"name":"'+name2+'","serviceName":"'+serviceName2+'","serviceCode":"SNC9600","shipmentCost":15.99,"otherCost":6.99,"transitTimeMin":2,"transitTimeMax":3,"transitTimeUnit":"Days","processTime":1,"processTimeUnit":"Days"}}]';
}

// Structure to store the shipping options retrieved from external service.
Expand All @@ -188,6 +207,11 @@ global class B2CDeliverySample implements sfdc_checkout.CartShippingCharges {
private String classOfService;
private String referenceNumber;
private Boolean isActive;
private Integer transitTimeMin;
private Integer transitTimeMax;
private String transitTimeUnit;
private Integer processTime;
private String processTimeUnit;

public ShippingOptionsAndRatesFromExternalService() {
name = '';
Expand All @@ -199,10 +223,16 @@ global class B2CDeliverySample implements sfdc_checkout.CartShippingCharges {
classOfService = '';
referenceNumber = '';
isActive = true;
transitTimeMin = 0;
transitTimeMax = 0;
transitTimeUnit = '';
processTime = 0;
processTimeUnit = '';
}

public ShippingOptionsAndRatesFromExternalService(String someName, String someProvider, Decimal someRate, Decimal someOtherCost, String someServiceName,
String someCarrier, String someClassOfService, String someReferenceNumber, Boolean someIsActive) {
String someCarrier, String someClassOfService, String someReferenceNumber, Boolean someIsActive, Integer someTransitTimeMin, Integer someTransitTimeMax,
String someTransitTimeUnit, Integer someProcessTime, String someProcessTimeUnit) {
name = someName;
provider = someProvider;
rate = someRate;
Expand All @@ -212,6 +242,11 @@ global class B2CDeliverySample implements sfdc_checkout.CartShippingCharges {
classOfService = someClassOfService;
referenceNumber = someReferenceNumber;
isActive = someIsActive;
transitTimeMin = someTransitTimeMin;
transitTimeMax = someTransitTimeMax;
transitTimeUnit = someTransitTimeUnit;
processTime = someProcessTime;
processTimeUnit = someProcessTimeUnit;
}

public String getProvider() { return provider; }
Expand All @@ -223,10 +258,15 @@ global class B2CDeliverySample implements sfdc_checkout.CartShippingCharges {
public String getClassOfService() { return classOfService; }
public String getReferenceNumber() { return referenceNumber; }
public Boolean isActive() { return isActive; }
public Integer getTransitTimeMin() { return transitTimeMin; }
public Integer getTransitTimeMax() { return transitTimeMax; }
public String getTransitTimeUnit() { return transitTimeUnit; }
public Integer getProcessTime() { return processTime; }
public String getProcessTimeUnit() { return processTimeUnit; }
}

// Create a CartDeliveryGroupMethod record for every shipping option returned from the external service
private void populateCartDeliveryGroupMethodWithShippingOptions(ShippingOptionsAndRatesFromExternalService shippingOption,
private CartDeliveryGroupMethod populateCartDeliveryGroupMethodWithShippingOptions(ShippingOptionsAndRatesFromExternalService shippingOption,
Id cartDeliveryGroupId,
Id webCartId){
// When inserting a new CartDeliveryGroupMethod, the following fields have to be populated:
Expand All @@ -241,19 +281,35 @@ global class B2CDeliverySample implements sfdc_checkout.CartShippingCharges {
// ReferenceNumber: Reference Number from External Service
// IsActive: If this Option is Active

// Below fields are available only for api version >= 61

// TransitTimeMin: Minimum Transit Time
// TransitTimeMax: Maximum Transit Time
// TransitTimeUnit: Time Unit for Transit Time (Valid Values are "Hours", "Days", "Weeks")
// ProcessTime: Process Time
// ProcessTimeUnit: Time Unit for Process Time (Valid Values are "Hours", "Days", "Weeks")

Id productId = getDefaultShippingChargeProduct2Id();
CartDeliveryGroupMethod cartDeliveryGroupMethod = new CartDeliveryGroupMethod(
CartDeliveryGroupId = cartDeliveryGroupId,
ExternalProvider = shippingOption.getProvider(),
Name = shippingOption.getName(),
ShippingFee = shippingOption.getRate(),
WebCartId = webCartId,
Carrier = shippingOption.getCarrier(),
ClassOfService = shippingOption.getClassOfService(),
ProductId = productId,
ReferenceNumber = shippingOption.getReferenceNumber(),
IsActive = shippingOption.isActive()
);

CartDeliveryGroupMethod cartDeliveryGroupMethod = new CartDeliveryGroupMethod();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is CartDeliveryGroupMethodDto, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wei-liu-salesforce We do not use DTO in legacy Apex Integration Samples.

cartDeliveryGroupMethod.put('CartDeliveryGroupId', cartDeliveryGroupId);
cartDeliveryGroupMethod.put('ExternalProvider', shippingOption.getProvider());
cartDeliveryGroupMethod.put('Name', shippingOption.getName());
cartDeliveryGroupMethod.put('ShippingFee', shippingOption.getRate());
cartDeliveryGroupMethod.put('WebCartId', webCartId);
cartDeliveryGroupMethod.put('Carrier', shippingOption.getCarrier());
cartDeliveryGroupMethod.put('ClassOfService', shippingOption.getClassOfService());
cartDeliveryGroupMethod.put('ProductId', productId);
cartDeliveryGroupMethod.put('ReferenceNumber', shippingOption.getReferenceNumber());
cartDeliveryGroupMethod.put('IsActive', shippingOption.isActive());

if(apexClass.ApiVersion >= 61) {
gurpreetsainisalesforce marked this conversation as resolved.
Show resolved Hide resolved
cartDeliveryGroupMethod.put('TransitTimeMin', shippingOption.getTransitTimeMin());
cartDeliveryGroupMethod.put('TransitTimeMax', shippingOption.getTransitTimeMax());
cartDeliveryGroupMethod.put('TransitTimeUnit', shippingOption.getTransitTimeUnit());
cartDeliveryGroupMethod.put('ProcessTime', shippingOption.getProcessTime());
cartDeliveryGroupMethod.put('ProcessTimeUnit', shippingOption.getProcessTimeUnit());
}

Boolean multiCurrencyEnabled = UserInfo.isMultiCurrencyOrganization();
if(multiCurrencyEnabled) {
Expand All @@ -262,7 +318,7 @@ global class B2CDeliverySample implements sfdc_checkout.CartShippingCharges {
String cartDeliveryGroupMethodCurrency = (String) cartDeliveryGroup.get('CurrencyIsoCode');
cartDeliveryGroupMethod.put('CurrencyIsoCode', cartDeliveryGroupMethodCurrency);
}
insert(cartDeliveryGroupMethod);
return cartDeliveryGroupMethod;
}

private sfdc_checkout.IntegrationStatus integrationStatusFailedWithCartValidationOutputError(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>59.0</apiVersion>
<apiVersion>61.0</apiVersion>
<status>Active</status>
</ApexClass>
Loading