I am trying to charge cards via Python.
However, I cannot get it to work.
according to documentation I can use the test stripe cards
https://stripe.com/docs/testing
However when using one of the test cards, i am getting error
stripe.error.InvalidRequestError: Request req_y3tPpc4nHFd7Pi: No such token: 4242424242424242
Please let me know how I can test. I do not have a stripe account with cards or balances.
stripe.api_key = "sk_test_4eC39HqLyjWDarjtT1zdp7dc"
a = stripe.Charge.create(
amount=2000,
currency="usd",
source="4242424242424242",
description="My First Test Charge (created for API docs)",
)
The correct syntax is [1]:
# `source` is obtained with Stripe.js; see https://stripe.com/docs/payments/accept-a-payment-charges#web-create-token
stripe.Charge.create(
amount=2000,
currency="eur",
source="tok_visa", # <-- The tokenized card information not a CC number
description="My First Test Charge (created for API docs)",
)
You need to tokenize the card information using Stripe.js using createToken(). [2]
All that being said, the recommended integration path is using PaymentIntents and not Tokens and Charges. [3]
Hope that helps!
[1] https://stripe.com/docs/api/charges/create?lang=python
[2] https://stripe.com/docs/js/tokens_sources/create_token?type=cardElement
[3] https://stripe.com/docs/payments/accept-a-payment#web
Related
I been trying to figure out how to create a user and store his credit card info in stripe for like a day and I cant get it to work.
I can see the following docs:
https://stripe.com/docs/api/customers/create
const stripe = require('stripe')('sk_test_xxx');
const customer = await stripe.customers.create({
description: 'My First Test Customer (created for API docs at https://www.stripe.com/docs/api)',
});
I understand the secret key aspect of the code.
I dont understand how to create the user object, though. where do I add the user name and info and email and stuff?
is there an example I can see using angular?
maybe the example can be for creating and storing the following information:
first name: juan
last name: cheese
email: jcasas#gmail.com
creditcard: 424242424242
csv: 231
creditcard name: juan cheese
exp: 02/02/25
i literally bough a course on udemy for this but that does not work either :(
If your intention is to save payment details, without an initial payment then you should follow this guide from the Stripe documentation.
The process creates a Customer object, and uses Setup Intents to correctly setup and save the payment details to that customer. Stripe.js and the Payment Element are used to safely collect payment details in a PCI compliant manner and handle any requested 3DS/authentication.
Using Stripe/iOS, the user of the app can pay by credit card or by Apple Pay.
I want, from the app (not the server), to request an email receipt when payment is successful.
For Credit Card payment I do the following:
let paymentIntentParams = STPPaymentIntentParams(clientSecret: <paymentIntentClientSecret>)
paymentIntentParams.paymentMethodParams = paymentMethodParams
if let receiptEmail = self.receiptEmail {
paymentIntentParams.receiptEmail = <email address>
}
let paymentHandler = STPPaymentHandler.shared()
paymentHandler.confirmPayment(withParams: paymentIntentParams,
authenticationContext: authenticationContext) {
…
}
However for Apple Pay payment I don’t know which property to set. In the app, I implement the STPApplePayContextDelegate protocol.
Question: For Apple Pay payment, what and where I need to set the email-address for the receipt? Can you please provide a code snippet?
Thanks
After discussion with Stripe support, it seems that using STPApplePayContextDelegate there is no way to set the receiptEmail property in the payment intent.
The best approach is to change the server and set the receiptEmail at
the server
If you still want to make the change at the app, the workaround is to use the REST API to update the payment intent.
Below is the reply from Stripe support:
The STPApplePayContext does not support this directly -- it needs to be set on the payment intent. The iOS flow for card payments is described in [0] and support receiptEmail in the STPPaymentIntentParams [1], but the Apple Pay flow is different. If not set when you create the payment intent, you would then want to make a call to your server to update the payment intent [2] in the payment flow where you call the completion. Alternately, confirm the payment intent server-side after collecting the Apple Pay payment method client-side [3].
[0] https://stripe.com/docs/payments/accept-a-payment?platform=ios&ui=custom#ios-submit-payment
[1] https://stripe.dev/stripe-ios/docs/Classes/STPPaymentIntentParams.html#/c:#M#Stripe#objc(cs)STPPaymentIntentParams(py)receiptEmail
[2] https://stripe.com/docs/api/payment_intents/update#update_payment_intent-receipt_email
[3] https://stripe.com/docs/apple-pay?platform=ios#client-side
Background:
Having obtained a paymentMethodId from the client with Stripe Elements, I am doing this on the server side:
stripe.paymentMethods.attach(..),
stripe.customers.update(...default_payment_method: ...).
stripe.subscriptions.create(..).
When additional action is required by 3DSecure, the subscription object (returned from create or fetched at a later date) includes a pending_setup_intent, which gives the info needed to resolve the intent (and i've got that working ok).
However if we run the subscriptions.create call when there aren't any payment methods on the Stripe Customer, the subscription object doesn't include a pending_setup_intent or anything else to indicate that the subscription doesn't have a way to pay.
Question:
Short of waiting for webhooks is there some way to have reasonably high confidence that the first payment on the subscription is likely to succeed, ie. there is a card, it's going to accept the payment (and yes, the 3DSecure stuff is sorted)?
Notes:
Although I'm not using trials on the subscription, the billing_cycle_anchor is in the future to provide a free period (basically a trial, but not explicitly making use of the stripe trial concept).
I'm using Stripe API version: 2015-03-24 (upgrading is a lower priority than resolving this)
Further clarification:
At the point we make the call to stripe.subscriptions.create(), the code is not aware of whether the paymentMethod was successfully attached and set as the default (I suppose we could check this, but my hope was that Stripe can handle that for us). For example when using the test card 4000000000000002 the payment method is not attached.
The call to stripe.subscriptions.create is of the form:
{
customer: stripeCustomerId,
billing_cycle_anchor: 1606946400,
cancel_at_period_end: undefined,
items: [{plan: "pro_annual"}],
prorate: false,
expand: ["pending_setup_intent", "pending_setup_intent.latest_attempt"]
}
$stripe = new \Stripe\StripeClient(
'sk_test_123'
);
$stripe->customers->create([
'description' => 'My First Test Customer (created for API docs)',
]);
SHOULD I START WITH CREATING A CUSTOMMER ?? AND THEN SENDING AN INVOICE LATER ON.
You should follow the docs for saving a card during payment or saving a card without payment, depending on your needs. Both include samples for charging the saved card later.
I want to use Stripe to charge cards recurrently every 30 days with amounts that oscilate.
From the docs I got that if there is a possibility that the card requires 3DS we should use Sources so I switched to sources ;)
From the source object stripe.js retrieves I look at three_d_secure param to decide whether to create a source object that requires 3DS or a normal card charging.
The flow:
With JS I get the source object that has three_d_secure set to either optional or required.
When it's set to optional after I retrieve the source with: source = Stripe::Source.retrieve(source_id) it looks like this:
"status": "chargeable",
"type": "card",
"usage": "reusable",
"card":{"exp_month":12,"exp_year":2032,"brand":"Visa",...
I attach it to a customer and charge it. I guess usage: reusable means that I can charge the card again later...
When three_d_secure=='required' I create a new source calling this:
source = Stripe::Source.create({
amount: amount,
currency: currency,
type: 'three_d_secure',
three_d_secure: {
card: source_id, #src_xcvxcvxcvc
},
redirect: {
return_url: return_url
},
})
I redirect the user to the URL Stripe provides, user enters his 3DS PIN and gets back to my return_url. When Stripe redirects the user back to my return_url I retrieve the source again and get something like this:
"status": "chargeable",
"type": "three_d_secure",
"usage": "single_use",
"three_d_secure": {"card":"src_1B1JzQHopXUl9h9Iwk05JV1z","authenticated":true,"customer":null}
I would expect that after passing the 3DS the source becomes reusable and chargeable until the date of expiry or so :|
My questions are:
1 Why is the 3DS source single_use? Is this like this only in sanbox environment or with the card I am using to test?
2 Can a 3DS protected card be charged again at all?
3 What's the correct approach to attach to customer sources (3DS or normal) that can be charged again and again?
Thank you!
Because it is a source payment token, not a source card token. It expires on a due date or when is consumed. You can use reusable token to create single_use tokens. reusable one represents a card source token
Yes if a 3ds is optional or not_supported, no if required. If required then every payement needs to fulfill a 3ds.
Steps:
Create an src_card_token for a card or use saved one (reusable)
Create an customer object with an src from src_card_token
Create an src_payment_token for a customer using one of his saved cards (as token)
fullfil a 3ds redirect process if required.
create a charge