alt

Important information

Updates to Visa secure data field mandate: Read more about the mandatory changes.

Unzer

Enable Recurring Payments

Enable recurring payments for payment transactions

Supported payment methods

The SDK allows activation of recurring payment for the following payment types:

  • Card (Credit Card and Debit Card)
  • PayPal
  • Unzer Direct Debit (secured)

Card recurring transaction

For recurring card payment set the recurrenceType for your charge/authorize transaction according to your recurring use case. For more details on recurring payments for credit cards, see card use cases.

package com.unzer.payment.integration.paymenttypes;

import com.unzer.payment.AbstractTransaction;
import com.unzer.payment.Charge;
import com.unzer.payment.Unzer;
import com.unzer.payment.enums.RecurrenceType;
import com.unzer.payment.models.AdditionalTransactionData;
import com.unzer.payment.models.CardTransactionData;
import com.unzer.payment.paymenttypes.Card;
import java.math.BigDecimal;
import java.net.URL;
import java.util.Currency;

class Example {
  void run() throws Exception {
    Unzer unzer = new Unzer("s-priv-xxxxxxxxxx");

    // Create card payment type
    Card card = unzer.createPaymentType(
        new Card("4444333322221111", "12/30", "123")
    );

    // Perform recurring charge
    Charge charge = unzer.charge((Charge) new Charge()
        // Set required parameters
        .setTypeId(card.getId())
        .setAmount(BigDecimal.valueOf(55))
        .setCurrency(Currency.getInstance("EUR"))
        .setReturnUrl(new URL("https://example.com"))

        // Set recurrence type
        .setAdditionalTransactionData(
            new AdditionalTransactionData()
                .setCard(new CardTransactionData()
                    .setRecurrenceType(RecurrenceType.SCHEDULED)
                )
        )
    );

    assert charge.getStatus() != AbstractTransaction.Status.ERROR;
  }
}

PayPal recurring transaction

Note

icon
Note that if you want to use PayPal’s recurring or save payment data features, you have to activate the PayPal Billing Agreements.

PayPal recurring transactions are available via Unzer::recurring

Unzer unzer = new Unzer("s-priv-xxxxxxxxxx");
Recurring recurring = unzer.recurring("s-crd-9wmri5mdlqps", "https://your.return.url");

When activating recurring, an object of type Recurring is returned which has the property redirectUrl. If this URL is not empty, you need to redirect the customer to it so that they can authenticate the payment.

// Redirect to the 3ds page or to success depending on the state of the transaction
if(recurring.getRedirectUrl() != "")
  if (recurring.getRedirectUrl() == "" && recurring.Status.equals(Recurring.Status.SUCCESS)) {
    // Redirect to success page
  } else if (recurring.getRedirectUrl() != "" && recurring.Status.equals(Recurring.Status.PENDING)) {
    // redirect to recurring.getRedirectUrl()
  }

When the customer returns and all has been successful the method getRecurring of the payment type should return true.

Unzer unzer = new Unzer('s-priv-xxxxxxxxxx');
Paypal paypal = unzer.fetchPaymentType('s-crd-9wmri5mdlqps');

if (paypal.getRecurring()) {
  // handle as success
}
// else handle as failure

Arguments to Unzer.recurring

ParameterTypeDescription
typeIdStringReference to the payment type to use by ID

Required: true
customerIdStringReference to the customer resource to use by ID

Required: false
Default: null
metadataIdStringReference to the metadata resource to use by ID

Required: false
Default: null
returnUrlURLThe URL the API leads the customer to after they finished entering payment information outside of the shop (for example, PayPal).
This needs to be set to a valid URL, no matter whether a redirect is necessary or not.
Required: true
recurrenceTypeRecurrenceTypeRecurrence type used for card recurring payment.

Required: false
Default: null

Unzer Direct Debit recurring transaction

With Unzer Direct Debit recurring is activated automatically when a charge has been successful.