|
|
|
@ -99,72 +99,107 @@ class UserRegisterPayment(Resource):
|
|
|
|
|
logging.debug("Got data: {}".format(str(data))) |
|
|
|
|
otp_response = check_otp(data["name"], data["realm"], |
|
|
|
|
data["token"]) |
|
|
|
|
last4 = data['card_number'].strip()[-4:] |
|
|
|
|
if otp_response != 200: |
|
|
|
|
return {"message": "Wrong Credentials"}, 403 |
|
|
|
|
|
|
|
|
|
stripe_utils = StripeUtils() |
|
|
|
|
|
|
|
|
|
# Does customer already exist ? |
|
|
|
|
stripe_customer = stripe_utils.get_stripe_customer_from_email( |
|
|
|
|
data["email"]) |
|
|
|
|
token = UserRegisterPayment.get_token( |
|
|
|
|
data["card_number"], data["cvc"], data["expiry_month"], |
|
|
|
|
data["expiry_year"] |
|
|
|
|
) |
|
|
|
|
if token: |
|
|
|
|
if stripe_customer is None: |
|
|
|
|
|
|
|
|
|
# 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["expiry_year"] |
|
|
|
|
) |
|
|
|
|
if token_response["response_object"]: |
|
|
|
|
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( |
|
|
|
|
name=data["card_holder_name"], |
|
|
|
|
token=token, |
|
|
|
|
token=token_response["response_object"].id, |
|
|
|
|
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[ |
|
|
|
|
"response_object"] |
|
|
|
|
return {"message": |
|
|
|
|
"Card ending in {} registered as your payment " |
|
|
|
|
"source".format(last4) |
|
|
|
|
}, 200 |
|
|
|
|
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@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: |
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
logging.error("Could not obtain token") |
|
|
|
|
return {"message": "Error with payment gateway. " |
|
|
|
|
"Contact support"}, 400 |
|
|
|
|
except KeyError as key_error: |
|
|
|
|
logging.error("Key error occurred") |
|
|
|
|
logging.error(str(key_error)) |
|
|
|
|