Simplify logic and log everything

This commit is contained in:
PCoder 2019-09-14 12:28:28 +05:30
parent ba9961a01a
commit 15911872f3

View file

@ -99,72 +99,107 @@ class UserRegisterPayment(Resource):
logging.debug("Got data: {}".format(str(data))) logging.debug("Got data: {}".format(str(data)))
otp_response = check_otp(data["name"], data["realm"], otp_response = check_otp(data["name"], data["realm"],
data["token"]) data["token"])
last4 = data['card_number'].strip()[-4:]
if otp_response != 200: if otp_response != 200:
return {"message": "Wrong Credentials"}, 403 return {"message": "Wrong Credentials"}, 403
stripe_utils = StripeUtils() stripe_utils = StripeUtils()
# Does customer already exist ?
stripe_customer = stripe_utils.get_stripe_customer_from_email( stripe_customer = stripe_utils.get_stripe_customer_from_email(
data["email"]) data["email"])
token = UserRegisterPayment.get_token(
# Does customer already exist ?
if stripe_customer is not None:
logging.debug(
"Customer {} exists already".format(data['email'])
)
# Check if the card already exists
ce_response = stripe_utils.card_exists(
stripe_customer.id, cc_number=data["card_number"],
exp_month=int(data["expiry_month"]),
exp_year=int(data["expiry_year"]),
cvc=data["cvc"])
if ce_response["response_object"]:
message = ("The given card ending in "
"{} exists already.").format(last4)
logging.debug(message)
return { "message": message }, 400
elif ce_response["response_object"] is False:
# Associate card with user
logging.debug("Adding card ending in {}".format(last4))
token_response = stripe_utils.get_token_from_card(
data["card_number"], data["cvc"], data["expiry_month"], data["card_number"], data["cvc"], data["expiry_month"],
data["expiry_year"] data["expiry_year"]
) )
if token: if token_response["response_object"]:
if stripe_customer is None: logging.debug(
"Token {}".format(
token_response["response_object"].id
)
)
resp = stripe_utils.associate_customer_card(
stripe_customer.id,
token_response["response_object"].id
)
if resp["response_object"]:
return {"message":
"Card ending in {} registered as your payment "
"source".format(last4)
}, 200
else:
logging.error("Could not obtain token")
return {"message": "Error with payment gateway. "
"Contact support"}, 400
else:
logging.error(
"Error occurred {}".format(ce_response["error"])
)
return {"message": "Error: {}".format(
ce_response["error"]
)}, 400
else:
# Stripe customer does not exist, create a new one
logging.debug(
"Customer {} does not exist, "
"creating new".format(data['email'])
)
token_response = stripe_utils.get_token_from_card(
data["card_number"], data["cvc"], data["expiry_month"],
data["expiry_year"]
)
if token_response["response_object"]:
logging.debug(
"Token {}".format(
token_response["response_object"].id))
#Create stripe customer
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, token=token_response["response_object"].id,
email=data["email"] email=data["email"]
) )
if stripe_customer_resp: if stripe_customer_resp["response_object"]:
logging.debug(
"Created stripe customer {}".format(
stripe_customer_resp["response_object"].id
)
)
stripe_customer = stripe_customer_resp[ stripe_customer = stripe_customer_resp[
"response_object"] "response_object"]
return {"message":
"Card ending in {} registered as your payment "
"source".format(last4)
}, 200
else: else:
logging.error("Could not get/create stripe_customer " logging.error("Could not get/create stripe_customer "
"for {}".format(data["email"])) "for {}".format(data["email"]))
return {"message": return {"message":
"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 obtain token")
return {"message": "Error with card. " return {"message": "Error with payment gateway. "
"Contact support@ungleich.ch" "Contact support"}, 400
}, 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:
user_key = "/v1/users/{}/cards/".format(data["email"])
user_value = token
client.put(user_key, user_value, value_in_json=False)
fresh_token = UserRegisterPayment.get_token(
data["card_number"], data["cvc"], data["expiry_month"],
data["expiry_year"]
)
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_object"].card.id,
data["email"]
)
)
return {"message": "Error with card. Contact support"}, 400
except KeyError as key_error: except KeyError as key_error:
logging.error("Key error occurred") logging.error("Key error occurred")
logging.error(str(key_error)) logging.error(str(key_error))