Last active
October 25, 2017 19:29
-
-
Save iloveitaly/1222a3d2341b837d80533729802fa53b to your computer and use it in GitHub Desktop.
Example of creating a order in Stripe to generate either a NetSuite SalesOrder or Invoice using http://SuiteSync.io/
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: Example of creating a order in Stripe to generate either a NetSuite SalesOrder or Invoice. | |
# Usage: | |
# | |
# export STRIPE_KEY=sk_test | |
# gem install stripe | |
# ruby create_netsuite_order_or_invoice_with_stripe.rb | |
require 'stripe' | |
require 'date' | |
Stripe.api_key = ENV['STRIPE_KEY'] | |
Stripe.api_version = '2017-08-15' | |
# NOTE normally you will create products manually in the dashboard | |
# this product and SKU combo is created for demo purposes only | |
# https://dashboard.suitesync.io/docs/orders | |
def create_sku | |
sku = Stripe::SKU.list.data.sample | |
return sku if !sku.nil? | |
randomization_token = Time.now.to_i | |
product = Stripe::Product.list.data.sample | |
if product.nil? | |
product = Stripe::Product.create( | |
name: 'Your Product', | |
description: 'A description', | |
id: "sample_product_#{randomization_token}", | |
# `shippable = true` creates a InventoryItem instead of a NonInventoryItem | |
shippable: false | |
) | |
end | |
Stripe::SKU.create( | |
id: "sample_sku_#{randomization_token}", | |
product: product.id, | |
price: 15_00, | |
currency: 'usd', | |
inventory: { | |
# use infinite for digital products | |
'type' => 'infinite' | |
} | |
) | |
end | |
# 1. Create Customer & Order with Shipping Address & VAT ID | |
vat_id = '123456789' | |
customer = Stripe::Customer.create( | |
description: "New Customer", | |
email: "new-customer@example.com", | |
business_vat_id: vat_id, | |
shipping: { | |
name: 'Your Name', | |
phone: '123 123 1234', | |
address: { | |
line1: '3180 18th St', | |
city: 'San Francisco', | |
postal_code: '94110', | |
state: 'CA', | |
country: 'US', | |
} | |
} | |
) | |
sku = create_sku | |
order = Stripe::Order.create( | |
currency: 'usd', | |
items: [ | |
{ | |
type: 'sku', | |
parent: sku.id | |
} | |
], | |
customer: customer.id | |
) | |
# use `amount` on these objects to display subtotal to the customer | |
items = order.items.select { |i| i.type == 'sku' } | |
taxes = order.items.detect { |i| i.type == 'tax' } | |
shipping = order.items.detect { |i| i.type == 'shipping' } | |
# 2. Present the finalized order information to the customer and collect payment. | |
# Payment can be collected using Stripe checkout: | |
# https://gist.github.com/iloveitaly/b2867f58fd50a07ea4cd145788b363b2 | |
card_token = Stripe::Token.create({ | |
card: { | |
number: 4242424242424242, | |
exp_month: 8, | |
exp_year: (Date.today>>24).year, | |
cvc: "314", | |
# used as the NetSuite billing address | |
# https://dashboard.suitesync.io/docs/customers#billing--shipping-address | |
address_city: 'San Francisco', | |
address_country: 'United States', | |
address_line1: '3180 18th St', | |
address_line2: '', | |
address_state: 'CA', | |
address_zip: '94110', | |
} | |
}) | |
customer.sources.create(source: card_token.id) | |
order.pay(customer: customer) | |
puts "Order created!" | |
puts order.id | |
puts order.charge |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment