17

I want to collect all of a Braintree Customer's subscriptions. When I browse to a customer's page in the gateway, I can see their subscriptions, but it doesn't seem that a method like subscriptions exists for Braintree::Customer, or that I can search for Braintree::Subscriptions by customer_id.

There are roundabout ways that I can access all of a customer's subscriptions, but they are very slow. For example, I can retrieve all of the customer's transactions, and for each transaction, get the subscription_id (if it exists), and then retrieve the subscription with that ID. This involves a lot of communication with the Braintree API, and I was hoping for a more efficient solution.

Oh, and I'm programming this in rails, but the question doesn't seem Rails-specific.

agf
  • 171,228
  • 44
  • 289
  • 238
bissej
  • 285
  • 3
  • 9

1 Answers1

30

I work at Braintree.

Customers have a credit_cards array, and each credit card has a subscriptions array. So, if you want all the subscriptions for a customer, you can do:

customer.credit_cards.map(&:subscriptions).flatten
Nil
  • 174
  • 1
  • 3
  • 14
agf
  • 171,228
  • 44
  • 289
  • 238
  • 1
    What needs to be changed to take subscriptions via paypal into account? – Christoph Geschwind Aug 13 '14 at 12:58
  • 3
    @ChristophGeschwind In Ruby, you can do `customer.payment_methods.map(&:subscriptions).flatten` to get them all, or both `customer.credit_cards` and `customer.paypal_accounts` to get them each separately. – agf Aug 14 '14 at 16:38
  • Thank you, I think an update on your answer would be appropriate, too. – Christoph Geschwind Sep 23 '14 at 14:30
  • 1
    Is there an easy way to pull the customer associated with a subscription? So basically the reverse of what OP is asking. – Sonny Parlin Dec 20 '14 at 17:22
  • 3
    @SonnyParlin [Subscriptions have a payment method token](https://developers.braintreepayments.com/android+ruby/sdk/server/recurring-billing/details), so you can [find a payment method](https://developers.braintreepayments.com/android+ruby/sdk/server/payment-method-management/search#single-payment-method). [Payment methods have a customer id](https://developers.braintreepayments.com/android+ruby/reference/objects/credit-card#string-attributes) so you can [find a customer](https://developers.braintreepayments.com/android+ruby/sdk/server/customer-management/search#single-customer). – agf Dec 21 '14 at 02:27
  • Whatever happened to `customer.payment_methods` ? I can only see credit_cards and paypal_accounts. – Martol1ni Jan 03 '15 at 12:00
  • @Martol1ni It's not a documented part of our api and may not exist in all languages. You can see it in Ruby [on github](https://github.com/braintree/braintree_ruby/blob/master/lib/braintree/customer.rb#L145-L147). – agf Jan 03 '15 at 18:40
  • Hi, Is there a way to get list of subscriptions that are past due for payment in python? – Yaseen Shareef Jan 09 '15 at 11:36
  • @YaseenShareef you can do a [subscription search by status](https://developers.braintreepayments.com/javascript+python/sdk/server/recurring-billing/search#status). – agf Jan 09 '15 at 16:57
  • @agf how do I get a customers subscriptions via customerId/email in PHP – Ali Gajani Sep 21 '15 at 11:40
  • I tried to use this approach and failed because [my subscription had no `payment_method_token`](http://stackoverflow.com/questions/33218867/why-would-a-subscription-have-no-payment-method-token-and-how-can-i-map-it-to-a). Any idea why? – Mark Amery Oct 19 '15 at 16:01
  • 3
    I was integrating node.js with braintree subscription. Braintree provided good documentation. However, it lacks of a guide for implementing a complete flow for implementing subscription and it took me the whole day to figure it out. Hence, I have wrote a guide to help whoever want to use braintree subscription for their services here http://enormers.com/blog/a-brief-guide-to-implement-braintree-subscription-with-node-js-and-mongodb/ – Mark Thien Jan 18 '16 at 01:33
  • @agf, how do I get a customers subscriptions via the customerId in PHP API? – Ali Gajani Apr 07 '16 at 23:26
  • @AliGajani You do a [customer find](https://developers.braintreepayments.com/reference/request/customer/find/php) then you can find each payment method's subscriptions at: `$customer->paymentMethods[0]->subscriptions` etc. – agf Apr 08 '16 at 21:16
  • 1
    @agf Is there a reason why you guys can't just put the customer id in the subscription response object? We ran into an issue migrating data associated with a legacy app where there were hundreds of cancelled subscriptions that didn't have a payment_method_token so we were unable to associate the subscription to a customer. Braintree also has no customer information associated with the subscriptions in the dashboard, so I guess even the API doesn't know who they belong to. – trueinViso Feb 24 '17 at 20:48
  • I think someone deleted the customer, so I guess that would be the problem -_-. – trueinViso Feb 24 '17 at 20:53
  • I can see that the Customer object has the credit card array but also has a lot of other arrays, like ApplePayCards, AndroidPayCards, PayPalAccounts, etc. Should I iterate over all of these arrays? If braintree adds a new object to that object, will I have to change my code? – vgaltes Dec 12 '18 at 15:07
  • @vgaltes https://developers.braintreepayments.com/reference/response/customer/ruby#payment_methods – agf Dec 13 '18 at 02:27
  • Is it just me or is this a shortcoming of the API? `SubscriptionSearchRequest` should either expose a `customerId()` or `CustomerSearchRequest` should expose something like `subscription().status()` - this request should not have anything to do with payment methods. I just want to make sure a client does not accidentally subscribe for a subscription twice. But fair enough - iterating all payment methods and their respective subscriptions is probably one way to do it .. – Stefan Falk Dec 31 '18 at 13:11