#james-checkout-php

1 messages · Page 1 of 1 (latest)

manic narwhalBOT
#

Hello james_35976, we'll be with you shortly! Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.
james_35976, 3 days ago, 18 messages

fluid remnant
#

@novel river the 500 is likely from your own code really in PHP. You're getting an API error (expected) and not handling that error in the code you shared

gleaming sequoia
#

i just use the sample code , not anything else

fluid remnant
#

First carefully read https://stripe.com/docs/error-handling to understand how to handle all expected errors returned by our API and display a useful error message instead of displaying a crash

gleaming sequoia
#

require_once('stripe/init.php');

$stripe = new \Stripe\StripeClient("sk_test_4eC39HqLyjWDarjtT1zdp7dc");

$stripe->checkout->sessions->create([
'success_url' => 'https://example.com/success',
'line_items' => [
[
'price' => 'price_H5ggYwtDq4fbrJ',
'quantity' => 2,
],
],
'mode' => 'payment',
]);

fluid remnant
#

Second: you're mixing up ids and API keys here. The API key is the default one when not logged into your account but the Price id is from a different Stripe account,

gleaming sequoia
fluid remnant
#

Ultimately: clean up the code, add error handling, use your real API key with a real Price id from your own account

#

james-checkout-php

gleaming sequoia
#

OK. I see .i thought it should return a error message.

fluid remnant
#

it does

gleaming sequoia
#

For the check out session . I need to create a price Id first . Right ?

#

but Now it return 500 HTTP Error instead a error msg

fluid remnant
#

Our API returns a clear error message, just your code is not handling any error so it completely crashes like any code would

gleaming sequoia
#

OK. Let me try see if it can get the error msg

#

I do not think so. The problem is coding issue . Error code is HTTP500

#

http 500 internal server error

#

means it can not find the coding or coding error

fluid remnant
#

I'm certain of it. This is 100% your bug!

gleaming sequoia
#

I am using the PHP SDK

#

and using the sample code copied from your website

fluid remnant
#

You have to catch errors. You write the PHP code here, you're the developer controlling the full behaviour. It's on you to write safe code that handles errors which we raise as exceptions. I shared the exact documentation page to read and follow https://stripe.com/docs/error-handling

gleaming sequoia
#

I can create the price code successed.

fluid remnant
#

no idea what those words mean I'm sorry

gleaming sequoia
#

function example_function($args) {
try {
$stripe->paymentIntents->create($args);
error_log("No error.");
} catch(\Stripe\Exception\CardException $e) {
error_log("A payment error occurred: {$e->getError()->message}");
} catch (\Stripe\Exception\InvalidRequestException $e) {
error_log("An invalid request occurred.");
} catch (Exception $e) {
error_log("Another problem occurred, maybe unrelated to Stripe.");
}
}

#

I tried above , the page is blank

fluid remnant
#

that's a completely different big of code creating a PaymentIntent which has nothing to do with what you were doing

gleaming sequoia
#

require 'vendor/autoload.php';

#

I can not even find this file in SDK

fluid remnant
#

I'm sorry you're going way too fast without taking the time to understand. You're the developer here, you're accepting payments from real customers, you have to be really careful about what you are doing. You're just throwing random lines of PHP code at me right now
Please take a step back, breathe and think about the code

gleaming sequoia
#

so I am thinking if downloaded wrong SDK

#

Now I am using require_once('stripe/init.php');

#

can not find vendor/autoload.php file

#

in the SDK

#

did you check this image ?

fluid remnant
#

It's just a random picture of your IDE on your own

#

but yes that's stripe-php as far as I can tell

gleaming sequoia
#

$stripe->checkout->sessions->create does not exist

#

how can it works ?

fluid remnant
#

It depends how you installed the librar, whether you use Composer or not. It's all documented. Most experienced PHP developers would be using Composer for example but it is not a requirement

gleaming sequoia
#

I do not use composer

#

i have loaded it manually with cod below

#

require_once('stripe/init.php');

fluid remnant
#

Yep I'm just explaining the difference between the two. Once. you load that file it loads the library as expected.

gleaming sequoia
#

$stripe = new \Stripe\StripeClient('sk_test_BQokikJOvBiI2HlWgH4olfQ2');
$customer = $stripe->customers->create([
'description' => 'example customer',
'email' => 'email@example.com',
'payment_method' => 'pm_card_visa',
]);
echo $customer;

