Thursday, July 28, 2016

Rails Stripe Subscription tax_percent Injection | Fixed issues

Newest questions tagged ruby-on-rails - Stack Overflow

Rails Stripe Subscription tax_percent Injection | Fixed issues


Rails Stripe Subscription tax_percent Injection

Posted: 28 Jul 2016 07:59 AM PDT

I'm looking for feedback on the best way to inject "tax_percent" into my app's Stripe subscription at checkout.

Since I live in NJ, I need to determine if the subscription purchaser (current_user) also lives in NJ. Right now, I'm asking the user at checkout what state they live in and if they select NJ, I add in 7% sales tax through javascript call to update the displayed tax and order total.

When the form is submitted and the subscription created, it's not passing in the tax_percent value as a subscription object since it's only a client-side function. So my challenge is getting that tax_percent injected into the subscription form so it gets submitted to Stripe's API and recorded with Stripe.

I think I have a couple options and I was wondering what the best approach to this issue is.

  1. The first option I thought of is to ask the user what state they live in when they first sign-up to the site through devise. I can add a home_state column to the user's table. I'll also need to add in a tax_percent column to my subscription table. Then I'll have a current_user.home_state value I can query at checkout.

At checkout I can essentially ask:

<% if current_user.home_state == "NJ" %>       <%= hidden_field :subscription, :tax_percent, :value => '7' %>  <% end %>  

But since this value can be changed by a savvy user, this might not be the best approach.

How else can I set the :tax_percent more securely? Again, it only applies to NJ residents. If you don't live in NJ, tax_percent can be null.

For some background, I was initially trying to figure out how to pass it in from the javascript math (Stripe and tax_percent in my Rails app)

Thanks!

Select equality expressions in Rails

Posted: 28 Jul 2016 07:58 AM PDT

I'm looking to select equality expressions in Rails, for simple possible-duplicate candidate detection.

Given the fields first_name, last_name, and email a query like the following would help identify possible duplicates:

SELECT id,         -- Equality Expressions         (first_name = :first_name) AS first_name_match,         (last_name  = :last_name)  AS last_name_match,         (email = :email)           AS email_match  FROM examples WHERE (id <> :id)  

I've tried to do this using ActiveRecord as well as Arel but it appears you can't alias equality expressions in Arel.

I know this isn't the best performance method although I'm not expecting there to be enough records for the performance of this query to be an issue.

Is there a way I can build this query using ActiveRecord or Arel without using raw SQL?

Upgrading Dynamoid from 0.7 to 1.0

Posted: 28 Jul 2016 07:58 AM PDT

I am getting the following error on migrating from Dynamoid 0.7 to 1.0.

Error:

cannot load such file -- dynamoid/adapter/aws_sdk

The error arises at the line :

require 'dynamoid/adapter/aws_sdk'  

Can any one help me figure out the issue.

Rails Stripe Gem - page needs to be refreshed before payment will process

Posted: 28 Jul 2016 07:49 AM PDT

I'm currently building an Events site using rails. I'm using the Stripe gem to process payments. Everything seems to be working fine other than whenever I want to make a payment (in test/development mode at present) I have to refresh the page and re-enter payment details before the payment will process. There's no error code coming up but it happens every time.

What could be causing this? Here's the relevant code -

bookings_controller.rb

class BookingsController < ApplicationController    before_action :authenticate_user!    def new      # booking form      # I need to find the event that we're making a booking on      @event = Event.find(params[:event_id])      # and because the event "has_many :bookings"      @booking = @event.bookings.new      # which person is booking the event?      @booking.user = current_user      #@booking.quantity = @booking.quantity      #@total_amount = @booking_quantity.to_f * @event_price.to_f    end    def create      # actually process the booking      @event = Event.find(params[:event_id])      @booking = @event.bookings.new(booking_params)      @booking.user = current_user      #@total_amount = @booking.quantity.to_f * @event.price.to_f        Booking.transaction do          @booking.save!          @event.reload          if @event.bookings.count > @event.number_of_spaces              flash[:warning] = "Sorry, this event is fully booked."              raise ActiveRecord::Rollback, "event is fully booked"          end       end        if @booking.save            # CHARGE THE USER WHO'S BOOKED          # #{} == puts a variable into a string          Stripe::Charge.create(amount: @event.price_pennies, currency: "gbp",              card: @booking.stripe_token, description: "Booking number #{@booking.id}")            flash[:success] = "Your place on our event has been booked"          redirect_to event_path(@event)      else          flash[:error] = "Payment unsuccessful"          render "new"      end        if @event.is_free?            @booking.save!          flash[:success] = "Your place on our event has been booked"          redirect_to event_path(@event)      end  end      #def total_amount      #@total_amount = @booking.quantity * @event.price  

No comments:

Post a Comment