Stripe Payment Gateway In PHP Demo Step By Step

By : PHPErrorCode July 16, 2017 2660 Views php

In this post i am going to give you example of how to integrate Stripe payment gateway in php.

I can state this more clearly: you should be thinking about using Stripe for every e-commerce project you do. Period. Exclamation mark. Although Stripe has only been around since 2011, it’s quickly made a name for itself as an excellent payment solution that is dead-simple to use. I first heard about Stripe in the winter of 2015, when a client wanted me to use Stripe for his e-commerce project. And I’ve since used it on a couple more projects.

Although Stripe is easy to use, I believe that more documentation, specifically a series that puts together the whole Stripe workflow, is warranted. In this first part of an exhaustive series, I’ll introduce Stripe for those not yet using this great service. In subsequent posts, you’ll learn everything you can ever possibly need to know to use Stipe today. Well, almost everything.

Step 1: Create account on Stripe

You need to create an account on https://dashboard.stripe.com/register to generate the Test Secret Key and test Publishable Key.

After register with stripe, you will get an email from the stripe to verify your email address. Click on the email link to verify your email and then login in stripe dashboard.

Step 2: Now login to your account and go to API menu

click on the API option to get your Test Secret Key and test Publishable Key.  ->https://dashboard.stripe.com/account/apikeys

stripe api key seting

Step 3: Create index.php file

To get started, add the following code to your index.php page.

<form action="payment.php" method="post">
    <script
    	src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    	data-key="pk_test_OUmIWpBCVZySKKeoLKTyqVkA"
    	data-amount="1000"
    	data-locale="us-en"
    	data-name="Product Plan Demo"
    	data-description="Product description ($10)"
    	data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
    	data-locale="auto">
    </script>
</form>

Insert above code where you want to add Payment Button in the web page. Now, Open page in browser you will see Payment button where you embedded the stripe script. The stripe checkout script will automatically creates the HTML form for the payment. It also validates the user input and generates unique token for the payment.

stripe paymet getway

note: The key thing to notice is the data-key attribute we added on the script tag which identifies your website when communicating with Stripe. Note that we've pre-filled the example with your test publishable API key.

stripe output

Step 4: Open Payment input box

In By clicking on the button, Payment Popup will show having Payment inputs such as Email Address, Card Number, Expiry Date, CVV and Remember me option. By clicking on Payment button Checkout sends all details to stripe server directly. After validating checkout details stripe submits form and returns token or error message if the checkout fails to the URL you have mentioned in form action.

Add data from below for testing

Testing Card Number : 4242424242424242
CVV Number : 1234
Card Expiry Date : Use any future date
Charge User is Card

Once Checkout process completes and collects checkout details, Last step is to charge the card. It will use Stripe Library to charge user’s card.

Step 5: Create payment.php file

Now add create stripe object and charge user’s card by using Create method.

<?php
require_once('vendor/autoload.php');

if(!empty($_POST['stripeToken']))
{
	\Stripe\Stripe::setApiKey("sk_test_GNSGTSx1tgAY8kmC5IVmXwzu");

	// Token submitted by the plan form:
	$stripetoken = $_POST['stripeToken'];

	// Charge the user card:
	$charge = \Stripe\Charge::create(array(
	  "amount" => 1000,
	  "currency" => "usd",
	  "description" => "Product Plan Demo",
	  "source" => $stripetoken,
	  "metadata" => array("purchase_order_id" => "SKA123456") // Custom parameter
	));
	$chargeJson = json_decode($charge);

	if($chargeJson['amount_refunded'] == 0) 
	{
		echo "Transaction completed successfully";
	}
	else
	{
		echo "Transaction has been failed";
	}
}
else
{
	echo "Transaction has been failed Token Emp";
}
?>

The code checks whether the information is in correct format and throws an exception if there is a problem. If everything is in order, the code forwards the payment request. If all goes through, a success message is returned.

Step 6: Create composer.json file

open composer.json file and add the following code in it after require:


// composer.json
{
	"require": {
		"stripe/stripe-php": "4.*" // Version
	}
}

First of All, install Stripe Library if you have not installed yet. To install library, Add stripe/stripe-php in composer.json and update the composer by running composer update command.

composer update

Click this link in reading more about Composer

Alternatively, you can download the source directly.

Note: most impotant scripe link

Remember to replace the test key with your live key in production. You can get all your keys from your account page or find out about live mode and testing

testing mode to live mode Document ->https://stripe.com/docs/dashboard#livemode-and-testing
Read more about Checkout's reference. ->https://stripe.com/docs/checkout

 

In this tutorial, I demonstrated how to set up and integrate stripe API for credit card payment processing, using Stripe as an example. You could also setup two-step checkout process using Stripe. On the first page, verify the credit card information and on the second, make the payment. If you have any questions about the code or would like to contribute to the discussion, please leave a comment below.

Thanks.....