forked from uncloud/uncloud
- Added PricingPlan Model
- Implement a complete cycle for buying a Matrix Chat Host - Refactor the Payement cycle and stripe related methods
This commit is contained in:
parent
e205d8d07c
commit
b7aa1c6971
81 changed files with 5079 additions and 810 deletions
|
|
@ -9,6 +9,8 @@ from django.contrib.auth import get_user_model
|
|||
|
||||
from .models import StripeCustomer, StripeCreditCard
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
CURRENCY = 'chf'
|
||||
|
||||
stripe.api_key = settings.STRIPE_KEY
|
||||
|
|
@ -77,9 +79,24 @@ def create_setup_intent(customer_id):
|
|||
def get_setup_intent(setup_intent_id):
|
||||
return stripe.SetupIntent.retrieve(setup_intent_id)
|
||||
|
||||
@handle_stripe_error
|
||||
def get_payment_method(payment_method_id):
|
||||
return stripe.PaymentMethod.retrieve(payment_method_id)
|
||||
|
||||
@handle_stripe_error
|
||||
def get_card_from_payment(user, payment_method_id):
|
||||
payment_method = stripe.PaymentMethod.retrieve(payment_method_id)
|
||||
if payment_method:
|
||||
if 'card' in payment_method:
|
||||
sync_cards_for_user(user)
|
||||
return payment_method['card']
|
||||
return False
|
||||
|
||||
|
||||
@handle_stripe_error
|
||||
def attach_payment_method(payment_method_id, customer_id):
|
||||
return stripe.PaymentMethod.attach(payment_method_id, customer=customer_id)
|
||||
|
||||
@handle_stripe_error
|
||||
def create_customer(name, email):
|
||||
return stripe.Customer.create(name=name, email=email)
|
||||
|
|
@ -142,7 +159,7 @@ def sync_cards_for_user(user):
|
|||
)
|
||||
|
||||
@handle_stripe_error
|
||||
def charge_customer(user, amount, currency='CHF'):
|
||||
def charge_customer(user, amount, currency='CHF', card=False):
|
||||
# Amount is in CHF but stripes requires smallest possible unit.
|
||||
# https://stripe.com/docs/api/payment_intents/create#create_payment_intent-amount
|
||||
# FIXME: might need to be adjusted for other currencies
|
||||
|
|
@ -153,14 +170,14 @@ def charge_customer(user, amount, currency='CHF'):
|
|||
return Exception("Programming error: unsupported currency")
|
||||
|
||||
try:
|
||||
card = StripeCreditCard.objects.get(owner=user,
|
||||
card = card or StripeCreditCard.objects.get(owner=user,
|
||||
active=True)
|
||||
|
||||
except StripeCreditCard.DoesNotExist:
|
||||
raise ValidationError("No active credit card - cannot create payment")
|
||||
|
||||
customer_id = get_customer_id_for(user)
|
||||
|
||||
|
||||
return stripe.PaymentIntent.create(
|
||||
amount=adjusted_amount,
|
||||
currency=currency,
|
||||
|
|
@ -169,3 +186,64 @@ def charge_customer(user, amount, currency='CHF'):
|
|||
off_session=True,
|
||||
confirm=True,
|
||||
)
|
||||
|
||||
@handle_stripe_error
|
||||
def get_payment_intent(user, amount, currency='CHF', card=False):
|
||||
# Amount is in CHF but stripes requires smallest possible unit.
|
||||
# https://stripe.com/docs/api/payment_intents/create#create_payment_intent-amount
|
||||
# FIXME: might need to be adjusted for other currencies
|
||||
|
||||
if currency == 'CHF':
|
||||
adjusted_amount = int(amount * 100)
|
||||
else:
|
||||
return Exception("Programming error: unsupported currency")
|
||||
|
||||
try:
|
||||
card = card or StripeCreditCard.objects.get(owner=user,
|
||||
active=True)
|
||||
|
||||
except StripeCreditCard.DoesNotExist:
|
||||
raise ValidationError("No active credit card - cannot create payment")
|
||||
|
||||
customer_id = get_customer_id_for(user)
|
||||
|
||||
return stripe.PaymentIntent.create(
|
||||
amount=adjusted_amount,
|
||||
currency=currency,
|
||||
customer=customer_id,
|
||||
payment_method=card.card_id,
|
||||
setup_future_usage='off_session',
|
||||
confirm=False,
|
||||
)
|
||||
|
||||
@handle_stripe_error
|
||||
def get_or_create_tax_id_for_user(stripe_customer_id, vat_number,
|
||||
type="eu_vat", country=""):
|
||||
def compare_vat_numbers(vat1, vat2):
|
||||
_vat1 = vat1.replace(" ", "").replace(".", "").replace("-","")
|
||||
_vat2 = vat2.replace(" ", "").replace(".", "").replace("-","")
|
||||
return True if _vat1 == _vat2 else False
|
||||
|
||||
tax_ids_list = stripe.Customer.list_tax_ids(
|
||||
stripe_customer_id,
|
||||
limit=100,
|
||||
)
|
||||
for tax_id_obj in tax_ids_list.data:
|
||||
if compare_vat_numbers(tax_id_obj.value, vat_number):
|
||||
return tax_id_obj
|
||||
else:
|
||||
logger.debug(
|
||||
"{val1} is not equal to {val2} or {con1} not same as "
|
||||
"{con2}".format(val1=tax_id_obj.value, val2=vat_number,
|
||||
con1=tax_id_obj.country.lower(),
|
||||
con2=country.lower().strip()))
|
||||
logger.debug(
|
||||
"tax id obj does not exist for {val}. Creating a new one".format(
|
||||
val=vat_number
|
||||
))
|
||||
tax_id_obj = stripe.Customer.create_tax_id(
|
||||
stripe_customer_id,
|
||||
type=type,
|
||||
value=vat_number,
|
||||
)
|
||||
return tax_id_obj
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue