I've been trying to create a checkout session for a payment which should be directed in connected account.
whenever I'm trying to create a session using the code below I get InvalidRequestException saying Invalid array
Here's my code below,
\Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
$stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
$session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [
'price_data' => [
'unit_amount' => 25000,
'currency' => 'usd',
'product_data' => ['name' => 'Product8', 'active' => true],
],
'quantity' => 2,
],
'mode' => 'payment',
'success_url' => 'http://devweb.drivinggradebook.com/',
'cancel_url' => 'https://www.drivinggradebook.com/',
'payment_intent_data' => [
'application_fee_amount' => 10,
],
], ['stripe_account' => 'acct_1L7ugjSJzLhcy6eF']);
Please help me out with it,
Thanks
In my case I have it running as follows with laravel 8.75 and stripe/stripe-php 10.1
public function checkout(Request $request)
{
\Stripe\Stripe::setApiKey(STRIPE_SECRET);
header('Content-Type: application/json');
$checkout_session = \Stripe\Checkout\Session::create([
'line_items' => [
[
'price_data' => [
'currency' => 'eur',
'product_data' => [
'name' => 'Home'
],
'unit_amount' => 500
],
'quantity' => 1
],
],
'mode' => 'payment',
'success_url' => url("/stripe_success?session_id={CHECKOUT_SESSION_ID}&tenant_id=$request->id"),
'cancel_url' => route('payments.index'),
]);
return redirect()->away($checkout_session->url);
}
Related
I just want to add a redirect to a certain url after purchase by using the Stripe API for PHP. My approach so far is:
$link = $stripe->paymentLinks->create([
'line_items' => [
[
'price' => $product['default_price'],
'quantity' => 1,
],
],
'after_completion' => [
[
'redirect' => [
'url' => 'https://www.trainer-va.de/Trainer-Cloud/123.php?product=' . $product['default_price'] . '',
],
'type' => 'redirect'
],
]);
but it's not working. Any hints what's going wrong here?
Thanks in advance!
Your request body is malformatted at the after_completion attribute level[1].
You should do like this instead:
$link = $stripe->paymentLinks->create([
'line_items' => [
[
'price' => $product['default_price'],
'quantity' => 1,
],
],
'after_completion' => [
'redirect' => [
'url' => 'https://www.trainer-va.de/Trainer-Cloud/123.php?product=' . $product['default_price'] . '',
],
'type' => 'redirect'
],
]);
[1] https://stripe.com/docs/api/payment_links/payment_links/create#create_payment_link-after_completion
This is the way how I create checkout function:
foreach($request->arrayOfObjects as $req) {
array_push($lineItems, [
'price' => $req['priceID'],
'quantity' => (int) $req['quantity']
]);
}
if (Auth::check()) {
$charge = $stripeClient->checkout->sessions->create([
'payment_method_types' => [
'card',
'sepa_debit',
'giropay',
'sofort',
'alipay'
],
'success_url' => env('APP_URL').'/success',
'cancel_url' => env('APP_URL').'/cancel',
'shipping_address_collection' => [
'allowed_countries' => ['DE'],
],
'shipping_options' => [
[
'shipping_rate' => $request->lieferung ? env('SHIPPING_RATE_LIEFERUNG') : env('SHIPPING_RATE_ABHOLUNG')
],
],
'line_items' => [$lineItems],
'mode' => 'payment',
'metadata' => [
'lieferung' => $request->lieferung,
'isCustomer' => true
],
'allow_promotion_codes' => true,
'customer' => Auth::user()->stripe_id
]);
}
It works fine though... However, I'd like to know is there way to describe measurements of an article?
For example I have ordered 10 packages of Parquet Flooring Classic and each package has 3m2 of parquet.
I'd like to see it in the bill, like:
Article 1001 - Parquet Flooring Classic (10x Packages - 3m2 each)
I found that metadata could be provided but only for the global checkout level, not per item.
Is there any way to do this?
You can provide metadata per item using Stripe Checkout. Here's an example:
$charge = $stripeClient->checkout->sessions->create([
...
'line_items' => [
// item 1
[
'price_data' => [
'product_data' => [
'name' => 'Item A',
],
// 10.99 USD
'currency' => 'usd',
'unit_amount' => 1099,
],
'quantity' => 3,
],
// item 2
[
'price_data' => [
'product_data' => [
'name' => 'Item B',
],
// 5.49 USD
'currency' => 'usd',
'unit_amount' => 549,
],
'quantity' => 2,
],
],
...
]);
At checkout, it will give you a neat list of
3 x Item A ....... 32.97 $ USD
2 x Item B ....... 10.98 $ USD
Check all the information on line_items in The stripe API documentation (click on the 'show child attributes' button for all the possible keys and what they're for.)
I am getting following error when trying to get page url
$YOUR_DOMAIN = $_SERVER['HTTPS'];
$v=$_POST['price'];
$p=$_POST['product'];
$d=$_POST['domain'];
$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'unit_amount' => $v,
'product_data' => [
'name' => $p,
'images' => ["https://i.imgur.com/EHyR2nP.png"],
],
],
'quantity' => 1,
]],
"metadata[domain]"=>$d,
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN . '/success.php?domain='.$d,
'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);
Fatal error: Uncaught (Status 400) (Request
req_FafwbsfPd38bhj) Not a valid URL
Use $_SERVER['SERVER_NAME'] instead of $_SERVER['HTTPS']
Can anyone help me? I migrate form v2 to v3 checkout.
how can I send my custom description order in stripe dashboard description column?
now I get only the payment id pi_1IrhQALKfdoxxl3X07seJ5anto
with old API by description I would do:
$charge = \Stripe\Charge::create(array(
"amount" => $_POST['amount'],
"currency" => "EUR",
"description" => "Order #".$_POST["order"],
"source" => $token,
));
with the new API :
$stripe->checkout->sessions->create([
'success_url' => 'https://example.com/success',
'cancel_url' => 'https://example.com/cancel',
'payment_method_types' => ['card'],
'line_items' => [
[
'price' => 'price_H5ggYwtDq4fbrJ',
'quantity' => 2,
],
],
'mode' => 'payment',
]);
Thank you
according to the stripe documentation : https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-payment_intent_data-description
$stripe->checkout->sessions->create([
'success_url' => 'https://example.com/success',
'cancel_url' => 'https://example.com/cancel',
'payment_method_types' => ['card'],
'line_items' => [
[
'price' => 'price_H5ggYwtDq4fbrJ',
'quantity' => 2,
],
],
'mode' => 'payment',
'payment_intent_data' => [
'description' => "Order #".$_POST["order"]
]
]);
I developped the last Stripe module for the end of the year as they ask, but since I put it, I haven't any more the description and the name of the customer in Stripe.
Do you know how can I put it again ?
This is my code in PHP :
try {
$session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'customer_email' => $email,
'line_items' => [[
'price_data' => [
'product_data' => [
'name' => $custom['item_name'],
'metadata' => [
'pro_id' => $custom['item_name']
]
],
'unit_amount' => (isset($custom['amountTotal']) ? $custom['amountTotal'] : $custom['amount'])*100,
'currency' => $custom['currency_code'],
],
'quantity' => 1,
'description' => $custom['item_name'],
]],
'mode' => 'payment',
'success_url' => $url,
'cancel_url' => $request->cancel,
], ['stripe_account' => $_SESSION['param']->stripeUID]);
}catch(Exception $e) {
$api_error = $e->getMessage();
}
if(empty($api_error) && $session){
$response = array(
'status' => 1,
'message' => 'Checkout Session created successfully!',
'sessionId' => $session['id']
);
}else{
$response = array(
'status' => 0,
'error' => array(
'message' => 'Checkout Session creation failed! '.$api_error
)
);
}
And this is what I have now
You should be able to set it here: https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-payment_intent_data-description
just add this line so the description appears, I mark it with asterisks or in bold
'payment_method_types' => ['card'],
'**payment_intent_data**' => [
'description' => ''.$productName.''
],
'line_items' => [[
'price_data' => [
'product_data' => [
'name' => $productName,
'description' => ''.$productName.'',
'metadata' => [
'pro_id' => $productID
]
],
'unit_amount' => $stripeAmount,
'currency' => $currency,
],
'quantity' => 1,
'description' => $descripcion,
]],
'mode' => 'payment',