Created
March 7, 2018 21:52
-
-
Save iloveitaly/d51934280bbb79e04c96ea349f2d910f to your computer and use it in GitHub Desktop.
Use Stripe Subscriptions to create a payment plan for a single NetSuite Invoice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Michael Bianco <mike@suitesync.io> | |
# Description: Use Stripe Subscriptions to create a payment plan against a single NetSuite Invoice | |
# Link: | |
# 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' | |
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, | |
# This is a (very) poorly documented Stripe feature | |
# 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 | |
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