#

This code works perfect

#

but not the check out session one

fluid remnant
#

I mean same answer as the first line I gave you. You are rushing through things and copy-pasting tons of stuff. You are not really listening yet to what I am saying unfortunately.

To create a Checkout Session you have to

  1. Use the right API key (that's the wrong one)
  2. Use a Price id from your own account (you were using the wrong one)
  3. Properly use try/catch to catch an exception, which is needed for most modern PHP code overall
gleaming sequoia
#

even there has a error ,but should not use code 500

#

use code 500 means the coding issue in my opinion.

fluid remnant
#

I'm sorry, I will pause for now. You are not reading anything I say and are just asserting this is our fault over and over. I'm sorry but you're the developer here and you have to understand the 500 is caused by your own bug

gleaming sequoia
#

no problem

fluid remnant
#

Once you have read carefully what I explained multiple times, read the doc about error handling, and fixed the 3 points I have explained above, your code will work. If it still doesn't, please explain exactly what you did and shared your real code with the real try/catch, all in one message clearly laid out and I should be able to point you towards the issue

gleaming sequoia
#

I understand fix the 3 lines you mentioned may fix this error, but this is not the way I handle the erorr(samply Throw a 500 error ,instead of the reason cause the error.)

fluid remnant
#

We (Stripe) are not throwing a 500 error at all .We cleanly returned a detailed error with a message, error code, dashboard url and all the required information
You (the developer) are the one ignoring the clear error and not handling errors at all and crashing the server right now. This 500 is 100% from you not Stripe.

gleaming sequoia
#

Again I did not write any coding now , just run the sample codes.

fluid remnant
#

Sure but you are the developer here, you copy/pasted that code to make it work. It is a really basic and simple PHP example. It does no error handling

gleaming sequoia
#

even the API key is wrong ,and price id not exist . I am expect receive a error msg , not crashed.

#

anyway I will try fix it with your instruction.

fluid remnant
#

All you have to do is add a try catch around that code example and you'd get a clear error instead of a crash. The crash is your own doing
If we put a try/catch around every single example, most junior developers would be completely lost

gleaming sequoia
#

ok.

manic narwhalBOT
gleaming sequoia
#

I have successed created the session.

#

The problem was caused by wrong key and price ID

rigid dust
#

Glad to hear you were able to figure it out!

gleaming sequoia
#

BUT I still think it should not handle the error by developer .

#

what I except i got a blank page so I know I need to find out the error

#

If your sdk throw 500 error. I thought my coding has error

rigid dust
#

Like koopajah explained before - this was an error coming from your own server because it wasn't handling potential API errors correctly. It was not our SDK that was throwing the 500 error, our API did throw an error which you, as the developer are expected to handle

gleaming sequoia
#

you guys offer a sdk to make customer easy to use your api , not make the confusions

rigid dust
#

Erorr handling is something that you have to deal with in almost any API you integrate with

gleaming sequoia
#

I have always used the API with my own coding, not use the SDK

#

I know i need to handle the error , but your file through the 500 error makes me confustion.

#

I do not think every code I need to use Try catch...

#

what I expect is when I use wrong KEY and PriceId , I got a blank data/page

#

anyway .the problem is fixed. thank you for your help

#

one more questin, can I see the sessions created on your website ?

rigid dust
#

We don't have a dashboard UI for Checkout Sessions (you'd have to use the API to list them)

gleaming sequoia
#

ok

#

How many price/product I can create?

#

is that requires with session payment?

#

because sometime we need to change the product.

rigid dust
#

There's no limit to the number of prices/products

gleaming sequoia
#

price

rigid dust
#

You can create as many as you want

gleaming sequoia
#

it measn I can create one time use product.

#

only for that session payment.

#

becase we may offer different customer different price.

#

we for this api for phone call order.

#

We added the product in our winform app and create a payment link , and text the link to the customer.

#

then we need to collect the customer's shipping address after he /she successed paid and add to our system.

rigid dust
#

Yes, you can create a one-time price for just that Checkout sessino

gleaming sequoia
#

is there a notification available after paid ?

rigid dust
#

You'll want the checkout.session.completed event (which gets sent when checkout is complete)

gleaming sequoia
#

yes

#

got it thank you

#

make me a lot easier to finish the app