1

I'm working on getting all the active members in our Braintree account. I can list all customers but I don't know how to list the active ones.

In the Braintree dashboard, I can easily see it by going to subscriptions and filtering all Active subscriptions then clicking the subscription ID. From there I can see which customer has that subscription ID.

Then I tried getting all the active subscriptions first but I can't find any connection with any customers either.

I'm using the PHP SDK.

Here's how I get our active subscriptions.

My subscription code in my library:

function active_subscriptions(){
    return Braintree_Configuration::gateway()->subscription()->search([
        Braintree_SubscriptionSearch::status()->in([Braintree_Subscription::ACTIVE])
      ]);
}

Here's for the controller:

function active_subscriptions(){
    $active_subscriptions = $this->braintree_lib->active_subscriptions();
    $counter = 5;
    foreach($active_subscriptions as $subscription) {
        if($counter == 0){
            die();
        }
        echo 'Subscription ID: '.$subscription->id.'<br />';
        echo 'merchantAccountId: '.$subscription->merchantAccountId.'<br />';
        echo 'planId: '.$subscription->planId.'<br /><br />';

        $counter--;
    }
}

I found this but it's in Ruby on Rails and there are no details about it in the documentation.

elimariaaa
  • 796
  • 4
  • 10
  • 30
  • Braintree has a reasonably well documented API... Did you check this? https://developers.braintreepayments.com/reference/response/credit-card/php – Javier Larroulet Sep 26 '18 at 23:12

1 Answers1

3

Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.

Just to make sure I understand the result you are looking for: You are currently attempting to retrieve all of the customer records that are tied to active subscriptions, correct?

If so, you can achieve this by doing the following:

  1. Iterate over the collection of active subscriptions you received from your search result.

  2. Parse each subscription object for the payment method token associated to each subscription.

  3. Separately, run a paymentMethod.find() call to retrieve the payment method's object.

  4. The payment method object will contain the customer ID.

After following the above steps you can then create a list from your results, which will contain all customers with active subscriptions.

Here is a basic example of how this would be achieved:

$collection = $gateway->subscription()->search([
  Braintree_SubscriptionSearch::status()->in(
    [Braintree_Subscription::ACTIVE]
    )
]);

foreach($collection as $subscription) {
    $token = $subscription->paymentMethodToken;
    $paymentMethod = $gateway->paymentMethod()->find($token);
    $customer = $paymentMethod->customerId;
    echo $customer . "\n";
}

If this isn't what you are looking for, or if you have any additional questions you can reach out to Braintree Support directly and we can assist you further.

Community
  • 1
  • 1
bbusby
  • 148
  • 7
  • Hi @bbusby, this works but it takes a lot of time. My program is getting error: `Maximum execution time of 120 seconds exceeded`. Is there a faster way? – elimariaaa Sep 30 '18 at 10:10
  • Hello @jeepers_creepers. Depending on the amount of records you are attempting to pull as well as the amount of associated records to each subscription, payment method, and customer, this search can sometimes take a bit to complete. If you would like additional assistance I would recommend emailing in to Braintree Support and including your merchant ID as well as your search attempts. This will allow us to review your specific gateway and offer any suggestions based off of your specific search. – bbusby Oct 01 '18 at 13:18