diff --git a/ucloud_pay.py b/ucloud_pay.py index a1badb0..a347a6a 100644 --- a/ucloud_pay.py +++ b/ucloud_pay.py @@ -225,6 +225,8 @@ class ProductOrder(Resource): if otp_response != 200: return {"message": "Wrong Credentials"}, 403 + stripe_utils = StripeUtils() + # Validate the given product is ok product_id = data["product_id"] product = client.get( @@ -237,8 +239,29 @@ class ProductOrder(Resource): logging.debug("Got product {}: {}".format(product.key, product.value)) # Check the user has a payment source added + stripe_customer = stripe_utils.get_stripe_customer_from_email( + data["email"] + ) + + if not stripe_customer or len(stripe_customer.sources) == 0: + logging.error("{} does not exist in Stripe => no cards".format( + data["email"]) + ) + return {"message": "Please register a payment source"}, 400 # Initiate a one-time/subscription based on product type + product_obj = product.value + if product_obj['type'] == "recurring": + logging.debug("Product {} is recurring payment".format( + product_obj["name"]) + ) + elif product_obj['type'] == "one-time": + logging.debug( + "Product {} is one-time " + "payment".format(product_obj["type"]) + ) + + # If charge successful, create an order object