#ranran-event-casting
1 messages ยท Page 1 of 1 (latest)
Yes
in the stripeEvent object I have the id with a value evt_xxx
Sure, can you share that exact Event id here so I can have a look?
Sure, value is evt_1NUuBMDtQLochef4CH2M6DBa
Are you using the CLI? Are you sure you get the correct version of the Event? I assume the Event you get has the wrong API version
Yes, I am using CLI
The API version that gives me this answer is 2019-12-03
Although the previous tests that I performed already receiving the answers from the development environment gave me the same problem.
If you log what you get, check the exact API version I assume you get the wrong version of the Event on your endpoint
Sorry, I really don't understand this last one
The ApiVersion of the stripeEvent object I get is 2019-12-03
Where do you see that exactly though? Sorry I don't see your computer so I need you to try and be explicit and explain exactly what you're getting, how you are debugging
Ah perfect thanks, so you do get the correct information. So what's the issue? What error do you get when calling ConstructEvent exactly?
or is that picture right after that call?
var stripeEvent = EventUtility.ConstructEvent(
json,
Request.Headers["Stripe-Signature"],
webhook_secret
);
// OKEY
if (stripeEvent.Type == Events.InvoicePaymentSucceeded) // OKEY
{
var stripeInvoice = stripeEvent.Data.Object as Stripe.Invoice; // ERROR - Here is the problem
// stripeInvoice.BillingReason -> null Exception
}
Despite receiving a correct response that line of code does not assign any value to my stripeInvoice variable.
Yeah I assume you have something on the Invoice object that isn't compatible with that version of stripe-dotnet (which is 4 years old).
Unfortunately I'm not sure what that is. When you get that error you should see a clearer exception/problem
I have tried to catch an exception in that line but it does not jump any.
I understand that it could be a problem with the version and that updating to the latest version v41.24.0 should solve it.
I understand that there must be some page where you can find out according to the API version which version of stripe-dotnet or other implementations can be used.
Although I have not been lucky enough to find it.
I don't think the API version per se is the issue here. More likely that this is really old and you have something on the Invoice resource preventing the compatibility. Is there a reason you're using such an old version of the library?
(I'm trying to see if I can repro but will take a bit of time)
It is an inherited project in which at this moment I have had to make changes in this part.
Before updating the package I wanted to see if I had made a mistake, but I guess I'll have to update .
for what it's worth I tried your exact code and it works fine for me
var stripeEvent = EventUtility.ParseEvent(json, true);
Console.WriteLine(stripeEvent.Id);
if (stripeEvent.Type == Events.InvoicePaymentSucceeded)
{
var stripeInvoice = stripeEvent.Data.Object as Stripe.Invoice; // Dont work
Console.WriteLine(stripeInvoice.Id);
// stripeInvoice.BillingReason -> null
}```
I copy-pasted the exact raw JSON from the Event (and replaced all the " with ') and it works fine and prints the Invoice id as expected
Can you try logging the .Id like I did?
One moment, I'll try it now
mmm it worked for me
I don't understand why
The problem seems to be in EventUtility.ConstructEvent.
๐ I thought you said that worked fine? I'm confused
var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();
var stripeEventJSon = EventUtility.ParseEvent(json, true);
if (stripeEventJSon.Type == Events.InvoicePaymentSucceeded)
{
var stripeInvoicev2 = stripeEvent.Data.Object as Stripe.Invoice;
var id = stripeInvoicev2.Id; // Has value and i can access
}
With that now my variable stripeInvoicev2 gives me access to the properties and their values as expected
Sure but earlier I asked explicitly if ConstructEvent worked and you said yes right?
I don't really understand it either.
EventUtility.ConstructEvent does not give me failure or exception, from the debugger I can see that it gives me all the information.
But the line of " as Stripe.Invoice " as I commented is the one that fails me.
I'm actually using ConstructEvent elsewhere
in which I have a I have a cast as follows
var session = stripeEvent.Data.Object as Session;
and it works
Hum this is really strange
yes
especially if you say it's the same exact JSON
Exactly.
Maybe some kind of bug that they fixed in some later version of stripe-dotnet than the one I'm using.
maybe but ConstructEvent is supposed to just be checking the signature + ParseEvent
I still think upgrading is a better path forward but I really don't see how the ConstructEvent could succeed but then the cast would fail
I think I have it
public static Event ConstructEvent(string json, string stripeSignatureHeader, string secret, long tolerance = 300, bool throwOnApiVersionMismatch = true);
public static Event ConstructEvent(string json, string stripeSignatureHeader, string secret, long tolerance, long utcNow, bool throwOnApiVersionMismatch = true);
public static Event ParseEvent(string json, bool throwOnApiVersionMismatch = true);
public static void ValidateSignature(string json, string stripeSignatureHeader, string secret, long tolerance = 300);
public static void ValidateSignature(string json, string stripeSignatureHeader, string secret, long tolerance, long utcNow);
the parameter "throwOnApiVersionMismatch".
๐ stepping in as koopajah needed to step away
Sounds like you have a mismatch of pinned SDK version vs. Event API Version, is that right?
I don't know what happened
I have retested the way I had it.
A whole afternoon with that problem and now it works.
( In fact in the form that koopajah asked me to test, I had a bug that basically kept using the variable that ConstructEvent returns to me to do the casting )
Yes, it started working after trying the other way, using EventUtility.ParseEvent.
It is really strange.
Thank you very much for your time and support and sorry for the inconvenience.