Cancel a charge (Refund)

Transfer money back from the merchant to the customer.


Canceling a charge transfers the money back from you (the merchant) to the customer. This is also called refund. You can only perform the Cancel transaction on an existing Charge. This transaction type is available for all payment methods.

Refund on payment
You cannot refund the payment resource itself. You must create a cancel transaction for the charge.


Refund is triggered by calling the cancel of the charge object. You can call up the cancellation charge with an amount. In this case, only this part of the charge amount is transferred back. If you do not specify an amount, the entire charge is canceled. You can also provide the transaction’s description during refund.


Unzer unzer  = new Unzer("s-priv-xxxxxxxxxx");
Charge charge = unzer.fetchCharge('s-pay-1', 's-chg-1');
Cancel cancel = charge.cancel();
Unzer unzer  = new Unzer("s-priv-xxxxxxxxxx");
Charge charge = unzer.fetchCharge('s-pay-1', 's-chg-1');
Cancel cancel = charge.cancel(BigDecimal.valueOf(50.0));


Even though, a direct cancel on the payment resource is not possible, the SDK provides a function in the payment class that automatically performs cancellations on existing authorizations of that payment.

Unzer unzer  = new Unzer("s-priv-xxxxxxxxxx");
Payment payment = unzer.fetchPayment('s-pay-1');
Cancel cancel  = payment.cancel(49.5);

Arguments to Charge.cancel | Payment.cancel

Parameter Type Description
amount float The amount that must be canceled. If the amount is not set, the full charged amount is refunded.
This is transmitted as amountGross in case of Unzer Instalment.
Required: false
Default: null

Transaction Results

The transaction response will be stored in the transaction object. It contains a paymentId (for example, s-pay-1), the transactionId, and other properties of the response. The transaction object provides getter functions to access those properties, for example cancel.getPaymentId().

The status-parameter indicates the result of the transaction. The Enum can only be SUCCESS, PENDING or ERRROR

If a transaction fails, the payment API returns an error resource instead of a transaction. In the SDK, this is handled as an UnzerPaymentException. Make sure to catch that case properly.

To view and example of the transaction response, see Authorize a payment.

try {
    $unzer     = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
    $charge    = $unzer->fetchCharge($paymentId, $chargeId);

    if ($charge->isSuccess) {
    // Transaction has to be pending at this point.
} catch (UnzerApiException $e) {
    // Transaction failed. API returned error resource.
    $this->redirectToFailure(); // redirect to the failure page of your shop
} catch (RuntimeException $e) {
    $merchantMessage = $e->getMessage();

See also