From 6c2183a6c8eeeaba52604e470ab65bb510ee7781 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 15 Sep 2019 13:05:14 +0530 Subject: [PATCH] Improve the details in products that we show --- ucloud_pay.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/ucloud_pay.py b/ucloud_pay.py index a259b9d..cd148ab 100644 --- a/ucloud_pay.py +++ b/ucloud_pay.py @@ -53,13 +53,41 @@ def get_order_id(): client.put("/v1/last_order_id", str(order_id)) return "OR-{}".format(order_id) +def get_human_readable_period(recurring_period_secs): + if recurring_period_secs <= 24 * 60 * 60: + return "day" + elif recurring_period_secs <= 30 * 24 * 60 * 60: + return "month" + elif recurring_period_secs <= 365 * 30 * 24 * 60 * 60: + return "year" + +def get_pricing(price_in_chf_cents, product_type, recurring_period_secs): + if product_type == "recurring": + return "CHF {}/ {}".format( + price_in_chf_cents/100, + get_human_readable_period(recurring_period_secs) + ) + elif product_type == "one-time": + return "CHF {}".format(price_in_chf_cents/100) + + class ListProducts(Resource): @staticmethod def get(): products = client.get_prefix("/v1/products/", value_in_json=False) prod_dict = {} for p in products: - prod_dict[p.key] = json.loads(p.value) + p_json = json.loads(p.value) + product_id = p.key[p.key.rindex("/")+1:] + actual_product = { + "name": p_json["name"], + "description": p_json["product_description"], + "product_id": product_id, + "pricing": get_pricing(p_json["price"], p_json["type"], + p_json["recurring_duration"]), + "minumum_subscription_duration": "1 month" + } + prod_dict[product_id] = actual_product logging.debug("Products = {}".format(prod_dict)) return prod_dict, 200