chargify / chargify_api_ares

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

Multi-site support? #174

Open toddrun opened 4 years ago

toddrun commented 4 years ago

We're currently using chargify_api_ares for a small set of products. The business would like to convert another profit center to use Chargify, but because the products are so different, they've asked to create the products in a new Site.

Because Chargify.config sets the domain/site globally, how would we be able to support using the gem with different Sites? Any suggestions are most appreciated!

toddrun commented 4 years ago

We attempted to solve this by cloning Chargify, but THIS DOES NOT WORK.

In "lib/chargify_sites":

module ChargifySites
  def self.acme
    @acme ||= Chargify.clone
  end

  def self.other
    @other ||= Chargify.clone
  end
end

In an initializer:

ChargifySites.acme.configure do |config|
  config.api_key = ENV["CHARGIFY_ACME_API_KEY"]
  config.subdomain = ENV["CHARGIFY_ACME_SUBDOMAIN"]
end

ChargifySites.other.configure do |config|
  config.api_key = ENV["CHARGIFY_OTHER_ACME_API_KEY"]
  config.subdomain = ENV["CHARGIFY_OTHER_ACME_SUBDOMAIN"]
end

# To allow our legacy code to continue working until we convert it to be part of ChargifySite 
Chargify.configure do |config|
  config.api_key = ENV["CHARGIFY_API_KEY"]
  config.subdomain = ENV["CHARGIFY_SUBDOMAIN"]
end

This lets us get objects with different "api_key" and "subdomain" values. However, calls are made through Chargify, meaning you're still stuck with a single site.

Here's an example of why this won't work:


Chargify set to "other"
irb(main):001:0> ENV["CHARGIFY_API_KEY"]
=> "qd...D4"
irb(main):002:0> ChargifySite.other::Subscription.find(:all).count
=> 15
irb(main):003:0> ChargifySite.acme::Subscription.find(:all).count
=> 15

Chargify set to "acme"
irb(main):001:0> Chargify.subdomain
=> "demo-4930603888"
irb(main):002:0> ENV["CHARGIFY_API_KEY"]
=> "Ac..LI"
irb(main):003:0> ChargifySite.other::Subscription.find(:all).count
=> 20
irb(main):004:0> ChargifySite.acme::Subscription.find(:all).count
=> 20

Chargify set to nothing
irb(main):001:0> ENV["CHARGIFY_API_KEY"]
=> "XXXXXXXXXXXXXXXXXXXX"
irb(main):006:0> ChargifySite.other::Subscription.find(:all)
Traceback (most recent call last):
        1: from (irb):6
ActiveResource::UnauthorizedAccess (Failed.  Response code = 401.  Response message = Unauthorized.)