Strip payment error (you must provide either card or customer id) using Laravel
I am trying to use stripe to create a small checkout form in my application using Laravel. I followed the Laracast tutorials. I am getting this error
Stripe_InvalidRequestError
You must supply either a card or a customer id
...
//billing.js
(function(){
var StripeBilling = {
init: function(){
this.form=$('#billing-form');
this.submitButton = this.form.find('input[type=submit]');
this.submitButtonValue = this.submitButton.val();
var stripeKey=$('meta[name="publishable-key"]').attr('content');
Stripe.setPublishableKey(stripeKey);
this.bindEvents();
},
bindEvents: function(){
this.form.on('submit', $.proxy(this.sendToken, this));
},
sendToken: function(event){
this.submitButton.val('One Moment').prop('disabled', true);
Stripe.createToken(this.form, $.proxy(this.stripeResponseHandler, this) );
event.preventDefault();
},
stripeResponseHandler: function(status, response){
if(response.error){
this.form.find('.payment-errors').show().text(response.error.message);
return this.submitButton.prop('disabled', false).val(this.submitButtonValue);
}
$('<div>', {
type: 'hidden',
name: 'stripe-token',
value: response.id
}).appendTo(this.form);
this.form[0].submit();
}
};
StripeBilling.init();
})();
...
//StripeBilling.php
<?php
namespace Acme\Billing;
use Stripe;
use Stripe_Charge;
use Config;
class StripeBilling implements BillingInterface {
public function __construct()
{
Stripe::setApiKey(Config::get('stripe.secrete_key'));
}
public function charge(array $data)
{
try
{
return Stripe_Charge::create([
'amount' => 1000, // $10
'currency' => 'usd',
'description' => $data['email'],
'card'=>$data['token']
]);
}
catch(Stripe_CardError $e)
{
dd('Card was declined');
}
}
}
What could be the problem? I even took the same code from github but with the same error. Everything is the same as in Larakasta. Any idea?
source to share
Edit 2: which you used secrete_key
in Stripe::setApiKey(Config::get('stripe.secrete_key'))
- should it be secret_key
?
Edit 1: the only difference between your billing and larakast is the space between the two closing brackets at the end Stripe.createToken
:
Stripe.createToken(this.form, $.proxy(this.stripeResponseHandler, this) );
Assuming that doesn't solve it, have you tried building a Stripe client before handling the charge? I have a similar system (from the same Laracast) and it creates the client first:
public function createStripeCustomer($email, $token)
{
$key = Config::get('stripe.secret');
Stripe::setApiKey($key);
$customer = Stripe::customers()->create([
'card' => $token,
'email' => $email,
'description' => 'desc'
]);
// error checking
return $customer['id'];
You want to return the customer ID, which is then used in an array Stripe_Charge
:
return Stripe_Charge::create(
[
'amount' => 1000, // $10
'currency' => 'usd',
'customer' => $customer['id'],
'description' => $data['email'],
'card'=>$data['token']
]);
source to share