This repository has been archived by the owner on Apr 17, 2020. It is now read-only.
forked from DynamoMTL/spree_chimpy
-
Notifications
You must be signed in to change notification settings - Fork 7
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 DynamoMTL#76 from jkelleyj/feature/mailchimp-3.0
Add support for Mailchimp v3.0 API via Gibbon
- Loading branch information
Showing
17 changed files
with
886 additions
and
246 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
module Spree::Chimpy | ||
module Interface | ||
class CustomerUpserter | ||
delegate :log, :store_api_call, to: Spree::Chimpy | ||
|
||
def initialize(order) | ||
@order = order | ||
end | ||
# CUSTOMER will be pulled first from the MC_EID if present on the order.source | ||
# IF that is not found, customer will be found by our Customer ID | ||
# IF that is not found, customer is created with the order email and our Customer ID | ||
def ensure_customer | ||
# use the one from mail chimp or fall back to the order's email | ||
# happens when this is a new user | ||
customer_id = customer_id_from_eid(@order.source.email_id) if @order.source | ||
customer_id || upsert_customer | ||
end | ||
|
||
def self.mailchimp_customer_id(user_id) | ||
"customer_#{user_id}" | ||
end | ||
|
||
def customer_id_from_eid(mc_eid) | ||
email = Spree::Chimpy.list.email_for_id(mc_eid) | ||
if email | ||
begin | ||
response = store_api_call | ||
.customers | ||
.retrieve(params: { "fields" => "customers.id", "email_address" => email }) | ||
|
||
data = response["customers"].first | ||
data["id"] if data | ||
rescue Gibbon::MailChimpError => e | ||
nil | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def upsert_customer | ||
customer_id = self.class.mailchimp_customer_id(@order.user_id) | ||
begin | ||
response = store_api_call | ||
.customers(customer_id) | ||
.retrieve(params: { "fields" => "id,email_address"}) | ||
rescue Gibbon::MailChimpError => e | ||
# Customer Not Found, so create them | ||
response = store_api_call | ||
.customers | ||
.create(body: { | ||
id: customer_id, | ||
email_address: @order.email.downcase, | ||
opt_in_status: Spree::Chimpy::Config.subscribe_to_list || false | ||
}) | ||
end | ||
customer_id | ||
end | ||
|
||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
module Spree::Chimpy | ||
module Interface | ||
class OrderUpserter | ||
delegate :log, :store_api_call, to: Spree::Chimpy | ||
|
||
def initialize(order) | ||
@order = order | ||
end | ||
|
||
def upsert | ||
Products.ensure_products(@order) | ||
perform_upsert | ||
end | ||
|
||
private | ||
|
||
def perform_upsert | ||
data = order_hash | ||
log "Adding order #{@order.number} for #{data[:customer][:id]} with campaign #{data[:campaign_id]}" | ||
begin | ||
find_and_update_order(data) | ||
rescue Gibbon::MailChimpError => e | ||
log "Order #{@order.number} Not Found, creating order" | ||
create_order(data) | ||
end | ||
end | ||
|
||
def find_and_update_order(data) | ||
# retrieval is checks if the order exists and raises a Gibbon::MailChimpError when not found | ||
response = store_api_call.orders(@order.number).retrieve(params: { "fields" => "id" }) | ||
log "Order #{@order.number} exists, updating data" | ||
store_api_call.orders(@order.number).update(body: data) | ||
end | ||
|
||
def create_order(data) | ||
store_api_call | ||
.orders | ||
.create(body: data) | ||
rescue Gibbon::MailChimpError => e | ||
log "Unable to create order #{@order.number}. [#{e.raw_body}]" | ||
end | ||
|
||
def order_variant_hash(line_item) | ||
variant = line_item.variant | ||
{ | ||
id: "line_item_#{line_item.id}", | ||
product_id: Products.mailchimp_product_id(variant), | ||
product_variant_id: Products.mailchimp_variant_id(variant), | ||
price: variant.price.to_f, | ||
quantity: line_item.quantity | ||
} | ||
end | ||
|
||
def order_hash | ||
customer_id = CustomerUpserter.new(@order).ensure_customer | ||
source = @order.source | ||
|
||
lines = @order.line_items.map do |line| | ||
# MC can only associate the order with a single category: associate the order with the category right below the root level taxon | ||
order_variant_hash(line) | ||
end | ||
|
||
data = { | ||
id: @order.number, | ||
lines: lines, | ||
order_total: @order.total.to_f, | ||
financial_status: @order.payment_state || "", | ||
fulfillment_status: @order.shipment_state || "", | ||
currency_code: @order.currency, | ||
processed_at_foreign: @order.completed_at ? @order.completed_at.to_formatted_s(:db) : "", | ||
updated_at_foreign: @order.updated_at.to_formatted_s(:db), | ||
shipping_total: @order.ship_total.to_f, | ||
tax_total: @order.try(:included_tax_total).to_f + @order.try(:additional_tax_total).to_f, | ||
customer: { | ||
id: customer_id | ||
} | ||
} | ||
|
||
if source | ||
data[:campaign_id] = source.campaign_id | ||
end | ||
|
||
data | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.