Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# Michael Bianco <mike@suitesync.io>
# Description: Example of using SuiteSync's auto-match functionality to apply payments
# https://dashboard.suitesync.io/docs/payment-application
#
# 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'
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
# NOTE these variables would be defined upstream by your app
internal_common_invoice_identifier = "abc123"
customer_email_address = "autoapply+#{Time.now.to_i}@example.com"
# Create Stripe Charge
customer = Stripe::Customer.create({
email: customer_email_address,
# NOTE instead of relying on email as a common identifer, you can specify the ID
# of the customer directly via metadata.
# https://dashboard.suitesync.io/docs/field-customization
# https://dashboard.suitesync.io/docs/preventing-duplicate-customers#automatically-detecting-duplicate-customers-by-emai
# metadata: {
# netsuite_customer_id: 1234
# }
})
customer.sources.create(card: 'tok_visa')
# 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,
metadata: {
netsuite_common_identifier: internal_common_invoice_identifier
}
)
# == Create NetSuite Invoice
# This can be done via a CSV import or directly using SuiteTalk or SuiteScript
# NOTE static NS variables that are defined upstream by your app
ns_item_id = 384208
ns_customer_id = 383315
ns_invoice = NetSuite::Records::Invoice.new(
entity: { internal_id: ns_customer_id },
# Add the common identifier to the NetSuite Invoice; SuiteSync automagically applied the payment to this Invoice
other_ref_num: internal_common_invoice_identifier,
item_list: { item: [
{
item: { internal_id: ns_item_id },
quantity: 2,
rate: 100.0
}
] }
)
ns_invoice.add
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment