Skip to content

Commit

Permalink
[Coupons] Support custom metadata mapped from Order / Order LIne (#962)
Browse files Browse the repository at this point in the history
  • Loading branch information
nadaismail-stripe authored Dec 16, 2022
1 parent dcaf3a5 commit 78aba0d
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 494 deletions.
6 changes: 2 additions & 4 deletions lib/stripe-force/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ module Constants

# custom coupon objects
QUOTE_SF_STRIPE_COUPON = 'Quote_Stripe_Coupon__c'
QUOTE_SF_STRIPE_COUPON_ASSOCIATION = 'Quote_Stripe_Coupon_Association__c'
QUOTE_LINE_SF_STRIPE_COUPON_ASSOCIATION = 'Quote_Line_Stripe_Coupon_Association__c'
ORDER_SF_STRIPE_COUPON = 'Order_Stripe_Coupon__c'
SF_STRIPE_COUPON_QUOTE_ASSOCIATION = 'Quote_Stripe_Coupon_Association__c'
SF_STRIPE_COUPON_QUOTE_LINE_ASSOCIATION = 'Quote_Line_Stripe_Coupon_Association__c'
SF_STRIPE_COUPON_ORDER_ASSOCIATION = 'Order_Stripe_Coupon_Association__c'
SF_STRIPE_COUPON_ORDER_ITEM_ASSOCIATION = 'Order_Item_Stripe_Coupon_Association__c'

SF_ID = 'Id'
SF_LAST_MODIFIED_DATE = 'LastModifiedDate'
Expand Down
26 changes: 8 additions & 18 deletions lib/stripe-force/translate/coupon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ def create_coupon_from_sf_coupon(order_sf_coupon)
end

# create a new stripe coupon
stripe_coupon = create_stripe_object(Stripe::Coupon, quote_sf_coupon)
stripe_coupon = create_stripe_object(Stripe::Coupon, order_sf_coupon)

# update the quote sf coupon stripe id
update_sf_stripe_id(quote_sf_coupon, stripe_coupon)
# update the order sf coupon stripe id
update_sf_stripe_id(order_sf_coupon, stripe_coupon)
# also update the quote sf coupon stripe id so the same coupon object can be reused in another quote/order
update_sf_stripe_id(quote_sf_coupon, stripe_coupon)

stripe_coupon
end
Expand Down Expand Up @@ -86,30 +86,20 @@ def get_salesforce_stripe_coupons_associated_to_sf_object(sf_client:, sf_object:
# coupons can either be related to an order or order item
source_sf_record_type = sf_object.sobject_type
if source_sf_record_type == SF_ORDER
association_obj_type = SF_STRIPE_COUPON_ORDER_ASSOCIATION
association_field = 'Order__c'
lookup_field = 'Order__c'
elsif source_sf_record_type == SF_ORDER_ITEM
association_obj_type = SF_STRIPE_COUPON_ORDER_ITEM_ASSOCIATION
association_field = 'Order_Item__c'
lookup_field = 'Order_Item__c'
else
# this should never happen since coupons can only be tied to an order or order item
raise Integrations::Errors::ImpossibleState.new("unsupported sf object type for coupons: #{source_sf_record_type}")
end

# check if there are any coupon associations to this order or order item
associations = sf_client.query("Select #{SF_ID} from #{prefixed_stripe_field(association_obj_type)} where #{prefixed_stripe_field(association_field)} = '#{sf_object.Id}'")
if !associations || associations.size == 0
log.info "no stripe coupon associations related to this sf object", salesforce_object: sf_object
return
end
coupon_data = sf_client.query("Select #{SF_ID} from #{prefixed_stripe_field(ORDER_SF_STRIPE_COUPON)} where #{prefixed_stripe_field(lookup_field)} = '#{sf_object.Id}'")

# there could be multiple coupons associated with a single order or order line
coupons = associations.map do |association|
association = sf_client.find(prefixed_stripe_field(association_obj_type), association.Id)

# return the coupon object
serializedCouponId = association[prefixed_stripe_field('Order_Stripe_Coupon__c')]
sf_client.find(prefixed_stripe_field(ORDER_SF_STRIPE_COUPON), serializedCouponId)
coupons = coupon_data.map do |data|
sf_client.find(prefixed_stripe_field(ORDER_SF_STRIPE_COUPON), data.Id)
end

coupons
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ public with sharing class test_updateOrderCouponTrigger {
System.assertEquals('once', quoteCoupon.Duration__c);

// trigger should have created a serialized Stripe Coupon
List<Order_Stripe_Coupon__c> serializedStripeCoupons = getStripeCouponSerialized(quoteCoupon.id);
System.assertEquals(1, serializedStripeCoupons.size());
List<Order_Stripe_Coupon__c> orderStripeCoupons = getStripeCouponSerialized(quoteCoupon.id);
System.assertEquals(1, orderStripeCoupons.size());

Order_Stripe_Coupon__c serializedStripeCoupon = serializedStripeCoupons.get(0);
System.assertEquals('55 percent off coupon', serializedStripeCoupon.Name__c);

// trigger should have created a Stripe Coupon Order Association__c as well
List<Order_Stripe_Coupon_Association__c> stripeCouponOrderAssociations = getstripeCouponOrderAssociations(orders.get(0).id);
System.assertEquals(1, stripeCouponOrderAssociations.size());
Order_Stripe_Coupon__c orderCoupon = orderStripeCoupons.get(0);
System.assertEquals('55 percent off coupon', orderCoupon.Name__c);
System.assertEquals(55, orderCoupon.Percent_Off__c);
System.assertEquals('once', orderCoupon.Duration__c);
System.assertEquals(quoteCoupon.id, orderCoupon.Quote_Stripe_Coupon_Id__c);
System.assertEquals(order.id, orderCoupon.Order__c);
}

static public SBQQ__Quote__c generateQuote() {
Expand Down Expand Up @@ -166,7 +166,7 @@ public with sharing class test_updateOrderCouponTrigger {

static public List<Order_Stripe_Coupon__c> getStripeCouponSerialized(String quoteCouponId) {
return [
SELECT Id, Name__c
SELECT Id, Name__c, Percent_Off__c, Duration__c, Quote_Stripe_Coupon_Id__c, Order__c
FROM Order_Stripe_Coupon__c
WHERE Quote_Stripe_Coupon_Id__c = :quoteCouponId];
}
Expand Down
10 changes: 8 additions & 2 deletions sfdx/force-app/main/default/classes/utilities.cls
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public with sharing class utilities {
return !String.isEmpty(constants.NAMESPACE_API);
}

public static Order_Stripe_Coupon__c cloneStripeCoupon(Quote_Stripe_Coupon__c quoteCoupon)
public static Order_Stripe_Coupon__c cloneStripeCoupon(Quote_Stripe_Coupon__c quoteCoupon, SObject parentRecord)
{
// clone the Stripe Coupon on the quote, it will have a different Id
Order_Stripe_Coupon__c clonedCoupon = new Order_Stripe_Coupon__c();
Expand All @@ -16,7 +16,13 @@ public with sharing class utilities {
clonedCoupon.Name__c = quoteCoupon.Name__c;
clonedCoupon.Percent_Off__c = quoteCoupon.Percent_Off__c;
clonedCoupon.Quote_Stripe_Coupon_Id__c = quoteCoupon.Id;


if (parentRecord.getSObjectType() == Order.getSObjectType()) {
clonedCoupon.Order__c = parentRecord.Id;
} else if (parentRecord.getSObjectType() == OrderItem.getSObjectType()) {
clonedCoupon.Order_Item__c = parentRecord.Id;
}

// insert the cloned Stripe coupon
Database.SaveResult result = Database.insertImmediate((sObject)clonedCoupon);
if (!result.isSuccess()) {
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 78aba0d

Please sign in to comment.