Dileet
Dileet

Reputation: 2064

Stripe payment logic in rails

I'm trying to having users fill out a form, and then pay to submit it. Once the users pays I need rails to change a boolean from a separate model to true.

Here is my charges controller, exactly from the docs.

class ChargesController < ApplicationController
def new
end

def create
  # Amount in cents
    @amount = 2900

    customer = Stripe::Customer.create(
      :email => current_user.email,
      :card  => params[:stripeToken]
    )

    charge = Stripe::Charge.create(
      :customer    => current_user.email,
      :amount      => @amount,
      :description => 'OneApp',
      :currency    => 'usd'
    )

  rescue Stripe::CardError => e
    flash[:error] = e.message
    redirect_to charges_path
  end

end 

Upvotes: 1

Views: 504

Answers (1)

Miotsu
Miotsu

Reputation: 1776

If the creation of a Stripe::Charge is enough for you, it's as easy as retrieving the instance of the model you want to modify in your create method and set your boolean there.

Say, for example, you want to set a subscribed boolean for the current user, so in your create method you add:

current_user.subscribed = true

or, say you want to set a paid boolean on an Order model instance, then in the create method you add:

order = Order.find_by_some_way(:some_way => the_value_you_want)
order.paid = true unless order.nil?

If you need to konw when the money has actually been transferred, you have to ask Stripe. There's a good gem to integrate Stripe's webhooks:

https://github.com/integrallis/stripe_event

Anyway, if you are trying to know whether a user bought something or not, I'd suggest to wait for the actual transfer notification, because the Charge doesn't actually tell you whether you got the money or not.

Upvotes: 2

Related Questions