How to retrieve stripe session id php - php

I hate to ask such a simple question on here but I can't seem to figure this out. I create a stripe checkout session that goes through successfully, but am having trouble getting a response. I have read through stripe docs and answers on here, but still cannot seem to get the stripe session id returned. Here's what I have tried
<?php
require_once('stripe-php-7.86.0/init.php');
$priceId = $_POST['priceId'];
$stripe = new \Stripe\StripeClient(
'my secret key here'
);
$stripe->checkout->sessions->create([
'success_url' => 'http://localhost/stripe_test/success.html',
'cancel_url' => 'http://localhost/stripe_test/cancel.html',
'payment_method_types' => ['card'],
'line_items' => [
[
'price' => $priceId,
'quantity' => 1,
],
],
'mode' => 'subscription',
]);
// I've tried all of these individually, none of them work.
echo json_encode($stripe);
echo $stripe['id'];
echo json_encode($stripe['id']);
echo $stripe->checkout->sessions['id'];
echo json_encode(['sessionId' => $stripe['id']]);

As per the official Stripe documentation, you can to assign the returned value of $stripe->checkout->sessions->create to the variable you want to use, then access the property id.
$stripe->checkout->sessions->create returns the Session object
For example
$stripe_session = $stripe->checkout->sessions->create([
'success_url' => 'http://localhost/stripe_test/success.html',
'cancel_url' => 'http://localhost/stripe_test/cancel.html',
'payment_method_types' => ['card'],
'line_items' => [
[
'price' => $priceId,
'quantity' => 1,
],
],
'mode' => 'subscription',
]);
echo $stripe_session->id;

I think response->id will be you session id

Related

Attach metadata to Stripe Checkout Subscription

I'm trying to use Stripe Checkout for a subscription service.
Using the PHP SDK
Session::create([
'customer' => $user->stripeCustomerId,
'payment_method_types' => ['card'],
'line_items' => [[
'price' => 'price_0MACBYAGG6RS7KP5c1fNa6v9',
'quantity' => $amount,
]],
'subscription_data' => [
'metadata' => [
'message' => $message,
],
],
'mode' => 'subscription',
'success_url' => UrlHelper::url('?session_id={CHECKOUT_SESSION_ID}'),
'cancel_url' => UrlHelper::url('?cancel=true'),
]);
but metadata is never attached as per the docs https://support.stripe.com/questions/using-metadata-with-checkout-sessions?locale=en-GB
I have a single charge option that passes:
'payment_intent_data' => [
'metadata' => [
'message' => $message,
],
],
and that attaches perfectly. What am I missing?
Using subscription_data[metadata] (API ref) as you've done is the way this is intended to work. Have you confirmed in your request logs that the metadata was included in your request as you expect? It's possible your $message might be empty in the subscription case.
If your Checkout session request did include the subscription_data metadata as expected, then you should also see this on the subscription object after the customer completes the Checkout session. Where are you looking at the resulting subscription which makes you conclude that it does not have metadata?
Turns out that I was looking in the wrong place. The meta data is not attached to each payment but on the actual subscription itself.

I'm trying to add a custom product/price to the Stripe Checkout Page using php

I am trying to customise stripe checkout to take a dynamic price from my cart. To test that I am simply trying to create a product and price within the strip config and then pass that price into the line item.
The code that I am trying keeps telling me that is no such product. I don't know if I am making a syntax error or if what I am trying is just wrong. can anyone advise?
include './stripe/init.php';
\Stripe\Stripe::setApiKey('API KEY GOES HERE');
header('Content-Type: application/json');
$YOUR_DOMAIN = 'https://www.XXXXX.XX.XX';
$product = \Stripe\Product::create([
'name' => 'Elearn Product',
]);
$price = \Stripe\Price::create([
'product' => '{{$product}}',
'unit_amount' => 1100,
'currency' => 'usd',
'recurring' => [
'interval' => 'month',
],
]);
$checkout_session = \Stripe\Checkout\Session::create([
'line_items' => [[
'price' => '{{$price}}',
'quantity' => 1,
]],
'payment_method_types' => [
'card',
],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN . '/enrolment-success',
'cancel_url' => $YOUR_DOMAIN . '/enrolment-failure',
]);
header("HTTP/1.1 303 See Other");
header("Location: " . $checkout_session->url);
You need to specify the Product and Price IDs like this:
'product' => $product->id,
And this:
'price' => $price->id,
Also, you can create multiple Prices for one Product, so if you're selling the same thing at different price points you can create the Product once and create many Prices for it instead of creating a new Product each time.

How can i insert metadata details or expand new properties using stripe

I just want to apply what the stripe's docs said about adding the Metadata property and then i can add everything and it can be displayed in the webhook, but i have tried to add the metadata , even to expand details but i didnt know how , this is the code :
$session = \Stripe\Checkout\Session::create([
'success_url' => 'https://example.com/success',
'cancel_url' => 'https://example.com/cancel',
'payment_method_types' => ['card'],
'line_items' => [
[
'price' => $price_id,
'metadata' =>['prod_id' => ' TEST'], //// i want to store something like that
'quantity' => 1,
],
],
'mode' => 'payment',
]);
You can add metadata to the object, but not inside the line-items array.
Add metadata like below
$session = \Stripe\Checkout\Session::create([
'success_url' => 'https://example.com/success',
'cancel_url' => 'https://example.com/cancel',
'payment_method_types' => ['card'],
'line_items' => [
[
'price' => $price_id,
'quantity' => 1,
],
],
'metadata' =>['prod_id' => ' TEST'], //// i want to store something like that
'mode' => 'payment',
]);

Can you pass a shipping address to Stripe Checkout

I am using Stripe Checkout API to direct a website user to make payment.
Is there a way to pass a shipping address to the hosted checkout page so it's gathered from the referrer rather then Stripe themselves?
function createSession()
{
require 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('[API_KEY_REDACTED]');
$YOUR_DOMAIN = '[SITE_URL_REDACTED]';
// Calculate price
$price = 50;
$checkout_session = \Stripe\Checkout\Session::create([
'billing_address_collection' => 'required',
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'gbp',
'unit_amount' => $price,
'product_data' => [
'name' => 'Product'
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN . '/success',
'cancel_url' => $YOUR_DOMAIN . '/cancel',
]);
return json_encode(['id' => $checkout_session->id]);
}
You can add the following line to make Stripe ask for a shipping address, but I want to pass this from the referrer instead.
'shipping_address_collection' => [
'allowed_countries' => ['US', 'CA'],
],
To do this you would create a Customer and provide their shipping address, then provide that existing Customer when creating the Checkout session:
$checkout_session = \Stripe\Checkout\Session::create([
'customer' => 'cus_123',
...
]);

Stripe # missing param success_url , even if it is exists

trying to understand, why i'm getting error (developer took money and gone)
I'm trying to buy a stuff, and once I click BUY, I receive this error Missing required param: success_url.
I'm using Laravel.
but in code, I can see that, success_url exists.
\Stripe\Stripe::setApiKey(Setting::get('stripe_secret_key'));
$session = \Stripe\Checkout\Session::create(
[
'payment_method_types' => ['card'],
'line_items' => [
[
'name' => 'Test',
'description' => 'Test2: '.implode(', ', $vouchersNames),
'amount' => $total * 100,
'currency' => 'eur',
'quantity' => 1,
],
],
'success_url' => env('APP_URL').'buy_vouchers_done?session_id={CHECKOUT_SESSION_ID}&vouchers='.urlencode(implode(',', $vouchers->pluck('id')->toArray())),
'cancel_url' => env('APP_URL'),
]);
Where is a problem? Thank you!

Categories