Skip to content

Instantly share code, notes, and snippets.

@lefterisnik lefterisnik/test.rb
Last active Sep 10, 2019

Embed
What would you like to do?
def do_sign_up
begin
# HERE, I CREATE THE SUBSCRIPTION
subscription = Integrations::Stripe.create_or_end_trial_on_subscription(
current_user.client,
params[:customer],
params[:plan],
params[:number_licenses].to_i,
params[:country_code],
coupon: session[:coupon].present? ? session[:coupon].id : nil
)
rescue Stripe::CardError => e
if e.json_body.present? and e.json_body[:error].present?
message = e.json_body[:error][:message]
else
message = nil
end
return render(
json: {error: Integrations::Stripe.card_error_customer_message(message, type: "json")},
status: :unprocessable_entity,
)
rescue RuntimeError
return render(
json: {error: Integrations::Stripe.generic_error(type: "json")},
status: :unprocessable_entity,
)
else
# INSIDE THAT FUNCTION I AM USING THE STATUS
# I AM USING THE STRIPE_MOCK_RUBY WHICH CAN CREATE SUBSCRIPTIONS WITH STATUS=`active`. AND I WANT TO STUB THAT
# AND UPDATE THE STATUS TO `incomplete`
Integrations::Stripe.check_sca(subscription, current_user.client, "signup")
end
return render(
json: {
start_polling: true,
},
status: :ok,
)
end
# create_or_end_trial_subscription
def create_or_end_trial_on_subscription(client, customer, plan, number_licenses, country_code, coupon: nil)
subscriptions_for_customer = list_subscriptions(customer.id)
if subscriptions_for_customer.data.size == 0
return create_subscription(customer, plan, number_licenses, country_code, coupon: coupon)
end
new_plans = retrieve_plans(plan, country_code: country_code)
subscription = ::Stripe::Subscription.retrieve(subscriptions_for_customer.first.id)
subscription.off_session = false
subscription.items = [
{
id: subscription.items.data.first.id,
plan: new_plans[:base_plan].id
},
{
id: subscription.items.data.second.id,
plan: new_plans[:plan_extra_targets].id,
quantity: number_licenses
}
]
subscription.metadata = {
retries: subscription.metadata.respond_to?(:retries) ? subscription.metadata.retries.to_i + 1 : 1,
}
# TODO: This needs to be move in a different place. We will work with that when we change also the trial
# activation. It needs to be moved because we don't know if the payment was successful or not. We need to
# end the trial only when the payment is successful. Possible place is the handler of the
# invoice.payment_succeeded
if client.trial?
subscription.trial_end = 'now'
subscription.cancel_at_period_end = false
end
if coupon.present?
subscription.coupon = coupon
end
subscription.save
end
# create_subscription
def create_subscription(customer, plan, number_licenses, country_code, trial_period: nil, coupon: nil)
subscriptions_for_customer = list_subscriptions(customer.id)
if subscriptions_for_customer.data.size > 0
subscriptions_for_customer.data.each do |subscription|
subscription.delete
end
end
if trial_period.present?
trial_end = (Time.current + trial_period.days).to_i
else
trial_end = nil
end
stripe_plans = retrieve_plans(plan, country_code: country_code)
# if we have no country code, we want to charge VAT by default. Better to
# collect tax and then refund it than having to recover tax at a later stage
# If the country is in the EU, we always charge VAT
if not country_code.present? or ISO3166::Country.new(country_code).in_eu?
begin
tax_rates = [::Stripe::TaxRate.retrieve(Rails.configuration.stripe_vat_tax_rate_id)]
rescue ::Stripe::InvalidRequestError, ::Stripe::APIConnectionError => e
Logging::Stripe.error(
<<-message.squish
Stripe customer id #{customer.id}: Retrieving VAT tax rate failed. Cannot add
tax to subscription. (#{e.class}) #{e}.
message
)
tax_rates = nil
end
else
# country code is present and customer is outside of the EU
tax_rates = nil
end
begin
::Stripe::Subscription.create(
customer: customer.id,
items: [
{
plan: stripe_plans[:base_plan].id
},
{
plan: stripe_plans[:plan_extra_targets].id,
quantity: number_licenses,
}
],
default_tax_rates: tax_rates,
trial_end: trial_end,
coupon: coupon
)
rescue ::Stripe::CardError => e
Logging::Stripe.warn(
"Stripe customer id #{customer.id}: Creating subscription failed. #{e}."
)
raise e
rescue ::Stripe::RateLimitError,
::Stripe::InvalidRequestError,
::Stripe::AuthenticationError,
::Stripe::APIConnectionError,
::Stripe::StripeError => e
Logging::Stripe.error(
"Stripe customer id #{customer.id}: Creating subscription failed. (#{e.class}) #{e}."
)
raise RuntimeError
end
end
# Test
def test_do_sign_up__valid_with_customer_sca_required
sign_in(@user)
payment_intent = Stripe::PaymentIntent.create(amount: 3184, currency: "usd", next_action: {type: "use_stripe_sdk"})
invoice = Stripe::Invoice.create(payment_intent: payment_intent)
Stripe::Invoice.stubs(:retrieve).returns(invoice)
Module.stubs(:define_method).with(:status).returns("incomplete")
post do_sign_up_url, params: {
number_licenses: "2",
plan: "essential",
stripe_token_id: @stripe_helper.generate_card_token,
country_code: "US",
}, as: :json
response_body = JSON.parse(response.body)
assert_response :ok
assert response_body["start_polling"]
assert_equal 1, Stripe::Customer.list.data.size
assert_equal 1, Event.all.count
assert_equal "client_sign_up_requires_sca", Event.take.event_type
assert Event.take.requires_processing
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.