1

I am using Braintree payment gateway in my Rails application. I am wondering if I can retrieve the customer's subscriptions details from it. According to the documentation, one of the way of doing this is subscription = Braintree::Subscription.find(id)

When creating a subscription, basic objects such as plan_id and amount was saved into the database. So, how do I retrieve subscription's information such as next_billing_date that is associated to the customer?

Terng Gio
  • 69
  • 1
  • 1
  • 8

1 Answers1

1

Assuming you have a subscription ID:

# Find the subscription
subscription = Braintree::Subscription.find(sub_id)

# Access the subscription's next billing date
subscription.next_billing_date

Braintree::Subscription.find() returns a Braintree::Subscription result object.

Assuming you have a customer ID:

# Find the customer
customer = Braintree::Customer.find(cust_id)

# Retrieve the customer's first payment method
pm = customer.payment_methods.first

# Retrieve the subscriptions created with that payment method
subscriptions = pm.subscriptions

# Access the subscription's next billing date
subscriptions.first.next_billing_date

Braintree::Customer.find() returns a Braintree::Customer response object. The customer's payment methods can then be retrieved. Subscriptions are associated to payment methods. Once you have a payment method, you can retrieve an array of Braintree::Subscription objects.

Shea
  • 886
  • 6
  • 12
  • After retrieving the for example `subscription.next_billing_date`, how do I display it to user on the html because the `next_billing_date` is not in my database. – Terng Gio Sep 15 '17 at 03:33
  • That is a separate question. I recommend starting a new question if you can't find the answer through research. – Shea Sep 15 '17 at 14:25