title: paypal recurring gem & trial period
body: <p>I implemented paypal recurring by following Rails Casts EP289. <a href="http://railscasts.com/episodes/289-paypal-recurring-billing?view=asciicast" rel="nofollow noreferrer">http://railscasts.com/episodes/289-paypal-recurring-billing?view=asciicast</a></p>
  <p>It works fine for normal process, but when I tried to implement trial period, I got some problem. It charges the sum of trial amount and recurring billing amount at the first billing.
  (I only want to charge trial amount)</p>
  <p>I checked through internet, but I can't figure out how to do that so far.
  What am I missing about implementing this correctly?</p>
  <p>Below is the code.</p>
  <h1>paypal_payment.rb</h1>
  <pre><code>class PaypalPayment
    def initialize(subscription)
      @subscription = subscription
      @price = Price.find_by_currency_code_and_plan_id(&quot;JPY&quot;, @subscription.plan.id)
    end
  
    def checkout_details
      PayPal::Recurring.new(token: @subscription.paypal_payment_token).checkout_details
    end
  
    def checkout_url(options)
      process(:checkout, options).checkout_url
    end
  
    def make_recurring
      process :request_payment
      process :create_recurring_profile, period: :monthly, frequency: 1, start_at: Time.now + 1.month
    end
  
  private
  
    def process(action, options = {})
      options = options.reverse_merge(
        token: @subscription.paypal_payment_token,
        payer_id: @subscription.paypal_customer_token,
        item_name: @subscription.plan.name,
        item_amount: @price.amount.to_i,
        description: &quot;Something&quot;
        amount: @price.amount.to_i,
        outstanding: :next_billing,
        trial_amount: @price.initial_amount.to_i,
        trial_period: :monthly,
        trial_frequency: 1,
        trial_length: 1,
        currency: @price.currency_code,
        locale: &quot;ja_JP&quot;
      )
      response = PayPal::Recurring.new(options).send(action)
      raise response.errors.inspect if response.errors.present?
      response
    end
  end
  </code></pre>
  <h1>subscription.rb</h1>
  <pre><code>class Subscription &lt; ActiveRecord::Base
    belongs_to :plan
    belongs_to :student
    has_many :transactions, :class_name =&gt; &quot;SubscriptionTransaction&quot;
    validates_presence_of :plan_id
    validates_presence_of :student_id
    attr_accessor :paypal_payment_token
    attr_accessible :paypal_customer_token, :paypal_recurring_profile_token, :plan_id, :student_id, :paypal_payment_token
  
    def save_with_payment
      if valid?
        if payment_provided?
          save_with_paypal_payment
        end
      end
    end
  
    def paypal
      PaypalPayment.new(self)
    end
  
    def save_with_paypal_payment
      response = paypal.make_recurring
      self.paypal_recurring_profile_token = response.profile_id
      save!
    end
  
    def payment_provided?
      paypal_payment_token.present?
    end
  end
  </code></pre>
  <h1>subscriptions_controller.rb</h1>
  <pre><code>require 'time'
  class SubscriptionsController &lt; ApplicationController
    before_filter :authenticate_user!, :is_student?
  
    def index
      @title = I18n.t &quot;subscriptions.index.title&quot;
      @student = Student.find_by_id(current_user.profile_id)
      if @student.is_trial == true
        redirect_to 'new'
      end
      @subscription = @student.subscription
      @plan = @subscription.plan
      Time.zone = &quot;Tokyo&quot;
      @date = Time.now.in_time_zone.to_date
      @total_lesson_times = Lesson.find_all_by_student_id(@student.id).size
      @lastm_lesson_times = Lesson.where(&quot;student_id = :student_id AND lesson_day &gt;= :start_date AND lesson_day &lt;= :end_date&quot;,
        {:student_id =&gt; @student.id, :start_date =&gt; @date.beginning_of_month()-1.month, :end_date =&gt; @date.end_of_month()-1.month}).size
  
      @thism_lesson_times = Lesson.where(&quot;student_id = :student_id AND lesson_day &gt;= :start_date AND lesson_day &lt;= :end_date&quot;,
          {:student_id =&gt; @student.id, :start_date =&gt; @date.beginning_of_month(), :end_date =&gt; @date.end_of_month()}).size
    end
  
    def new
      @title = I18n.t &quot;subscriptions.new.title&quot;
      @plans = Plan.all
      @student = Student.find_by_id(current_user.profile_id)
      if @student.is_trial == false &amp;&amp; @student.is_active == false
        redirect_to 'index'
      end
      @subscription = Subscription.new
      @date = Time.now.in_time_zone.to_date
      @total_lesson_times = Lesson.find_all_by_student_id(@student.id).size
      @lastm_lesson_times = Lesson.where(&quot;student_id = :student_id AND lesson_day &gt;= :start_date AND lesson_day &lt;= :end_date&quot;,
        {:student_id =&gt; @student.id, :start_date =&gt; @date.beginning_of_month()-1.month, :end_date =&gt; @date.end_of_month()-1.month}).size
  
      @thism_lesson_times = Lesson.where(&quot;student_id = :student_id AND lesson_day &gt;= :start_date AND lesson_day &lt;= :end_date&quot;,
          {:student_id =&gt; @student.id, :start_date =&gt; @date.beginning_of_month(), :end_date =&gt; @date.end_of_month()}).size
    end
  
    def modify
  
    end
  
    def cancel
      student = Student.find(current_user.profile_id)
      @subscription = student.subscription
      ppr = PayPal::Recurring.new(:profile_id =&gt; @subscription.paypal_recurring_profile_token)
      response = ppr.suspend
      if response.success?
        student.update_attributes(:is_active =&gt; false)
        flash[:notice] = I18n.t &quot;subscriptions.cancel.notice_flash&quot;
        redirect_to root_path
      end
    end
  
    def reactivate
      student = Student.find_by_id(current_user.profile_id)
      @subscription = student.subscription
      ppr = PayPal::Recurring.new(:profile_id =&gt; @subscription.paypal_recurring_profile_token)
      response = ppr.reactivate
      if response.success?
        student.update_attributes(:is_active =&gt; true)
        flash[:success] = I18n.t &quot;subscriptions.reactivate.success_flash&quot;
        redirect_to root_path
      end
    end
  
    def paypal_checkout
      plan = Plan.find(params[:plan_id])
      student = Student.find(current_user.profile_id)
      subscription = plan.subscriptions.build
      redirect_to subscription.paypal.checkout_url(
        return_url: subscriptions_confirm_url(:plan_id =&gt; plan.id),
        cancel_url: new_subscription_url
      )
    end  
  
    def confirm
      @plan = Plan.find(params[:plan_id])
      @student = Student.find(current_user.profile_id)
      @subscription = @plan.subscriptions.build
      if params[:PayerID]
        @subscription.student_id = @student.id
        @subscription.paypal_customer_token = params[:PayerID]
        @subscription.paypal_payment_token = params[:token]
      end
    end
  
    def complete
      student = Student.find(current_user.profile_id)
      Time.zone = student.user.time_zone
      current_time = Time.now.in_time_zone
      current_date = current_time.to_date
      @subscription = Subscription.new(
        :plan_id =&gt; params[:subscription][:plan_id],
        :student_id =&gt; params[:subscription][:student_id],
        :paypal_customer_token =&gt; params[:subscription][:paypal_customer_token],
        :paypal_payment_token =&gt; params[:subscription][:paypal_payment_token]
      )
      if @subscription.save_with_payment
        student.update_attributes(:is_active =&gt; true, :is_trial =&gt; false, :expiration_date =&gt; current_date + 1.month - 1.day)
        redirect_to root_path, :notice =&gt; (I18n.t &quot;subscriptions.complete.success_flash&quot;)
      else
        flash[:notice] = I18n.t &quot;error_flash&quot;
        redirect_to new_subscription_path
      end
    end
  
    private
    def is_student?
      if current_user.profile_type != &quot;Student&quot;
        flash[:notice] = I18n.t &quot;is_student_notice_flash&quot;
        redirect_to root_path
      end
    end
  end
  </code></pre>