| <?php | |
| // Edited boucher/gist:1750375 to work with stripe-php v2 | |
| require 'stripe-php/init.php'; | |
| if ($_POST) { | |
| \Stripe\Stripe::setApiKey("YOUR-API-KEY"); | |
| $error = ''; | |
| $success = ''; | |
| try { | |
| if (!isset($_POST['stripeToken'])) | |
| throw new Exception("The Stripe Token was not generated correctly"); | |
| $charge = \Stripe\Charge::create(array( | |
| "amount" => 1000, // amount in cents, again | |
| "currency" => "usd", | |
| "source" => $_POST['stripeToken'], | |
| "description" => "Example charge") | |
| ); | |
| if ($charge->status == 'succeeded') { | |
| $success = 'Your payment was sucessful.'; | |
| } else { | |
| $success = 'Your payment failed.'; | |
| } | |
| } catch(\Stripe\Error\Card $e) { | |
| // The card has been declined | |
| $error = $e->getMessage(); | |
| } | |
| } | |
| ?> | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> | |
| <title>Stripe Getting Started Form</title> | |
| <script type="text/javascript" src="https://js.stripe.com/v2/"></script> | |
| <!-- jQuery is used only for this example; it isn't required to use Stripe --> | |
| <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> | |
| <script type="text/javascript"> | |
| // this identifies your website in the createToken call below | |
| Stripe.setPublishableKey('YOUR-PUBLISHABLE-API-KEY'); | |
| function stripeResponseHandler(status, response) { | |
| if (response.error) { | |
| // re-enable the submit button | |
| $('.submit-button').removeAttr("disabled"); | |
| // show the errors on the form | |
| $(".payment-errors").html(response.error.message); | |
| } else { | |
| var form$ = $("#payment-form"); | |
| // token contains id, last4, and card type | |
| var token = response['id']; | |
| // insert the token into the form so it gets submitted to the server | |
| form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />"); | |
| // and submit | |
| form$.get(0).submit(); | |
| } | |
| } | |
| $(document).ready(function() { | |
| $("#payment-form").submit(function(event) { | |
| // disable the submit button to prevent repeated clicks | |
| $('.submit-button').attr("disabled", "disabled"); | |
| // createToken returns immediately - the supplied callback submits the form if there are no errors | |
| Stripe.createToken({ | |
| number: $('.card-number').val(), | |
| cvc: $('.card-cvc').val(), | |
| exp_month: $('.card-expiry-month').val(), | |
| exp_year: $('.card-expiry-year').val() | |
| }, stripeResponseHandler); | |
| return false; // submit from callback | |
| }); | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| <h1>Charge $10 with Stripe</h1> | |
| <!-- to display errors returned by createToken --> | |
| <span class="payment-errors"><?= $error ?></span> | |
| <span class="payment-success"><?= $success ?></span> | |
| <form action="" method="POST" id="payment-form"> | |
| <div class="form-row"> | |
| <label>Card Number</label> | |
| <input type="text" size="16" autocomplete="off" class="card-number" maxlength="16" /> | |
| </div> | |
| <div class="form-row"> | |
| <label>CVC</label> | |
| <input type="text" size="4" autocomplete="off" class="card-cvc" maxlength="4" /> | |
| </div> | |
| <div class="form-row"> | |
| <label>Expiration (MM/YYYY)</label> | |
| <input type="text" size="2" class="card-expiry-month" maxlength="2"/> | |
| <span> / </span> | |
| <input type="text" size="4" class="card-expiry-year" maxlength="4"/> | |
| </div> | |
| <button type="submit" class="submit-button">Submit Payment</button> | |
| </form> | |
| </body> | |
| </html> |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
mhosks
commented
Sep 6, 2015
|
I added maxlength attributes and reduced the size of the CC field to 16 if you're interested in merging those changes: |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
Thanks, I updated the code. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
LuceroGera
May 31, 2016
Hello my friend,
Excuse me what do you mean in the line 3? What is it the content of that file?
Thanks
LuceroGera
commented
May 31, 2016
|
Hello my friend, |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
katronix
Jul 1, 2016
LuceroGera I do see there is an init.php 1 folder above lib, I'm going to assume that is what he is referring to (if you download a new copy of the API you should see it)
katronix
commented
Jul 1, 2016
|
LuceroGera I do see there is an init.php 1 folder above lib, I'm going to assume that is what he is referring to (if you download a new copy of the API you should see it) |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
bateller
Aug 11, 2016
@LuceroGera Sorry I just saw this.
@katronix is correct. This is the link to the init.php in the stripe-php folder. More specifically this: https://github.com/stripe/stripe-php/blob/master/init.php
This only works with the latest Stripe-PHP. If you wish to use the old v1 Stripe-PHP (unadvised) use this gist: https://gist.github.com/boucher/1750375
|
@LuceroGera Sorry I just saw this. @katronix is correct. This is the link to the init.php in the stripe-php folder. More specifically this: https://github.com/stripe/stripe-php/blob/master/init.php This only works with the latest Stripe-PHP. If you wish to use the old v1 Stripe-PHP (unadvised) use this gist: https://gist.github.com/boucher/1750375 |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
juragandotid
Mar 14, 2018
@bateller I had a problem
I'm try make request from xampp with the code but It showing any error response
You must supply either a card, customer, PII data, bank account, or account legal entity to create a token. If you're making this request with a library, be sure to pass all of the required parameters for creating a token. If you're making this request manually, be sure your POST parameters begin with the token type. For example, a PII token would require pii[personal_id_number], while an account token would require a parameter beginning with account[legal_entity]. See the API reference for more information: https://stripe.com/docs/api#token_object
Can u help me?
Thank you.
juragandotid
commented
Mar 14, 2018
|
@bateller I had a problem I'm try make request from xampp with the code but It showing any error response You must supply either a card, customer, PII data, bank account, or account legal entity to create a token. If you're making this request with a library, be sure to pass all of the required parameters for creating a token. If you're making this request manually, be sure your POST parameters begin with the token type. For example, a PII token would require Can u help me? Thank you. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
jamminjames
Aug 30, 2018
I am testing with Stripe, and am getting the "Unexpected error communicating with Stripe" error. I believe curl and TLS are set up correctly, so I don't know what the problem is. I am using my test account API keys from Stripe, and this form.
I ran tls_test.php on the server, and got:
OS: Linux
uname: Linux dcn-colo-251-95.dcn.davis.ca.us 3.10.0-327.13.1.el7.x86_64 #1 SMP Thu Mar 31 16:04:38 UTC 2016 x86_64
PHP version: 7.0.27
curl version: 7.29.0
SSL version: NSS/3.34
SSL version number: 0
OPENSSL_VERSION_NUMBER: 100020bf
TLS test (default): TLS 1.2
TLS test (TLS_v1): TLS 1.2
TLS test (TLS_v1_2): TLS 1.2
When I use the line:
\Stripe\Stripe::setVerifySslCerts(false);
...Stripe reports on the dashboard that a successful payment was made, even though on my test form I get a "Your payment failed" error after submitting. So, there's something about the SSL cert it doesn't like, and I am looking into that. It should be fine, as shown above, and I checked the expiration, which is not until March 2019, so it's odd.
Thanks for any help!
jamminjames
commented
Aug 30, 2018
|
I am testing with Stripe, and am getting the "Unexpected error communicating with Stripe" error. I believe curl and TLS are set up correctly, so I don't know what the problem is. I am using my test account API keys from Stripe, and this form. I ran tls_test.php on the server, and got: OS: Linux When I use the line: Thanks for any help! |
I added maxlength attributes and reduced the size of the CC field to 16 if you're interested in merging those changes:
https://gist.github.com/mhosks/aae90e060c7a60c87368