Stripe # missing param success_url , even if it is exists - php

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!

Related

charge payment after 5 minutes

I have a function a stripe function that slashes the price of a product, and I am trying to charge the newly slashed price after 5 minutes.
in my checkoutController I have
public function secondHalf($id){
// fetch the price
$productprice = Product::where('id', $id)
->pluck('price')
->firstOrFail();
// slash price into half
$slashPrice = intdiv($productprice,2); // divide by 2
// commence payment
\Stripe\Stripe::setApiKey(config('stripe.sk'));
$session = \Stripe\Checkout\Session::create([
'line_items' => [
[
'price_data' => [
'currency' => 'gbp',
'product_data' => [
'name' => 'tax online payment',
],
'unit_amount' => $slashPrice*100,
],
'quantity' => 1,
],
],
'mode' => 'payment',
'success_url' => route('success'),
'cancel_url' => url('/single-product/'.$id),
]);
$details = ['email' => 'thomsontochi#gmail.com'];
$emailJob = (new AfterPayment($details))
->delay(Carbon::now()
->addMinutes(5));
dispatch($emailJob);
return redirect()->away($session->url);
}
it basically find the product, slash the price and I then charge the slashed price.
I have tried making a job , called LatePaymentCharge and I took the function that handles the charge to the job like so
public function handle()
{
$slashPrice = $this->slashPrice;
//dd('stipe checkout');
\Stripe\Stripe::setApiKey(config('stripe.sk'));
$session = \Stripe\Checkout\Session::create([
'line_items' => [
[
'price_data' => [
'currency' => 'gbp',
'product_data' => [
'name' => 'staxo online payment',
],
'unit_amount' => $slashPrice*100,
],
'quantity' => 1,
],
],
'mode' => 'payment',
'success_url' => route('success'),
// 'cancel_url' => url('/single-product/'.$id),
]);
return redirect()->away($session->url);
}
I am currently having issues on how to pass the slash price value from my checkoutController to my job and im not sure if this is the right way to do it . please help

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',
]);

How to retrieve stripe session id 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

How can I adapt this Stripe checkout code to work with cents as well as dollars?

I am working on this code which is from the Stripe website
URL : https://stripe.com/docs/checkout/integration-builder
this is directly from the website and works fine for me
$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'unit_amount' => 2000,
'product_data' => [
'name' => 'Stubborn Attachments',
'images' => ["https://i.imgur.com/EHyR2nP.png"],
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN . '/success.html',
'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);
However when I change the code to the below to try to get the dollar and cents working ( using either unit_amount as in the example ... or unit_amount_decimal as suggested by this page https://stripe.com/docs/billing/subscriptions/decimal-amounts ) then there are issues which I dont really understand, particularly the error returned when using unit_amount_decimal because it appears that there's no way to do decimal places in Stripe ... which hopefully is not the case.
How would I do this if not as they suggest on that page ... which seems to not work?
same code with 2000.55 instead of 2000
$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'unit_amount_decimal' => 2000.55,
'product_data' => [
'name' => 'Stubborn Attachments',
'images' => ["https://i.imgur.com/EHyR2nP.png"],
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN . '/success.html',
'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);
network tab errors
using unit_amount Invalid integer: 2000.55
using unit_amount_decimal Checkout does not currently support usd prices with more than 2 decimal places in line_items[0]. You passed 2000.55 corresponding to $20.0055, which is not supported.
The second error seems to be suggesting that this is not possible, yet this makes no sense since products are not always even dollar amounts.
Stripe measures amounts in the smallest unit of a currency - for example cents for USD:
'unit_amount' => 2000, //2000 cents or $20
'unit_amount' => 2050, //2050 cents or $20.50
If you need to use a decimal amount of the smallest unit - e.g. 1.5 cents per action then use 'unit_amount_decimal'
'unit_amount_decimal' => 1.5 // 1.5ยข or $0.015 per action

Stripe API and ApplePay "Billing Address Invalid" issue

I am using this code to create Stripe checkout:
$checkout_session = \Stripe\Checkout\Session::create([
'customer_email' => 'my_test_email#gmail.com',
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'unit_amount' => 1000,
'product_data' => [
'name' => 'Balance top-up',
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'client_reference_id' => $clientReferenceId,
'success_url' => 'https://example.com/user-deposit/?result=success',
'cancel_url' => 'https://example.com/user-deposit/?result=failed',
'metadata' => [
'user_id' => '1',
],
]);
Everything works fine, but if I try to pay with ApplePay from the mobile browser - I got such an error (although everything is fine with this card and the billing address is filled in in the Apple account settings):
What should I do? In fact, I don't want to ask my users for the billing address, could I use some stored info (like country and city) to pass to this API request, or perhaps I could somehow force Stripe to do not ask for billing/shipping addresses (I do not sell goods, just for top-up an internal balance in my service)?

Categories