Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Last active December 23, 2020 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iloveitaly/4d85803709ef9bf18149cfdc9fe9eb9d to your computer and use it in GitHub Desktop.
Save iloveitaly/4d85803709ef9bf18149cfdc9fe9eb9d to your computer and use it in GitHub Desktop.
Example of using Stripe to pay a NetSuite invoice using http://SuiteSync.io
# Michael Bianco <mike@suitesync.io>
# Link: https://gist.github.com/iloveitaly/4d85803709ef9bf18149cfdc9fe9eb9d
# Description: Use Stripe to pay a NetSuite Invoice by specifying an invoice ID
require 'stripe'
# Replace this test mode key and run this example on your account
Stripe.api_key = ENV['STRIPE_KEY']
# Create a card token to be associated with a customer. This is normally done
# on your payment frontend using Stripe.js https://stripe.com/docs/custom-form
card_token = Stripe::Token.create(
:card => {
:number => '4242424242424242',
:exp_month => 8,
:exp_year => (Date.today>>24).year,
:cvc => "314"
}
)
# Associate the token with the Stripe customer
customer = Stripe::Customer.create(
:description => "New NetSuite Customer",
:email => "new-netsuite@example.com",
)
customer.sources.create(card: card_token.id)
charge = Stripe::Charge.create(
:amount => 200_00,
:currency => "usd",
:customer => customer.id,
:description => "This is added to the NetSuite customer payment's memo",
:metadata => {
# By specifying this key in the charge metadata, you are instructing the NetSuite
# integration to apply the CustomerPayment against this NetSuite invoice ID
'netsuite_invoice_id' => 123
}
)
# Wait for the CustomerPayment to be created in NetSuite
# NOTE your account needs to be connected to SuiteSync for this to work
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