Skip to content

Commit

Permalink
Fixed crash caused by Facebook Beta Shopify Connector (#119)
Browse files Browse the repository at this point in the history
- Facebook's beta connector introduced a null receipt in the transaction object from Shopify
- This patch handles the null case for a receipt in a transaction, preventing a crash
  • Loading branch information
kcharwood authored Aug 30, 2021
1 parent 2f57e65 commit 6a63657
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
47 changes: 26 additions & 21 deletions tap_shopify/streams/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,34 @@
# their values are not equal
def canonicalize(transaction_dict, field_name):
field_name_upper = field_name.capitalize()
value_lower = transaction_dict.get('receipt', {}).get(field_name)
value_upper = transaction_dict.get('receipt', {}).get(field_name_upper)
if value_lower and value_upper:
if value_lower == value_upper:
LOGGER.info((
"Transaction (id=%d) contains a receipt "
"that has `%s` and `%s` keys with the same "
"value. Removing the `%s` key."),
# Not all Shopify transactions have receipts. Facebook has been shown
# to push a null receipt through the transaction
receipt = transaction_dict.get('receipt', {})
if receipt:
value_lower = receipt.get(field_name)
value_upper = receipt.get(field_name_upper)
if value_lower and value_upper:
if value_lower == value_upper:
LOGGER.info((
"Transaction (id=%d) contains a receipt "
"that has `%s` and `%s` keys with the same "
"value. Removing the `%s` key."),
transaction_dict['id'],
field_name,
field_name_upper,
field_name_upper)
transaction_dict['receipt'].pop(field_name_upper)
else:
raise ValueError((
"Found Transaction (id={}) with a receipt that has "
"`{}` and `{}` keys with the different "
"values. Contact Shopify/PayPal support.").format(
transaction_dict['id'],
field_name,
field_name_upper,
field_name_upper)
transaction_dict['receipt'].pop(field_name_upper)
else:
raise ValueError((
"Found Transaction (id={}) with a receipt that has "
"`{}` and `{}` keys with the different "
"values. Contact Shopify/PayPal support.").format(
transaction_dict['id'],
field_name_upper,
field_name))
elif value_upper:
transaction_dict["receipt"][field_name] = transaction_dict['receipt'].pop(field_name_upper)
field_name))
elif value_upper:
# pylint: disable=line-too-long
transaction_dict["receipt"][field_name] = transaction_dict['receipt'].pop(field_name_upper)


class Transactions(Stream):
Expand Down
6 changes: 6 additions & 0 deletions tests/unittests/test_transaction_canonicalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def test_lowercases_if_capital_only_exists(self):
canonicalize(record, "foo")
self.assertEqual(record, expected_record)

def test_null_receipt_record(self):
record = {"receipt": None}
expected_record = {"receipt": None}
canonicalize(record, "foo")
self.assertEqual(record, expected_record)

def test_removes_uppercase_if_both_exist_and_are_equal(self):
record = {"receipt": {"Foo": "bar", "foo": "bar"}, "id": 2}
expected_record = {"receipt": {"foo": "bar"}, "id": 2}
Expand Down

0 comments on commit 6a63657

Please sign in to comment.