#pkdiscgolf-errors-PHP
1 messages ยท Page 1 of 1 (latest)
Hi there!
It is definitely recommended that you handle errors yourself server-side and display a readable message to your client.
Can you tell me more about the issue you are having in doing this?
You also said: "2nd issue - I have users double clicking the payment button which is triggering the idempotency error often. Is anyone able to walk me through the process of disabling the pay button after it is clicked"
Sure... can you share you client-side code?
Are you using vanilla JS or React?
The issue is - i dont know how. I found the error "string" and am adding it to my error log table in the databse. But is there an easy way to detect which message it is - so that i can rewrite it and pass it on to my userse
I do not believe i am using javascript at all. I am using PHP
Yeah we don't have a list of errors we can share. You are expected to test and handle these gracefully. Really you shouldn't be hitting most of these errors at all โ for instance you should disable the button to avoid the duplicate idempotency error. Other than that, you have a list of errors you do want to surface, and then you also have a generic default error that you can throw if the error that you hit is unexpected.
Ah okay, can you share your client-side code that you are rendering with PHP?
How do i test and handle that error gracefully? Sorry i am a pretty newb programmer. Never had a job doing it. But i wrote this app and i am doing 10000 plus transactions/year
That is my code for the charge page
I know that i have to disable the pay button after it was clicked to stop the idempotency error from happening
How - for example - would i gracefully handle "Your card has insufficient funds."
(i do not know how to disable the button though. just that if i do, it will stop the idempotency error)
The main problem i am having - is people are clicking multiple times- and instead of being redirected to the success screen - it shows them the error screen.
Sometimes the payment works when they get to the error screen
Sometimes - they double click. It triggers the idempotency error. But the original payment doesnt go through ("insuffecient funds") but they are at the error screen and it is frustrating for the user because they dont know what they are doing wrong (they ignore the message which i put which tells them to not double click)
Okay let's slow down and talk through one thing at a time.
Let's first talk about disabling the button
Let me take a look at your code
One sec
Thanks. I appreciate your help. I agree that preventing the idempotency error in the first place is a logical step
Signup Page (redirects to the other code)
Sorry, server is busy.
๐ hopping in to help
You're just trying to disable your button after it's been pressed, right?
One of a few issues i need to fix
well let's start there
Sounds good
So what have you tried so far?
Last time i tried was over a year ago. The button would disable when i click it. But the disable would also prevent the payment from being sent in the first place
i unfortanetely do not have my code from then though
There's a number of different ways to do this - it's really up to you to find a solution you like. As a quick example, here's some suggestions on how to disable the button after submission: https://stackoverflow.com/questions/19194356/how-to-disable-a-submit-button-after-submission-in-php
Yea....i dont succeed when i try random stuff on stack overflow
Maybe you can help me with my other issue
I am getting some plaayers who are having errors. I would like to try and improve the view that the user sees when they get an error.
Currently i am logging $e->getMessage() after an exception is thrown
Is there a way i can get perhaps on error code of some sort? I wanted to "re-write" the message because the wording of the error message is sometimes confusing (There is currently another in-progress request using this Idempotent Key (that probably means you submitted twice, and the other request is still going through): 2017647348628ab121814b7209676637. Please try again later.)
Disabling the button is exactly what you want to do to prevent this error though
i agree - but it is not the only error that my users seeing
i looked at the page you sent me
it has code on it
but i have no idea where i would put that code
It's jquery - you'd put it with the rest of your front-end code
<input type="submit" name="SUBMIT" value="Submit Form"
onclick="this.disabled='disabled';this.form.submit();" />
It has this on it as well. But the thing is a <button> not a <input>
Like i put it above the <button> that the user clicks?
How do i indicate to the code that i am not using html, but jquery
looks like all of the answers on this page are based on an <input> componenet rather than a <button>
Let's back up here for a minute - do you have resources available to you for learning these concepts? While we're happy to help guide people through the usage of Stripe, your questions right now are going back to some frontend development fundamentals which are important to get right.
I have a bachelor in software development (pretty worthless it seems)
And i have 10000+ visitors to my app every month
So i have some basic skills here. But I have never worked in a formal environement. And do not have anyone to turn to
I'd really suggest hiring someone at this point - they'll be able to give you more specific guidance that's tailored to your integration. Like I can send you suggestions on different options you can try, but to actually apply the solutions you need to tailor them to work with your own code
that would be nice
But i am going to have to figure this out on my own
So how about this.
Can you help me figure out how to send a refund from my software? I have to go into my stripe dashboard and refund the person manually. And then go into my database and remove the record of payment.
How do i create a way to "refund" a person from my software rather then logigng into stripe
That I can definitely help you with - you'll want to look at https://stripe.com/docs/api/refunds/create.
So when your customer wants a refund you'll need to tie it back to the original charge or payment intent ID. Then using that ID you can create the refund
is the payment intent id different from the transaction id?
What is the format of the transaction IDs you're looking at?
i store a transaction id that looks like this "ch_3L2eEqDDHpo65KvC1rMwxV8g" in my database after a successful payment
Awesome, some that's the Charge ID (which you can use as an alternative to a Payment Intent ID when you create a refund https://stripe.com/docs/api/refunds/create#create_refund-charge)
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Is this a simlilar process to making a payemnt? I am not sure what "curl" means. I assume that i have some code already that connects to stripes api, and i have to copy/paste/modify that code to send a refund request instead of a payment?
curl is just a command line tool for making requests (this is not what you want) - the link I sent you also has example of how to make these requests in PHP.
like if you're already making requests to create charges, you'd do something similar to create refunds
under create a refund? I am not seeing the php example code
You'd click "curl" and select php in the dropdown
$stripe->refunds->create([
'charge' => 'ch_1Iy68KDDHpo65KvCzn54hgaZ',
]);
So it looks like its only a couple lines of code. I just set the api key, and then send the request with the charge id.
Also a heads up I deleted the message w/ your code you sent over earlier since you included your secret keys in the code you pasted earlier (so you should roll those keys immediately).
yup
oh crap - i thought i had removed those before copy/paste
Ok i think this refund bit is in my wheelhouse
Also i notice that I am logging the e.message() but there is also and e.decline_code() which is probably what i need in order to report to the user their error appropriately.
So i am going to try and get these 2 things fixed. And then i will double back and work on this disable button
I assume there is no need for idempotency in sending a refund. If i try to refund something that is already refunded (double click refund) what is going to happen
?
If you try and refund something that's already been refunded then it'll fail
I need to head out now, but if need anything else @raw cave is here to help
๐
Thanks. i am working on my code for refunds right now i will let you know when i get some questions
$stripe = new \Stripe\StripeClient(
'sk_test_51Hyq5oDDHpo65KvC9M37BoZSqxeilWPSxHW81yHkCogIqKvT3k82JjhqAzl4QQ0KxxTGiEe9iub6Re6cgmb3xzsw00iIbRGVG2'
);
The docs indicate that i need 2 lines. This one above, and the next one contains the chargeid
But, when i put my own API key in (instead of that example key), I get an error
Parse error: syntax error, unexpected 'MYKEY' (T_STRING), expecting ')' in /homepages/7/d762472547/htdocs/kpss/tools_send_refund.php on line 7
Looks like i had a typo
This is the actual error below
Fatal error: Uncaught Error: Class 'Stripe\StripeClient' not found in /homepages/7/d762472547/htdocs/kpss/tools_send_refund.php:6 Stack trace: #0 {main} thrown in /homepages/7/d762472547/htdocs/kpss/tools_send_refund.php on line 6
did you install stripe-php client correctly?
I assume so - it works when my user pays. Do i need to install a seperate client for refunds
You don't need a separate client but the error seems to suggest that your tools_send_refund.php isn't able to find the StripeClient
If you've installed it correctly, it should work.
Hey good point
I didnt have my "import" require_nce statement included.
So i am connecting now i believe
Great!
Alright so I was able to succesfully send a refund
Awesome! glad we could help ๐
I would like to optimize this a little better though
Essentially - I do not have a way of telling if the it was successful or not
You can check the dashboard
I know...but really i would like my software to alert me as to whether it worked or didnt, so i do not have to log in
$stripe = new \Stripe\StripeClient(
'MYKEY'
);
//sanitize post array
$POST = filter_var_array($_POST,FILTER_SANITIZE_STRING);
//set variables from array
$rdid = trim($POST['rdidref']);
//get charge_id from DB
$sql_rounds = "SELECT transaction_id FROM round_details WHERE round_detail_id = " . $rdid;
$result_rounds = $conn->query($sql_rounds);
if ($result_rounds->num_rows > 0) {
// output new row for each record:
while($row_rounds = $result_rounds->fetch_assoc()) {
$charge_id = $row_rounds["transaction_id"];
}
}
error_reporting(0);
try {
$stripe->refunds->create([
'charge' => $charge_id,
]);
}catch(Exception $e) {
echo "catch";
}
I would hope that if it didnt work (due to already refunded perhaps) it would go to the catch block
But i am not seeing the catch block hit at all
Yes it would if an exception was thrown on failure (as in it was a coding issue).
An alternative and more reliable way would be to implement a webhook to receive real time updates from Stripe
https://stripe.com/docs/webhooks
And you would listen for charge.refunded or charge.refund.updated event for its actual status
https://stripe.com/docs/api/events/types#event_types-charge.refunded
https://stripe.com/docs/api/events/types#event_types-charge.refund.updated
Interesting. That sounds pretty complicated. I am going to have to double back to that once i have my Idempotency/double click prooblem fixed
๐
so, if i understand right, the issue is that the refund is asynchronous, so there is no response sent from the server when i send the refund?
you should see a response object as shown in the doc here
https://stripe.com/docs/api/refunds/create
You'd probably want to store it in a variable and then access it
OK, i will try to log that response in my database after calling the refund
I am retiring for the day. I will be back into this tomorrow. Thanks for helping
NP! ๐ Good luck