1

I'm trying to implement stripe on my laravel eCommerce project, after calling a charge on my checkout page that show me error: Must provide source or customer. This error is due to the lack of stripeToken creation, and i'm really, really stack on this.

checkout.js:

Stripe.setPublishableKey('pk_test_51H3t5vAzmeZmoL9jwK8YCeFMxvDnuC6FsV0Rg0XvMejoZziTbS6SMQlAIkOjLj68lGqkE57Yu46jF51zg6HrnyBK00Yg8Iyy45');

var $form = $('#checkout-form');

$form.submit(function(event) {

    event.preventDefault();
    $('#charge-error').addClass('hidden');
    $form.find('button').prop('disabled', true);

    Stripe.card.createToken({
      number: $('#card-number').val(),
      cvc: $('#card-cvc').val(),
      exp_month: $('#card-expiry-month').val(),
      exp_year: $('#card-expiry-year').val(),
      name: $('#card-name').val()
    }, stripeResponseHandler);
    return false;
});

function stripeResponseHandler(status, response) {
    if (response.error) {
        $('#charge-error').removeClass('hidden');
        $('#charge-error').text(response.error.message);
        $form.find('button').prop('disalbed', false);
    } else {// Token was created!

    // Get the token ID:
        var token = response.id;
        console.log(token);
        // Insert the token into the form so it gets submitted to the server:
        $form.append($('<input type="hidden" name="stripeToken" />').val(token));
        console.log($form)
        

        // Submit the form:
        $form.get(0).submit();
    }
}

controller.php:

public function postCheckout(Request $request)
    {
        if (!Session::has('cart')) {
            return redirect()->route('shoppingCart');
        }
        $oldCart = Session::get('cart');
        $cart = new Cart($oldCart);



        Stripe::setApiKey('sk_test_?????');

        try {

            Charge::create(array(
            "amount" => $cart->totalPrice,
            "currency" => "usd",
            "source" => $request->input('stripeToken'), // obtained with Stripe.js
            "description" => "My First Test Charge"
        ));

       } catch (\Exeption $e) {
           return redirect()->route('checkout')->with('error', $e->getMessage());
       }

       Session::forget('cart');
       return redirect()->route('home')->with('succes', 'Successfully purchaised!');
    }

}
Gazmir Sulcaj
  • 91
  • 3
  • 10
  • Does this answer your question? [Stripe Api error: Must provide source or customer](https://stackoverflow.com/questions/62917841/stripe-api-error-must-provide-source-or-customer) – floatingLomas Jul 21 '20 at 07:08
  • I know this answer before asking, and nothing .... – Gazmir Sulcaj Jul 21 '20 at 08:49
  • 1
    `$request->input('stripeToken')` contains nothing. That's the only way you can get this error. You'll have to debug this on your end; it's not something someone else will be able to help with I don't think. I would suggest you log the contents of $request->input('stripeToken') and also $request to see what's in there; that will likely help. – floatingLomas Jul 22 '20 at 05:51

0 Answers0