Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
refactor: Add logging to mobile IAP checkout/ API (#4000)
Browse files Browse the repository at this point in the history
  • Loading branch information
moeez96 authored and mobeenali12 committed Jul 25, 2023
1 parent 6dd9b7e commit 0984ac2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions ecommerce/extensions/iap/api/v1/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
LOGGER_BASKET_CREATED = "Basket created for user [%s] with SKUS [%s]"
LOGGER_BASKET_CREATION_FAILED = "Basket creation failed for user [%s]. Error: [%s]"
LOGGER_BASKET_NOT_FOUND = "Basket [%s] not found for user [%s]."
LOGGER_CHECKOUT_ERROR = "Checkout failed with the error [%s] and status code [%s]."
LOGGER_EXECUTE_ALREADY_PURCHASED = "Execute payment failed for user [%s] and basket [%s]. " \
"Products already purchased."
LOGGER_EXECUTE_GATEWAY_ERROR = "Execute payment validation failed for user [%s] and basket [%s]. Error: [%s]"
Expand Down
7 changes: 3 additions & 4 deletions ecommerce/extensions/iap/api/v1/ios_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ def validate(self, receipt, configuration):
Apple for the mentioned productId.
"""
bundle_id = configuration.get('ios_bundle_id')
# If True, automatically query sandbox endpoint if validation fails on production endpoint
# TODO: Add auto_retry_wrong_env_request to environment variables
auto_retry_wrong_env_request = True
validator = AppStoreValidator(bundle_id, auto_retry_wrong_env_request=auto_retry_wrong_env_request)
# auto_retry_wrong_env_request = True automatically queries sandbox endpoint if
# validation fails on production endpoint
validator = AppStoreValidator(bundle_id, auto_retry_wrong_env_request=True)

try:
validation_result = validator.validate(
Expand Down
18 changes: 15 additions & 3 deletions ecommerce/extensions/iap/api/v1/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
LOGGER_BASKET_CREATED,
LOGGER_BASKET_CREATION_FAILED,
LOGGER_BASKET_NOT_FOUND,
LOGGER_CHECKOUT_ERROR,
LOGGER_EXECUTE_ALREADY_PURCHASED,
LOGGER_EXECUTE_GATEWAY_ERROR,
LOGGER_EXECUTE_ORDER_CREATION_FAILED,
Expand Down Expand Up @@ -641,6 +642,7 @@ def setUp(self):
'basket_id': self.basket.id,
'payment_processor': 'android-iap'
}
self.logger_name = 'ecommerce.extensions.iap.api.v1.views'

def test_authentication_required(self):
""" Verify the endpoint requires authentication. """
Expand All @@ -651,10 +653,20 @@ def test_authentication_required(self):
def test_no_basket(self):
""" Verify the endpoint returns HTTP 400 if the user has no associated baskets. """
self.user.baskets.all().delete()
expected_status = 400
expected_error = "Basket [%s] not found." % str(self.post_data['basket_id'])
expected_content = b'{"error": "Basket [%s] not found."}' % str(self.post_data['basket_id']).encode()
response = self.client.post(self.path, data=self.post_data)
self.assertEqual(response.status_code, 400)
self.assertEqual(response.content, expected_content)
with LogCapture(self.logger_name) as logger:
response = self.client.post(self.path, data=self.post_data)
logger.check(
(
self.logger_name,
'ERROR',
LOGGER_CHECKOUT_ERROR % (expected_error, expected_status)
),
)
self.assertEqual(response.status_code, expected_status)
self.assertEqual(response.content, expected_content)

@override_settings(
PAYMENT_PROCESSORS=['ecommerce.extensions.iap.processors.android_iap.AndroidIAP']
Expand Down
4 changes: 2 additions & 2 deletions ecommerce/extensions/iap/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
LOGGER_BASKET_CREATED,
LOGGER_BASKET_CREATION_FAILED,
LOGGER_BASKET_NOT_FOUND,
LOGGER_CHECKOUT_ERROR,
LOGGER_EXECUTE_ALREADY_PURCHASED,
LOGGER_EXECUTE_GATEWAY_ERROR,
LOGGER_EXECUTE_ORDER_CREATION_FAILED,
Expand Down Expand Up @@ -158,10 +159,9 @@ class MobileCheckoutView(APIView):
permission_classes = (IsAuthenticated,)

def post(self, request):
# TODO: Add check for products_in_basket_already_purchased
# TODO: Add logging for api/user_info after reading from request obj
response = CheckoutView.as_view()(request._request) # pylint: disable=W0212
if response.status_code != 200:
logger.exception(LOGGER_CHECKOUT_ERROR, response.content.decode(), response.status_code)
return JsonResponse({'error': response.content.decode()}, status=response.status_code)

return response
Expand Down

0 comments on commit 0984ac2

Please sign in to comment.