From 0359eb22f9400538f027b981b3f1ca8bde25c734 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 14 Sep 2019 01:14:02 +0530 Subject: [PATCH] Create a fresh token to avoid issue with reuse of tokens (doesn't work well entirely -> creates duplicate cards) --- ucloud-pay.py | 55 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/ucloud-pay.py b/ucloud-pay.py index b65026d..e8402a7 100644 --- a/ucloud-pay.py +++ b/ucloud-pay.py @@ -80,6 +80,18 @@ class AddProduct(Resource): class UserRegisterPayment(Resource): + + @staticmethod + 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 + @staticmethod def post(): try: @@ -93,17 +105,15 @@ class UserRegisterPayment(Resource): stripe_utils = StripeUtils() stripe_customer = stripe_utils.get_stripe_customer_from_email( data["email"]) - token_response = stripe_utils.get_token_from_card( + token = UserRegisterPayment.get_token( data["card_number"], data["cvc"], data["expiry_month"], data["expiry_year"] ) - logging.debug("token type = {} {} ".format(type(token_response), - token_response)) - if token_response["response_object"]: + if token: if stripe_customer is None: stripe_customer_resp = stripe_utils.create_customer( name=data["card_holder_name"], - token=token_response["response_object"].id, + token=token, email=data["email"] ) if stripe_customer_resp: @@ -116,33 +126,40 @@ class UserRegisterPayment(Resource): "Error with card. Contact support"}, 400 else: logging.error("Could not get token for the card") - return {"message": "Error with card. Details: {}. " - "Contact support@ungleich.ch".format( - token_response["error"]) + return {"message": "Error with card. " + "Contact support@ungleich.ch" }, 400 user_key = "/v1/users/{}/stripe_customer/".format(data["email"]) user_value = stripe_customer.id client.put(user_key, user_value, value_in_json=False) - if token_response: + if token: user_key = "/v1/users/{}/cards/".format(data["email"]) - user_value = token_response["response_object"].card.id + user_value = token client.put(user_key, user_value, value_in_json=False) - resp = stripe_utils.associate_customer_card( - stripe_customer.id, token_response["response_object"].id + fresh_token = UserRegisterPayment.get_token( + data["card_number"], data["cvc"], data["expiry_month"], + data["expiry_year"] ) - if resp["response_object"]: - return {"message": - "Card ending in {} registered as your payment " - "soruce".format( - data['card_number'].strip()[-4:]) - }, 200 + if fresh_token: + resp = stripe_utils.associate_customer_card( + stripe_customer.id, fresh_token + ) + if resp["response_object"]: + return {"message": + "Card ending in {} registered as your payment " + "soruce".format( + data['card_number'].strip()[-4:]) + }, 200 + else: + logging.error("Error obtaining fresh token") + return {"message": "Error with card. Contact support"}, 400 else: logging.error( "Unable to associate card " "{} with customer {}".format( - token_response["response_object"].card.id, + token["response_object"].card.id, data["email"] ) )