Skip to content
/ pay Public
forked from pay-rails/pay

Payments for Ruby on Rails apps

License

Notifications You must be signed in to change notification settings

rathboma/pay

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pay

Codeship Status for jasoncharnes/pay

Pay is a subscription engine for Ruby on Rails.

Supports Ruby on Rails 4.2 and higher.

Current Payment Providers

  • Stripe (API version 2018-08-23 or higher required)

Payment Providers In Development

  • Braintree

Want to add a new payment provider? Contributions are welcome and the instructions are here.

Installation

Add this line to your application's Gemfile:

gem 'pay'

And then execute:

$ bundle

Or install it yourself as:

$ gem install pay

Setup

Migrations

This engine will create a subscription model and the neccessary migrations for the model you want to make "billable." The most common use case for the billable model is a User.

To add the migrations to your application, run the following migration:

$ bin/rails pay:install:migrations

This will install two migrations:

  • db/migrate/create_subscriptions.rb
  • db/migrate/add_fields_to_users.rb

Non-User Model

If you need to use a model other than User, check out the wiki page.

Run the Migrations

Finally, run the migrations with $ rake db:migrate

Stripe

You'll need to add your private Stripe API key to your Rails secrets. config/secrets.yml

development:
  stripe_api_key: sk_test_....

Usage

Include the Pay::Billable module in the model you want to know about subscriptions.

# app/models/user.rb
class User < ActiveRecord::Base
  include Pay::Billable
end

To see how to use Stripe Elements JS & Devise, click here.

User API

Creating a Subscription

user = User.find_by(email: '[email protected]')
user.card_token = 'stripe-token'
user.subscribe

A card_token must be provided as an attribute.

The subscribe method has three optional arguments with default values.

def subscribe(name = 'default', plan = 'default', processor = 'stripe')
  ...
end
Name

Name is an internally used name for the subscription.

Plan

Plan is the plan ID from the payment processor.

Retrieving a Subscription from the Database

user = User.find_by(email: '[email protected]')
user.subscription

Checking a User's Subscription Status

user = User.find_by(email: '[email protected]')
user.subscribed?

The subscribed? method has two optional arguments with default values.

def subscribed?(name = 'default', plan = nil)
  ...
end
Name

Name is an internally used name for the subscription.

Plan

Plan is the plan ID from the payment processor.

Processor

Processor is the string value of the payment processor subscription. Pay currently only supports Stripe, but other implementations are welcome.

Retrieving a Payment Processor Account

user = User.find_by(email: '[email protected]')
user.customer

Updating a Customer's Credit Card

user = User.find_by(email: '[email protected]')
user.update_card('stripe-token')

Retrieving a Customer's Subscription from the Processor

user = User.find_by(email: '[email protected]')
user.processor_subscription(subscription_id)

Subscription API

Checking a Subscription's Trial Status

user = User.find_by(email: '[email protected]')
user.subscription.on_trial?

Checking a Subscription's Cancellation Status

user = User.find_by(email: '[email protected]')
user.subscription.cancelled?

Checking a Subscription's Grace Period Status

user = User.find_by(email: '[email protected]')
user.subscription.on_grace_period?

Checking to See If a Subscription Is Active

user = User.find_by(email: '[email protected]')
user.subscription.active?

Cancel a Subscription (At End of Billing Cycle)

user = User.find_by(email: '[email protected]')
user.subscription.cancel

Cancel a Subscription Immediately

user = User.find_by(email: '[email protected]')
user.subscription.cancel_now!

Swap a Subscription to another Plan

user = User.find_by(email: '[email protected]')
user.subscription.swap("yearly")

Resume a Subscription on a Grace Period

user = User.find_by(email: '[email protected]')
user.subscription.resume

Retrieving the Subscription from the Processor

user = User.find_by(email: '[email protected]')
user.subscription.processor_subscription

Contributors

Contributing

👋 Thanks for your interest in contributing. Feel free to fork this repo.

If you have an issue you'd like to submit, please do so using the issue tracker in GitHub. In order for us to help you in the best way possible, please be as detailed as you can.

If you'd like to open a PR please make sure the following things pass:

  • rake test
  • rubocop

These will need to be passing in order for a Pull Request to be accepted.

License

The gem is available as open source under the terms of the MIT License.

About

Payments for Ruby on Rails apps

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 85.4%
  • HTML 10.9%
  • JavaScript 3.2%
  • CSS 0.5%