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() != BaseTransaction.Status.ERROR;
}
}
PayPal recurring transaction
note-paypalPayPal recurring transactions are available via Unzer::recurring
Unzer unzer = new Unzer("s-priv-xxxxxxxxxx");
Recurring recurring = unzer.recurring("s-crd-xxxxxxxxxx", new URL("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() != null) {
if (recurring.getStatus() == Recurring.Status.PENDING) {
// Redirect to recurring.getRedirectUrl()
}
} else if (recurring.getRedirectUrl() == null && recurring.getStatus() == Recurring.Status.SUCCESS) {
// Redirect to success page
}
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 = (Paypal) unzer.fetchPaymentType("s-crd-xxxxxxxxxx");
if (paypal.getRecurring()) {
// Handle as success
} else {
// Else handle as failure
}
Arguments to Unzer.recurring
| Parameter | Type | Description |
|---|---|---|
typeId | String | Reference to the payment type to use by ID Required: true |
customerId | String | Reference to the customer resource to use by ID Required: false Default: null |
metadataId | String | Reference to the metadata resource to use by ID Required: false Default: null |
returnUrl | URL | The 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 |
recurrenceType | RecurrenceType | Recurrence 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.
