Invalid Request Error - Basic Stripe

2,163 views
Skip to first unread message

Dylan Hobbs

unread,
May 31, 2015, 12:59:34 AM5/31/15
to api-d...@lists.stripe.com
HI everyone,
 
I'm just setting up a basic account with the checkout feature.

I'm currently getting this error and do not know how to fix it:

Uncaught exception 'Stripe\Error\InvalidRequest' with message 'Must provide source or customer.'

I've installed composer, have all of the stripe files etc..

Here is my code;

<?php

// This charge script is getting JWT and send it to stripe as one-time chage
// Author: @greenido
// Date: 28.2.2015
// Get the latest version from:
// https://github.com/stripe/stripe-php/releases
require_once('vendor/autoload.php');
require_once("vendor/stripe/stripe-php/lib/Stripe.php");

// 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("sk_test_tNNbXLsv3VsnAR2FKGs4fY3l");

// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];

// 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" => "Example charge")
);
} catch(\Stripe\Error\Card $e) {
  // The card has been declined
}

?>

<form action="" method="POST">
  <script
    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    data-key="pk_test_5zhgzJy1wb4cRwDOBnmeOPoi"
    data-amount="2000"
    data-name="Demo Site"
    data-description="2 widgets ($20.00)"
    data-image="/128x128.png">
  </script>



</form>


If anyone can help out then thanks in advance! :)

Matthew Arkin

unread,
May 31, 2015, 1:05:46 AM5/31/15
to api-d...@lists.stripe.com
Hi Shawn,

This sort of tech support issues are best handled via sup...@stripe.com or #stripe on freenode :)


Also never share your secret key (sk_test_tNNbXLsv3VsnAR2FKGs4fY3l) in public as lots of bad stuff can happen (I'd recommend going to https://dashboard.stripe.com/account/apikeys and clicking the little refresh button next to the key to void it and get a new one).

A couple issues with your code,

Right now you're attempting to create a charge every-time that page loads, whether or not they clicked "Pay With Card", you probably only want to run that top code in response to POST requests.

Another thing is you're only catching CardErrors, not any of the other potential errors, and you're not really handling those errors which is something you're going to want to do in production.

And 

require_once('vendor/autoload.php');
require_once("vendor/stripe/stripe-php/lib/Stripe.php");

If you're doing autoload, you don't need to manually include Stripe.php, and loading Stripe.php directly is going to cause issues anyways since the manual (non Composer) method is to include init.php


Hope that helps put you on the right path, 

Matt


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

Reply all
Reply to author
Forward
0 new messages