Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grab invoice instead of iterating through receipts. #1311

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions pay-api/src/pay_api/services/paybc_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,21 @@ def get_receipt(self, payment_account: PaymentAccount, pay_response_url: str, in
parsed_url = parse_url_params(pay_response_url)
receipt_number: str = parsed_url.get('receipt_number') if 'receipt_number' in parsed_url else None
if not receipt_number: # Find all receipts for the site and then match with invoice number
receipts_response = self.get(receipt_url, access_token, AuthHeaderType.BEARER, ContentType.JSON,
retry_on_failure=True).json()
for receipt in receipts_response.get('items'):
expanded_receipt = self._get_receipt_by_number(access_token, receipt_url, receipt.get('receipt_number'))
for invoice in expanded_receipt.get('invoices'):
if invoice.get('invoice_number') == invoice_reference.invoice_number:
return receipt.get('receipt_number'), parser.parse(
expanded_receipt.get('receipt_date')), float(invoice.get('amount_applied'))
invoice_url = current_app.config.get(
'CFS_BASE_URL') + f'/cfs/parties/{payment_account.cfs_party}/accs/' \
f'{payment_account.cfs_account}/sites/{payment_account.cfs_site}/invs/' \
f'{invoice_reference.invoice_number}'
invoice_response = self.get(invoice_url, access_token, AuthHeaderType.BEARER, ContentType.JSON,
retry_on_failure=True).json()

if invoice_response.get('receipts') and invoice_response['receipts'][0].get('links'):
if receipt_url := invoice_response['receipts'][0]['links'][0].get('href'):
receipt = self.get(receipt_url, access_token, AuthHeaderType.BEARER, ContentType.JSON,
retry_on_failure=True).json()
for invoice in receipt.get('invoices'):
if invoice.get('invoice_number') == invoice_reference.invoice_number:
return receipt.get('receipt_number'), parser.parse(
receipt.get('receipt_date')), float(invoice.get('amount_applied'))

if receipt_number:
receipt_response = self._get_receipt_by_number(access_token, receipt_url, receipt_number)
Expand Down
Loading