Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Last active March 14, 2017 23:54
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/8e3b2bb72e13c61536f953e081e91290 to your computer and use it in GitHub Desktop.
Save iloveitaly/8e3b2bb72e13c61536f953e081e91290 to your computer and use it in GitHub Desktop.
Leverage http://SuiteSync.io/ to apply Stripe payments to NetSuite invoices asynchronously
# Michael Bianco <mike@suitesync.io>
# Description: Example of using SuiteSync's auto-match functionality to apply payments
# to NetSuite invoices.
# Usage:
#
# export STRIPE_KEY=sk_test NETSUITE_EMAIL=user@company.com NETSUITE_PASSWORD=password NETSUITE_ACCOUNT=
# gem install stripe netsuite
# ruby stripe_auto_match_payment_to_invoice.rb
#
require 'stripe'
require 'netsuite'
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
# Static NS references
ns_item_id = 384208
ns_customer_id = 383315
# Create Stripe Charge
customer = Stripe::Customer.create({
description: "Auto-match Payment #{Date.today.to_s}",
# NOTE the Stripe customer must be linked to the same customer as the NetSuite invoice
metadata: {
netsuite_customer_id: ns_customer_id
}
})
card_token = Stripe::Token.create(
:card => {
:number => '4242424242424242',
:exp_month => 8,
:exp_year => (Date.today>>24).year,
:cvc => "314"
}
)
customer.sources.create(card: card_token.id)
# NOTE this charge is brought over to NetSuite immediately as an unapplied CustomerPayment
stripe_charge = Stripe::Charge.create(
currency: 'usd',
amount: 100_00,
customer: customer.id,
# NOTE metadata can be used to pass custom information over to the NetSuite CustomerPayment
# https://dashboard.suitesync.io/docs/field-customization
# metadata: {
# netsuite_custom_payment_information: 'custom ID'
# }
)
# Create NetSuite Invoice
ns_invoice = NetSuite::Records::Invoice.new(
entity: { internal_id: ns_customer_id },
item_list: { item: [
{
item: { internal_id: ns_item_id },
quantity: 1,
rate: 100.0
}
] }
)
ns_invoice.add
# SuiteSync will pull this charge over to NetSuite and automatically match it to the open invoice previously created
loop do
stripe_charge.refresh
if ns_payment_id = stripe_charge.metadata['netsuite_customer_payment_id']
puts "Found CustomerPayment: #{ns_payment_id}"
break
end
puts "Waiting for CustomerPayment..."
sleep(2)
end
loop do
stripe_charge.refresh
if ns_invoice_id = stripe_charge.metadata['netsuite_invoice_id']
puts "Found Invoice: #{ns_invoice_id}"
break
end
puts "Waiting for Invoice application..."
sleep(2)
end
ns_invoice.refresh
# the payment has been created, and then fully applied to the invoice
# the NetSuite Invoice's status is now "Paid In Full"
puts "Invoice Status: #{ns_invoice.status}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment