chargify / chargify_api_ares

A Chargify API wrapper for Ruby using ActiveResource
http://chargify.com
MIT License
161 stars 95 forks source link

Update Subscription's payment profile #158

Open rscardinho opened 6 years ago

rscardinho commented 6 years ago

Hey,

In the platform that I'm working on, I got blocked (for a short period) because this gem doesn't support the change_payment_profile endpoint add by July 2016.

I was thinking about adding support for the endpoint and using it like this:

subscription = Chargify::Subscription.find(id)
payment_profile = Chargify::PaymentProfile.create

subscription.update_payment_profile(payment_profile.id)

Any suggestions?

API reference https://reference.chargify.com/v1/subscriptions/cancel-subscription-remove-delayed-method#change-default-payment-profile

blakeprudhomme commented 6 years ago

@rscardinho we ran into this issue as well when we wanted to allow our subscribers to add and/or switch between multiple payment methods. Currently we are accomplishing it like this...

module Chargify
  class SubscriptionPaymentProfile < Base
    set_prefix "/subscriptions/:subscription_id/"
    self.collection_name = "payment_profiles"
    include ResponseHelper

    def change_payment_profile
      post :change_payment_profile
    end
  end
end
def update_default_payment_profile(payment_profile_id)
  sub = Chargify::Subscription.find(sub_id)

  pp = Chargify::SubscriptionPaymentProfile.new
  pp.id = payment_profile_id
  pp.prefix_options = { subscription_id: sub.id }
  pp.instance_variable_set(:@persisted, true)
  pp.change_payment_profile
end