#MYoussef - Checkout Payment Method ID

1 messages ยท Page 1 of 1 (latest)

trim mauve
worldly idol
#

i am using this API to create session in setup mode

#

should i create setup intent and pass to checkout session creation ?

trim mauve
#

No, Checkout will create the Setup Intent for you.

worldly idol
#

but i received PaymentIntentId = null

#

this my code to create session

#

var options = new SessionCreateOptions
{

            PaymentMethodTypes = new List<string> {
              "card",
            },
            Mode = "setup",
            Customer = "cus_L8E153vH4CURUb",
            SuccessUrl = domain + "/successtest?membershipId=" + "&session_id={CHECKOUT_SESSION_ID}",
            CancelUrl = "https://example.com/cancel",
        };
#

and this is my code to retrieve the session data

#

var sessionService = new SessionService();
var stripeSession = sessionService.Get(session_id);

trim mauve
#

Yeah, the Payment Method doesn't exist yet. It only exists after Checkout is complete.

worldly idol
#

i called the support before and they asked me to these steps then pass the payment method id from this code to session creation to collect the payment

#

how to complete the checkout as you mentioned ?

trim mauve
#

The steps would be:

  1. Create the Checkout Session
  2. Send your customer to Checkout
  3. Your customer provides payment info and completes Checkout
  4. You can now access the Payment Method ID as described above
worldly idol
#

i already did that

trim mauve
#

That is only #1 above.

#

Where are you doing #2? And #3 cannot be done in code.

worldly idol
#

in the first function test

#

Response.Headers.Add("Location", session.Url);
return new StatusCodeResult(303);

#

it redirect to this page

#

and the customer fill the data, then it will be redirected to success function as i mentioned in my code

#

and success i tried to get the session details

var sessionService = new SessionService();
var stripeSession =sessionService.Get(session_id);

outer bay
#

That all sounds right so far. So that Checkout Session will have a payment_intent property, that payment_intent is associated with the payment_method that you are looking for. You can actually retrieve the full payment intent data by using expansion

var sessionService = new SessionService();
var options = new SessionGetOptions();
options.AddExpand("customer");
var stripeSession =sessionService.Get(session_id, options);

https://stripe.com/docs/api/expanding_objects

worldly idol
#

i tried this code as you mentioned

#

but PaymentIntentId = null

#

so how to retrieve the payment method id to use it in session creation in subsequent steps ?

#

?

outer bay
#

I haven't used our .NET library in a bit. I think when you expand the payment intent, the Session comes back with the PaymentIntent property filled instead of the PaymentIntent ID properly

#

Pulling my code up to check but you might be able to see it there now

worldly idol
#

this is what i received

outer bay
worldly idol
#

req_L5XXMsmru7FujT

outer bay
#

That request is for creating the Checkout Session. Was that screenshot from after the CheckoutSession was created or retrieved?

#

I have time to look at my .NET code now, will get back with what I can find from that

worldly idol
#

thanks, i appreciate

outer bay
#

Apologies, I copy-pasted a wrong line of code in the snipped I sent you. It should have been expanding payment_intent not customer

        var get_options = new SessionGetOptions();
        get_options.AddExpand("payment_intent");
        var stripeSession = sessionService.Get(session_id, get_options);
        Console.WriteLine(stripeSession.PaymentIntent.PaymentMethodId);```
#

If you do that on a Checkout Session that is completed you should see the payment method ID

worldly idol
#

i added your code in the success function of checkout session creation in setup mode but still null

#

and when i checked the log on dashboard i found that there is a method "POST /v1/payment_methods" is called that i did not call but it contains the the payment method id

#

what i missing and what should i add to my code to get the payment method id as you mentioned ?

outer bay
#

Can you send me the request ID from the GET request you just made where the payment method was null?

#

Actually please send both if you can, the POST method will help to

worldly idol
#

this is the full code
1- function to create checkout session in setup mode and redirect customer to stripe form where he can add the card information

2- success function where after the customer fill the form

#

public async Task<IActionResult> test()
{
var domain = Request.Scheme + "://" + Request.Host;

        var options = new SessionCreateOptions
        {
            
            PaymentMethodTypes = new List<string> {
              "card",
            },
            Mode = "setup",
            Customer = "cus_L8E153vH4CURUb",
            SuccessUrl = domain + "/home/successtest?membershipId=" + "&session_id={CHECKOUT_SESSION_ID}",
            CancelUrl = "https://example.com/cancel",
        };

        //options.AddExpand("customer");

        var service = new SessionService();
        var session = await service.CreateAsync(options);

        
        Response.Headers.Add("Location", session.Url);
        return new StatusCodeResult(303);
    }

    public IActionResult successtest(string session_id)
    {
        var sessionService = new SessionService();
        var get_options = new SessionGetOptions();
        get_options.AddExpand("payment_intent");
        var stripeSession = sessionService.Get(session_id, get_options);
        Console.WriteLine(stripeSession.PaymentIntent.PaymentMethodId);

        return View();
    }
outer bay
#

Those look like they should work as long as the Checkout Session is successful. So you are only calling successtest, once the user has completed Checkout and navigated to your success_url correct?

worldly idol
#

yes, after customer click save card button then code will redirect to successtest

outer bay
#

Oh you are using Checkout in Setup mode. My apologies for missing that.

#

You can do the same thing but with setup_intent instead of payment_intent

#
            var get_options = new SessionGetOptions();
            get_options.AddExpand("setup_intent");
            var stripeSession = sessionService.Get(session_id, get_options);
            Console.WriteLine(stripeSession.SetupIntent.PaymentMethodId);
worldly idol
#

awesome, i really appreciate it.

Many thanks for giving me this opportunity to help me

#

๐Ÿ’

outer bay
#

Of course, happy to help!

#

Thank you for your patience

worldly idol
#

Most welcome, you saved my day thankyou