Last active
April 9, 2018 20:40
-
-
Save joebartels/e54b70a44ba84d6f57d3304a25192b6a to your computer and use it in GitHub Desktop.
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
# setup | |
customer = Stripe::Customer.create(email: '[email protected]') | |
customer.sources.create(source: { object: 'card', exp_month: 12, exp_year: 22, number: '4242424242424242', cvc: 123 }) | |
coupon = Stripe::Coupon.create(id: 'THEDUDEABIDES_2', duration: 'forever', percent_off: 25) | |
plan_month_1 = Stripe::Plan.create(id: 'tmp_monthly_plan_1', name: 'MONTH 1', interval: 'month', amount: 9900, currency: 'usd') | |
plan_month_2 = Stripe::Plan.create(id: 'tmp_monthly_plan_2', name: 'MONTH 2', interval: 'month', amount: 9900, currency: 'usd') | |
plan_year = Stripe::Plan.create(id: 'tmp_annual_plan', name: 'YEAR 1', interval: 'year', amount: 999900, currency: 'usd') | |
# plan_month_1 = Stripe::Plan.retrieve('tmp_monthly_plan_1') | |
# plan_month_2 = Stripe::Plan.retrieve('tmp_monthly_plan_2') | |
# plan_year = Stripe::Plan.retrieve('tmp_annual_plan') | |
subscription = Stripe::Subscription.create(customer: customer.id, coupon: coupon.id, items: [{ plan: plan_month_1.id, quantity: 1 } ]) | |
subscription_year = Stripe::Subscription.create(customer: customer.id, coupon: coupon.id, items: [{ plan: plan_year.id, quantity: 1 } ]) | |
# | |
# TEST 0 (passes) | |
# | |
# confirm a discount exists on subscription | |
# | |
raise 'coupon applied but not on susbcription' if subscription.discount.nil? | |
# | |
# TEST 1 (passes) | |
# | |
# preview an invoice going from month -> month, without trying to clear out the coupon | |
# | |
plans_to_remove = subscription.items.data.map { |item| { id: item.id, deleted: true } } | |
plans_to_add = [{ plan: plan_month_2.id, quantity: 1 }] | |
plan_changes = plans_to_remove + plans_to_add | |
invoice_for_month_to_month_upgrade = Stripe::Invoice.upcoming(customer: customer.id, subscription: subscription.id, subscription_items: plan_changes, subscription_prorate: true) | |
raise 'I expect a coupon but it\'s not there' if invoice_for_month_to_month_upgrade.discount.nil? | |
# | |
# TEST 2 (passes) | |
# | |
# preview an invoice going from month -> month, and clear out the subscription's coupon | |
# | |
invoice_for_month_to_month_upgrade = Stripe::Invoice.upcoming(coupon: '', customer: customer.id, subscription: subscription.id, subscription_items: plan_changes, subscription_prorate: true) | |
raise 'I said no coupon! but there\'s a coupon' unless invoice_for_month_to_month_upgrade.discount.nil? | |
# | |
# TEST 3 (fails) | |
# | |
# now try previewing an invoice going from MONTH -> YEAR | |
plans_to_remove_2 = subscription.items.data.map { |item| { id: item.id, deleted: true } } | |
plans_to_add_2 = [{ plan: plan_year.id, quantity: 1 }] | |
plan_changes_2 = plans_to_remove_2 + plans_to_add_2 | |
invoice_for_month_to_year_upgrade = Stripe::Invoice.upcoming(coupon: '', customer: customer.id, subscription: subscription.id, subscription_items: plan_changes_2, subscription_prorate: true) | |
# drumroll..... | |
# drumroll..... | |
raise '(month->year) I said no coupon! but there\'s a coupon' unless invoice_for_month_to_year_upgrade.discount.nil? | |
# | |
# Going from year to month also fails but I can't tell if it's actually applying the discount to the amount on the newly added monthly plan: | |
# | |
# TEST 4 (fails) | |
# | |
# now try previewing an invoice going from YEAR -> MONTH | |
plans_to_remove_3 = subscription_year.items.data.map { |item| { id: item.id, deleted: true } } | |
plans_to_add_3 = [{ plan: plan_month_1.id, quantity: 1 }] | |
plan_changes_3 = plans_to_remove_3 + plans_to_add_3 | |
invoice_for_year_to_month_upgrade = Stripe::Invoice.upcoming(coupon: '', customer: customer.id, subscription: subscription_year.id, subscription_items: plan_changes_3, subscription_prorate: true) | |
raise '(year->month) I said no coupon! but there\'s a coupon' unless invoice_for_year_to_month_upgrade.discount.nil? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment