public final class AddTransaction extends UpdateActionImpl<Payment>
See also PaymentUpdateCommand.
withCustomerAndFilledCart(client(), (customer, cart) -> {
final MonetaryAmount totalAmount = cart.getTotalPrice();
final PaymentMethodInfo paymentMethodInfo = PaymentMethodInfoBuilder.of()
.paymentInterface(randomKey())
.method("CREDIT_CARD")
.build();
final TransactionDraft chargeTransaction = TransactionDraftBuilder
.of(TransactionType.CHARGE, totalAmount, ZonedDateTime.now())
.build();
final PaymentDraftBuilder paymentDraftBuilder = PaymentDraftBuilder.of(totalAmount)
.customer(customer)
.paymentMethodInfo(paymentMethodInfo)
.amountPaid(totalAmount)
.transactions(Collections.singletonList(chargeTransaction));
final Payment payment = client().executeBlocking(PaymentCreateCommand.of(paymentDraftBuilder.build()));
assertThat(payment.getCustomer()).isEqualTo(payment.getCustomer());
assertThat(payment.getPaymentMethodInfo()).isEqualTo(paymentMethodInfo);
assertThat(payment.getAmountPlanned()).isEqualTo(totalAmount);
final MonetaryAmount firstRefundAmount = EURO_10;
final TransactionDraft firstRefundTransaction = TransactionDraftBuilder.of(TransactionType.REFUND, firstRefundAmount, ZonedDateTime.now()).build();
final Payment paymentWithFirstRefund = client().executeBlocking(PaymentUpdateCommand.of(payment, asList(SetAmountRefunded.of(firstRefundAmount), AddTransaction.of(firstRefundTransaction))));
assertThat(paymentWithFirstRefund.getTransactions()).hasSize(2);
assertThat(paymentWithFirstRefund.getTransactions().get(0).getId()).isNotEmpty();
final Query<PaymentTransactionAddedMessage> messageQuery = MessageQuery.of().withPredicates(m -> m.resource().is(payment))
.forMessageType(PaymentTransactionAddedMessage.MESSAGE_HINT);
assertEventually(() -> {
final PagedQueryResult<PaymentTransactionAddedMessage> messageQueryResult = client().executeBlocking(messageQuery);
assertThat(messageQueryResult.getTotal()).isGreaterThanOrEqualTo(1L);
final PaymentTransactionAddedMessage paymentTransactionAddedMessage = messageQueryResult.head().get();
assertThat(paymentTransactionAddedMessage.getTransaction().getTimestamp()).isEqualTo(firstRefundTransaction.getTimestamp());
});
final MonetaryAmount secondRefundAmount = EURO_5;
final TransactionDraft secondRefundTransaction = TransactionDraftBuilder.of(TransactionType.REFUND, secondRefundAmount, ZonedDateTime.now()).build();
final MonetaryAmount totalRefundAmount = secondRefundAmount.add(paymentWithFirstRefund.getAmountRefunded());
final Payment paymentWithSecondRefund = client().executeBlocking(PaymentUpdateCommand.of(paymentWithFirstRefund, asList(SetAmountRefunded.of(totalRefundAmount), AddTransaction.of(secondRefundTransaction))));
assertThat(paymentWithSecondRefund.getTransactions()).hasSize(3);
assertThat(paymentWithSecondRefund.getAmountRefunded()).isEqualTo(totalRefundAmount);
});
See the test code.
Modifier and Type | Method and Description |
---|---|
TransactionDraft |
getTransaction() |
static AddTransaction |
of(TransactionDraft transaction) |
getAction
public static AddTransaction of(TransactionDraft transaction)
public TransactionDraft getTransaction()