code: def pay_with_account_credit_cards(invoice_id) -> Optional[Transaction]:
      logger.debug('invoice-payment-started', invoice_id=invoice_id)
      with transaction.atomic():
          invoice = Invoice.objects.select_for_update().get(pk=invoice_id)
          if not invoice.in_payable_state:
              raise PreconditionError('Cannot pay invoice with status {}.'.format(invoice.status))
          due = invoice.due().monies()
          if len(due) == 0:
              raise PreconditionError('Cannot pay empty invoice.')
          if len(due) > 1:
              raise PreconditionError('Cannot pay invoice with more than one currency.')
          amount = due[0]
          if amount.amount <= 0:
              raise PreconditionError('Cannot pay invoice with non-positive amount.')
          valid_credit_cards = CreditCard.objects.valid().filter(account=invoice.account).order_by('status')
          if not valid_credit_cards:
              raise PreconditionError('No valid credit card on account.')
          for credit_card in valid_credit_cards:
              try:
                  success, payment_psp_object = psp.charge_credit_card(
                      credit_card_psp_object=credit_card.psp_object,
                      amount=amount,
                      client_ref=str(invoice_id))
                  payment = Transaction.objects.create(
                      account=invoice.account,
                      invoice=invoice,
                      amount=amount,
                      success=success,
                      payment_method=credit_card.type,
                      credit_card_number=credit_card.number,
                      psp_object=payment_psp_object)
                  if success:
                      invoice.pay()
                      invoice.save()
                      logger.info('invoice-payment-success', invoice=invoice_id, payment=payment)
                      return payment
                  else:
                      logger.info('invoice-payment-failure', invoice=invoice_id, payment=payment)
              except Exception as e:
                  logger.error('invoice-payment-error', invoice_id=invoice_id, credit_card=credit_card, exc_info=e)
          return None