Ejemplo n.º 1
0
    def _create_transaction(payment: Payment,
                            request_json: Dict,
                            invoice: Invoice = None):
        # Cannot start transaction on completed payment
        if payment.payment_status_code in (PaymentStatus.COMPLETED.value,
                                           PaymentStatus.DELETED.value):
            raise BusinessException(Error.COMPLETED_PAYMENT)

        pay_system_service: PaymentSystemService = PaymentSystemFactory.create_from_payment_method(
            # todo Remove this and use payment.payment_method_code when payment methods are not created upfront
            payment_method=invoice.payment_method_code if invoice else payment.
            payment_method_code)

        # If there are active transactions (status=CREATED), then invalidate all of them and create a new one.
        existing_transaction = PaymentTransactionModel.find_active_by_payment_id(
            payment.id)
        if existing_transaction and existing_transaction.status_code != TransactionStatus.CANCELLED.value:
            existing_transaction.status_code = TransactionStatus.CANCELLED.value
            existing_transaction.transaction_end_time = datetime.now()
            existing_transaction.save()
        transaction = PaymentTransaction()
        transaction.payment_id = payment.id
        transaction.client_system_url = request_json.get('clientSystemUrl')
        transaction.status_code = TransactionStatus.CREATED.value
        transaction_dao = transaction.flush()
        transaction._dao = transaction_dao  # pylint: disable=protected-access
        if invoice:
            transaction.pay_system_url = PaymentTransaction._build_pay_system_url_for_invoice(
                invoice, pay_system_service, transaction.id,
                request_json.get('payReturnUrl'))
        else:
            transaction.pay_system_url = PaymentTransaction._build_pay_system_url_for_payment(
                payment, pay_system_service, transaction.id,
                request_json.get('payReturnUrl'))
        transaction_dao = transaction.save()
        transaction = PaymentTransaction.__wrap_dao(transaction_dao)
        return transaction
Ejemplo n.º 2
0
 def find_active_by_payment_id(payment_identifier: int):
     """Find active transaction by id."""
     current_app.logger.debug('>find_active_by_payment_id')
     active_transaction = PaymentTransactionModel.find_active_by_payment_id(payment_identifier)
     return PaymentTransaction.populate(active_transaction)