Stripe Payment Error: Must provide source or customer - ruby-on-rails

I've been trying to implement a payment method for a shopping cart using card information attached to the user via a subscription model. And while the latter works fine, whenever I attempt to create a charge for the cart, I get the "Must provide source or error."
Here's some code to get started. Let me know if you need more.
checkouts_controller.rb
def create
customer = Stripe::Customer.create(
source: params[:stripeToken],
email: 'paying.user#example.com',
)
charge = Stripe::Charge.create(
amount: current_cart.total,
currency: 'usd',
description: "Order ##{current_cart.id}"
)
end
Error logs from Stripe Post Body
{
"amount": "9500",
"currency": "usd",
"description": "Order #1"
}
And from the response body
{
"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’m using the standard 4242 4242 4242 4242 card that is actually recorded and attached to my user via my subscription model. Card info like the brand, last four digits, expiration month and year all appear in my view when I go to add a different card. The stripe_id is also present when I check in the console.
I have checked stripe documentation and copied the snippet from their create action from https://stripe.com/docs/checkout/rails but this only trigged the error “Cannot charge a customer with no active card” despite the fact that this card is currently being used for a subscription.
So where did I goof at?

You need to make sure that the customer you create is passed in on the charge create call:
charge = Stripe::Charge.create(
customer: customer,
amount: current_cart.total,
currency: 'usd',
description: "Order ##{current_cart.id}"
)
Or you can use the source directly as:
charge = Stripe::Charge.create(
source: params[:stripeToken],
amount: current_cart.total,
currency: 'usd',
description: "Order ##{current_cart.id}"
)
Both options and examples can be found here

moving from comment to an answer
Hi #Grasshopper27, looks like the customer you created is not being created with the source. You should look into Dashboard -> Logs to look at the request body for the /customer creation request and see if the token is being passed in.
I would also like to note that this Rails doc is a bit outdated, you should try out updated Checkout to really simplify your integration: stripe.com/docs/payments/checkout

Related

STRIPE : This customer has no attached payment source or default payment method. RUBY ON RAILS

I've got some issues, i'm trying to implement subscription with stripe > it works when there is for exemple 3 items in my order > it create a subscription for the 3 items.
The problem is that if the customer wants to stop sub only for ONE element, i dont know how to handle this ...
So i was wondering to create a subscription for each element, this is my code
customer = Stripe::Customer.create
#order.line_items.each do |line_item|
product = Stripe::Product.create(
{
name: line_item.product.name,
metadata: {
product_id: line_item.product.id,
line_item_id: line_item.id
}
}
)
price = Stripe::Price.create(
{
product: product.id,
unit_amount: line_item.product.price_cents,
currency: 'eur',
recurring: {
interval: 'month'
}
}
)
Stripe::Subscription.create({
customer: customer.id,
items: [
{price: price.id, quantity: line_item.quantity}
]
})
but i got this error This customer has no attached payment source or default payment method.
and i dont know how to attach it, even with documentation..
any help please ? thank you
As said in comments; To fix the error in the title:
You need to first have the "payment method", if you haven't already, create one:
Maybe using Stripe.js and it's elements API.
Which nowadays has "payment" element, allowing users to choose their payment-method.
And supports Setup-Intent's "client_secret" (beside Payment-Intent's), submittable using confirmSetup(...) method.
Then (using Stripe API):
Attach said "payment method" to the Customer:
(Optionally) set it as their default for invoices (with invoice_settings.default_payment_method).
And, while creating the subscription, pass the customer (that which you atteched said "payment method" to).

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.
Update2: I see that a customer is being created when I send a charge.
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?
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.

Rails and Stripe. No such customer found on dev server

I'm using Rails and Stripe to charge payments on our site. The live payment site is still working, but when I try to put a test payment through I get an error No such customer: cus_C0HNm69CCk3abH. This is on the master branch, and this used to work as of Thursday and no code has been changed. Here's the relevant section of the code.
stripe_customer = Stripe::Customer.create(
email: trip_params[:customer_email],
metadata: {
customer_name: trip_params[:customer_name]
},
source: params[:stripeToken]
)
token = Stripe::Token.create(
card: {
number: trip_params[:card_number],
exp_month: trip_params[:date][:month],
exp_year: trip_params[:date][:year],
cvc: trip_params[:card_cvv]
}
)
stripe_customer.sources.create(source: token.id)
The last line is where it fails. Any ideas?
If you are using the "test" Stripe keys, Stripe will look for it in their "test" database. Since your customer is a "live" customer, it wouldn't be found in the "test" database. Of course, you don't want to put a "test" payment through a "live" customer. I'd create a "test" account for that customer (different tokens, of course) and try it that way.

Validate card details on stripe

I have a functionality where I am storing users credit card details in the database. Here is my same code
require "stripe"
Stripe.api_key = "stripe_api_key"
# create token for generating customer id
token =
Stripe::Token.create(
card: {
number: "xxxxxxxxxxxx",
exp_month: xx,
exp_year: xxxx,
cvc: "xxx"
},
)
# Create a Customer:
customer = Stripe::Customer.create(
email: "paying.user#example.com",
source: token.id,
)
user.stripe_customer_id = customer.id
user.save
But before storing this customer id I need to validate given card i.e whether given card is exits and it is not expired.
So I have few doubts
Is there any stripe api that can provide me given details.
What is minimum charge we can make within Stripe.
Is there any better way to achieve this functionality. (Ex. charge certain amount to user and then refund it immediately)
Thanks

Stripe: Cannot charge a customer that has no active card

I am trying to create a customer and charge that customer using Rails and Stripe. The customer is getting created in Stripe, but I keep getting the error Cannot charge a customer that has no active card when trying to do the charge.
I am following a stripe tutorial as a Rails beginner, so I'm not sure where to go from here. Any help would be greatly appreciated.
Rails code:
def process_payment
customer = Stripe::Customer.create email: email,
card: card_token
Stripe::Charge.create customer: customer.id,
amount: level.price*100,
description: level.name,
card: card_token,
currency: 'usd'
end
def create
#registration = Registration.new registration_params.merge(email: stripe_params["stripeEmail"],
card_token: stripe_params["stripeToken"])
raise "Please, check registration errors" unless #registration.valid?
#registration.process_payment
#registration.save
redirect_to #registration, notice: 'Registration was successfully created.'
end
Try creating the charge without the card_token. You shouldn't need to specify the card a second time, since the card is attached to the customer, which you're specifying though the customer parameter.
customer = Stripe::Customer.create email: email,
source: card_token
Stripe::Charge.create customer: customer.id,
amount: level.price*100,
description: level.name,
currency: 'usd'
Also, pass the card_token through the source param, rather than the deprecated card param. Here is a blog post that talks about this change: https://stripe.com/blog/unifying-payment-types-in-the-api
More info: https://stripe.com/docs/api/ruby#create_customer
and: https://stripe.com/docs/api#create_charge
What you are trying to do is charge a customer once you have created a payment method for her. However, you cannot use the Charge endpoint to do that if you have not set any subscriptions because this method is made for recurring payments linked to subscriptions.
In order to be able to charge your client, set a subscription. This will activate the card. You'll then be able to debit the card.
You're trying to access the parameters, so you must use the params keyword.
Instead of card_token, use params["card_token"]

Resources