示例#1
0
    def get_receipt(self, payment_account: PaymentAccount, pay_response_url: str, invoice_reference: InvoiceReference):
        """Get receipt from paybc for the receipt number or get receipt against invoice number."""
        current_app.logger.debug('<paybc_service_Getting token')
        access_token: str = CFSService.get_token().json().get('access_token')
        current_app.logger.debug('<Getting receipt')
        receipt_url = current_app.config.get('CFS_BASE_URL') + '/cfs/parties/{}/accs/{}/sites/{}/rcpts/'.format(
            payment_account.cfs_party, payment_account.cfs_account, payment_account.cfs_site)
        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'))

        if receipt_number:
            receipt_response = self.__get_receipt_by_number(access_token, receipt_url, receipt_number)
            receipt_date = parser.parse(receipt_response.get('receipt_date'))

            amount: float = 0
            for invoice in receipt_response.get('invoices'):
                if invoice.get('invoice_number') == invoice_reference.invoice_number:
                    amount += float(invoice.get('amount_applied'))

            return receipt_number, receipt_date, amount
        return None
示例#2
0
    def get_receipt(self, payment_account: PaymentAccount,
                    pay_response_url: str,
                    invoice_reference: InvoiceReference):
        """Get the receipt details by calling PayBC web service."""
        # If pay_response_url is present do all the pre-check, else check the status by using the invoice id
        current_app.logger.debug(
            f'Getting receipt details {invoice_reference.invoice_id}. {pay_response_url}'
        )
        if pay_response_url:
            parsed_args = parse_url_params(pay_response_url)

            # validate if hashValue matches with rest of the values hashed
            hash_value = parsed_args.pop('hashValue', None)
            pay_response_url_without_hash = urlencode(parsed_args)

            # Check if trnApproved is 1=Success, 0=Declined
            trn_approved: str = parsed_args.get('trnApproved')
            if trn_approved == '1' and not HashingService.is_valid_checksum(
                    pay_response_url_without_hash, hash_value):
                current_app.logger.warning(
                    f'Transaction is approved, but hash is not matching : {pay_response_url}'
                )
                return None
            # Get the transaction number from args
            paybc_transaction_number = parsed_args.get('pbcTxnNumber')

        else:
            # Get the transaction number from invoice reference
            paybc_transaction_number = invoice_reference.invoice_number

        # If transaction number cannot be found, return None
        if not paybc_transaction_number:
            return None

        # Call PAYBC web service, get access token and use it in get txn call
        access_token = self.__get_token().json().get('access_token')

        paybc_transaction_url: str = current_app.config.get(
            'PAYBC_DIRECT_PAY_BASE_URL')
        paybc_ref_number: str = current_app.config.get(
            'PAYBC_DIRECT_PAY_REF_NUMBER')

        transaction_response = self.get(
            f'{paybc_transaction_url}/paybc/payment/{paybc_ref_number}/{paybc_transaction_number}',
            access_token,
            AuthHeaderType.BEARER,
            ContentType.JSON,
            return_none_if_404=True)

        if transaction_response:
            response_json = transaction_response.json()
            if response_json.get('paymentstatus') in STATUS_PAID:
                return response_json.get('trnorderid'), parser.parse(
                    response_json.get('trndate')), float(
                        response_json.get('trnamount'))
        return None
示例#3
0
 def get_pay_system_reason_code(self, pay_response_url: str) -> str:  # pylint:disable=unused-argument
     """Return the Pay system reason code."""
     if pay_response_url is not None and 'trnApproved' in pay_response_url:
         parsed_args = parse_url_params(pay_response_url)
         trn_approved: str = parsed_args.get('trnApproved')
         # Check if trnApproved is 1=Success, 0=Declined
         if trn_approved != '1':
             # map the error code
             message_text = unquote_plus(
                 parsed_args.get('messageText')).upper()
             pay_system_reason_code = PAYBC_TRANSACTION_ERROR_MESSAGE_DICT.get(
                 message_text, 'GENERIC_ERROR')
             return pay_system_reason_code
     return None