def _get_or_create_customer():
assert HAVE_PAYMENTS, "Payments are not enabled"
assert g.auth.user != PUBLIC
db_customer = Customer.query.filter_by(id=g.auth.user).one_or_none()
if db_customer is None:
try:
# Insert a placeholder with no Stripe ID just to lock the row.
db_customer = Customer(id=g.auth.user)
db.session.add(db_customer)
db.session.flush()
except IntegrityError:
# Someone else just created it, so look it up.
db.session.rollback()
db_customer = Customer.query.filter_by(id=g.auth.user).one()
else:
# Create a new customer.
plan = PaymentPlan.FREE.value
customer = stripe.Customer.create(
email=g.auth.email,
description=g.auth.user,
)
stripe.Subscription.create(
customer=customer.id,
plan=plan,
)
db_customer.stripe_customer_id = customer.id
db.session.commit()
customer = stripe.Customer.retrieve(db_customer.stripe_customer_id)
assert customer.subscriptions.total_count == 1
return customer
评论列表
文章目录