Create a fresh token to avoid issue with reuse of tokens

(doesn't work well entirely -> creates duplicate cards)
This commit is contained in:
PCoder 2019-09-14 01:14:02 +05:30
parent 77c1ca76fa
commit 0359eb22f9
1 changed files with 36 additions and 19 deletions

View File

@ -80,6 +80,18 @@ class AddProduct(Resource):
class UserRegisterPayment(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 @staticmethod
def post(): def post():
try: try:
@ -93,17 +105,15 @@ class UserRegisterPayment(Resource):
stripe_utils = StripeUtils() stripe_utils = StripeUtils()
stripe_customer = stripe_utils.get_stripe_customer_from_email( stripe_customer = stripe_utils.get_stripe_customer_from_email(
data["email"]) data["email"])
token_response = stripe_utils.get_token_from_card( token = UserRegisterPayment.get_token(
data["card_number"], data["cvc"], data["expiry_month"], data["card_number"], data["cvc"], data["expiry_month"],
data["expiry_year"] data["expiry_year"]
) )
logging.debug("token type = {} {} ".format(type(token_response), if token:
token_response))
if token_response["response_object"]:
if stripe_customer is None: if stripe_customer is None:
stripe_customer_resp = stripe_utils.create_customer( stripe_customer_resp = stripe_utils.create_customer(
name=data["card_holder_name"], name=data["card_holder_name"],
token=token_response["response_object"].id, token=token,
email=data["email"] email=data["email"]
) )
if stripe_customer_resp: if stripe_customer_resp:
@ -116,33 +126,40 @@ class UserRegisterPayment(Resource):
"Error with card. Contact support"}, 400 "Error with card. Contact support"}, 400
else: else:
logging.error("Could not get token for the card") logging.error("Could not get token for the card")
return {"message": "Error with card. Details: {}. " return {"message": "Error with card. "
"Contact support@ungleich.ch".format( "Contact support@ungleich.ch"
token_response["error"])
}, 400 }, 400
user_key = "/v1/users/{}/stripe_customer/".format(data["email"]) user_key = "/v1/users/{}/stripe_customer/".format(data["email"])
user_value = stripe_customer.id user_value = stripe_customer.id
client.put(user_key, user_value, value_in_json=False) client.put(user_key, user_value, value_in_json=False)
if token_response: if token:
user_key = "/v1/users/{}/cards/".format(data["email"]) 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) client.put(user_key, user_value, value_in_json=False)
resp = stripe_utils.associate_customer_card( fresh_token = UserRegisterPayment.get_token(
stripe_customer.id, token_response["response_object"].id data["card_number"], data["cvc"], data["expiry_month"],
data["expiry_year"]
) )
if resp["response_object"]: if fresh_token:
return {"message": resp = stripe_utils.associate_customer_card(
"Card ending in {} registered as your payment " stripe_customer.id, fresh_token
"soruce".format( )
data['card_number'].strip()[-4:]) if resp["response_object"]:
}, 200 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: else:
logging.error( logging.error(
"Unable to associate card " "Unable to associate card "
"{} with customer {}".format( "{} with customer {}".format(
token_response["response_object"].card.id, token["response_object"].card.id,
data["email"] data["email"]
) )
) )