Fix errors in --register-payment

This commit is contained in:
PCoder 2019-09-14 00:43:27 +05:30
parent 5b7e7a4606
commit e1f9db5479
1 changed files with 37 additions and 23 deletions

View File

@ -85,56 +85,70 @@ class UserRegisterPayment(Resource):
stripe_utils = StripeUtils()
stripe_customer = stripe_utils.get_stripe_customer_from_email(
data["email"])
if stripe_customer is None:
token_response = stripe_utils.get_token_from_card(
data["number"], data["cvc"], data["expiry_month"],
data["expiry_year"]
)
if token_response["response"]:
token_response = stripe_utils.get_token_from_card(
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 stripe_customer is None:
stripe_customer_resp = stripe_utils.create_customer(
token_response["response"].id
name=data["card_holder_name"],
token=token_response["response_object"].id,
email=data["email"]
)
if stripe_customer_resp:
stripe_customer = stripe_customer_resp["response"]
stripe_customer = stripe_customer_resp[
"response_object"]
else:
logging.error("Could not get/create stripe_customer "
"for {}".format(data["email"]))
return {"message":
"Error with card. Contact support"}, 400
else:
logging.error("Could not get token for the card")
return {"message": "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"])
}, 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)
user_key = "/v1/users/{}/cards/".format(data["email"])
user_value = [token_response["response"].card.id]
client.put(user_key, user_value, value_in_json=False)
resp = stripe_utils.associate_customer_card(stripe_customer.id,
token_response["response"].id)
if resp["response"]:
return {"message":
"Card ending in {} registered as your payment "
"soruce".format(data['card'].strip()[-4:])
}, 200
if token_response:
user_key = "/v1/users/{}/cards/".format(data["email"])
user_value = token_response["response_object"].card.id
client.put(user_key, user_value, value_in_json=False)
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 "
"soruce".format(
data['card_number'].strip()[-4:])
}, 200
else:
logging.error(
"Unable to associate card "
"{} with customer {}".format(
token_response["response"].card.id, data["email"]
token_response["response_object"].card.id,
data["email"]
)
)
return {"message": "Error with card. Contact support"}, 400
except KeyError as key_error:
logging.error("Key error occurred")
logging.error(str(key_error))
return {"message": "Missing parameters"}, 400
return {"message": "Missing or wrong parameters"}, 400
api.add_resource(ListProducts, "/product/list")
api.add_resource(AddProduct, "/product/add")
api.add_resource(UserRegisterPayment, "/user/register_payment")
if __name__ == '__main__':
app.run(host="::", port=APP_PORT, debug=True)