Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe API Invalid Request: Must provide source or customer

I'm working on the Stripe API and building a custom checkout function - My error is specifically telling me I must provide a source or customer. This is my controller for creating charges:

I thought I was successfully creating a source and/or customer with the following code (I also included the post logs on my stripe dashboard)

 Stripe::Charge.create(
    :amount => @amount,
    :currency => 'usd',
    :source => params[:stripeToken],
)
Stripe::Customer.create(
    :email => params[:stripeEmail],
    :source => params[:stripeToken]
  )
  "error": {
    "code": "parameter_missing",
    "doc_url": "https://stripe.com/docs/error-codes/parameter-missing",
    "message": "Must provide source or customer.",
    "type": "invalid_request_error"
  }
}

I did go through the docs but I guess I'm still a little lost

Thanks for any assistance!

Update: here is the return of my API POST requests. my api requests Update2: I see that a customer is being created when I send a charge. My events tab

Update 3: Source property is set to the stripeToken parameter, representing the payment method provided. The token is automatically created by Checkout. - this seems possibly connected to my issue - maybe it's not correctly posting?

like image 826
Jason Harder Avatar asked Apr 20 '18 19:04

Jason Harder


People also ask

What is 402 error code stripe?

Many platforms use error code 402 in the spirit in which it was developed: to signal errors with payments. Two examples that we mentioned before are Shopify and Stripe, which return these error codes when there's a problem with a payment, such as a card being declined.

What does invalid account mean on stripe?

invalid_account. The card, or account the card is connected to, is invalid. The customer needs to contact their card issuer to check that the card is working correctly. invalid_amount. The payment amount is invalid, or exceeds the amount that's allowed.

What is strip error?

Test strip errors often occurs when there is a problem with the test strip, or when the environment in which the test strip is used is below or above the operating range.


1 Answers

So it did turn out to be a token request - since I was using a test card for test purposes I imagine I had to pass a test token to ensure that the test card would work.

I believe the Rails Params I was using (: source => params[:stripeToken] ) are fine for production but not when checking against given cards. In case someone comes across this as I did (and of course this probably isn't the first time it was asked on SO)

When using the Stripe API you see there is a token tab beside the test card numbers - I assumed those were optional or "for something else" for some reason. THEY ARE NOT.

You'll want to make sure the token matches whatever test card you plan on using (I think)

My Stripe controller now looks like this

Stripe::Charge.create({
    :amount => @amount,
    :currency => 'usd',
    :source => 'tok_visa',  #params[:stripeToken] might be for in production I think this is for testing.,
    :description => 'Your Donation to Victoria University',
    :statement_descriptor => 'Victoria University'

    # it seems test tokens must be set as string.
  })

Decided to leave my comments in there - because why not?

P.S You'll need different token types for different card payment types. If you switch cards - switch tokens as well !!!! - the tokens are tabbed beside the test card numbers.

like image 100
Jason Harder Avatar answered Oct 25 '22 20:10

Jason Harder