1
votes

Using Stripe elements and Stripe.net. I get the token and create a customer and source and charge it using the following code - works fine

            StripeConfiguration.SetApiKey(stripeKey);
            var charges = new StripeChargeService();
            var charge = charges.Create(new StripeChargeCreateOptions
            {
                Amount = thisCharge.Amount,
                Currency = "usd",
                Description = thisCharge.ProductDesc,
                StatementDescriptor = thisCharge.ProductDesc,
                //SourceTokenOrExistingSourceId = source.Id,

                ReceiptEmail = thisCharge.EmailAddress,
                CustomerId = thisCharge.StripeCustomerID,
                Metadata = new Dictionary<String, String>()
                    {
                        { "Name", thisCharge.CardHolderName }, { "Email",thisCharge.EmailAddress }
                    }
            });

I then try a second charge using a different card (5555 5555 5555 4444) with that same customer but apparently I am missing something.

using this code

    static StripeSource NewSource(string stripeKey, Charge  charge)
    {
        StripeConfiguration.SetApiKey(stripeKey);
        var sourceOptions = new StripeSourceCreateOptions
        {
            Type = StripeSourceType.Card,
            Currency = "usd",
            Amount = charge.Amount,
            Token = charge.Token,              

            Owner = new StripeSourceOwner
            {
                Email = charge.EmailAddress,
                Name = charge.CardHolderName
            }
        };
        var sourceService = new StripeSourceService();
        StripeSource source = sourceService.Create(sourceOptions);
        return source;
    }

But I get this error:

Invalid token id: src_....

If I skip adding the new source then the charge goes through but against the original source.

So obviously missing something... any help is appreciated.

1
hmm, in the above i see you have code to create a charge, and then to create a source (from a token). are you attaching the new source to the customer before attempting to charge? If you are not already doing that i think that could be your issue here. stripe.com/docs/api/sources/attach?lang=dotnetduck
@duck - I figured it out - when I submit the form I get a token "src_" which is already a source. So I didn't need to create a new source - and couldn't with that token. What I needed to do was add that source to the customer. duh.clarence_odbody

1 Answers

1
votes

Duh on my part but in case anyone else has the same issue.

When you submit the elements form it returns a token, in this case a source token "src_". So you don't create a new source, you simply assign it to your existing customer.

            StripeConfiguration.SetApiKey(stripeKey);
            var options = new StripeCustomerUpdateOptions
            {
                SourceToken = charge.Token,
            };

            var service = new StripeCustomerService();
            StripeCustomer customer = service.Update(charge.StripeCustomerID, options);