Reputation: 971
I have stripe subscriptions on my site. yesterday i created a basic way for the user to view their payment history and cards used. i now want to break this up into two views so that the payment history is on one page and the card(s) used is on another page. I feel like i must be missing something but when i tried to do this nothing is showing on the page. I am pretty new to rails so the whole stripe subscriptions thing has been a bit of a challenge but getting there. This is the original code could someone offer some guidance as to the steps i need to follow. I tried literally splitting this into two different view file but that didn't work so i have gone back to where i was originally now!
subcriptions/show.html.erb
<h1>View your payment history and cards</h1>
<h4><%= @charges.data.length %> Charges! </h4>
<% @charges.data.each do |charge| %>
<%= charge.statement_description %>
£<%= charge.amount %>
<%= Time.at(charge.created).strftime("%d/%m/%y") %>
<% end %>
<h4><%= @charges.data.length %> Cards! </h4>
<% @cards.each do |card| %>
<%= card.brand %>
**** **** **** <%= card.last4 %>
<%= card.exp_month %>/<%= card.exp_year %>
<% end %>
subscriptions controller:
class SubscriptionsController < ApplicationController
def new
@subscription = Subscription.new
end
def create
# raise 'a'
@subscription = Subscription.new(params[:subscription].permit(:stripe_card_token))
@subscription.user = current_user
if @subscription.save_with_payment(params[:plan]) #(current_user)
redirect_to @subscription, :notice => "Thank you for subscribing!"
else
render :new
end
end
def destroy
end
def show
customer_token = current_user.subscription.try(:stripe_customer_token)
@charges = customer_token ? Stripe::Charge.all(customer: customer_token) : []
@cards = customer_token ? Stripe::Customer.retrieve(customer_token).cards : []
end
end
subscriptions model
class Subscription < ActiveRecord::Base
belongs_to :user
validates_presence_of :user_id #it should be always compulsary
# attr_accessible :plan
attr_accessor :stripe_card_token
def save_with_payment(plan)
if valid?
customer = Stripe::Customer.create(description: 'subscription', plan: plan, card: stripe_card_token)
self.stripe_customer_token = customer.id
save!
end
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
errors.add :base, "There was a problem with your credit card."
false
end
end
Upvotes: 0
Views: 96
Reputation: 52377
The most simple way to achieve it, is to create 2 methods in controller (for example payments
and cards
):
def payments
customer_token = current_user.subscription.try(:stripe_customer_token)
@charges = customer_token ? Stripe::Charge.all(customer: customer_token) : []
end
def cards
customer_token = current_user.subscription.try(:stripe_customer_token)
@cards = customer_token ? Stripe::Customer.retrieve(customer_token).cards : []
end
create 2 views for these (payments.html.erb
and cards.html.erb
under /views/subscriptions/
directory):
payments.html.erb:
<h1>View your payment history</h1>
<h4><%= @charges.data.length %> Charges! </h4>
<% @charges.data.each do |charge| %>
<%= charge.statement_description %>
£<%= charge.amount %>
<%= Time.at(charge.created).strftime("%d/%m/%y") %>
<% end %>
cards.html.erb:
<h1>View your cards</h1>
<h4><%= @charges.data.length %> Cards! </h4>
<% @cards.each do |card| %>
<%= card.brand %>
**** **** **** <%= card.last4 %>
<%= card.exp_month %>/<%= card.exp_year %>
<% end %>
and edit routes by:
resources :subscriptions do
get '/payments', to: 'subscriptions#payments' # or get 'subscriptions/payments', to: 'subscriptions#payments' (depending on which link you would like to see)
get '/cards', to: 'subscriptions#cards'
end
This would be pretty much it. Run rake routes
to see generated routes to use it correctly and you're good to go. Please check for spelling mistakes in case you'll copy something - not sure if I didn't make any.
Upvotes: 1