#jay_subscription-go
1 messages ¡ Page 1 of 1 (latest)
đ Welcome to your new thread!
â˛ď¸ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question. Thank you for your patience!
âąď¸ We automatically close idle threads, which makes them read-only. Make sure you stick around to chat in realtime! If this thread is closed and you have another question you'll need to start a new thread.
đ This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1214621986026688582
đ Have more to share? You can add more detail below, including code, screenshots, videos, etc.
This is a spew.Dump of the request API call. 2024/03/05 12:01:59 sub_1Or1IuAJcl9NAQwvAzoezwpz
(*stripe.Invoice)(0xc000a1e400)({
APIResource: (stripe.APIResource) {
LastResponse: (*stripe.APIResponse)(<nil>)
},
AccountCountry: (string) "",
AccountName: (string) "",
AccountTaxIDs: ([]*stripe.TaxID) <nil>,
AmountDue: (int64) 0,
AmountPaid: (int64) 0,
AmountRemaining: (int64) 0,
AmountShipping: (int64) 0,
Application: (*stripe.Application)(<nil>),
ApplicationFeeAmount: (int64) 0,
AttemptCount: (int64) 0,
Attempted: (bool) false,
AutoAdvance: (bool) false,
AutomaticTax: (*stripe.InvoiceAutomaticTax)(<nil>),
BillingReason: (stripe.InvoiceBillingReason) "",
Charge: (*stripe.Charge)(<nil>),
CollectionMethod: (stripe.InvoiceCollectionMethod) "",
Created: (int64) 0,
Currency: (stripe.Currency) "",
Customer: (*stripe.Customer)(<nil>),
CustomerAddress: (*stripe.Address)(<nil>),
CustomerEmail: (string) "",
CustomerName: (string) "",
CustomerPhone: (string) "",
CustomerShipping: (*stripe.ShippingDetails)(<nil>),
CustomerTaxExempt: (*stripe.CustomerTaxExempt)(<nil>),
CustomerTaxIDs: ([]*stripe.InvoiceCustomerTaxID) <nil>,
CustomFields: ([]*stripe.InvoiceCustomField) <nil>,
DefaultPaymentMethod: (*stripe.PaymentMethod)(<nil>),
DefaultSource: (*stripe.PaymentSource)(<nil>),
DefaultTaxRates: ([]*stripe.TaxRate) <nil>,
})
ID: (string) (len=27) "in_1Or1IuAJcl9NAQwv9dvZ7fTn",
InvoicePDF: (string) "",
Issuer: (*stripe.InvoiceIssuer)(<nil>),
LastFinalizationError: (*stripe.Error)(<nil>),
LatestRevision: (*stripe.Invoice)(<nil>),
Lines: (*stripe.InvoiceLineItemList)(<nil>),
Livemode: (bool) false,
Metadata: (map[string]string) <nil>,
I don't really follow your full dump so I don't understand what you are asking unfortunately
- Share your exact code instead of a dump of text
func UpdateStripeTableData(subscriptions []*stripego.Subscription) {
var validSubscriptionsFromStripe []db_model.Stripe
for _, s := range subscriptions {
var cancelAt time.Time
if s.CancelAt > 0 {
cancelAt = time.Unix(s.CancelAt, 0)
}
// invoiceID := s.LatestInvoice.ID
// tempInvoice, _ := stripe.GetInvoiceByID(invoiceID, config.Cfg.StripeSecret)
// spew.Dump(tempInvoice)
price := float64(s.LatestInvoice.AmountPaid) / 100.0
stripeSubscription := db_model.Stripe{
SubID: s.ID,
SubCancelAt: cancelAt,
SubCancelAtPeriodEnd: s.CancelAtPeriodEnd,
SubCreated: time.Unix(s.Created, 0),
SubCurrentPeriodStart: time.Unix(s.CurrentPeriodStart, 0),
SubCurrentPeriodEnd: time.Unix(s.CurrentPeriodEnd, 0),
SubStatus: string(s.Status),
StartDate: time.Unix(s.StartDate, 0),
EndedAt: time.Unix(s.EndedAt, 0),
Price: price,
LastPaymentReceivedDate: time.Unix(s.LatestInvoice.Created, 0),
}
log.Println(s.ID)
spew.Dump(s.LatestInvoice)
log.Fatal(s.LatestInvoice.AmountPaid)
//Does it already exist?
existingItem := repositories.StripeBySubID(gormDBReadReplica, s.ID)
if existingItem.ID != "" {
stripeSubscription.ID = existingItem.ID
}
repositories.StripeUpdate(gormDB, stripeSubscription)
s := db_model.Stripe{
SubID: s.ID,
}
validSubscriptionsFromStripe = append(validSubscriptionsFromStripe, s)
}
}
I'm looping the data from this call txn := newrelic.FromContext(ctx)
defer txn.StartSegment("Stripe - GetValidSubscriptions").End()
stripe.Key = stripeKey
limit := int64(100)
count := 2
var sList *stripe.SubscriptionList
var subscriptions []*stripe.Subscription
var lastID *stripe.Subscription
for counter := 1; counter < count; {
params := stripe.SubscriptionListParams{
ListParams: stripe.ListParams{
Limit: &limit,
},
Status: stripe.String(status),
}
config.Log.Sugar().Infof("Pulling %d items from stripe subscription list", int64(counter)*limit)
time.Sleep(100 * time.Millisecond)
if lastID != nil {
params.StartingAfter = &lastID.ID
}
subscriptionsList := sub.List(¶ms)
sList = subscriptionsList.SubscriptionList()
subscriptions = append(subscriptions, sList.Data...)
if !sList.HasMore {
break
}
lastID = sList.Data[len(sList.Data)-1]
log.Println(lastID)
counter++
}
return subscriptions
Sorry that's so much code
You asked lol
Sure okay let's ignore all of this then
I'm calling sub.List then looping it and trying to grab latestinvoice from it
- Call the Retrieve Subscription API
- Share the exact code just for this
jay_subscription-go
I'm not using a single sub api call
I'm using sub.List
to get all active subs on file
I understand that part, but you said "LatestInvoice is null" and so I would like to focus first on simply retrieving one Subscription and printing its LatestInvoice details
That would work fine but that is not the issue at hand here. I'm trying to avoid calling a single sub id since we have too many and your api speed/rate limit isn't going to work with what we are doing.
I'm asking sub.List to give me that data since it pulls all available subs
It's not filled in when making that call
only the invoice ID is filled in making me yet call another API call
Sorry we're talking a bit past each other here. I'm not telling you to do this for every Subscription. I'm asking you to work with me on a simple flow to debug your problem so that we find out what's wrong and then you can go back to your long code and fix the bug, that's all.
Just to clarify you're telling me that sub.List should contain a invoice for latest invoice on your end?
yes
okay one momento
subscriptionsList := sub.List(¶ms)
sList = subscriptionsList.SubscriptionList()
subscriptions = append(subscriptions, sList.Data...)
if !sList.HasMore {
break
}
lastID = sList.Data[len(sList.Data)-1]
That's not what I asked though. Can you pause on that completely and focus solely on retrieving on Subscription first?
Just a single sub itself like by a single sub id?
yes
I believe you mean? stripe.Key = stripeKey
params := stripe.SubscriptionParams{}
subscription, err := sub.Get(subscriptionID, ¶ms)
if err != nil {
config.Log.Sugar().Infof("GetSubscriptionByID error: %v \n", err)
}
spew.Dump(subscription.LatestInvoice)
log.Fatal("stop")
yes perfect so what does that log?
invoice is null
(*stripe.Invoice)(0xc000efc000)({
APIResource: (stripe.APIResource) {
LastResponse: (*stripe.APIResponse)(<nil>)
},
AccountCountry: (string) "",
AccountName: (string) "",
AccountTaxIDs: ([]*stripe.TaxID) <nil>,
Sub id was sub_1OpdqRAJcl9NAQwvKDPpNp5h
Okay now do spew.Dump(subscription.LatestInvoice.ID) what does that return?
in_1Or1dOAJcl9NAQwvJd8OQK3z
okay so clearly the Invoice ID is here as expected right?
yes sir
Do you know what our Expand feature is in our API? https://stripe.com/docs/expand are the docs and https://www.youtube.com/watch?v=m8Vj_CEWyQc is an end to end video about the feature. I highly recommend watching it
oh nice
In most languages/SDKs the way our API works is that it returns a string like latest_invoice: 'in_123' in the API but you can "expand it" to get latest_invoice: {id: 'in_123', object: 'invoice', ... all other properties ... }
No was not aware of this
In stripe-go those properties are always modeled as the full object/API resource. But depending on whether we returned a string or the full object you get either only ID filled or the whole thing
The question is though would that work entirely with sub.List without another API call?
For reference I was looking at your docs here https://docs.stripe.com/api/subscriptions/list
try to follow what I am explaining for a few minutes please
alright
So in your earlier code, you had log.Println(s.ID) spew.Dump(s.LatestInvoice) log.Fatal(s.LatestInvoice.AmountPaid)
but that doesn't work because what you get in the API by default is just ID so if you added log.Println(s.LatestInvoice.ID) you would have seen that Invoice ID in your List calls the exact same way
ok
Does that part make sense overall?
I believe so.
Okay so now are you okay with just the id of the Invoice in_123 or did you want the full Invoice object to store other information?
Full invoice
I'm guessing based on your explanation I need the params to contain expand for it
yes
The exact syntax is a bit hazy at the moment
so let's go back to your retrieve
ok
k
try that and share the exact code after and I'll confirm
Perfect!
So now you have to do the same for the List API, and it's a little bit different, see https://docs.stripe.com/expand?lang=go#lists
i got it data.latest_invoice works
wow this is cool i was not aware of expand before
thanks
yeah that feature is so powerful and simple to use, but it takes a while to grasp it
I recommend watching the video I shared earlier, it makes it click a lot more easily!
I am
the crappy part is that it's a string so i have to figure out exactly what it matches
I mean stripe-go tells you right? LatestInvoice is a stripe.Invoice
well for lists i mean it ended up being data.
As long as the fields always match it should be fine really
One more question. I just upgraded from 74 to 76. Where is the best place to figure out if it will break anything?
https://github.com/stripe/stripe-go/wiki has migration guides!
bookmarked thanks