Skip to content

Instantly share code, notes, and snippets.

@bateller
Forked from boucher/gist:1750375
Last active July 1, 2020 23:11
Show Gist options
  • Save bateller/154c6e5d1f6e0e53e527 to your computer and use it in GitHub Desktop.
Save bateller/154c6e5d1f6e0e53e527 to your computer and use it in GitHub Desktop.
Stripe Example (Works with v2 of Stripe-PHP)
<?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>
@juragandotid
Copy link

@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.

@jamminjames
Copy link

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!

@bstonedev
Copy link

@bateller thank you so much for putting this together.

Owe you a 🍺

@bateller
Copy link
Author

@bateller thank you so much for putting this together.

Owe you a 🍺

Thanks! I'm glad it's still providing use to someone. Please note though, it is from 2015, and I haven't really reviewed it since.

@kasoramaster
Copy link

@beteller please can you work me through how to create bank charges in Stripe?

@bateller
Copy link
Author

bateller commented Feb 4, 2020

@kasoramaster You'll need to sign up for an account at Stripe.com. This will include providing them Tax info and banking info (Checking/Savings Account # + Routing #)

Once you've done that you'll need to sign into your account and get your 2 API Keys.

After that you can follow my example above, or go to this repo for more information (note my example and the Stripe-PHP SDK are PHP specific): https://github.com/stripe/stripe-php

If you're not sure how the handshaking works, or need more specific help getting started with Stripe, i'd recommend reading through these docs: https://stripe.com/docs/development

@Barrytron1983
Copy link

Thanks!

@nathanlawson16394
Copy link

nathanlawson16394 commented Jul 1, 2020

Hi @bateller,
Does this still work? I'm not seeing the payments on my Stripe account. If not, does anyone have a newer version of this?

TIA!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment