When trying to implement Stripe charging on the server. I followed the documentation code below…
@anvil.server.callable
def add_card(token, email):
user = anvil.users.get_user()
if user['stripe_id'] is None:
customer = anvil.stripe.new_customer(email, token)
user['stripe_id'] = customer['id']
else:
customer = anvil.stripe.get_customer(user['stripe_id'])
customer.add_token(token)
I think the last code line - customer.add_token(token)
is in the wrong place. Should it not be…
@anvil.server.callable
def add_card(token, email):
user = anvil.users.get_user()
if user['stripe_id'] is None:
customer = anvil.stripe.new_customer(email, token)
user['stripe_id'] = customer['id']
else:
customer = anvil.stripe.get_customer(user['stripe_id'])
customer.add_token(token)
as the adding of token is only applicable in the case of an existing Stripe customer found. For a new Stripe customer is created with a token added.