Skip to content

Commit

Permalink
Merge pull request #273 from Jerome-Celle/feature-add_detail_paysafe_…
Browse files Browse the repository at this point in the history
…error

Add detail in paysafe error for the front
  • Loading branch information
RignonNoel authored Nov 20, 2019
2 parents a917bc0 + b19f69c commit ce2bdc4
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 34 deletions.
21 changes: 14 additions & 7 deletions retirement/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,8 @@ def update(self, instance, validated_data):
)
except PaymentAPIError as err:
raise serializers.ValidationError({
'message': err
'message': err,
'detail': err.detail
})
# Create local profile
profile = PaymentProfile.objects.create(
Expand Down Expand Up @@ -660,10 +661,12 @@ def update(self, instance, validated_data):
'non_field_errors': [_(
"The order has not been charged yet. "
"Try again later."
)]
)],
'detail': err.detail
})
raise serializers.ValidationError({
'message': str(err)
'message': str(err),
'detail': err.detail
})

if payment_token and int(amount):
Expand All @@ -676,7 +679,8 @@ def update(self, instance, validated_data):
)
except PaymentAPIError as err:
raise serializers.ValidationError({
'message': err
'message': err,
'detail': err.detail
})

elif single_use_token and int(amount):
Expand All @@ -693,7 +697,8 @@ def update(self, instance, validated_data):
)
except PaymentAPIError as err:
raise serializers.ValidationError({
'message': err
'message': err,
'detail': err.detail
})
charge_res_content = charge_response.json()
order.authorization_id = charge_res_content['id']
Expand Down Expand Up @@ -738,10 +743,12 @@ def update(self, instance, validated_data):
'non_field_errors': [_(
"The order has not been charged yet. "
"Try again later."
)]
)],
'detail': err.detail
})
raise serializers.ValidationError({
'message': str(err)
'message': str(err),
'detail': err.detail
})

new_retreat = retreat
Expand Down
4 changes: 3 additions & 1 deletion retirement/tests/tests_viewset_Reservation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,9 @@ def test_delete_refund_too_fast(self):
]
}

self.assertEqual(json.loads(response.content), content)
self.assertEqual(
json.loads(response.content).get('non_field_errors'),
content.get('non_field_errors'))

self.reservation.refresh_from_db()

Expand Down
2 changes: 2 additions & 0 deletions retirement/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ def destroy(self, request, *args, **kwargs):
"The order has not been charged yet. Try "
"again later."
)],
'detail': err.detail
})
raise rest_framework_serializers.ValidationError(
{
Expand All @@ -421,6 +422,7 @@ def destroy(self, request, *args, **kwargs):
"An error occured with the payment system."
" Please try again later."
)],
'detail': err.detail
}
)
instance.cancelation_action = 'R'
Expand Down
5 changes: 4 additions & 1 deletion store/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ class PaymentAPIError(Exception):
"""
Raised when a payment related action fails.
"""
pass
def __init__(self, error, detail):

super(PaymentAPIError, self).__init__(error)
self.detail = detail
12 changes: 8 additions & 4 deletions store/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ def create(self, validated_data):
)
except PaymentAPIError as err:
raise serializers.ValidationError({
'non_field_errors': [err]
'non_field_errors': [err],
'detail': err.detail
})

charge_res_content = charge_response.json()
Expand Down Expand Up @@ -572,7 +573,8 @@ def create(self, validated_data):
)
except PaymentAPIError as err:
raise serializers.ValidationError({
'non_field_errors': [err]
'non_field_errors': [err],
'detail': err.detail
})
# Create local profile
profile = PaymentProfile.objects.create(
Expand Down Expand Up @@ -797,7 +799,8 @@ def create(self, validated_data):
)
except PaymentAPIError as err:
raise serializers.ValidationError({
'non_field_errors': [err]
'non_field_errors': [err],
'detail': err.detail
})

elif need_transaction and single_use_token and int(amount):
Expand All @@ -814,7 +817,8 @@ def create(self, validated_data):
)
except PaymentAPIError as err:
raise serializers.ValidationError({
'non_field_errors': [err]
'non_field_errors': [err],
'detail': err.detail
})
elif (membership_orderlines
or package_orderlines
Expand Down
39 changes: 25 additions & 14 deletions store/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@


def manage_paysafe_error(err, additional_data):
content_dict = json.loads(err.response.content)
try:
err_code = json.loads(err.response.content)['error']['code']
err_code = content_dict['error']['code']

Log.error(
source='PAYSAFE',
Expand All @@ -109,7 +110,10 @@ def manage_paysafe_error(err, additional_data):
)

if err_code in PAYSAFE_EXCEPTION:
raise PaymentAPIError(PAYSAFE_EXCEPTION[err_code])
raise PaymentAPIError(
error=PAYSAFE_EXCEPTION[err_code],
detail=content_dict
)
except json.decoder.JSONDecodeError as err:
print(err.response)

Expand All @@ -119,7 +123,10 @@ def manage_paysafe_error(err, additional_data):
message=err,
additional_data=json.dumps(additional_data)
)
raise PaymentAPIError(PAYSAFE_EXCEPTION['unknown'])
raise PaymentAPIError(
error=PAYSAFE_EXCEPTION['unknown'],
detail=content_dict
)


