forked from uncloud/uncloud
Replace legacy Stripe Charge API by Payment{setup, intent}
This commit is contained in:
parent
47148454f6
commit
bf83b750de
10 changed files with 250 additions and 50 deletions
|
|
@ -10,6 +10,10 @@ import uncloud.secrets
|
|||
# Static stripe configuration used below.
|
||||
CURRENCY = 'chf'
|
||||
|
||||
# README: We use the Payment Intent API as described on
|
||||
# https://stripe.com/docs/payments/save-and-reuse
|
||||
|
||||
# For internal use only.
|
||||
stripe.api_key = uncloud.secrets.STRIPE_KEY
|
||||
|
||||
# Helper (decorator) used to catch errors raised by stripe logic.
|
||||
|
|
@ -82,6 +86,9 @@ class CreditCard():
|
|||
|
||||
# Actual Stripe logic.
|
||||
|
||||
def public_api_key():
|
||||
return uncloud.settings.STRIPE_PUBLIC_KEY
|
||||
|
||||
def get_customer_id_for(user):
|
||||
try:
|
||||
# .get() raise if there is no matching entry.
|
||||
|
|
@ -99,15 +106,17 @@ def get_customer_id_for(user):
|
|||
return None
|
||||
|
||||
@handle_stripe_error
|
||||
def create_card(customer_id, credit_card):
|
||||
return stripe.Customer.create_source(
|
||||
customer_id,
|
||||
card={
|
||||
'number': credit_card.number,
|
||||
'exp_month': credit_card.exp_month,
|
||||
'exp_year': credit_card.exp_year,
|
||||
'cvc': credit_card.cvc
|
||||
})
|
||||
def create_setup_intent(customer_id):
|
||||
return stripe.SetupIntent.create(customer=customer_id)
|
||||
|
||||
@handle_stripe_error
|
||||
def get_setup_intent(setup_intent_id):
|
||||
return stripe.SetupIntent.retrieve(setup_intent_id)
|
||||
|
||||
def get_payment_method(payment_method_id):
|
||||
return stripe.PaymentMethod.retrieve(payment_method_id)
|
||||
|
||||
## Legacy
|
||||
|
||||
@handle_stripe_error
|
||||
def get_card(customer_id, card_id):
|
||||
|
|
@ -116,13 +125,16 @@ def get_card(customer_id, card_id):
|
|||
@handle_stripe_error
|
||||
def charge_customer(amount, customer_id, card_id):
|
||||
# Amount is in CHF but stripes requires smallest possible unit.
|
||||
# See https://stripe.com/docs/api/charges/create
|
||||
# https://stripe.com/docs/api/payment_intents/create#create_payment_intent-amount
|
||||
adjusted_amount = int(amount * 100)
|
||||
return stripe.Charge.create(
|
||||
amount=adjusted_amount,
|
||||
currency=CURRENCY,
|
||||
customer=customer_id,
|
||||
source=card_id)
|
||||
return stripe.PaymentIntent.create(
|
||||
amount=adjusted_amount,
|
||||
currency=CURRENCY,
|
||||
customer=customer_id,
|
||||
payment_method=card_id,
|
||||
off_session=True,
|
||||
confirm=True,
|
||||
)
|
||||
|
||||
@handle_stripe_error
|
||||
def create_customer(name, email):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue