Validate card details on stripe - ruby-on-rails

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

Related

Stripe Payment Error: Must provide source or customer

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

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.

Braintree—how do I get the last_4 credit card details from a transaction OR subscription

From the result of a Transaction.sale() OR a Subscription.create(), how do I access the credit card details in the payment?
I have the following methods:
def bt_make_customer(donation, nonce)
result = Braintree::Customer.create(
first_name: donation.user.first_name,
last_name: donation.user.last_name,
email: donation.user.email,
payment_method_nonce: nonce
)
end
def bt_make_payment(donation, customer, nonce)
if donation.type == "ReoccurringDonation"
result = Braintree::Subscription.create(
payment_method_token: customer.payment_methods[0].token,
price: donation.amount,
plan_id: "pay-monthly"
)
elsif donation.type == "SingleDonation"
result = Braintree::Transaction.sale(
:amount => donation.amount,
:payment_method_nonce => nonce,
:options => {:submit_for_settlement => true}
)
end
end
As you can see, the program accepts one-time donations, or monthly subscriptions. When either is made, I want to get the credit card details such as last_4 to display in a custom receipt.
You can call
result.last_4
for getting The last 4 digits of the credit card number.
For more help visit here
To access the credit card fields, you need to type
result.transaction.credit_card_details.{field you want}
You can pause the program with byebug after the transaction, and type result.inspect in your console to see what fields the result contains.

Retrieve Card Info from Charge in Stripe

I am triggering a charge using:
charge = Charge.create(chargeParams)
In the chargeParams I am passing the card ID associated with stripe but in the charge object the card object is null. How do I fetch card info. from a charge in stripe. I also tried Charge.retrieve(id) but it also returned null for card.
You can create charge with stored card as:
token = params[:stripeToken]
# create a Customer
customer = Stripe::Customer.create(
card: token,
description: 'description for payinguser#example.com',
email: 'payinguser#example.com'
)
# charge the Customer instead of the card
Stripe::Charge.create(
amount: 1000, # in cents
currency: 'usd',
customer: customer.id
)
For further reference

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