Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe: Getting Credit Card's Last 4 Digits

I have upgraded the Stripe.net to the latest version which is 20.3.0 and now I don't seem to find the .Last4 for the credit card. I had the following method:

    public void CreateLocalCustomer(Stripe.Customer stipeCustomer)
    {
        var newCustomer = new Data.Models.Customer
        {
            Email = stipeCustomer.Email,
            StripeCustomerId = stipeCustomer.Id,
            CardLast4 = stipeCustomer.Sources.Data[0].Card.Last4
        };

        _dbService.Add(newCustomer);

        _dbService.Save();
    }

But now the stipeCustomer.Sources.Data[0].Card.Last4 says 'IPaymentSource' does not contain a definition for 'Card'. Does anyone know how I can get the card details now? The flow is that I create the customer by passing the Stripe token to Stripe, then I get the above stripeCustomer. So I expect it to be somewhere in that object. But I can't find it. The release notes can be found here. Thank you.

like image 876
Stackedup Avatar asked Jan 01 '23 16:01

Stackedup


1 Answers

In the old world of Stripe, there only used to be one type of payment method you could attach to a Customer; specifically, Card-objects. You would create a Card-object by using Stripe.js/v2 or the Create Token API Endpoint to first create a Token-object and then attach that token to a Customer-object with the Create Card API Endpoint.

Once Stripe expanded to support a number of other payment methods though, Stripe built support for a new object type that encapsulated a number of payment methods (including credit cards) called Source-objects. A Source-object is created either by using Stripe.js/v3 or the Create Source API Endpoint. It can also be attached to a Customer-object in much the same way as the Card-objects mentioned above, except they retain their object type. They're still a Source. You use the Attach Source API Endpoint to do this (that is notably identical to the Create Card API Endpoint mentioned above).

What I'm getting at here, is there are now two different object types (or more) that you can expect to see returned in the sources-array (or Sources in .NET). All of these methods though inherit from the IPaymentSource-interface. So if you know you have a Card-object getting returned, you can simply cast the returned object to the Card-class.

Something like this should get you going:

CardLast4 = ((Card) stipeCustomer.Sources.Data[0]).Last4

You can see what I mean by inheritance by looking at this line in the Card-class file:

https://github.com/stripe/stripe-dotnet/blob/master/src/Stripe.net/Entities/Cards/Card.cs#L7

Good luck!

like image 53
korben Avatar answered Jan 31 '23 11:01

korben