Returns a collection of Subscription response objects.

You can search for subscriptions using a variety of criteria. For operators available on search fields, see the search fields page.

Node.js
var stream = gateway.subscription.search(function (search) {
  search.planId().is("the_plan_id");
}, function (err, response) {
  response.each(function (err, subscription) {
    console.log(subscription.billingDayOfMonth);
  });
});

If you want to look up a single subscription using its ID, use subscription.find() instead.

Parameters
billingCyclesRemaining range

The number of billing cycles remaining in the subscription.

createdAt range

The date/time the subscription was created.

daysPastDue range

The number of days past due the subscription is.

id text

The ID of the subscription.

ids multiple

A list of subscription IDs to search for.

inTrialPeriod multiple

Whether the subscription is in a trial period. Can be true or false. This parameter must be used in conjunction with status.

merchantAccountId multiple

A list of merchant account IDs to search for.

nextBillingDate range

The date the subscription will next be billed.

planId multiple

A list of plan IDs to search for.

price range

The price of the subscription.

status multiple

The status of the subscription. Possible values:

  • Active
  • Canceled
  • PastDue
  • Pending
  • Expired
transactionId text

The transaction ID associated with the subscription.

Search results

All examples assume version 1.11.0 or greater. The search method returns a node stream and can be consumed as any other stream.

Node.js
var stream = gateway.subscription.search(function (search) {
  search.price().between(5, 15);
});

stream.pipe(someWritableStream);
Node.js
var stream = gateway.subscription.search(function (search) {
  search.price().between(5, 15);
});

stream.on("data", function (subscription) {
  // ...
});

stream.on("end", function () {
  // ...
});

stream.resume();

An example of searching prior to version 1.11.0 can be found on the search results page.

Examples

Searching by price

Searching on price uses a range field.

Node.js
var stream = gateway.subscription.search(function (search) {
  search.price().between(5, 15);
});

Searching by plan ID

Searching on plan ID uses a multiple value field.

Node.js
var stream = gateway.subscription.search(function (search) {
  search.planId().is("goldPlan");
});

Searching by status

Searching on status uses a multiple value field.

Node.js
var stream = gateway.subscription.search(function (search) {
  search.status().is(braintree.Subscription.Status.Active);
});

When searching for subscriptions that are active, you have the ability to search for all active subscriptions (with or without a trial period), subscriptions with a trial period, or subscriptions without a trial period.

Node.js
var stream = gateway.subscription.search(function (search) {
  search.status().is(braintree.Subscription.Status.Active);
});
Node.js
var stream = gateway.subscription.search(function (search) {
  search.status().is(braintree.Subscription.Status.Active);
  search.inTrialPeriod().is(true);
});
Node.js
var stream = gateway.subscription.search(function (search) {
  search.status().is(braintree.Subscription.Status.Active);
  search.inTrialPeriod().is(false);
});

Searching by days past due

Searching on days past due uses a range field.

Node.js
var stream = gateway.subscription.search(function (search) {
  search.daysPastDue().min(42);
});

Searching by merchant account ID

Searching on merchant account ID acts like a multiple value field.

Node.js
var stream = gateway.subscription.search(function (search) {
  search.merchantAccountId().in("42", "43");
});

Searching by billing cycles remaining

Searching on billing cycles remaining will find subscriptions which have a set limit to the number of times they will recur. It uses a range field.

Node.js
var stream = gateway.subscription.search(function (search) {
  search.billingCyclesRemaining().between(1, 2);
});

Searching by next billing date

Searching on next billing date will return subscriptions that will bill again during the date range you have given it. The example below will return any subscriptions that will be billed in the next five days.

Node.js
var today = new Date();
var fiveDaysFromNow  = new Date();
fiveDaysFromNow.setDate(today.getDate() + 5)

var stream = gateway.subscription.search(function (search) {
  search.nextBillingDate().max(fiveDaysFromNow);
});

Searching by created-at date/time

Searching on created-at date/time will return subscriptions that were created during the range you have given.

Node.js
var today = new Date();
var yesterday = new Date();
yesterday.setDate(today.getDate() - 1);

var stream = gateway.subscription.search(function (search) {
  search.createdAt().between(
    yesterday,
    today
  );
});

Searching a combination

You can combine any of the search fields into a single search.

Node.js
var stream = gateway.subscription.search(function (search) {
  search.planId().is("goldPlan");
  search.status().is(braintree.Subscription.Status.Active);
});

See also