Cancel an authorization

Cancel the reserved amount for a payment.

Overview

Canceling an authorization releases the reserved money for the customer’s payment method. This is also called reversal. You can only perform the Cancel transaction on an existing authorization. This transaction is possible for selected payment methods only. For more details, see Payment methods.

Total amount
If you cancel an authorization, the total amount of the payment is reduced by the amount reversed.

Example

Reversal is triggered by calling the cancel of the authorize object. You can specify the amount for canceling the authorization. In this case, only this amount of the authorization is released. If you do not specify an amount, the entire authorization is canceled.

Request

// full cancel on Authorization
$unzer         = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
$authorization = $unzer->fetchAuthorization('s-pay-1');
$cancellation  = $authorization->cancel();
// part cancel on Authorization
$unzer         = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
$authorization = $unzer->fetchAuthorization('s-pay-1');
$cancellation  = $authorization->cancel(50.0);

Arguments to Authorize::cancel

Parameter Type Description
amount float The amount to reduce the authorization by. The method will perform a full cancel of the authorization (and payment) if the amount is not set.
Required: false
Default: null

Transaction Results

The transaction response will be stored in the transaction object. It contains a paymentId (e.g 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 parameters isSuccess, isPending, isError indicate the result of the transaction. Only one of these three can be true.

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

You can find an example for the transaction response on the Cancel an authorization (reversal) page.

try {
    $unzer     = new UnzerSDK\Unzer('s-priv-xxxxxxxxxx');
    $authorization= $unzer->fetchAuthorization($paymentId, $chargeId);
    $cancellation  = $authorization->cancel(100.0);

    if ($cancelation->isSuccess) {
        // Handle successful Cancelation
    }
} catch (UnzerApiException $e) {
    // Transaction failed. API returned error resource.
    $this->log($e->getClientMessage());
    $this->log($e->getMerchantMessage());
    // Handle failed Cancelation
} catch (RuntimeException $e) {
    $merchantMessage = $e->getMessage();
}

See also