Skip to content

Cancel all or part of an authorized payment that has not yet been captured, such as when an item is removed from an order before shipment.

Cancellations can be full or partial, and multiple cancellations can be made against the same transaction, provided the total cancelled amount does not exceed the remaining authorized amount.

For more information, see the Payment API 2.0 documentation.

import com.ratepay.sdk.CancellationOutcome;
import com.ratepay.sdk.model.api.payment.CancellationRequest;
import java.math.BigDecimal;

CancellationRequest request = new CancellationRequest(
    new BigDecimal("249.00"), // grossAmount, cancelling the full remaining authorization
    null, // items
    null, // partnerOperationId
    null, // partnerShopId
    null, // specialItems
    null  // vats
);

CancellationOutcome outcome = client.cancel(ratepayTransactionId, request, null);

if (outcome.kind() == CancellationOutcome.Kind.ACCEPTED) {
    var accepted = (CancellationOutcome.Accepted) outcome;
    System.out.println("Cancelled: " + accepted.getRatepayCancellationId());
} else {
    var declined = (CancellationOutcome.Declined) outcome;
    System.out.println("Cancellation declined: " + declined.getDecline().getCategory());
}

A cancellation may be declined if the requested amount exceeds the remaining authorized amount. Check Declined.decline.reasons for CANCELLATION_AMOUNT_TOO_HIGH and Declined.decline.category for the broader decline category.

Exceptions

ExceptionHTTP Status Code
RatepayAuthException401/403
RatepayValidationException400
RatepayTransactionNotFoundException404
RatepayLegalException451
RatepayNetworkException5xx/transport

Next