#MYoussef - Checkout Payment Method ID
1 messages ยท Page 1 of 1 (latest)
Hello! The Checkout Session will have a setup_intent property pointing to the Setup Intent, and the Setup Intent will have a payment_method property: https://stripe.com/docs/api/setup_intents/object#setup_intent_object-payment_method
i am using this API to create session in setup mode
should i create setup intent and pass to checkout session creation ?
No, Checkout will create the Setup Intent for you.
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);
Yeah, the Payment Method doesn't exist yet. It only exists after Checkout is complete.
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 ?
The steps would be:
- Create the Checkout Session
- Send your customer to Checkout
- Your customer provides payment info and completes Checkout
- You can now access the Payment Method ID as described above
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);
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);
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 ?
?
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
Can you send me the request ID for that call? (req_123) https://stripe.com/docs/api/request_ids
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
req_L5XXMsmru7FujT
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
thanks, i appreciate
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
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 ?
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
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();
}
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?
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);
awesome, i really appreciate it.
Many thanks for giving me this opportunity to help me
๐
Most welcome, you saved my day 