Creating Subscriptions
Minimal Example
To create a subscription, you only need a payment method token from a created credit card and a plan id from a plan created via the control panel. The subscription will then be created using the price, trial duration (if any), and billing cycle of the plan.
SubscriptionRequest request = new SubscriptionRequest().
paymentMethodToken("the_token").
planId("the_plan_id");
Result<Subscription> result = gateway.subscription().create(request);
Overriding Plan Details
You can override the plan price, trial duration, start date, number of billing cycles per subscription, and add-on and discount details. You can also add additional add-ons and discounts that were not defaulted from the plan.
Price
SubscriptionRequest request = new SubscriptionRequest().
paymentMethodToken("the_token").
planId("the_plan_id").
price(new BigDecimal("12.00"));
Result<Subscription> result = gateway.subscription().create(request);
Trial Period
SubscriptionRequest request = new SubscriptionRequest().
paymentMethodToken("the_token").
planId("the_plan_id").
trialDuration(3).
trialDurationUnit(DurationUnit.MONTH);
Result<Subscription> result = gateway.subscription().create(request);
If you specify a trialDuration of 0, we will start the subscription immediately and not consider it in a trial period.
You can also disable the trial period if a subscription would normally have one using the given plan.
SubscriptionRequest request = new SubscriptionRequest().
paymentMethodToken("the_token").
planId("the_plan_id").
trialPeriod(false);
Result<Subscription> result = gateway.subscription().create(request);
Number of Billing Cycles
SubscriptionRequest request = new SubscriptionRequest().
paymentMethodToken("the_token").
planId("silver_plan").
numberOfBillingCycles(12);
Result<Subscription> result = gateway.subscription().create(request);
result.getTarget().getNumberOfBillingCycles();
// 12
Start Date
You can override the start date from the plan in three different ways:
- Set
firstBillingDateto a specific date. - Set
billingDayOfMonth. - Set the option
startImmediately, which overrides any plan start date and starts the subscription immediately.
Examples of billingDayOfMonth:
- If today is May 3, and the
billingDayOfMonthis 10, the first bill date will be May 10. - If today is May 3, and the
billingDayOfMonthis 1, the first bill date will be June 1. - If today is May 3, and the
billingDayOfMonthis 3, the subscription will be charged immediately (as ifstartImmediatelywas true). - If you want the billing to occur on the last day of the month, regardless of the length of the month, set
billingDayOfMonthto 31.
Passing in any of these options overrides the trial period from the plan (if one exists).
If the start date is in the future, the new subscription will be in Pending status.
Calendar date = Calendar.getInstance();
date.add(Calendar.DAY_OF_MONTH, 3);
SubscriptionRequest request = new SubscriptionRequest().
paymentMethodToken("the_token").
planId("silver_plan").
firstBillingDate(date);
Result<Subscription> result = gateway.subscription().create(request);
result.getTarget().getStatus();
// Pending
SubscriptionRequest request = new SubscriptionRequest().
paymentMethodToken("the_token").
planId("silver_plan").
billingDayOfMonth(15);
Result<Subscription> result = gateway.subscription().create(request);
result.getTarget().getStatus();
// Pending
result.getTarget().getBillingDayOfMonth();
// 15
SubscriptionRequest request = new SubscriptionRequest().
paymentMethodToken("the_token").
planId("silver_plan").
options().
startImmediately(true).
done();
Result<Subscription> result = gateway.subscription().create(request);
result.getTarget().getStatus();
// Active
result.getTarget().getTransactions().size();
// 1
Note: Passing in more than one of these fields will result in a validation error.
Subscription ID
You can set the subscription ID, or if you leave it blank the gateway will generate one for you.
SubscriptionRequest request = new SubscriptionRequest().
id("subscription_1234").
paymentMethodToken("the_token").
planId("the_plan_id");
Result<Subscription> result = gateway.subscription().create(request);
Start Immediately
If a subscription does not have a trial period and will start immediately, we’ll charge the customer right away. If the transaction is declined, the subscription will not be created. You can get transaction details from the result object.
SubscriptionRequest request = new SubscriptionRequest().
paymentMethodToken("the_token").
planId("the_plan_id").
price(TransactionAmount.DECLINE.amount).
trialPeriod(false);
Result<Subscription> result = gateway.subscription().create(request);
result.getTransaction().getStatus();
// PROCESSOR_DECLINED
If the transaction is authorized, you can get both the subscription and transaction information from the result object.
SubscriptionRequest request = new SubscriptionRequest().
paymentMethodToken("the_token").
planId("the_plan_id").
trialPeriod(false);
Result<Subscription> result = gateway.subscription().create(request);
Subscription subscription = result.getTarget();
Transaction transaction = subscription.getTransactions().get(0);
Specifying Merchant Account
If you have multiple merchant accounts, you can pass the merchant account ID to specify which merchant account to use to process transactions for the subscription. If a merchant account ID is not passed, we’ll use your default merchant account. Just like when specifying merchant account for transactions, the merchant account will determine the currency for the subscription.
SubscriptionRequest request = new SubscriptionRequest().
paymentMethodToken("the_token").
planId("the_plan_id").
merchantAccountId("gbp_account");
Result<Subscription> result = gateway.subscription().create(request);
Add-ons/Discounts
By default, a subscription will inherit the add-ons and discounts from its plan. When creating the subscription, you can modify the default behavior in 3 ways:
- New add-ons/discounts can be added to the subscription.
- Add-ons/discounts that would be inherited from the plan can be updated on the subscription.
- Add-ons/discounts that would be inherited from the plan can be removed from the subscription.
SubscriptionRequest request = new SubscriptionRequest().
paymentMethodToken("the_payment_method_token").
planId("the_plan_id").
addOns().
add().
inheritedFromId("add_on_id_1").
amount(new BigDecimal("20.00")).
done().
update("add_on_id_2").
quantity(2).
done().
remove("add_on_id_3").
done().
discounts().
add().
inheritedFromId("discount_id_1").
amount(new BigDecimal("15.00")).
done().
update("discount_id_2").
quantity(3).
done().
remove("discount_id_3").
done();
Result<Subscription> result = gateway.subscription().create(request);
When creating a subscription you can add, update and remove multiple add-ons/discounts at the same time.
SubscriptionRequest request = new SubscriptionRequest().
paymentMethodToken("the_payment_method_token").
planId("the_plan_id").
addOns().
add().
inheritedFromId("add_on_id_1").
amount(new BigDecimal("20.00")).
done().
add().
inheritedFromId("add_on_id_2").
amount(new BigDecimal("30.00")).
done().
update("add_on_id_3").
quantity(2).
done().
update("add_on_id_4").
quantity(3).
done().
remove("add_on_id_5", "add_on_id_6").
done();
Result<Subscription> result = gateway.subscription().create(request);
When adding add-ons/discounts to a subscription, all details will be inherited from the add-on/discount specified by the inheritedFromId. When updating add-ons/discounts, all details will be inherited from the add-on/discount specified by the existingId. You can override any of the following:
amountnumberOfBillingCycles or neverExpiresquantity
SubscriptionRequest request = new SubscriptionRequest().
paymentMethodToken("the_payment_method_token").
planId("the_plan_id").
addOns().
add().
inheritedFromId("add_on_id_1").
amount(new BigDecimal("20.00")).
numberOfBillingCycles(2).
quantity(4).
done().
update("add_on_id_2").
amount(new BigDecimal("15.00")).
neverExpires(true).
quantity(2).
done().
done();
Result<Subscription> result = gateway.subscription().create(request);
If you prefer to create a subscription without inheriting any add-ons/discounts from the plan, you can pass the doNotInheritAddOnsOrDiscounts option.
SubscriptionRequest request = new SubscriptionRequest().
paymentMethodToken("the_payment_method_token").
planId("the_plan_id").
options().
doNotInheritAddOnsOrDiscounts(true).
done();
Result<Subscription> result = gateway.subscription().create(request);
You can only add an add-on or discount to a subscription once. If you’d like to apply an add-on or discount to a subscription several times, you can pass quantity when creating or updating the add-on/discount.