Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Last active October 30, 2017 13:54
Show Gist options
  • Save iloveitaly/754a5bbc184bc7a6c730f9ce6dfc902d to your computer and use it in GitHub Desktop.
Save iloveitaly/754a5bbc184bc7a6c730f9ce6dfc902d to your computer and use it in GitHub Desktop.
Attach a Stripe Charge to a NetSuite SalesOrder as a CustomerDeposit using http://SuiteSync.io/
# Michael Bianco <mike@suitesync.io>
# Description: Example of how to attach a Stripe charge as a CustomerDeposit
# against a SalesOrder in NetSuite
# Usage:
# gem install stripe
# export STRIPE_KEY=sk_test_123
# ruby attach_customer_deposit_to_invoice.rb
require 'stripe'
# Replace this test mode key and run this example on your account
Stripe.api_key = ENV['STRIPE_KEY']
# Associate a card token with a customer. This would be done on your frontend using
# Stripe.js, Checkout, Elements, etc https://stripe.com/docs/custom-form
# Associate the token with the Stripe customer
customer = Stripe::Customer.create(
email: "new-netsuite@example.com",
)
customer.sources.create(card: 'tok_visa')
charge = Stripe::Charge.create(
amount: 200_00,
currency: "usd",
customer: customer.id,
description: "This will be added to the CustomerDeposits's memo in NetSuite.",
metadata: {
# By specifying this key in the charge metadata, you are instructing the NetSuite
# integration to attach a CustomerDeposit against this NetSuite SalesOrder
'netsuite_sales_order_id' => 123
}
)
# Wait for the CustomerDeposit to be created in NetSuite
# NOTE your account needs to be connecting to the integration for this to work
loop do
charge.refresh
if internal_id = charge.metadata['netsuite_customer_deposit_id']
puts "CustomerDeposit Internal ID: #{internal_id}"
exit(0)
end
puts "Waiting for Charge to translate to NetSuite..."
sleep(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment