Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Last active June 26, 2017 02:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iloveitaly/2426d234c7f17306ebe7 to your computer and use it in GitHub Desktop.
Save iloveitaly/2426d234c7f17306ebe7 to your computer and use it in GitHub Desktop.
Example of linking an existing Stripe customer with a NetSuite customer, and creating a new NetSuite customer with custom field data, using http://SuiteSync.io/
# Michael Bianco <mike@suitesync.io>
require 'stripe'
# Replace this test mode key and run this example on your account
Stripe.api_key = 'sk_test_123'
# Create a new Stripe customer, and prevent the creation of a new NetSuite customer
# by linking the Stripe customer with a NetSuite Customer internalID using the Stripe
# Customer's metadata.
existing_customer = Stripe::Customer.create(
:description => "Sample Stripe NetSuite Customer",
:email => "existing-netsuite@example.com",
:metadata => {
'netsuite_customer_id' => 123
}
)
# The above approach can be used to link existing Stripe customers by retrieving
# the existing Stripe customer and specifying the `netsuite_customer_id` metadata key
# before the NetSuite integration is enabled on your account. For example:
# customer = Stripe::Customer.retrieve('cus_123')
# customer.metadata['netsuite_customer_id'] = 123
# customer.save
# Create a new Stripe customer and provide additional NetSuite field data for
# the NetSuite customer to be created
customer = Stripe::Customer.create(
# `description` is used as the name of the NetSuite customer
# https://dashboard.suitesync.io/docs/customers#creating-a-individual-customer
description: "New NetSuite Customer",
# email can be used for automatic duplicate detection
# https://dashboard.suitesync.io/docs/preventing-duplicate-customers#automatically-detecting-duplicate-customers-by-emai
email: "new-netsuite@example.com",
# https://stripe.com/docs/api#create_customer-business_vat_id
business_vat_id: '123123',
shipping: {
name: 'Your Name',
phone: '123 123 1234',
address: {
line1: '3180 18th St',
city: 'San Francisco',
postal_code: '94110',
state: 'CA',
country: 'US',
}
},
metadata: {
netsuite_home_phone: '123-456-7890',
# provide record references by providing the NetSuite internalId
netsuite_sales_rep_id: 123,
# toggle the type of customer that is created in NetSuite
netsuite_company_customer: "true"
}
)
# create a Stripe charge and customize the 'department' NetSuite field on the
# CustomerPayment NetSuite record that will be created
card_token = Stripe::Token.create(
:card => {
:number => '4242424242424242',
:exp_month => 8,
:exp_year => (Date.today>>24).year,
:cvc => "314",
# this is used as the billing address
# https://dashboard.suitesync.io/docs/customers#billing--shipping-address
:address_city => 'San Francisco',
:address_country => 'United States',
:address_line1 => '3180 18th St',
:address_line2 => '',
:address_state => 'CA',
:address_zip => '94110',
}
)
customer.sources.create(card: card_token.id)
charge = Stripe::Charge.create(
:amount => 200_00,
:currency => "usd",
:customer => customer.id,
:description => "This will be added to the CustomerPayment's memo.",
:metadata => {
'netsuite_department_id' => 123
}
)
# retrieve the internalId of the created CustomerPayment
loop do
charge.refresh
if internal_id = charge.metadata['netsuite_customer_payment_id']
puts "CustomerPayment 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