Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# Michael Bianco <mike@suitesync.io>
# Description: Use Stripe Subscriptions to create a payment plan on a single invoice in NetSuite
# Usage:
#
# export STRIPE_KEY=sk_test NETSUITE_EMAIL= NETSUITE_PASSWORD= NETSUITE_ACCOUNT=
# gem install stripe netsuite
# ruby stripe_payment_plan_with_subscriptions.rb
require 'stripe'
require 'netsuite'
Stripe.api_key = ENV['STRIPE_KEY']
NetSuite.configure do
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']
end
# Static Values
ns_item_id = 384208
ns_customer_id = 383315
common_invoice_identifier = "ABC123"
# 1. Create a 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 * 4
}
] }
)
ns_invoice.custom_field_list.custbody_suitesync_authorization_code = common_invoice_identifier
NetSuite::Utilities.backoff do
ns_invoice.add
ns_invoice.refresh
end
puts "NetSuite Transaction ID: #{ns_invoice.tran_id}"
# 2. Create a Customer & Subscription that Automatically Terminates after 4 Months
customer = Stripe::Customer.create(description: "Payment Plan Customer")
customer.sources.create(card: 'tok_visa')
plan = Stripe::Plan.create(
amount: 100_00,
interval: 'month',
currency: 'usd',
name: 'Monthly Payment Plan',
id: "monthly_payment_plan_#{Time.now.to_i}",
)
customer.subscriptions.create({
plan: plan.id,
# `max_occurences` automatically cancels a subscription after a certain number of recurrences
# https://botbot.me/freenode/stripe/2015-11-05/?msg=53502137&page=5
max_occurrences: 4,
metadata: {
# An identifier that also exists on the NetSuite invoice should be added to the subscription
# This identifier can be used on the subsequent invoices created each month and used to match the
# payment to the correct invoice in NetSuite
# https://dashboard.suitesync.io/docs/payment-application#matching-on-common-identifier
netsuite_common_identifier: common_invoice_identifier
}
})
invoice = customer.invoices.first
puts invoice.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment