Create a fresh token to avoid issue with reuse of tokens
(doesn't work well entirely -> creates duplicate cards)
This commit is contained in:
parent
77c1ca76fa
commit
0359eb22f9
1 changed files with 36 additions and 19 deletions
|
@ -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,21 +126,25 @@ 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)
|
||||||
|
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(
|
resp = stripe_utils.associate_customer_card(
|
||||||
stripe_customer.id, token_response["response_object"].id
|
stripe_customer.id, fresh_token
|
||||||
)
|
)
|
||||||
if resp["response_object"]:
|
if resp["response_object"]:
|
||||||
return {"message":
|
return {"message":
|
||||||
|
@ -138,11 +152,14 @@ class UserRegisterPayment(Resource):
|
||||||
"soruce".format(
|
"soruce".format(
|
||||||
data['card_number'].strip()[-4:])
|
data['card_number'].strip()[-4:])
|
||||||
}, 200
|
}, 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"]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue