IgorF
IgorF

Reputation: 36

Freshly created PaymentTransaction the ID is null

i have an issue that a freshly created payment transaction has no ID.

    @Override
    @Transactional("blTransactionManager")
    public PaymentTransaction getNewTemporaryOrderPayment(Order cart, PaymentType paymentType) {
        OrderPayment tempPayment = null;

        if (CollectionUtils.isNotEmpty(cart.getPayments())) {
            Optional<OrderPayment> optionalPayment = NmcPaymentUtils.getPaymentForOrder(cart);
            if (optionalPayment.isPresent()) {
                tempPayment = optionalPayment.get();
                invalidateTemporaryPaymentTransactions(tempPayment);
            }else {
                throw new IllegalStateException("Missing payment");
            }
        } else {
            tempPayment = this.orderPaymentService.create();
        }
        tempPayment = this.populateOrderPayment(tempPayment, cart, paymentType);

        //its necessary to create every time a new transaction because the ID needs to be unique in the parameter passed to 24pay
        PaymentTransaction transaction = createPendingTransaction(cart);
        transaction.setOrderPayment(tempPayment);
        tempPayment.addTransaction(transaction);
        tempPayment = orderService.addPaymentToOrder(cart, tempPayment, null);
        orderPaymentService.save(transaction);
        orderPaymentService.save(tempPayment);
        return transaction;
    }

even if i do an explicit save on the returned PaymentTransaction, the ID is still null. It is correctly persisted and has an ID in the database.

        PaymentTransaction paymentTransaction = paymentService.getNewTemporaryOrderPayment(cart, PaymentType.CREDIT_CARD);
        orderPaymentService.save(paymentTransaction);

how can i explicitly refresh this entity ? or any other suggestions how to solve this? I can do something like this to find my pending transaction

OrderPayment orderPayment = paymentTransaction.getOrderPayment();
Optional<PaymentTransaction> any = orderPayment.getTransactions().stream().filter(t -> t.isActive()).findFirst();

but that seems like an extra step which should not be needed. Any suggestions how to solve this in an elegant way ?

Upvotes: 1

Views: 156

Answers (1)

mouse_8b
mouse_8b

Reputation: 534

The transaction object has a null id because that variable is not updated when the order is saved.

Calls to save() methods return a new object, and that new object will have its id set. Consider the following example:

Transaction transaction1 = createTransaction(...);
Transaction transaction2 = orderPaymentService.save(transaction1);

After this code executes, transaction1 will not have been changed in save(), so its id will still be null. Transaction2 will be a new object with the id set.

Therefore, the variable transaction, created with PaymentTransaction transaction = createPendingTransaction(cart);, is never updated with the saved value, so the id is still null at the end.

Further, the save() calls at the end for the transaction and payment probably won't work as you intend. This is because the orderService.addPaymentToOrder(cart, tempPayment, null); will save the order, which should also cascade to save the transaction and payment. I'm pretty sure that calling save again would result in new objects that are not connected to the saved order.

So what do you do about this?

The call to tempPayment = orderService.addPaymentToOrder(cart, tempPayment, null); returns a persisted OrderPayment. Read the transactions from that object to find the one you just created. It is very similar to the extra step you are trying to avoid, but you can at least cut out one line.

OrderPayment persistedPayment = orderService.addPaymentToOrder(cart, tempPayment, null);
Optional<PaymentTransaction> persistedTransaction = persistedPayment.getTransactions().stream().filter(t -> t.isActive()).findFirst();

Upvotes: 1

Related Questions