#ahsanetics
1 messages ยท Page 1 of 1 (latest)
Hi ๐ you can use price_data (instead of price) to create ad-hoc Prices instead of using ones that were built previously:
https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-line_items-price_data
Hey.. really , let me quickly check the link
So I removed price under line_items and added price_data=>2000. Got error, invalid object
So I added is as array price_data=>[ 'unit_amount'=>2000, 'currency'=>'usd"]. It is asking for product_data
Not sure what the product data should be?
Are you trying to create an adhoc Product as well, or are you intending to reuse an existing Product? If the former you use product_data (shown in the API spec):
https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-line_items-price_data-product_data-name
for the latter you would pass the ID of an existing Product via the product parameter:
https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-line_items-price_data-product
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
I don't want to create any product. Just wanted to send a price to the stripe to get it charged. Is this a possibility?
Not with Checkout Sessions, they rely on a line item structure, so you must pass at least enough information to create an adhoc Price and Product.
Hmm. So other than checkout session, is there any other option where I can do a onetime charge without having to create a product
Yup, you can create a Payment Intent and use the Payment Element to collect payment method details from your customers:
https://stripe.com/docs/payments/accept-a-payment?platform=web&ui=elements
Just to get some clarity, what do you mean by adhoc product?
A Product object that you didn't create ahead of time. Checkout Sessions require Price and Product objects to function, so you can either pass in existing objects that you created previously, or you can provide enough data for the Checkout Session creation request to create them on the fly.
So I passed the product_data and got a session id but when I log on to the dashboard, that product that i passed in product_data is not under Products.
Is that a concern? We're not as familiar with how the dashboard shows things as we primarily focus on helping with our API in this forum, but if you didn't want to create a Product in the first place then not seeing it in the dashboard sounds ideal.
It does.. ok so I will keep an eye.. may be make a payment and see if it shows. Thanks for your help. I will check the link as well.. appreciate your help
Another question
I tried to send a post data to php script that creates session but it was not receiving it
Is it blocked by your script?
I don't know what that means, can you elaborate?
So I wanted to send the amount that needs to be charged from client side. I send the amount as post request to the create_session.php. however, the script doesn't receives the post data. I have tried all possible ways but none worked. Hence, wondering if it is blocked by stripe
You should not allow client side code to directly control the amount being charged. Anyone with sufficient javascript knowledge could intercept that and change the amount they're paying.
That is a good point but then I would have to create create_session.php for each product
Since stripe allows product in catalog. Atleast product name could be sent as post data to the script
I'm still not sure I understand what script you're talking about. I just scanned our php repository and didn't find a create_session.php file. Is that a script that you built or got from a third party?
Yea it is a script that I built that create session with line_items etc. I
<?php
require_once '../vendor/autoload.php';
require_once '../secrets.php';
\Stripe\Stripe::setApiKey($stripeSecretKey);
header('Content-Type: application/json');
$YOUR_DOMAIN = 'http://localhost:4242';
$checkout_session = \Stripe\Checkout\Session::create([
'line_items' => [[
Provide the exact Price ID (e.g. pr_1234) of the product you want to sell
'price' => '{{PRICE_ID}}',
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN . '/success.html',
'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);
header("HTTP/1.1 303 See Other");
header("Location: " . $checkout_session->url);
It is from this link
๐ taking over and catching up, Can you give me a summary of your question?
Hey.. the above mentioned script is provided by stripe to create sessions. I am trying to pass post data to it. E.g product name but the script doesn't get post data. I have tried multiple ways but still nothing. I wanted to find out if passing the post data from front-end blocked?
No, Stripe SDK wouldn't really block anything.
If you're not seeing the POST data on your server then there must be something wrong with the request OR your server might have a middleware that clears out the POST data from the request
I tried with postman as well