2020-01-27 08:40:57 +00:00
|
|
|
import logging
|
2020-01-20 07:30:12 +00:00
|
|
|
|
2020-02-20 10:23:15 +00:00
|
|
|
import parsedatetime
|
|
|
|
|
|
|
|
from datetime import datetime
|
2020-01-27 08:40:57 +00:00
|
|
|
from stripe_utils import StripeUtils
|
2020-01-20 07:30:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-01-27 08:40:57 +00:00
|
|
|
def resolve_product(usable_id, etcd_client):
|
2020-01-20 07:30:12 +00:00
|
|
|
products = etcd_client.get_prefix('/v1/products/', value_in_json=True)
|
|
|
|
for p in products:
|
|
|
|
if p.value['usable-id'] == usable_id:
|
2020-01-27 08:40:57 +00:00
|
|
|
return p.value
|
2020-01-20 07:30:12 +00:00
|
|
|
return None
|
2020-01-27 08:40:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2020-02-20 10:23:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_order_valid(order_timestamp, renewal_period):
|
|
|
|
"""
|
|
|
|
Sample Code Usage
|
|
|
|
|
|
|
|
>> current_datetime, status = cal.parse('Now')
|
|
|
|
>> current_datetime = datetime(*current_datetime[:6])
|
|
|
|
|
|
|
|
>> print('Is order valid: ', is_order_valid(current_datetime, '1 month'))
|
|
|
|
>> True
|
|
|
|
"""
|
|
|
|
cal = parsedatetime.Calendar()
|
|
|
|
|
|
|
|
renewal_datetime, status = cal.parse(renewal_period)
|
|
|
|
renewal_datetime = datetime(*renewal_datetime[:6])
|
|
|
|
|
|
|
|
return order_timestamp <= renewal_datetime
|