andrewculver / koudoku

Robust subscription support for Rails with Stripe.
MIT License
1.16k stars 187 forks source link

Improvements and updates from Bullet Train #194

Open andrewculver opened 5 years ago

andrewculver commented 5 years ago

People frequently ask whether Koudoku is still maintained because master hasn't received much attention for a long time. Many people are still actively using Koudoku, but most of my personal efforts with development on it are now limited to keeping it working well in the context of Bullet Train specifically. This work is much easier for me and much less time consuming because I control so much more of the context of the apps that are built on top of Bullet Train (e.g. Rails version, Devise, CanCanCan, Bootstrap, UI, etc.) It also doesn't hurt that I'm compensated financially by folks who run into issues or need new features (e.g. the support we recently added for Rewardful and Link Mink affiliate programs.)

I've kept many of these updates in a separate branch until now. In the short term, this PR can help people understand which things have changed in the bullet-train branch of Koudoku. It's possible that using this branch will help Koudoku work better in modern Rails apps, but I can't be sure because the only modern Rails apps I work on anymore are Bullet Train-based.

The only thing not included in this branch are lots of modifications and customizations made to the .html.erb views. All of those are specific to Bullet Train's UI CSS and are only contained in the private Bullet Train repo. I have no idea off the top of my head what state the views in this repository are in, or which version of Bootstrap they use, etc. If someone wants to provide fixing updates for the latest version of Bootstrap, please send a PR!

andrewculver commented 5 years ago

Also, if you run into any trouble using this branch, the only other customizations (other than the views) that are in the Bullet Train repo would be around the Subscription model. Here's what it currently looks like in Bullet Train:

class Subscription < ActiveRecord::Base
  include Koudoku::Subscription

  belongs_to :team
  belongs_to :coupon, optional: true

  def active?
    plan.present?
  end

  def push_customer_details_to_stripe
    if subscriptions_enabled?
      unless test_record?

        customer = Stripe::Customer.retrieve(self.stripe_id, api_key: ENV['STRIPE_SECRET_KEY'])
        customer.email = team.formatted_email_address
        customer.description = team.billing_name

        # merge metadata as not to overwrite other people's metadata.
        metadata = JSON.parse(customer.metadata.to_s) || {}
        metadata.merge!(team.stripe_metadata)
        customer.metadata = metadata

        customer.save

        # also update the subscription quantity.
        customer.update_subscription({
          plan: self.plan.stripe_id,
          prorate: Koudoku.prorate,
          quantity: team.subscription_quantity,
        })

      end
    end
  end

  def test_record?
    self.stripe_id == 'cus_938yfhrfv7yzs9f8hed'
  end

end