Reputation: 2776
For some reason when some one charges to sign up to the subscription plan if their card is declined stripe is still creating the customer.
I'm not particularly sure why this is happening since I am creating the customer with the charge, how do I make it so if some one signs up but their card does not go through stripe doesn't make them a customer?
Here is my code
import stripe
@app.route('/stripe/charge', methods=['POST'])
def stripe_charge():
data = json.loads(request.data)
user = data['user']
source = data['stripeToken'] # obtained from Stripe.js
try:
customer = stripe.Customer.create(
source=source,
plan="gold5",
email=email,
)
except stripe.CardError, e:
return jsonify(error="%s" % e)
return jsonify(customer)
Upvotes: 3
Views: 679
Reputation: 17533
Because you create the customer with the plan
parameter, Stripe will attempt to create both the customer and the subscription in one go. Unless the plan has a trial period, Stripe will immediately attempt to bill the customer for the plan's amount (since Stripe's subscriptions are paid upfront), and if the charge fails, then the call will return an error and the customer will not be created.
If you're seeing a different behavior, I recommend you get in touch with Stripe's support to describe the problem. Make sure you include the customer ID and/or request ID in your message.
Upvotes: 2
Reputation: 3506
Are you sure its a CardError
that would be thrown? Try doing a general Exception
, see what error gets thrown when the card is bad. Also their API docs don't actually say an error will be thrown if the card is declined:
If an invoice payment is due and a source is not provided, the call will raise an error. If a non-existent plan or a non-existent or expired coupon is provided, the call will raise an error.
https://stripe.com/docs/api?lang=python#create_customer
Upvotes: 0