stripe integration using php

1,801 views
Skip to first unread message

Sunny Bhakta

unread,
Mar 10, 2015, 1:11:57 AM3/10/15
to api-d...@lists.stripe.com
 i am getting Fatal error: Class 'Stripe\Charge' not found  while working with stripe integration in php.

Jim Danz

unread,
Mar 10, 2015, 1:25:46 AM3/10/15
to api-d...@lists.stripe.com
Hi Sunny,

What version of PHP are you using?

Also, make sure to refer to the class as \Stripe\Charge
\Stripe\Charge::create();

Rather than
Stripe\Charge::create();

(If you can share some code, it might help us get to the bottom of
what's going on.)

Cheers,
Jim
> --
> You received this message because you are subscribed to the Google Groups
> "Stripe API Discussion" group.
> To post to this group, send email to api-d...@lists.stripe.com.
> Visit this group at
> http://groups.google.com/a/lists.stripe.com/group/api-discuss/.
>
> To unsubscribe from this group and stop receiving emails from it, send an
> email to api-discuss...@lists.stripe.com.

Sunny Bhakta

unread,
Mar 10, 2015, 1:43:00 AM3/10/15
to api-d...@lists.stripe.com
Hi Jim Dan
I am using php 5.6.3
Here is my code


  <script type="text/javascript" src="https://js.stripe.com/v2/"></script>
 

  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
 
  <script type="text/javascript">
    // This identifies your website in the createToken call below
    Stripe.setPublishableKey('sk_test_123456PgAzxUm3458');

    var stripeResponseHandler = function(status, response) {
      var $form = $('#payment-form');

      if (response.error) {
        // Show the errors on the form
        $form.find('.payment-errors').text(response.error.message);
        $form.find('button').prop('disabled', false);
      } else {
        // 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" />').val(token));
        // and re-submit
        $form.get(0).submit();
      }
    };

    jQuery(function($) {
      $('#payment-form').submit(function(e) {
        var $form = $(this);

        // Disable the submit button to prevent repeated clicks
        $form.find('button').prop('disabled', true);

        Stripe.card.createToken($form, stripeResponseHandler);

        // Prevent the form from submitting with the default action
        return false;
      });
    });
  </script>
</head>
<body>
  <h1>Charge $10 with Stripe</h1>
 
  <form action="" method="POST" id="payment-form">
    <span class="payment-errors"></span>
 
    <div class="form-row">
      <label>
        <span>Card Number</span>
        <input type="text" size="20" data-stripe="number"/>
      </label>
    </div>
 
    <div class="form-row">
      <label>
        <span>CVC</span>
        <input type="text" size="4" data-stripe="cvc"/>
      </label>
    </div>
 
    <div class="form-row">
      <label>
        <span>Expiration (MM/YYYY)</span>
        <input type="text" size="2" data-stripe="exp-month"/>
      </label>
      <span> / </span>
      <input type="text" size="4" data-stripe="exp-year"/>
    </div>
 
    <button type="submit">Submit Payment</button>
  </form>



<?php



include("vendor/stripe/stripe-php/lib/Stripe.php");


stripe\Stripe::setApiKey("pk_test_abcdeflkjojfgkjgf[ljgNFGHJ");

$token = $_POST['stripeToken'];

echo $token;

// Create the charge on Stripe's servers - this will charge the user's card
try {
$charge = \Stripe\Charge::create(array(
  "amount" => 1000, // amount in cents, again
  "currency" => "usd",
  "source" => $token,
  "description" => "payin...@example.com")
);
} catch(\Stripe\Error\Card $e) {
  // The card has been declined
}
?>

Matthew Arkin

unread,
Mar 10, 2015, 2:21:51 AM3/10/15
to api-d...@lists.stripe.com
A couple of things,

Per version 2.1.1 of the Stripe.php library, you no longer include Stripe.php you include init.php, this is documented here: https://github.com/stripe/stripe-php#manual-installation 

stripe\Stripe::setApiKey("pk_test_abcdeflkjojfgkjgf[ljgNFGHJ");

That should be \Stripe\Stripe::setApiKey(’sk_test_efsegegege);
So it needs a \, a capital S and to be your secret key not your publishable key.

   Stripe.setPublishableKey('sk_test_123456PgAzxUm3458’);
Here is where your publishable key would go, not your secret key. I’m a bit surprised you were able to generate a token, I know Stripe Checkout would have yelled at you.

try {
$charge = \Stripe\Charge::create(array(
  "amount" => 1000, // amount in cents, again
  "currency" => "usd",
  "source" => $token,
  "description" => "payin...@example.com")
);
} catch(\Stripe\Error\Card $e) {
  // The card has been declined
}

Here you will also want to catch more exceptions as a Card Error isn’t the only one, and you definitely want to handle them in a way that is more than do nothing. 

--
Matt Arkin
Kollective Solutions

signature.asc

Sunny Bhakta

unread,
Mar 10, 2015, 5:09:43 AM3/10/15
to api-d...@lists.stripe.com
Thank you Matthew Arkin that helped me.
I was including lib/strip.php  that was problem .
Thamks
Reply all
Reply to author
Forward
0 new messages