Skip to content

Instantly share code, notes, and snippets.

@fedeagripa
fedeagripa / stripe.rb
Created May 28, 2020 13:44
rails_stripe_initializer
# config/intializers/stripe.rb
Rails.configuration.stripe = {
publishable_key: ENV['STRIPE_PUBLISHABLE_KEY'],
secret_key: ENV['STRIPE_SECRET_KEY']
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]
@fedeagripa
fedeagripa / stripe.js
Created May 28, 2020 14:34
react_stripe_init
import React from 'react';
import { Elements, StripeProvider } from 'react-stripe-elements';
const withStripe = (WrappedComponent) => {
const Stripe = props => (
<StripeProvider apiKey={process.env.stripe_key}>
<Elements
fonts={[{
cssSrc: 'https://fonts.googleapis.com/css?family=Roboto:300,300i,400,500,600'
}]}
@fedeagripa
fedeagripa / billing_form.js
Last active May 28, 2020 15:24
billing_form
import {
CardNumberElement,
CardExpiryElement,
CardCVCElement,
injectStripe
} from 'react-stripe-elements';
import uuid from 'uuid/v1';
/* Your other imports for a usual form */
@fedeagripa
fedeagripa / stripe_service.rb
Created May 28, 2020 14:45
rails_stripe_service
# app/services/stripe_service.rb
require 'stripe'
class StripeService
class StripeException < StandardError
end
attr_reader :shop
@fedeagripa
fedeagripa / cc_controller.rb
Created May 28, 2020 14:46
credit_card_controller
# app/controllers/api/v1/credit_cards_controller.rb
module Api
module V1
class CreditCardsController < Api::V1::ApiController
helper_method :shop
def index
service = StripeService.new(shop)
@card = service.credit_card_info
@customer = service.customer
# app/models/subscription.rb
class Subscription < ActiveRecord::Base
belongs_to :user
belongs_to :purchase_plan # this can be optional if you have annual or monthly plans for example
has_many :subscription_items, dependent: :destroy # I'm going to explain this later
enum status: ['define_your_possible_statuses']
end
@fedeagripa
fedeagripa / stripe_service.rb
Created May 28, 2020 15:19
create subscription
# app/services/stripe_service.rb
def create_subscription
Stripe::Subscription.create(
customer: customer.id,
plan: subs_plan_id, # this is the id of your plan (eg: monthly, annual, etc)
coupon: discount_code # if you have any (check COUPONS section below to understand them in more detail)
)
end
@fedeagripa
fedeagripa / .env.dev
Created May 28, 2020 15:21
react_dev_env
# .env.dev
STRIPE_KEY="pk_test_TYooMQauvdEDq54NiTphI7jx"
@fedeagripa
fedeagripa / devise.rb
Created June 9, 2020 17:32
2fa_initializer
# config/initializers/devise.rb
config.saml_route_helper_prefix = 'saml'
callback = Rails.env.development? ? 'http://localhost:3000' : ENV['SAML_CALLBACK_ADDRESS']
# SAML configuration
config.saml_create_user = true
config.saml_update_user = true
config.saml_default_user_key = :email
config.saml_session_index_key = :session_index
config.saml_use_subject = true
@fedeagripa
fedeagripa / sessions_controller.rb
Created June 9, 2020 17:33
2fa_session_controller
# app/controllers/admin_users/sessions_controller.rb
module AdminUsers
class SessionsController < Devise::SessionsController
# As you are overwriting devise session controller you need this to allow to login with user & pass (dev mode)
prepend_before_action :require_no_authentication, only: [:new, :create]
layout 'active_admin_logged_out'
helper ::ActiveAdmin::ViewHelpers