How to Integrate Stripe Payment Gateway in Web Application
Integrating payments with Stripe is easy. You would need a quick registration on http://www.stripe.com, after which you can use the test version of the Stripe API until your web app is deployed and the live version afterwards. Stripe is responsible for processing and keeping clients’ credit/debit card data so no information of essence would be stored on your server and you would not have to comply with all the rules that come with storing credit/debit cards.
Create account on https://dashboard.stripe.com/register.
Now login to your account and go to Account Settings -> API Keys.
The easiest way to integrate Stripe is via Checkout, an embedded tool that takes care of building an HTML form, validating user input, and securing your customers’ card data. Using Checkout, sensitive credit card information is sent directly to Stripe, and does not touch your server. Stripe returns to your site a token representation of the card, and this token can then be used in a charge request. Please note to complete the payment process you need to fist get the token form stripe site, once you receive the token use that to charge the user card as per your requirement.
Embedding Checkout in your site and getting the token to charge the customer:
To get started, add the following code to your payment page. It will generate the Stripe payment button and once user fill in the details of his card a token will be returned to your as POST variable which can be used to charge the user.
<form action=”/your-server-side-code-to-charge-user-once-token-is-recieved” method=”POST”>
<script
src=”https://checkout.stripe.com/checkout.js” class=”stripe-button”
data-key=”[[Stripe Public key or Public Test Key]]”
data-amount=”500″ // amount would be multiple of hundred ie without decimal value. In example it will charge $5 from user card
data-name=”Your prduct name”
data-description=”Your prduct description”
data-image=”Path of your website logo” //https://stripe.com/img/documentation/checkout/marketplace.png
data-locale=”auto”
data-zip-code=”true”>
</script>
</form>
Now here’s the whole workflow:
- The customer arrives at your payment page that includes the Checkout code, loaded over HTTPS.
- The customer clicks the payment button (e.g., Pay with Card), completes the payment form, and clicks Pay $5.00 within the Checkout window (or whatever your Checkout pay button is).
- Checkout sends the payment details directly to Stripe from the customer’s browser, assuming the details pass basic validation.
- Stripe returns a token to Checkout, or an error message if the card-network validation fails.
- Checkout takes the returned token and stores it in the page’s primary form—the one surrounding the script tag above, in a hidden element named stripeToken.
- Checkout submits the form to your server.
- Your server uses the posted token to charge the card.
Once you’ve securely collected and tokenized your customer’s credit card using Checkout or Elements, you can charge the card. On your server, grab the Stripe token in the POST parameters submitted by your form. From there, it’s one simple API call to charge the card.
use below code on your form action page to charge the card.
You can use below code to print all the response you get as post method from stripe.
echo “post: <pre>”; print_r($_POST) ; echo “</pre>”;
You’ll need to have the following before going any further:
A PHP >= 5.3.3 environment
The Stripe PHP library https://github.com/stripe/stripe-php/releases
require_once(‘stripe/init.php’);
or
require_once(‘vendor/autoload.php’); as per PHP library
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey(“STRIPE_PRIVATE_KEY or Test Key”);
// Token is created using Stripe.js or Checkout!
// Get the payment token ID submitted by the form:
$token = $_POST[‘stripeToken’];
try {
\Stripe\Stripe::setApiKey(STRIPE_PRIVATE_KEY);
$charge = \Stripe\Charge::create(array(
‘amount’ => $amount, // Amount in cents!
‘currency’ => ‘usd’,
‘source’ => $token,
‘description’ => $email
));
} catch (\Stripe\Error\ApiConnection $e) {
// Network problem, perhaps try again.
} catch (\Stripe\Error\InvalidRequest $e) {
// You screwed up in your programming. Shouldn’t happen!
} catch (\Stripe\Error\Api $e) {
// Stripe’s servers are down!
} catch (\Stripe\Error\Card $e) {
// Card was declined.
}
If payment succesfull you can get the status by using $charge->status variable or get the transaction id using $charge->id.
To print all the variables returned in json format by stripe just use echo $charge and you can get any variable using above method for your reference.
Testing:
For test mode of your newly-created payment form Stripe offers several card numbers that simulate particular responses and errors. You can use few from below list:
4242424242424242 Visa
4000056655665556 Visa (debit)
5555555555554444 Mastercard
5200828282828210 Mastercard (debit)
5105105105105100 Mastercard (prepaid)
378282246310005 American Express
371449635398431 American Express
Though there are other numbers that enable testing invalid address line fails, risk levels, expired card messages, processing errors and much more. Moreover, there are test methods for Bitcoin and international credit cards, like Europe, Asia, Africa, and so on. You can find complete list here. https://stripe.com/docs/testing#cards
To see Checkout in action, click the button, filling in the resulting form with:
Any random, syntactically valid email address (the more random, the better)
One of Stripe’s test card numbers, such as 4242424242424242 given above
Any three-digit CVC code
Any expiration date in the future
Any billing ZIP code, such as 12345
Leave a Reply