Retrieve Card Info from Charge in Stripe - grails

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

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

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

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.

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"]

Stripe charge and Subscription

I have a rails 4 app that I'm using Stripe checkout. I've followed their tutorial, and my controller looks like:
def create
s = Subscription.new
s.user_id = current_user.id
s.plan_id = params[:plan_id]
s.stripe_token = params[:stripeToken]
s.save
# Amount in cents
#amount = 699
customer = Stripe::Customer.create(
:email => current_user.email,
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => #amount,
:description => 'Sitekite Pro',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
end
I looked at a couple other tutorials, looking for help with creating a subscription with Stripe checkout. Some of them dont have the Stripe::Charge part. Is the Stripe::Charge part only for single charges? How do I sign the user up for a monthly subscription with the same #amount?
The Stripe::Charge is indeed for single charges. The Customer create is what you need, but when you create the customer you specify a plan (plans are defined in your Stripe dashboard). The plan will specify the amount to charge and how often to charge it.
When the charge is actually made, which might be the same day or might be several days later depending on whether your plan provides, say, some free access time... the Stripe service can send the charge to a webhook... which is to say a route in your project for the dedicated use of the Stripe service.
Stripe will post charges (and failures) to your webhook, and you can handle them appropriately (logging the payments and maybe restricting the user if his card becomes expired or a regular payment fails for some other reason)

Resources