#sow-paymentmethod-detach

1 messages · Page 1 of 1 (latest)

stone ivy
#

Hello! Do you have an example event ID I can take a look at?

twin bolt
#

sure

#

evt_1KF4MxCHSKMd2K1kZuHE32s3

#

ie the event id for paymethod_detached

#

when I set it up using setupintent customer is null

#

I used the CLI for that , stripe trigger setup_intent.succeeded --add setup_intent:customer=cus_KutoTAloprgw5S

#

I want to know in real world, when we setup the setupintent, does it keep customerId with the associated payment method ?

stone ivy
#

I think this is expected - when the payment method has been detached, it is no longer tied to a customer. However, we still reflect what the customer was previously under previous_attributes (you can see for that event previous_attributes[customer]: cus_KutoTAloprgw5S

twin bolt
#

great, Let me quickly test it in my code

stone ivy
#

You can also see in the previous event for setup_intent.succeeded that it definitely has a customer at that point evt_1KF4HfCHSKMd2K1kEdBxe1YL

twin bolt
#

yes I am more interested on the paymentmethod_detached from a customer

#

is previous_attributes are part of PaymentMethod object ?

stone ivy
#

previous_attributes is part of the Event object, not the Payment Method object

twin bolt
#

oh ok

#

is this going to be always customer ?+ PreviousAttributes {{
"customer": "cus_KutoTAloprgw5S"
}} dynamic {Newtonsoft.Json.Linq.JObject}

#

for paymentmethod_detached event ?

#

also in stripe .NET SDK, is it equilant to customer object ?

#

I am trying to take it as strongly typed object

stone ivy
#

For the payment_method.detached event yes, that should always have customer in previous attributes.

#

This will not be the equivalent to a customer object - it's just an ID. if you want the full customer object you'd have to retrieve it

twin bolt
#

ok

#

var paymentMethod = stripeEvent.Data.Object as PaymentMethod;

#

var customer = stripeEvent.Data.PreviousAttributes as Customer;

#

I was trying to take like that

#

so instead of Customer I need to create a type my end ?

#

is Stripe SDK provides any objects for the PreviousAttributes

stone ivy
#

Stripe provides a type for the Event, which has EventData (see https://github.com/stripe/stripe-dotnet/blob/7b62c461d7c0cf2c9e06dce5e564b374a9d232e0/src/Stripe.net/Entities/Events/EventData.cs#L22) which includes Previous Attributes

GitHub

Stripe.net is a sync/async .NET 4.6.1+ client, and a portable class library for stripe.com. - stripe-dotnet/EventData.cs at 7b62c461d7c0cf2c9e06dce5e564b374a9d232e0 · stripe/stripe-dotnet

twin bolt
#

ok but not to the values right as it is dynamic

stone ivy
#

yup

twin bolt
#

Thanks, I am trying like this now and will see if that works, var paymentMethod = stripeEvent.Data.Object as PaymentMethod;

                    dynamic prevAttributes = JsonConvert.DeserializeObject(stripeEvent.Data.PreviousAttributes);

                    paymentMethod.CustomerId = prevAttributes?.customer;
#

My approach didnt work as it was a collection

#

var previousAttributesJson = JsonConvert.SerializeObject(stripeEvent.Data.PreviousAttributes) as string;

                    var previousAttributes = JsonConvert.DeserializeObject<IDictionary<string, object>>(previousAttributesJson);

                    previousAttributes.TryGetValue("customer", out var customerId);

                    paymentMethod.CustomerId = customerId.ToString();
#

that worked for me as per the doc you provided

#

Thank you so much 🙂

stone ivy
#

👍 awesome!