Last active
August 25, 2017 15:43
-
-
Save iloveitaly/4354a30945483ba6271d2f1aae1074ff to your computer and use it in GitHub Desktop.
Example of automating an eCommerce NetSuite order workflow using Stripe and http://SuiteSync.io/
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
# Michael Bianco <mike@suitesync.io> | |
# Description: Example of automating an eCommerce NetSuite order workflow using Stripe | |
# Learn more here: https://dashboard.suitesync.io/docs/ecommerce#implementation-guide | |
# Usage: | |
# | |
# export STRIPE_KEY=sk_test NETSUITE_EMAIL=user@company.com NETSUITE_PASSWORD=password NETSUITE_ACCOUNT= | |
# gem install stripe netsuite | |
# ruby netsuite_order_workflow_example.rb | |
require 'stripe' | |
require 'netsuite' | |
# 1. Stripe & NetSuite Authentication | |
Stripe.api_key = ENV['STRIPE_KEY'] | |
NetSuite.configure do | |
reset! | |
# NOTE that API versions > 2015_1 require a more complicated authentication setup | |
api_version '2015_1' | |
read_timeout 60 * 3 | |
silent ENV['NETSUITE_SILENT'].nil? || ENV['NETSUITE_SILENT'] == 'true' | |
email ENV['NETSUITE_EMAIL'] | |
password ENV['NETSUITE_PASSWORD'] | |
account ENV['NETSUITE_ACCOUNT'] | |
soap_header({ | |
'platformMsgs:preferences' => { | |
'platformMsgs:ignoreReadOnlyFields' => true, | |
} | |
}) | |
end | |
# 2. Create Stripe Charge | |
# This is done on your custom web application, or through a third-party application | |
# like Shopify, Recurly, Chargify, WooCommerce, etc. | |
stripe_charge = Stripe::Charge.create( | |
currency: 'usd', | |
amount: 100_00, | |
card: 'tok_visa', | |
# NOTE if an uncaptured charge is passed to the SalesOrder it will be captured | |
# when the SalesOrder is billed. https://dashboard.suitesync.io/docs/auth-fulfill-capture | |
# here's a more advanced example using auth-capture https://gist.github.com/iloveitaly/b9334ff30fa68f9b81c4fd59fc0a5d95 | |
# capture: false | |
) | |
# 3. Create NetSuite Sales Order | |
# NOTE these are IDs that will change dynamically based on the order content | |
ns_customer_id = 383315 | |
ns_item_id = 9 | |
ns_order = NetSuite::Records::SalesOrder.new( | |
entity: { internal_id: ns_customer_id }, | |
item_list: { item: [ | |
{ | |
item: { internal_id: ns_item_id }, | |
quantity: 1.0, | |
rate: 100.00 | |
} | |
] } | |
) | |
# NOTE This is all that needs to be done to integrate with SuiteSync | |
ns_order.custom_field_list.custbody_suitesync_authorization_code = stripe_charge.id | |
NetSuite::Utilities.backoff { ns_order.add } | |
# 4. Bill the order! This creates the invoice. | |
ns_invoice = NetSuite::Utilities.backoff { NetSuite::Records::Invoice.initialize(ns_order) } | |
# NOTE `ship_method = nil` works around a NetSuite bug | |
ns_invoice.ship_method = nil | |
NetSuite::Utilities.backoff { ns_invoice.add } | |
# 5. Ship the order! | |
ns_fulfillment = NetSuite::Utilities.backoff { NetSuite::Records::ItemFulfillment.initialize(ns_order) } | |
NetSuite::Utilities.backoff { ns_fulfillment.add } | |
NetSuite::Utilities.backoff do | |
ns_order.refresh | |
ns_invoice.refresh | |
ns_fulfillment.refresh | |
end | |
# NOTE these IDs are searchable in NetSuite | |
puts "Order Transaction ID: #{ns_order.tran_id}" | |
puts "Invoice Transaction ID: #{ns_invoice.tran_id}" | |
puts "Fulfillment Transaction ID: #{ns_fulfillment.tran_id}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment