3

I'm trying to use the stripe to make a small payment form in my application using Laravel. I followed even the tutorials from Laracast. I'm 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 might be the problem? I even took the same code from the github but same error. Everything is same as that of the Laracast. Any idea?

user1012181
  • 8,648
  • 10
  • 64
  • 106

1 Answers1

1

Edit 2: you have used secrete_key in Stripe::setApiKey(Config::get('stripe.secrete_key')) - should it be secret_key ?

Edit 1: the only difference between your billing.js and laracasts' is a space between the two closing brackets at the end of Stripe.createToken:

Stripe.createToken(this.form, $.proxy(this.stripeResponseHandler, this) );

Assuming this doesn't solve it, have you tried created a Stripe customer ahead of processing the charge? I have a similar system (from the same Laracast) and it first creates a customer:

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 be returning the customer id, which you then use in the Stripe_Charge array:

return Stripe_Charge::create(
        [
            'amount' => 1000, // $10
            'currency' => 'usd',
            'customer' => $customer['id'],
            'description' => $data['email'],
            'card'=>$data['token']
        ]);
taekni
  • 1,308
  • 3
  • 12
  • 20