#pkdiscgolf_api

1 messages ยท Page 1 of 1 (latest)

tawdry fernBOT
#

๐Ÿ‘‹ Welcome to your new thread!

โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

๐Ÿ”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1286388842731143332

๐Ÿ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

near oxide
#

//create a customer in stripe
$customer = \Stripe\Customer::create(array(
"email" => $email_address,
"source" => $stripe_token
)
);

$api_error='no error'; //just in case

//for idempotency
// error_reporting(0);
$idempotency = preg_replace('/[^a-z\d]/im', '', $_POST['idempotency']);

#

try {
$charge = \Stripe\Charge::create(
["amount" => $entry_cost,
"currency" => "usd",
"description" => $description,
"customer" => $customer->id ], [
"idempotency_key" => $idempotency]);
}catch(\Stripe\Exception\CardException $e) {
// Since it's a decline, \Stripe\Exception\CardException will be caught
echo "error 1<br>";
echo 'Status is:' . $e->getHttpStatus() . '\n';
echo 'Type is:' . $e->getError()->type . '\n';
echo 'Code is:' . $e->getError()->code . '\n';
// param is '' in this case
echo 'Param is:' . $e->getError()->param . '\n';
echo 'Message is:' . $e->getError()->message . '\n';
} catch (\Stripe\Exception\RateLimitException $e) {
// Too many requests made to the API too quickly
echo "error 2<br>";
echo 'Status is:' . $e->getHttpStatus() . '\n';
echo 'Type is:' . $e->getError()->type . '\n';
echo 'Code is:' . $e->getError()->code . '\n';
// param is '' in this case
echo 'Param is:' . $e->getError()->param . '\n';
echo 'Message is:' . $e->getError()->message . '\n';
} catch (\Stripe\Exception\InvalidRequestException $e) {
// Invalid parameters were supplied to Stripe's API
echo "error 3<br>";
echo 'Status is:' . $e->getHttpStatus() . '\n';
echo 'Type is:' . $e->getError()->type . '\n';
echo 'Code is:' . $e->getError()->code . '\n';
// param is '' in this case
echo 'Param is:' . $e->getError()->param . '\n';
echo 'Message is:' . $e->getError()->message . '\n';

#

} catch (\Stripe\Exception\AuthenticationException $e) {
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
echo "error 4<br>";
echo 'Status is:' . $e->getHttpStatus() . '\n';
echo 'Type is:' . $e->getError()->type . '\n';
echo 'Code is:' . $e->getError()->code . '\n';
// param is '' in this case
echo 'Param is:' . $e->getError()->param . '\n';
echo 'Message is:' . $e->getError()->message . '\n';
} catch (\Stripe\Exception\ApiConnectionException $e) {
// Network communication with Stripe failed
echo "error 5<br>";
echo 'Status is:' . $e->getHttpStatus() . '\n';
echo 'Type is:' . $e->getError()->type . '\n';
echo 'Code is:' . $e->getError()->code . '\n';
// param is '' in this case
echo 'Param is:' . $e->getError()->param . '\n';
echo 'Message is:' . $e->getError()->message . '\n';
} catch (\Stripe\Exception\ApiErrorException $e) {
echo "error 6<br>";
// Display a very generic error to the user, and maybe send
// yourself an email
echo 'Status is:' . $e->getHttpStatus() . '\n';
echo 'Type is:' . $e->getError()->type . '\n';
echo 'Code is:' . $e->getError()->code . '\n';
// param is '' in this case
echo 'Param is:' . $e->getError()->param . '\n';
echo 'Message is:' . $e->getError()->message . '\n';
} catch (Exception $e) {
// Something else happened, completely unrelated to Stripe
echo "error 7<br>";
echo 'Status is:' . $e->getHttpStatus() . '\n';
echo 'Type is:' . $e->getError()->type . '\n';
echo 'Code is:' . $e->getError()->code . '\n';
// param is '' in this case
echo 'Param is:' . $e->getError()->param . '\n';
echo 'Message is:' . $e->getError()->message . '\n';
}

dry forge
near oxide
#

Thanks. That is exactly where I copy and pasted my code from that is not working the way I expect it to

#

Instead of entering those error branches, I am having a "fatal error" printed out to my user instead of one of the echos in the appropriate error branch

dry forge
#

In the stack trace which line of the code above triggered that exception?

near oxide
#

Fatal error: Uncaught (Status 402) (Request req_o4oF28Bk741565) Your card has insufficient funds. thrown in /homepages/7/d762472547/htdocs/kpss/vendor/stripe/stripe-php/lib/Exception/ApiErrorException.php on line 38

dry forge
#

That's the error you shared above. What's the full stack trace?

near oxide
#

sorry, I am not familiar with a stack trace in PHP. Do you know where I would find that?

near oxide
dry forge
#

It should be in your PHP error log, in the same place where you got that error detail above.

near oxide
#

That error detail is actually printing out to the user in the webbrowser

dry forge
#

Can you look at the PHP error log on your server for more info?

near oxide
#

I will take a look. I have never seen a php error log on my server before but i will start digging ( i use ionos server)

dry forge
#

Oh, hang on.

#

I think the error is being thrown by your \Stripe\Customer::create call, which isn't inside a try...catch, so the error isn't being caught.

#

Yeah, that's what's happening. You're trying to attach a Token to a Customer and that process is failing.

#

Try moving that code inside your try block.

near oxide
#

Of course. It doesn't seem to me like the error should take place in the customer creation, but that must have been it because the catch block "CardException" fires off when I move the customer creation into the try

#

Thanks ๐Ÿ˜Ž

#

What is the oldest version of PHP that this stripe charge tool will work for???

dry forge
#

I'm not sure, but it's a pretty old version. I will say we do not recommend creating Charges like this directly, or using Tokens. Those approaches are legacy and shouldn't be used if you're building something new.

near oxide
#

I am mainly trying to improve a platform i wrote a few years ago

#

What is the recommended way in PHP to handle a transaction (if possible without a webhook)

#

So, I am noticing now that

$idempotency = preg_replace('/[^a-z\d]/im', '', $_POST['idempotency']);
try {

//create a customer in stripe
$customer = \Stripe\Customer::create(array(
    "email" => $email_address,
    "source" => $stripe_token
    )
);

$charge = \Stripe\Charge::create(1
    ["amount" => $entry_cost,
    "currency" => "usd",
    "description" => $description,
    "customer" => $customer->id ], [
"idempotency_key" => $idempotency]);

}catch(Exception $e) {

Does not catch the idempotency errors when customer is inside the catch block

dry forge
#

Can you give me an example of an idempotency exception you want to catch there?

near oxide
#

If i run the transaction with a bad zip/cvc and get the decline error

Then push "back" on my browser

And then try to resubmit the same form

I am getting the decline error rather then an idempotency error

tawdry fernBOT
dry forge
#

That sounds expected; you're making the same request with the same idempotency key, so you're getting the same result back.

near oxide
#

OK, the biggest issue I am having with this platform is that users are getting the error, and clicking back, and resubmitting the same form. I have a message on screen that tells them to refresh and not resubmit, but they miss it and get caught in this loop where they cant pay

dry forge
#

Idempotency is designed to prevent accidental duplicate submissions, it's not designed to guard against the scenario you're describing. I recommend you alter your page so, if someone goes back to it after it's already been used, you detect that and prevent it from being used again.

near oxide
#

Ok I dont have a vision yet on how I would do that, is that possible with php and js or do I need to be using something more sophisticated

dry forge
#

You can do it with PHP and JS. There are various techniques, but that's outside the scope of what we can help with here as it's not directly related to Stripe.