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.
Related
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).
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
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
I am working on an E-Commerce market place called foodsy. I am using stripe connect for the purpose. Connected accounts are created using stripe-connect-omniauth. And foodsy has several customers. An order for an Sku is created in rails controller by
Stripe.api_key = "sk_test_************"
Stripe::Order.create(
{:currency => 'usd',
:items => [
{
:type => 'sku',
:parent => "sku_************"
}
] },
{ :stripe_account => "acct_************" }
)
It creates an order with id or_************ .
The customer who exist on the foodsy platform buys it ,
order=Stripe::Order.retrieve("or_************",stripe_account: "acct_************")
order.pay(customer: "cus_************")
But this code returns an error No such customer: cus_************ (Stripe::InvalidRequestError).
The customer exist as I can see him on dashboard and source attribute is set on stripe. So why is it going wrong ?
The problem is that the customer exists on the platform's account, but not on the connected account you're trying to create the charge on.
You need to share the customer from the platform account to the connected account:
# Create a token from the customer on the platform account
token = Stripe::Token.create(
{:customer => "cus_7QLGXg0dkUYWmK"},
{:stripe_account => "acct_17BTxDCioT3wKMvR"}
)
# Retrieve the order on the connected account and pay it using the token
order = Stripe::Order.retrieve("or_17BUNHCioT3wKMvREWdDBagG",
stripe_account: "acct_17BTxDCioT3wKMvR"
)
order.pay(source: token.id)
This can also happen if you use the wrong APiKey
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"]