Current period end value saving as 0 instead of actual value - ruby-on-rails

I am trying to save the Stripe's user current_period_end to the database. When I create a new subscription the value saves as 0 instead of the actual date their subscription ends.
Is there something I'm missing?
It should be saving as:
"current_period_end": 1441292360
Subscription.rb:
def save_with_stripe_payment
customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
self.stripe_customer_token = customer.id
self.cancellation_date = customer.subscriptions.first.current_period_end
save!
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
errors.add :base, "There was a problem with your credit card."
false
end

It's possible that this could be an issue with the type of data you're receiving and how you're storing it in your database. Based on the information you have given I would expect you have the wrong field type.
Your code is correct so I would double check to see if your field is a integer.

When Strip creates a new customer it returns the object posted at the bottom of this question.
To access the current_period_end you would need to do customer.subscriptions.data.first["current_period_end"]
{
"object": "customer",
"created": 1409190039,
"id": "cus_4fdAW5ftNQow1a",
"livemode": false,
"description": "new_paying_customer",
"email": null,
"delinquent": false,
"metadata": {},
"subscriptions": {
"object": "list",
"total_count": 1,
"has_more": false,
"url": "/v1/customers/cus_4fdAW5ftNQow1a/subscriptions",
"data": [
{
"id": "sub_4fdAS9IlSOFfiv",
"plan": {
"interval": "month",
"name": "Basic Plan",
"created": 1409178429,
"amount": 1200,
"currency": "usd",
"id": "basic",
"object": "plan",
"livemode": false,
"interval_count": 1,
"trial_period_days": null,
"metadata": {},
"statement_description": null
},
"object": "subscription",
"start": 1409190039,
"status": "active",
"customer": "cus_4fdAW5ftNQow1a",
"cancel_at_period_end": false,
"current_period_start": 1409190039,
"current_period_end": 1411868439,
"ended_at": null,
"trial_start": null,
"trial_end": null,
"canceled_at": null,
"quantity": 1,
"application_fee_percent": null,
"discount": null,
"metadata": {}
}
]
},
"discount": null,
"account_balance": 0,
"currency": "usd",
"cards": {
"object": "list",
"total_count": 1,
"has_more": false,
"url": "/v1/customers/cus_4fdAW5ftNQow1a/cards",
"data": [
{
"id": "card_14WHtz4rPA98z9GRTW1QMenU",
"object": "card",
"last4": "4242",
"brand": "Visa",
"funding": "unknown",
"exp_month": 1,
"exp_year": 2015,
"fingerprint": "0qYXzA0d9EZtsgQ6",
"customer": "cus_4fdAW5ftNQow1a"
}
]
},
"default_card": "card_14WHtz4rPA98z9GRTW1QMenU"
}

