Last active
May 4, 2017 18:33
-
-
Save iloveitaly/0186a5de9a5a27b5fdb9de5f968efb1e to your computer and use it in GitHub Desktop.
Test various edge cases on the Stripe NetSuite integration (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
# Author: Michael Bianco <mike@suitesync.io> | |
# Description: | |
# Usage: | |
# | |
# export STRIPE_KEY=sk_test | |
# gem install stripe faker | |
# ruby stripe_test_edge_cases_for_netsuite.rb | tee results.log | |
# This script tests the following edge cases: | |
# | |
# - Disputes on a invoice-created charge | |
# - Invoice with prorations | |
# - Partial refund on a invoice | |
# - Invoice closed without payment | |
require 'stripe' | |
require 'faker' | |
Stripe.api_key = ENV['STRIPE_KEY'] | |
Stripe.api_version = "2017-02-14" | |
Stripe.max_network_retries = 10 | |
def netsuite_id_for_resource(stripe_resource, key) | |
loop do | |
stripe_resource.refresh | |
if internal_id = stripe_resource.metadata[key] | |
break internal_id | |
end | |
$stderr.puts "Waiting for #{stripe_resource.id} to translate to NetSuite..." | |
sleep(1) | |
end | |
end | |
def create_customer_with_card(number: 4242424242424242) | |
customer = Stripe::Customer.create({ | |
:description => Faker::Name.name, | |
:email => Faker::Internet.email | |
}) | |
card_token = Stripe::Token.create( | |
{ | |
:card => { | |
:number => number, | |
:exp_month => 8, | |
:exp_year => (Date.today>>24).year, | |
:cvc => "314", | |
} | |
} | |
) | |
customer.sources.create(card: card_token.id) | |
customer | |
end | |
all_plans = Stripe::Plan.list({ limit: 100 }) | |
example_plan = all_plans.data.shuffle.detect { |p| p.trial_period_days.nil? } | |
if !example_plan | |
puts "no plan without a trial period could be found" | |
abort | |
end | |
# == Create Subscription With Refund | |
# https://dashboard.suitesync.io/docs/refunds | |
customer = create_customer_with_card | |
puts "Subscription with refund:" | |
puts "Customer: #{customer.id}" | |
subscription = customer.subscriptions.create(plan: example_plan.id, trial_period_days: 0) | |
puts "Subscription: #{subscription.id}" | |
# refund the invoice | |
invoice = customer.invoices.first | |
while !invoice.charge do | |
puts "Waiting for invoice..." | |
sleep(1) | |
invoice.refresh | |
end | |
puts "Invoice: #{invoice.id}" | |
charge = Stripe::Charge.retrieve(invoice.charge) | |
puts "Charge: #{charge.id}" | |
charge.refund | |
refund = charge.refunds.first | |
netsuite_id_for_resource(refund, 'netsuite_customer_refund_id') | |
puts "Refund: #{refund.id}" | |
puts refund.metadata['netsuite_credit_memo_link'] | |
puts "" | |
# == Create a Dispute | |
# https://stripe.com/docs/testing#how-do-i-test-disputes | |
# https://dashboard.suitesync.io/docs/disputes | |
disputed_customer = create_customer_with_card(number: 4000000000000259) | |
disputed_subscription = disputed_customer.subscriptions.create(plan: example_plan.id, trial_end: 'now') | |
disputed_invoice = disputed_customer.invoices.first | |
puts "Invoice with Dispute" | |
puts "Customer: #{disputed_customer.id}" | |
puts "Disputed Invoice: #{disputed_invoice.id}" | |
charge = Stripe::Charge.retrieve(disputed_invoice.charge) | |
dispute = Stripe::Dispute.retrieve(charge.dispute) | |
netsuite_id_for_resource(dispute, 'netsuite_credit_memo_id') | |
puts dispute.metadata['netsuite_credit_memo_link'] | |
puts "" | |
# == Invoice with partial refund | |
partial_refund_customer = create_customer_with_card | |
subscription = partial_refund_customer.subscriptions.create(plan: example_plan.id, trial_end: 'now') | |
invoice = partial_refund_customer.invoices.first | |
while !invoice.charge do | |
puts "Waiting for invoice..." | |
sleep(1) | |
invoice.refresh | |
end | |
charge = Stripe::Charge.retrieve(invoice.charge) | |
refund = charge.refunds.create(amount: invoice.total / 2) | |
puts "Invoice with partial refund:" | |
puts "Partial Refund Customer: #{partial_refund_customer.id}" | |
puts "Partial Refund Invoice: #{invoice.id}" | |
puts "Partial Refund: #{refund.id}" | |
netsuite_id_for_resource(refund, 'netsuite_customer_refund_id') | |
puts refund.metadata['netsuite_credit_memo_link'] | |
# == Invoice with proration | |
proration_customer = create_customer_with_card | |
subscription = proration_customer.subscriptions.create(plan: example_plan.id, trial_end: 'now') | |
new_plan = all_plans.data.detect { |p| p != example_plan && p.amount > example_plan.amount } | |
if new_plan | |
subscription.plan = new_plan.id | |
subscription.quantity = 2 | |
subscription.trial_end = 'now' | |
subscription.save | |
proration_invoice = Stripe::Invoice.create(customer: proration_customer.id) | |
proration_invoice.pay | |
puts "Proration Customer: #{proration_customer.id}" | |
puts "Proration Invoice: #{proration_invoice.id}" | |
else | |
puts "Cannot find plan larger than #{example_plan.id}" | |
end | |
# == Invoice closed without payment | |
customer = create_customer_with_card | |
Stripe::InvoiceItem.create( | |
customer: customer.id, | |
amount: 100_00, | |
currency: "usd", | |
description: "Other line item" | |
) | |
invoice = Stripe::Invoice.create(customer: customer.id) | |
invoice.closed = true | |
invoice.forgiven = true | |
invoice.save | |
netsuite_id_for_resource(invoice, 'netsuite_invoice_id') | |
puts "Invoice closed without payment:" | |
puts "Invoice: #{invoice.id}" | |
puts invoice.metadata['netsuite_invoice_link'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment