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.
| Exception | HTTP Status Code |
|---|---|
RatepayAuthException | 401/403 |
RatepayValidationException | 400 |
RatepayTransactionNotFoundException | 404 |
RatepayLegalException | 451 |
RatepayNetworkException | 5xx/transport |