Skip to content

Instantly share code, notes, and snippets.

@Luiyit
Last active November 20, 2020 19:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Luiyit/307fb76a763bcf4a2b83faca0f6fc01f to your computer and use it in GitHub Desktop.
Save Luiyit/307fb76a763bcf4a2b83faca0f6fc01f to your computer and use it in GitHub Desktop.
# https://talent-hack.atlassian.net/browse/TAL-908
client_email = 'brittanebates@gmail.com'
talent_email = 'hello@emimoves.com'
pass_title = 'Monthly Unlimited Virtual Classes - Reoccurring'
# References involved
client_user = User.find_by(email: client_email)
talent_user = User.find_by(email: talent_email)
pass = Pass.find_by(user_id: talent_user.id, title: pass_title)
if client_user && talent_user && pass
# Subscription pass for client: brittanebates@gmail.com
pass_customer_subscription = client_user.pass_customers
.where(pass_id: pass.id)
.where.not(stripe_subscription_id: nil)
.first
# Stripe accound id of talnet: hello@emimoves.com
stripe_connected_account = TalentToolStatus.where(
user_id: talent_user.id,
name: 'stripe_connect',
status: TalentToolStatus::STRIPE_CONNECT_STATUSES.keys,
).last
if pass_customer_subscription && stripe_connected_account
Stripe.api_key = ENV['stripe_secret_key']
# Update subscription. Trial end will be November 30th, 2020
stripe_subscription = Stripe::Subscription.update(
pass_customer_subscription.stripe_subscription_id,
{
trial_end: DateTime.new(2020,12,1).to_i,
proration_behavior: 'none',
},
{ stripe_account: stripe_connected_account.data['stripe_user_id'] }
)
# success?
pp stripe_subscription
else
p "Subscription doesn't exist"
end
else
p "Client, talent or pass doesn't exist"
end
@brittlewis12
Copy link

do you mind capturing the result of the update call and printing it to console, either #inspect or pretty print (pp <var>)?

@Luiyit
Copy link
Author

Luiyit commented Oct 19, 2020

Script ran on production. The subscription was successfully updated.

{
  "id": "sub_I9vABzPU9CcDNz",
  "object": "subscription",
  "application_fee_percent": null,
  "billing": "charge_automatically",
  "billing_cycle_anchor": 1606780800,
  "billing_thresholds": null,
  "cancel_at": null,
  "cancel_at_period_end": false,
  "canceled_at": null,
  "collection_method": "charge_automatically",
  "created": 1602072036,
  "current_period_end": 1606780800,
  "current_period_start": 1603139866,
  "customer": "cus_I9vAuwVXu3ww0a",
  "days_until_due": null,
  "default_payment_method": null,
  "default_source": null,
  "default_tax_rates": [],
  "discount": null,
  "ended_at": null,
  "invoice_customer_balance_settings": {"consume_applied_balance_on_void":true},
  "items": {"object":"list","data":[{"id":"si_I9vAszvZaSzY76","object":"subscription_item","billing_thresholds":null,"created":1602072037,"metadata":{},"plan":{"id":"price_1HYyCtKCCDG4pPoPOYoH3GA5","object":"plan","active":true,"aggregate_usage":null,"amount":10000,"amount_decimal":"10000","billing_scheme":"per_unit","created":1601921683,"currency":"usd","interval":"month","interval_count":1,"livemode":true,"metadata":{},"nickname":null,"product":"prod_I9GkRGn7QNJEh2","tiers":null,"tiers_mode":null,"transform_usage":null,"trial_period_days":null,"usage_type":"licensed"},"price":{"id":"price_1HYyCtKCCDG4pPoPOYoH3GA5","object":"price","active":true,"billing_scheme":"per_unit","created":1601921683,"currency":"usd","livemode":true,"lookup_key":null,"metadata":{},"nickname":null,"product":"prod_I9GkRGn7QNJEh2","recurring":{"aggregate_usage":null,"interval":"month","interval_count":1,"trial_period_days":null,"usage_type":"licensed"},"tiers_mode":null,"transform_quantity":null,"type":"recurring","unit_amount":10000,"unit_amount_decimal":"10000"},"quantity":1,"subscription":"sub_I9vABzPU9CcDNz","tax_rates":[]}],"has_more":false,"total_count":1,"url":"/v1/subscription_items?subscription=sub_I9vABzPU9CcDNz"},
  "latest_invoice": "in_1He570KCCDG4pPoPhwf5dvTa",
  "livemode": true,
  "metadata": {"product_type":"pass","product_id":"91","user_id":"27631"},
  "next_pending_invoice_item_invoice": null,
  "pause_collection": null,
  "pending_invoice_item_interval": null,
  "pending_setup_intent": null,
  "pending_update": null,
  "plan": {"id":"price_1HYyCtKCCDG4pPoPOYoH3GA5","object":"plan","active":true,"aggregate_usage":null,"amount":10000,"amount_decimal":"10000","billing_scheme":"per_unit","created":1601921683,"currency":"usd","interval":"month","interval_count":1,"livemode":true,"metadata":{},"nickname":null,"product":"prod_I9GkRGn7QNJEh2","tiers":null,"tiers_mode":null,"transform_usage":null,"trial_period_days":null,"usage_type":"licensed"},
  "quantity": 1,
  "schedule": null,
  "start": 1603139866,
  "start_date": 1602072036,
  "status": "trialing",
  "tax_percent": null,
  "transfer_data": null,
  "trial_end": 1606780800,
  "trial_start": 1603139866
}

I also confirmed the trial_end date in the response
t = Time.at(1606780800) => 2020-11-30 19:00:00 -0500

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment