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