#jay_subscription-go

1 messages ¡ Page 1 of 1 (latest)

silent whaleBOT
#

👋 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.

bitter tartanBOT
ember tendon
#

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>,

sterile coral
#

I don't really follow your full dump so I don't understand what you are asking unfortunately

#
  1. Share your exact code instead of a dump of text
ember tendon
#

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(&params)
    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
sterile coral
#

Sorry that's so much code

ember tendon
#

You asked lol

sterile coral
#

Sure okay let's ignore all of this then

ember tendon
#

I'm calling sub.List then looping it and trying to grab latestinvoice from it

sterile coral
#
  1. Call the Retrieve Subscription API
  2. Share the exact code just for this
#

jay_subscription-go

ember tendon
#

I'm not using a single sub api call

#

I'm using sub.List

#

to get all active subs on file

sterile coral
#

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

ember tendon
#

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

sterile coral
#

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.

ember tendon
#

Just to clarify you're telling me that sub.List should contain a invoice for latest invoice on your end?

sterile coral
#

yes

ember tendon
#

okay one momento

#

subscriptionsList := sub.List(&params)
sList = subscriptionsList.SubscriptionList()
subscriptions = append(subscriptions, sList.Data...)
if !sList.HasMore {
break
}
lastID = sList.Data[len(sList.Data)-1]

sterile coral
#

That's not what I asked though. Can you pause on that completely and focus solely on retrieving on Subscription first?

ember tendon
#

Just a single sub itself like by a single sub id?

sterile coral
#

yes

ember tendon
#

I believe you mean? stripe.Key = stripeKey
params := stripe.SubscriptionParams{}
subscription, err := sub.Get(subscriptionID, &params)
if err != nil {
config.Log.Sugar().Infof("GetSubscriptionByID error: %v \n", err)
}
spew.Dump(subscription.LatestInvoice)
log.Fatal("stop")

sterile coral
#

yes perfect so what does that log?

ember tendon
#

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

sterile coral
#

Okay now do spew.Dump(subscription.LatestInvoice.ID) what does that return?

ember tendon
#

in_1Or1dOAJcl9NAQwvJd8OQK3z

sterile coral
#

okay so clearly the Invoice ID is here as expected right?

ember tendon
#

yes sir

sterile coral
ember tendon
#

oh nice

sterile coral
#

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 ... }

ember tendon
#

No was not aware of this

sterile coral
#

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

ember tendon
#

The question is though would that work entirely with sub.List without another API call?

sterile coral
#

try to follow what I am explaining for a few minutes please

ember tendon
#

alright

sterile coral
#

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

ember tendon
#

ok

sterile coral
#

Does that part make sense overall?

ember tendon
#

I believe so.

sterile coral
#

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?

ember tendon
#

Full invoice

#

I'm guessing based on your explanation I need the params to contain expand for it

sterile coral
#

yes

ember tendon
#

The exact syntax is a bit hazy at the moment

sterile coral
#

so let's go back to your retrieve

ember tendon
#

ok

sterile coral
ember tendon
#

k

sterile coral
#

try that and share the exact code after and I'll confirm

ember tendon
#

It works

#

params.AddExpand("latest_invoice")

sterile coral
#

Perfect!

ember tendon
#

i got it data.latest_invoice works

#

wow this is cool i was not aware of expand before

#

thanks

sterile coral
#

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!

ember tendon
#

I am

#

the crappy part is that it's a string so i have to figure out exactly what it matches

sterile coral
#

I mean stripe-go tells you right? LatestInvoice is a stripe.Invoice

ember tendon
#

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?

sterile coral
ember tendon
#

bookmarked thanks

sterile coral
#

Sure thing!

#

Anything else I can help with? Even if unrelated to your previous questions!