This is just a shot in the dark (because I don't know the format of your cancellation_date field or the database you are using), but are you sure you are handling the returned timestamp correctly?
If cancellation_date is a DateTime field, I believe you have to do something like:
cpe = customer.subscriptions.first.current_period_end
self.cancellation_date = Time.at(cpe).to_datetime
If that isn't the problem then I'd use the console and/or the server output to make sure current_period_end is returning what you expect it to when you call Stripe::Customer.create (like others have suggested). You really need to determine when the "switch to 0" is happening, so I'd check (in this order):
The response from Stripe when you create the customer
The value of customer.subscriptions.first.current_period_end
The value of self.cancellation_date after assignment but before saving
There may also be validations or before_save filters that are messing with this field before hitting the database.
That's all my ideas for now :)
Hope this helps!

Related

Stripe retrieve the good price ID

I setup my shop with Stripe. You can buy it with the good product and with different sizes. It works well until I have to sent an email to the seller with some informations like the name of the product bought, the size of the product (product.nickname), the customer information and so on. My problem is I can't get this 'nickname'.
As I understand in Stripe, you have some Products, which can have different Prices. For me, I have a product with 3 prices. Each price has his own ID and a 'label' (according to Stripe called 'nickname' as I saw in the request). The problem came when I want to retrieve this ID to get the specific nickname (the name if the size). For some reason, the ID returned for the selected product is not the same as the ID from Stripe Dashboard. And each time I got a new purchase, this returned ID change.
In my WebhooksController in def create:
case event.type
when 'checkout.session.completed'
session = event.data.object
#order = Stripe::Checkout::Session.retrieve({ id: session.id, expand: ["line_items", "customer"]})
OrderMailer.order_mail(#order).deliver
end
render json: { message: 'success' }
In my order_mail.html.erb:
<% #order.line_items.data.each do |line| %>
<%= line %>
<% end %>
Can someone explain me why the line_item ID returned in the webhook is not the same as the Stripe dashboard ID? And how can I get and display the nickname of the price selected in my mail?
here is what I got for the <%= line %>
{ "id": "li_1K41JPFE4oPSz5YO98K2r2CW", "object": "item", "amount_subtotal": 4000, "amount_total": 4000, "currency": "eur", "description": "Soleil Levant", "price": { "id": "price_1K41JPFE4oPSz5YORWdXXGuT", "object": "price", "active": false, "billing_scheme": "per_unit", "created": 1638874459, "currency": "eur", "livemode": false, "lookup_key": null, "metadata": { }, "nickname": null, "product": "prod_KjUHP8uALE6aZI", "recurring": null, "tax_behavior": "unspecified", "tiers_mode": null, "transform_quantity": null, "type": "one_time", "unit_amount": 4000, "unit_amount_decimal": "4000" }, "quantity": 1 }
If I cUrl the right price:
https://api.stripe.com/v1/prices/price_1JxC8kFE4oPSz5YOc391IZ9I\
-u sk_test_51JbmGvFE4oPSz5YOsl0JTTEi0quapEwPBXQsQRbRXPdr38hSiyVDPs9LnkH4nHCGXqwoYjOTnHoH6FtIC4EP32pr00KHIxFlLp:
I got this:
{
"id": "price_1JxC8kFE4oPSz5YOc391IZ9I",
"object": "price",
"active": true,
"billing_scheme": "per_unit",
"created": 1637247786,
"currency": "eur",
"livemode": false,
"lookup_key": null,
"metadata": {
"nickname": "S"
},
"nickname": "S",
"product": "prod_KcR0HgpfzldOj8",
"recurring": null,
"tax_behavior": "unspecified",
"tiers_mode": null,
"transform_quantity": null,
"type": "one_time",
"unit_amount": 4000,
"unit_amount_decimal": "4000"
}
What you are doing should work:
First, create a Price with nickname: "foobar", let’s say its ID is "price_abc".
Then, create a Checkout Session where you set line_items[0].price: "price_abc".
Later, you retrieve the Checkout Session with expand: ["line_items"]. If you look into checkout.line_items.data[].price, you will find an object that contains what you are looking for: { id: "price_abc", nickname: "foobar", … }. Note that you can see the structure of the line_items.data object in the Stripe documentation here.
So you need to double check that you are using the correct price ID when creating the Checkout Session, and that you are looking at checkout.line_items.data[].price to find the id and nickname of the price.

Stripe::Customer.create not attaching the payment method to customer

I am creating a setup intent and receiving the payment method's id from it. Then I am creating a customer using the following code
customer = Stripe::Customer.create({
email: current_user.email,
description: "Customer for subscription",
payment_method: params[:payment_method]
})
It is returning the following response
{
"id": "cus_IZmhg4VhIwFUBI",
"object": "customer",
"address": null,
"balance": 0,
"created": 1608037176,
"currency": null,
"default_source": null,
"delinquent": false,
"description": "Customer for subscription",
"discount": null,
"email": "customer#example.com",
"invoice_prefix": "76BF0C5E",
"invoice_settings": {
"custom_fields": null,
"default_payment_method": null,
"footer": null
},
"livemode": false,
"metadata": {
},
"name": null,
"next_invoice_sequence": 1,
"phone": null,
"preferred_locales": [
],
"shipping": null,
"tax_exempt": "none"
}
The problem is, it is not attaching the payment method to the customer, when I try to create a subscription using the customer object, it returns the following error:
Stripe::InvalidRequestError (This customer has no attached payment source or default payment method.):
But when I look in the dashboard, the payment method is already attached with the customer
The payment_method is attached, and you can list them for the customer. Then, you need to set explicitly the invoice_settings.default_payment_method for the Customer, which is what will be used for the subscription creation.
Inspired by the #Nolan's answer, I have fixed the issue by passing payment id to invoice_settings.default_payment_method while customer creation. Following is the code snippet:
customer = Stripe::Customer.create({
email: current_user.email,
description: "Customer for subscription",
payment_method: params[:payment_method],
invoice_settings: {
default_payment_method: params[:payment_method]
}
})

Stripe Webhook firing but Stripe event method not running

Using the stripe events gem I am trying to update a Stripe subscription on the event unsubscribe. My events are not firing even though they are receiving the webhook.
I am running my endpoint on ngrok and using the stripe-events gem. I have installed the stripe cli so I can see that the webhook events are firing.
My thinking is that there is a problem with the code I am trying to use
#user = User.find_by_stripe_id(event.data.object.customer) however I got this code from a thread on GoRails. Chris Oliver said that it looked correct and I have repurposed it for my app so that makes me think it might be something else.
# config/initializers/stripe_events.rb
Stripe.api_key = Rails.application.credentials.stripe_publishable_key
Stripe.api_key = Rails.application.credentials.stripe_signing_secret
StripeEvent.configure do |events|
events.subscribe "customer.subscription.updated" do |event|
#user = User.find_by_stripe_id(event.data.object.customer)
logger.info "Processing the request..."
if DateTime.now >= #user.subscription_end_date
logger.info "if statement is running..."
#user.update_attribute(subscribed: false)
#user.save!
end
end
end
I am not sure if this is needed but here is the method that triggers the webhook. I have added the DateTime.now-1 for testing if the webhook so that it simulates an expired subscription.
# app/controllers/subscriptions_controller.rb
def destroy
if Rails.env.production?
Stripe.api_key = Rails.application.credentials.stripe_live_api
else
Stripe.api_key = Rails.application.credentials.stripe_api_key
end
customer = Stripe::Customer.retrieve(current_user.stripe_id)
customer.subscriptions.retrieve(current_user.stripe_subscription_id)
Stripe::Subscription.update(current_user.stripe_subscription_id,:cancel_at_period_end => true)
##current_user.update(subscription_end_date: Time.at(customer.subscriptions[:data].first[:current_period_end]))
current_user.update(subscription_end_date: DateTime.now-1)
redirect_to root_path, notice: "Your subscription has been cancelled"
end
I tested the response from the stripe webhook
{
"object": {
"id": "sub_FqLLZZ4pKhlM0y",
"object": "subscription",
"application_fee_percent": null,
"billing": "charge_automatically",
"billing_cycle_anchor": 1568957293,
"billing_thresholds": null,
"cancel_at": 1571549293,
"cancel_at_period_end": true,
"canceled_at": 1568957302,
"collection_method": "charge_automatically",
"created": 1568957293,
"current_period_end": 1571549293,
"current_period_start": 1568957293,
"customer": "cus_FqLLXYHyVl7mJq",
"days_until_due": null,
"default_payment_method": null,
"default_source": null,
"default_tax_rates": [
],
"discount": null,
"ended_at": null,
"items": {
"object": "list",
"data": [
{
"id": "si_FqLLngQNhxl5p4",
"object": "subscription_item",
"billing_thresholds": null,
"created": 1568957293,
"metadata": {
},
"plan": {
"id": "plan_FYHrCVDopLPS37",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 2900,
"amount_decimal": "2900",
"billing_scheme": "per_unit",
"created": 1564792787,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {
},
"nickname": "Uproar Basic",
"product": "prod_FYHpmwiftJrqpC",
"tiers": null,
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"quantity": 1,
"subscription": "sub_FqLLZZ4pKhlM0y",
"tax_rates": [
]
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/subscription_items?subscription=sub_FqLLZZ4pKhlM0y"
},
"latest_invoice": "in_1FKefBJCkfo4YGOMvdEjZ3ny",
"livemode": false,
"metadata": {
},
"pending_setup_intent": null,
"plan": {
"id": "plan_FYHrCVDopLPS37",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 2900,
"amount_decimal": "2900",
"billing_scheme": "per_unit",
"created": 1564792787,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {
},
"nickname": "Uproar Basic",
"product": "prod_FYHpmwiftJrqpC",
"tiers": null,
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"quantity": 1,
"schedule": null,
"start": 1568957302,
"start_date": 1568957293,
"status": "active",
"tax_percent": null,
"trial_end": null,
"trial_start": null
},
"previous_attributes": {
"cancel_at": null,
"cancel_at_period_end": false,
"canceled_at": null,
"start": 1568957293
}
}
I'm not sure code in config/initializers/stripe_events.rb can have access to app/controllers once your app is launched.
You should call a service :
config/initializers/stripe_events.rb
StripeEvent.configure do |events|
events.subscribe "customer.subscription.updated" do |event|
StripeUdpateSubscription.new
end
end
app/services/stripe_update_subscription.rb
class StripeUpdateSubscription
def call(event)
..// you code here
end
end

Parse API output in ruby [duplicate]

This question already has answers here:
Parsing a JSON string in Ruby
(8 answers)
Closed 4 years ago.
Apologies if it is very basic one , completely new to ruby.
Below is the sample response I am getting while using the curl , need to get the values of body , created_at from the below output .
When I tried to check type of the value , puts returns true for (string) and false for hash and array
`#puts value.is_a?(Hash)
#puts value.is_a?(Array)
#puts value.is_a?(String)`
Not sure how to get value from the below output , Please help on with the first step/idea need to do here , will try on and revert back on getting any issues further
SAMPLE CALL
curl https://api.statuspage.io/v1/pages/qfn30z5r6s5h/incidents.json \
-H "Authorization: OAuth 2a7b9d4aac30956d537ac76850f4d78de30994703680056cc103862d53cf8074"
SAMPLE RESPONSE
[
{
"created_at": "2013-04-21T11:45:33-06:00",
"id": "tks5n8x7w24h",
"impact": "none",
"impact_override": null,
"incident_updates": [
{
"body": "We will be performing a data layer migration from our existing Postgres system over to our new, multi-region, distributed Riak cluster. The application will be taken offline during the entirety of this migration. We apologize in advance for the inconvenience",
"created_at": "2013-04-21T11:45:33-06:00",
"display_at": "2013-04-21T11:45:33-06:00",
"id": "kb4fpktpqm0l",
"incident_id": "tks5n8x7w24h",
"status": "scheduled",
"twitter_updated_at": null,
"updated_at": "2013-04-21T11:45:33-06:00",
"wants_twitter_update": false,
"affected_components": [
{
"code": "ftgks51sfs2d",
"name": "API",
"old_status": "operational",
"new_status": "operational"
}
]
}
],
"metadata": [
"jira": {
"issue_id": "value"
}
],
"monitoring_at": null,
"name": "Data Layer Migration",
"page_id": "jcm87b8scw0b",
"postmortem_body": null,
"postmortem_body_last_updated_at": null,
"postmortem_ignored": true,
"postmortem_notified_subscribers": false,
"postmortem_notified_twitter": false,
"postmortem_published_at": null,
"resolved_at": null,
"scheduled_auto_in_progress": false,
"scheduled_auto_completed": false,
"scheduled_for": "2013-05-04T01:00:00-06:00",
"scheduled_remind_prior": false,
"scheduled_reminded_at": null,
"scheduled_until": "2013-05-04T03:00:00-06:00",
"shortlink": "",
"status": "scheduled",
"updated_at": "2013-04-21T11:45:33-06:00"
},
{
"created_at": "2013-04-21T11:04:28-06:00",
"id": "cz46ym8qbvwv",
"impact": "critical",
"impact_override": null,
"incident_updates": [
{
"body": "A postmortem analysis has been posted for this incident.",
"created_at": "2013-04-21T11:42:31-06:00",
"display_at": "2013-04-21T11:42:31-06:00",
"id": "dn051mnj579k",
"incident_id": "cz46ym8qbvwv",
"status": "postmortem",
"twitter_updated_at": null,
"updated_at": "2013-04-21T11:42:31-06:00",
"wants_twitter_update": false
},
{
"body": "The application has returned to it's normal performance profile. We will be following up with a postmortem about future plans to guard against additional master database failure.",
"created_at": "2013-04-21T11:16:38-06:00",
"display_at": "2013-04-21T14:07:00-06:00",
"id": "ppdqv1grhm64",
"incident_id": "cz46ym8qbvwv",
"status": "resolved",
"twitter_updated_at": null,
"updated_at": "2013-04-21T11:36:15-06:00",
"wants_twitter_update": false,
"affected_components": [
{
"code": "ftgks51sfs2d",
"name": "API",
"old_status": "degraded_performance",
"new_status": "operational"
}
]
},
{
"body": "The slave database has been successfully promoted, but is running slow due to a cold query cache. The application is open and available for requests, but should will be performing in a degraded state for the next few hours. We will continue to monitor the situation.",
"created_at": "2013-04-21T11:14:46-06:00",
"display_at": "2013-04-21T11:14:46-06:00",
"id": "j7ql87ktwnys",
"incident_id": "cz46ym8qbvwv",
"status": "monitoring",
"twitter_updated_at": null,
"updated_at": "2013-04-21T11:14:46-06:00",
"wants_twitter_update": false,
"affected_components": [
{
"code": "ftgks51sfs2d",
"name": "API",
"old_status": "major_outage",
"new_status": "degraded_performance"
}
]
},
{
"body": "The slave database is 60% through it's recovery process. We will provide another update once the application is back up.",
"created_at": "2013-04-21T11:08:42-06:00",
"display_at": "2013-04-21T11:08:42-06:00",
"id": "xzgd3y9zdzt9",
"incident_id": "cz46ym8qbvwv",
"status": "identified",
"twitter_updated_at": null,
"updated_at": "2013-04-21T11:08:42-06:00",
"wants_twitter_update": false,
"affected_components": [
{
"code": "ftgks51sfs2d",
"name": "API",
"old_status": "major_outage",
"new_status": "major_outage"
}
]
},
{
"body": "The master database server could not boot due to a corrupted EBS volume. We are in the process of failing over to the slave database. ETA for the application recovering is 5 minutes.",
"created_at": "2013-04-21T11:06:27-06:00",
"display_at": "2013-04-21T11:06:27-06:00",
"id": "9307nsfg3dxd",
"incident_id": "cz46ym8qbvwv",
"status": "identified",
"twitter_updated_at": null,
"updated_at": "2013-04-21T11:06:27-06:00",
"wants_twitter_update": false,
"affected_components": [
{
"code": "ftgks51sfs2d",
"name": "API",
"old_status": "major_outage",
"new_status": "major_outage"
}
]
},
{
"body": "We're investigating an outage with our master database server.",
"created_at": "2013-04-21T11:04:28-06:00",
"display_at": "2013-04-21T11:04:28-06:00",
"id": "dz959yz2nd4l",
"incident_id": "cz46ym8qbvwv",
"status": "investigating",
"twitter_updated_at": null,
"updated_at": "2013-04-21T11:04:29-06:00",
"wants_twitter_update": false,
"affected_components": [
{
"code": "ftgks51sfs2d",
"name": "API",
"old_status": "operational",
"new_status": "major_outage"
}
]
}
],
"metadata": [
"jira": {
"issue_id": "value"
}
],
"monitoring_at": "2013-04-21T11:14:46-06:00",
"name": "Master Database Failure",
"page_id": "jcm87b8scw0b",
"postmortem_body": "##### Issue\r\n\r\nAt approximately 17:02 UTC on 2013-04-21, our master database server unexpectedly went unresponsive to all network traffic. A reboot of the machine at 17:05 UTC resulted in a failed mount of a corrupted EBS volume, and we made the decision at that time to fail over the slave database.\r\n\r\n##### Resolution\r\n\r\nAt 17:12 UTC, the slave database had been successfully promoted to master and the application recovered enough to accept web traffic again. A new slave database node was created and placed into the rotation to guard against future master failures. The promoted slave database performed slowly for the next couple of hours as the query cache began to warm up, and eventually settled into a reasonable performance profile around 20:00 UTC.\r\n\r\n##### Future Mitigation Plans\r\n\r\nOver the past few months, we've been working on an overhaul to our data storage layer with a migration from a Postgres setup to a distributed, fault-tolerant, multi-region data layer using Riak. This initiative has been prioritized, and the migration will be performed in the coming weeks. We will notify our clients of the scheduled downtime via an incident on this status site, and via a blog post.",
"postmortem_body_last_updated_at": "2013-04-21T17:41:00Z",
"postmortem_ignored": false,
"postmortem_notified_subscribers": false,
"postmortem_notified_twitter": false,
"postmortem_published_at": "2013-04-21T17:42:31Z",
"resolved_at": "2013-04-21T14:07:00-06:00",
"scheduled_auto_in_progress": false,
"scheduled_auto_completed": false,
"scheduled_for": null,
"scheduled_remind_prior": false,
"scheduled_reminded_at": null,
"scheduled_until": null,
"shortlink": "",
"status": "postmortem",
"updated_at": "2013-04-21T11:42:31-06:00"
},
{
"created_at": "2013-04-01T12:00:00-06:00",
"id": "2ggpd60zvx3c",
"impact": "none",
"impact_override": null,
"incident_updates": [
{
"body": "At approximately 6:55 PM, our network provider at ServerCo experienced a brief network outage at their New Jersey data center. The network outage lasted approximately 14 minutes, and all web requests during that time were not received. No data was lost, and the system recovered once the network outage at ServerCo was repaired.",
"created_at": "2013-04-21T11:02:00-06:00",
"display_at": "2013-04-21T11:02:00-06:00",
"id": "mkfzp9swbk4z",
"incident_id": "2ggpd60zvx3c",
"status": "investigating",
"twitter_updated_at": null,
"updated_at": "2013-04-21T11:02:00-06:00",
"wants_twitter_update": false
}
],
"metadata": [
"jira": {
"issue_id": "value"
}
],
"monitoring_at": null,
"name": "Brief Network Outage",
"page_id": "jcm87b8scw0b",
"postmortem_body": null,
"postmortem_body_last_updated_at": null,
"postmortem_ignored": false,
"postmortem_notified_subscribers": false,
"postmortem_notified_twitter": false,
"postmortem_published_at": null,
"resolved_at": null,
"scheduled_auto_in_progress": false,
"scheduled_auto_completed": false,
"scheduled_for": null,
"scheduled_remind_prior": false,
"scheduled_reminded_at": null,
"scheduled_until": null,
"shortlink": "",
"status": "resolved",
"updated_at": "2013-04-01T12:00:00-06:00"
}
]
It's JSON. Since you're using Rails, it will be sufficient to call
JSON.parse(value)
This will return an array of multiple hashes which you will be able to further map.

json data stripe webhook

Im trying to implement stripe webhooks in my rails app. The webhook send this json data.
{ "created": 1326853478, "livemode": false, "id": "evt_00000000000000", "type": "invoice.payment_succeeded", "object": "event", "request": null, "pending_webhooks": 1, "api_version": "2017-06-05", "data": {
"object": {
"id": "in_00000000000000",
"object": "invoice",
"amount_due": 500,
"application_fee": null,
"attempt_count": 1,
"attempted": true,
"charge": "_00000000000000",
"closed": true,
"currency": "usd",
"customer": "cus_00000000000000",
"date": 1501950408,
"description": null,
"discount": null,
"ending_balance": 0,
"forgiven": false,
"lines": {
"data": [
{
"id": "sub_BLXcTSekdHflq5",
"object": "line_item",
"amount": 500,
"currency": "usd",
"description": null,
"discountable": true,
"livemode": true,
"metadata": {
},
"period": {
"start": 1507246341,
"end": 1509924741
},
"plan": {
"id": "Subscriber",
"object": "plan",
"amount": 500,
"created": 1501901993,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {
},
"name": "Subscriber",
"statement_descriptor": "WatchBuddie Stream sub",
"trial_period_days": null
},
"proration": false,
"quantity": 1,
"subscription": null,
"subscription_item": "si_1AyqWnFr5iCt1Tv7n23zDLOM",
"type": "subscription"
}
],
"total_count": 1,
"object": "list",
"url": "/v1/invoices/in_1AnV6yFr5iCt1Tv7PnqZ0EUA/lines"
},
"livemode": false,
"metadata": {
},
"next_payment_attempt": null,
"paid": true,
"period_end": 1501950408,
"period_start": 1501950408,
"receipt_number": null,
"starting_balance": 0,
"statement_descriptor": null,
"subscription": "sub_00000000000000",
"subtotal": 500,
"tax": null,
"tax_percent": null,
"total": 500,
"webhooks_delivered_at": 1501950409
} } }
webhook method
def webhooks
begin
event_json = JSON.parse(request.body.read)
event_object = event_json['data']['object']
#refer event types here https://stripe.com/docs/api#event_types
case event_json['type']
when 'invoice.payment_succeeded'
#Update the total subscription total
#Send in email to the user telling them that they resubbed
logger.debug event_object['lines']['id']
end
My question is how do I get the "id": "sub_BLXcTSekdHflq5" I tried
logger.debug event_object['lines']['id']
It doesn't seem to be working what am I doing wrong? I'm sure its just a syntax thing that I am not understanding.
Thanks for all the help!
Figured it out the lines data was in a hash so you had to iterate over it. This is what I did for anyone who comes across this.
event_object['lines']['data'].each{ |i|
sub_id = i['id']
logger.debug sub_id
}

Resources