I'm trying to implement a stripe payment, and I know that stripe requires you to set the amount twice. Once at checkout and another on the server side.
I'm using web2py as my framework.
So my question is how do I make them match?
I made the server side dynamic via JS, but I'm struggling to the server side to have the same amount.
# Set your secret key: remember to change this to your live secret key in production
# See your keys here https://dashboard.stripe.com/account/apikeys
stripe.api_key = "sk_test_BQokikJOvBiI2HlWgH4olfQ2"
# Get the credit card details submitted by the form
token = request.POST['stripeToken']
# Create the charge on Stripe's servers - this will charge the user's card
try:
charge = stripe.Charge.create(
amount=1000, # how to make this portion match the check out amount
currency="usd",
source=token,
description="Example charge"
)
except stripe.error.CardError, e:
# The card has been declined
pass
is there away to the get more information?
The data-amount and data-currency Checkout configuration options are only used for display purposes. They're irrelevant to the actual charge's amount and currency.
To let your user specify the amount themselves, you could add an amount field to your form, that will be sent along with the "normal" Checkout parameters (stripeToken, stripeEmail, etc.).
Here's a simple JSFiddle to illustrate: https://jsfiddle.net/ywain/g2ufa8xr/
Server-side, all you'd need to do is get the amount from the POST parameters:
try:
charge = stripe.Charge.create(
amount=request.POST['amount']
# ...
Of course, in a real-world scenario, you should validate the amount field, both client-side and server-side. At the very least, you want to make sure that it's:
a strictly positive numerical value
above the minimum charge amount
below a reasonable maximum for your application
Related
I am listening to Stripe's invoice.payment_failed webhook with my web app and I would like to get the default_payment_method from each invoice but for some reason it always returns nothing but an empty array!
When I query for a Stripe invoice on the command line and expand on the default_payment_method like this...
Stripe::Invoice.retrieve('in_3K6dIY2KgYRkshw2LAzya63P', :expand => "default_payment_method")
...I also get empty arrays. This surprises me because all my Stripe customers do have a default payment method associated with them.
What am I missing here?
Thanks for any help.
There are three independent places a default payment method can be set. From more specific to less specific they go :
invoice.default_payment_method (which you are looking at)
subscription.default_payment_method
customer.invoice_settings.default_payment_method
Stripe charges the most specific one if it's set. When reading from the API, those values don't inherit from the level above, they can all be set individually, if they are not set explicitly then they are null. So that's why you see it as null on the Invoice level.
Instead you likely want to look at the Subscription object or the Customer object(and can leverage the expand feature for that), depending on how you built your integration and which one it sets.
Overall though, you probably actually want the PaymentMethod used in the invoice payment though? That would be from the last_payment_error.
inv = Stripe::Invoice.retrieve({
id: 'in_1K8iiKJoUivz182DMzSkuBgp',
expand: ["customer.invoice_settings.default_payment_method",
"subscription.default_payment_method",
"payment_intent"]
}
)
print("invoice : #{inv.default_payment_method} \n")
print("subscription : #{inv.subscription.default_payment_method} \n")
print("customer : #{inv.customer.invoice_settings.default_payment_method} \n")
print("failed charge : #{inv.payment_intent.last_payment_error.payment_method} \n")
I am using Stripe Checkout to process orders on my site and then save the order once its complete using the webhook Checkout.Session.Complete . It works all well and good but I would like to store the last 4 digits used to process the order. If I send a receipt using Stripe it lists that info so I know its possible, I just prefer to store it and send my own receipt to customize and assist customers if an order is off. The checkout session object doesnt list the last 4 digits for some reason so whats the best way to get that info based on what the session object returns???
The answer will depend on the type of Checkout Session you are working with.
If you are working with Checkout Sessions where mode:payment, then you should expand payment_intent.payment_method when you retrieve the Session. Then, you can check payment_intent.payment_method.card.last4 to get the last 4 digits.
If you are working with Checkout Sessions where mode:subscription, then you should expand subscription.default_payment_method when you retrieve the Session. Then, you can check subscription.default_payment_method.card.last4.
If you're not already familiar with expansion, you can read more about it here (https://stripe.com/docs/expand).
Let's say I'm getting payouts information https://stripe.com/docs/api/payouts/list
I want to get the destination info, so if we take the example from the docs:
I want the details from that destination, I don't mean the specific details like the exact credit card number, but the info when going on the page itself, this one:
It's not working listing the banks accounts https://stripe.com/docs/api/customer_bank_accounts/list , I'm getting zero results when list them all
I'm using Golang library but I think this solution can be solved by others programming languages
I'm also using expandable fields but I get blank results on bank_account and card details
what is interesting however, is when debugging the page, they make request to v1/payouts/{id} with expand[]: balance_transaction and the returned data contains the bank information, but when I do it with their API, that info is not returned back
The Payout destination is expandable, meaning you can have it replaced with the full object by sending you request with expand[]=destination or for a list command data.destination.
My app uses subscriptions with Stripe.
I want to create a standard "account" page, which will list the customer's current card information (like "MasterCard" and last 4 of card number), and give the customer the option of updating that information.
I'm stuck on the first piece--getting back the current card information. To do this, I need the current card_id. I have tried the "listSources" method, but this returns blank data where the card info is supposed to be. What do I need to do to get that card info?
Here is what I've tried:
(I'm using Node, and running this server side)
The closest method I have found is here:
var stripe = require('stripe')(STRIPE_TOKEN);
stripe.customers.listSources(
CUSTOMER_ID,
{object: 'bank_account', limit: 3},
function(err, cards) {
// asynchronously called
}
);
This returns information (there's no error), but the docs say this method should return a data array of the cards that includes the card id for each. In testing, the data array keeps coming back empty.
I am testing with a customer id that has a valid subscription and a card that I can see on my Stripe dashboard.
Why is the data array coming back empty?
Note: there's also a retrieve source method, which should give back card details, but this method requires you have the id of the card you want info on, and that's what I am not able to get right now.
Converting this to an answer...
Stripe has recently rolled out PaymentMethods, which replace (and are separate from) the older Tokens and Sources API.
OP's issue is that their integration creates PaymentMethod objects, which won't show up in the sources list, but can instead be accessed via stripe.paymentMethods.list.
I am using stripe latest version in production.
When I try to fetch transactions:
Stripe::Transfer.all.first.transactions
I fount, there is no transactions method for latest Stripe::Transfer
undefined method `transactions' for #<Stripe::Transfer
Stripe API is upgrades:
2014-08-04
The transactions, summary, and other_transfers properties in automatic transfer responses have been removed in favor of the balance history endpoint (/v1/balance/history), which can be called with the transfer id (using the ?transfer= parameter) to filter transactions by transfer.
So How we fetch transactions from Stripe transfer ?
But its working fine with Stripe old version.
You have a couple of options depending on what you want. If you a certain level of detail for charges you can fetch all the charges associated with a transfer which will fetch only that:
var options = {};
stripe.transfers.listTransactions(id, options);
If, on the other hand, you would like something more akin to the old summary object then I would get the balances associated with the transfer:
var options = { transfer: id };
stripe.balance.listTransactions(options);
The balances returned will contain a variety of balance types - the first one refers to the actual transfer as a whole and can be ignored and the rest is a combo of charges, refunds, adjustments, etc including the fees for each.
In both the above cases id refers to the transfer ID. Note that you will need to loop through these to fetch all items - this can be done in a couple of ways as listed under Pagination. In the above cases, I check if the result contains the has_more: true field and, if so, loop through again using the starting_after: X addecd to the options object where X is the ID of the last charge/balance returned on previous call.