Skip to content

Instantly share code, notes, and snippets.

@danveloper
Created February 12, 2013 04:49
Show Gist options
  • Save danveloper/4760316 to your computer and use it in GitHub Desktop.
Save danveloper/4760316 to your computer and use it in GitHub Desktop.
public enum ProductOrderRules implements ProductOrderRulesEngine {
/**
* If no shipping address was explicitly selected, then we'll ship to the billing address of the payment method.
* If the payment method doesn't have a billing address (gift card), then this rule will fail
*/
ShippingAddressRule {
public boolean apply(ProductOrder order) {
// check if the shipping address is null
if (order.shippingAddress == null && order.paymentMethod.getType() == PaymentMethod.PaymentMethodType.CREDIT_CARD) {
// if so, find the first credit card and use its billing address as the shipping address
order.shippingAddress = ((CreditCard)order.paymentMethod).billingAddress;
}
// if there wasn't a credit card, then fail the rule, otherwise, continue
return order.shippingAddress != null ? true : false;
}
},
/**
* A coupon can apply to 1..n product types. Each coupon should discount the applicable product types by the specified discount rate.
* If a coupon has been added to this order and there are no products for which it qualifies, then fail this rule as a last-ditch validation.
*/
CouponsRule {
public boolean apply(ProductOrder order) {
// Exit quickly if we don't have any coupons to process
if (order.coupons == null || order.coupons.size() == 0) {
return true;
}
boolean result = false;
// Loop the coupons that have been applied to the order
for (Coupon coupon : order.coupons) {
// Get the list of product types that this coupon can apply to
for (Product.ProductType productType : coupon.appliesTo) {
// Find the products in the order that this coupon can apply to, and apply the coupon's discount
for (Product product : order.products) {
if (product.type == productType) {
result = true;
product.cost = product.cost.multiply(BigDecimal.valueOf(1.00 - coupon.discountRate));
}
}
}
}
return result;
}
},
/**
* We're in NowheresVille, Utopia where we are not required to charge a tax rate, but California has some fucked up laws, so
* we have to charge them an 8% sales tax if we're shipping to them.
*/
TaxRule {
public boolean apply(ProductOrder order) {
if (order.shippingAddress.state == "CA") {
order.taxRate = 0.08;
}
return true;
}
},
/**
* By default, we'll use Stripe (www.stripe.com) for our payment processing, but if the chosen payment method is a gift card, then we'll use our
* internal processor to "apply" the payment.
*/
PaymentVendorRule {
public boolean apply(ProductOrder order) {
if (order.paymentMethod == null) {
return false;
}
if (order.paymentMethod.getType() == PaymentMethod.PaymentMethodType.GIFT_CARD) {
order.paymentVendor = PaymentVendor.INTERNAL;
}
return true;
}
},
/**
* If this is our customer's first order, then flag this order so that after the order is completed, we will send a nice 1st order email.
*/
FirstOrderEmailRule {
public boolean apply(ProductOrder order) {
if (++order.account.orderCount == 1) {
order.firstOrderThankYou = true;
}
return true;
}
};
public static void callRules(ProductOrder order) {
for (ProductOrderRules rule : ProductOrderRules.values()) {
if (!rule.apply(order)) {
throw new RuntimeException("Failed to execute rule"+rule.name()+".");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment