uncloud/helper.py

63 lines
1.9 KiB
Python
Raw Normal View History

2020-01-20 07:30:12 +00:00
import config
from stripe_utils import StripeUtils
etcd_client = config.etcd_client
def get_plan_id_from_product(product):
plan_id = 'ucloud-v1-'
plan_id += product['name'].strip().replace(' ', '-')
# plan_id += '-' + product['type']
return plan_id
def get_order_id():
order_id_kv = etcd_client.get('/v1/last_order_id')
if order_id_kv is not None:
order_id = int(order_id_kv.value) + 1
else:
order_id = 0
etcd_client.put('/v1/last_order_id', str(order_id))
return 'OR-{}'.format(order_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(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:
print(p.value['uuid'], usable_id)
return p.value['uuid']
return None