def charge_payment(amount, payment_token, reference_number):
Expand Down Expand Up @@ -160,10 +167,10 @@ def charge_payment(amount, payment_token, reference_number):
r.raise_for_status()
except requests.exceptions.HTTPError as err:
manage_paysafe_error(err, {
'amount': amount,
'payment_token': payment_token,
'reference_number': reference_number
})
'amount': amount,
'payment_token': payment_token,
'reference_number': reference_number
})
return r


Expand Down Expand Up @@ -345,12 +352,12 @@ def create_external_card(profile_id, single_use_token):
)
r.raise_for_status()
except requests.exceptions.HTTPError as err:
err = json.loads(err.response.content)
err_code = err['error']['code']
content_dict = json.loads(err.response.content)
err_code = content_dict['error']['code']
if err_code == "7503":
try:
r = get_external_card(
err['links'][0]['href'].split("/")[-1]
content_dict['links'][0]['href'].split("/")[-1]
)
card_data = json.loads(r.content)
delete_external_card(profile_id, card_data['id'])
Expand All @@ -366,11 +373,15 @@ def create_external_card(profile_id, single_use_token):
return r
except requests.exceptions.HTTPError as err:
if err_code in PAYSAFE_EXCEPTION:
raise PaymentAPIError(PAYSAFE_EXCEPTION[err_code])
raise PaymentAPIError(PAYSAFE_EXCEPTION['unknown'])
raise PaymentAPIError(PAYSAFE_EXCEPTION[err_code],
detail=content_dict)
raise PaymentAPIError(PAYSAFE_EXCEPTION['unknown'],
detail=content_dict)
if err_code in PAYSAFE_EXCEPTION:
raise PaymentAPIError(PAYSAFE_EXCEPTION[err_code])
raise PaymentAPIError(PAYSAFE_EXCEPTION['unknown'])
raise PaymentAPIError(PAYSAFE_EXCEPTION[err_code],
detail=content_dict)
raise PaymentAPIError(PAYSAFE_EXCEPTION['unknown'],
detail=content_dict)

return r

Expand Down
8 changes: 6 additions & 2 deletions store/tests/tests_viewset_CustomPayment.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ def test_create_with_invalid_single_use_token(self):
]
}

self.assertEqual(json.loads(response.content), content)
self.assertEqual(
json.loads(response.content).get('non_field_errors'),
content.get('non_field_errors'))

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

Expand Down Expand Up @@ -230,7 +232,9 @@ def test_create_payment_issue(self):
]
}

self.assertEqual(json.loads(response.content), content)
self.assertEqual(
json.loads(response.content).get('non_field_errors'),
content.get('non_field_errors'))

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

Expand Down
16 changes: 12 additions & 4 deletions store/tests/tests_viewset_Order.py
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,9 @@ def test_create_with_invalid_payment_token(self):
]
}

self.assertEqual(json.loads(response.content), content)
self.assertEqual(
json.loads(response.content).get('non_field_errors'),
content.get('non_field_errors'))

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

Expand Down Expand Up @@ -2030,7 +2032,9 @@ def test_create_with_invalid_single_use_token(self):
]
}

self.assertEqual(json.loads(response.content), content)
self.assertEqual(
json.loads(response.content).get('non_field_errors'),
content.get('non_field_errors'))

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

Expand Down Expand Up @@ -2082,7 +2086,9 @@ def test_create_with_invalid_single_use_token_no_profile(self):
]
}

self.assertEqual(json.loads(response.content), content)
self.assertEqual(
json.loads(response.content).get('non_field_errors'),
content.get('non_field_errors'))

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

Expand Down Expand Up @@ -2146,7 +2152,9 @@ def test_create_payment_issue(self):
]
}

self.assertEqual(json.loads(response.content), content)
self.assertEqual(
json.loads(response.content).get('non_field_errors'),
content.get('non_field_errors'))

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

Expand Down
5 changes: 4 additions & 1 deletion store/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ def cards(self, request, *args, **kwargs):
)
except PaymentAPIError as err:
return Response(
{'message': str(err)},
{
'message': str(err),
'detail': err.detail
},
status=status.HTTP_400_BAD_REQUEST,
)

Expand Down

0 comments on commit ce2bdc4

Please sign in to comment.