66 lines
2.2 KiB
Python
66 lines
2.2 KiB
Python
import logging
|
|
|
|
from stripe_utils import StripeUtils
|
|
|
|
|
|
def get_plan_id_from_product(product):
|
|
plan_id = 'ucloud-v1-'
|
|
plan_id += product['name'].strip().replace(' ', '-')
|
|
return plan_id
|
|
|
|
|
|
def get_pricing(price_in_chf_cents, product_type, recurring_period):
|
|
if product_type == 'recurring':
|
|
return 'CHF {}/{}'.format(price_in_chf_cents/100, recurring_period)
|
|
elif product_type == 'one-time':
|
|
return 'CHF {} (One time charge)'.format(price_in_chf_cents/100)
|
|
|
|
|
|
def get_user_friendly_product(product_dict):
|
|
uf_product = {
|
|
'name': product_dict['name'],
|
|
'description': product_dict['description'],
|
|
'product_id': product_dict['usable-id'],
|
|
'pricing': get_pricing(
|
|
product_dict['price'], product_dict['type'], product_dict['recurring_period']
|
|
)
|
|
}
|
|
if product_dict['type'] == 'recurring':
|
|
uf_product['minimum_subscription_period'] = product_dict['minimum_subscription_period']
|
|
return uf_product
|
|
|
|
|
|
def get_token(card_number, cvc, exp_month, exp_year):
|
|
stripe_utils = StripeUtils()
|
|
token_response = stripe_utils.get_token_from_card(
|
|
card_number, cvc, exp_month, exp_year
|
|
)
|
|
if token_response['response_object']:
|
|
return token_response['response_object'].id
|
|
else:
|
|
return None
|
|
|
|
|
|
def resolve_product(usable_id, etcd_client):
|
|
products = etcd_client.get_prefix('/v1/products/', value_in_json=True)
|
|
for p in products:
|
|
if p.value['usable-id'] == usable_id:
|
|
return p.value
|
|
return None
|
|
|
|
|
|
def calculate_charges(specification, data):
|
|
logging.debug('Calculating charges for specs:{} and data:{}'.format(specification, data))
|
|
one_time_charge = 0
|
|
recurring_charge = 0
|
|
for feature_name, feature_detail in specification['features'].items():
|
|
if feature_detail['constant']:
|
|
data[feature_name] = 1
|
|
|
|
if feature_detail['unit']['type'] != 'str':
|
|
one_time_charge += feature_detail['one_time_fee']
|
|
recurring_charge += (
|
|
feature_detail['price_per_unit_per_period'] * data[feature_name] /
|
|
feature_detail['unit']['value']
|
|
)
|
|
return one_time_charge, recurring_charge
|