alt

Important information

The API reference is now available here.
The deprecated API reference is available here.

Unzer

Cancel a charge (Refund)

Transfer money back from the merchant to the customer.

Overview

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.

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

Charge.cancel

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.

Request

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));

Payment.cancel

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

ParameterTypeDescription
amount
(required)
floatThe amount that must be canceled. If the amount is not set, the full charged amount is refunded. For Unzer Installment, it is transmitted as amountGross.

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 indicates the result of the transaction. Possible values: SUCCESS, PENDING, or ERROR.

If a transaction fails, the Unzer 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);
    $charge->cancel(100.0);

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

See also