I get error 500 (Internal Server Error) when I try to pay with Payola/Stripe (Custom Form) in my Rails 4.1.8 application

639 Views Asked by At

I will like some help. I am working on a "Rails Paid Job Board", and I get error: "**Completed 500 (Internal Server Error)**" whenever I click "PAY" for a job post with Payola(Stripe) Custom Form on my Development Portal.

It's killing enough I really don't know what to tackle to solve it.

Kindly see Chrome Browser Console Error dump

Browser Console Error Dump

Jobs_Controller.rb

class JobsController < ApplicationController
  def index
    @jobs = Job.paid_ad
  end

  def show
    @job = Job.find(params[:id])
  end

  def edit
    @job = Job.find(params[:id])
    redirect_to @job if @job.paid?
  end

  def update
    @job = Job.find(params[:id])
    if !(@job.paid?)
      @job.update_attributes(stripeEmail: params[:stripeEmail],
                             payola_sale_guid: params[:payola_sale_guid]
      )
      # The "has_key?" Returns true if the given key/attribute is present in hash e.g.
      # h = { "a" => 100, "b" => 200 }
      # h.has_key?("a")   #=> true
      # h.has_key?("z")   #=> false.
      # So this means --Update job_params if the given key/attribute is present or not in job.
      @job.update(job_params) if params.has_key?(:job)
      redirect_to  preview_job_path(@job)
    else
      render :edit
    end
  end

  def new
    @job = Job.new
  end

  def create
    @job = Job.new(job_params)
    if @job.save
      redirect_to preview_job_path(@job)
    else
      render :new
    end
  end

  def preview
    @job = Job.find(params[:id])
    redirect_to @job if @job.paid?
  end

  def payment
    @job = Job.find_by_permalink(params[:permalink])
    redirect_to job_path(@job) if @job.paid?
  end

  def search
    @jobs = Job.search(params)
  end


  private

  def job_params
    params.require(:job).permit(:title, :category, :location, :description, :to_apply, :email, :company_name, :website)
  end
end

Model job.rb

class Job < ActiveRecord::Base
  include Payola::Sellable

  validates :title,
            :category,
            :location,
            :description,
            :company_name,
            :website,
            :email,
            :to_apply,
            presence: true
  validates :title, length: { maximum: 75 }
  validates :description, length: { minimum: 300 }
  validates :to_apply, length: { maximum: 500 }

  validates_formatting_of :email, using: :email
  validates_formatting_of :website, using: :url

  before_validation :provide_name, :provide_permalink

  def self.paid_ad
    where.not('stripeEmail' => nil).where.not('payola_sale_guid' => nil).where('created_at > ?', 30.days.ago)
  end

  def paid?
    (!(self.stripeEmail == nil) && !(self.payola_sale_guid == nil))
  end

  def self.search(params)
    jobs = Job.where('name like ? or description like?', "%#{params[:search]}%', '%#{params[:search]}%" ) if params [:search]
    jobs
  end


  private

  def provide_name
    self.name = 'FarFlung' if self.name == nil
  end

  def provide_permalink
    self.permalink = "#{ self.name } #{ SecureRandom.hex }".parameterize if self.permalink == nil
  end
end

View for Payments

<div class="container">
  <div class="row">
    <div class='col-md-4'></div>
    <div class="col-md-4 panel panel-default">
      <%= render 'shared/two_breaks' %>

        <%= simple_form_for(@job, html: { class: 'payola-payment-form',
                                           'data-payola-base-path' => main_app.payola_path,
                                           'data-payola-product' => @job.product_class,
                                           'data-payola-permalink' => @job.permalink }) do |f| %>

            <p style="color:red"><span class="payola-payment-error"></span></p>

            <div class="form-row">
              <div class="col-lg-12 form-group required">
                <label class='control-label'>Your email address</label>
                <input class='form-control require-validation' type="email" name="stripeEmail" data-payola="email" value="<%= @job.email %>" >
              </div>
            </div>

            <div class='form-row'>
              <div class="col-lg-12 form-group card required">
                <label class="control-label">Card number</label>
                <input autocomplete='off' class='form-control card-number' type="text" data-stripe="number"/>
              </div>
            </div>

            <div class='form-row'>
                <div class='col-xs-4 form-group expiration required'>
                  <label class='control-label'>Month</label>
                  <input class='form-control' placeholder='MM' type="text" data-stripe="exp_month"/>
                </div>

                <div class='col-xs-4 form-group expiration required'>
                  <label class='control-label' >Year</label>
                  <input class='form-control' placeholder='YYYY' type="text" data-stripe="exp_year"/>
                </div>

                <div class='col-xs-4 form-group cvc required'>
                  <label class='control-label'>CVC</label>
                   <input autocomplete='off' class='form-control' placeholder='ex. 311' type="text" data-stripe="cvc"/>
                </div>
            </div>

            <div class='form-row'>
              <div class='col-xs-12'>
                <div class='form-control total btn btn-info'>
                  Total:
                  <span class='amount'>#3000</span>
                </div>
              </div>
            </div>


            <div class='form-row'>
              <div class="col-md-12 form-group">
                <%= f.submit 'PAY AND DISPLAY AD NOW', class: 'form-control btn btn-primary submit-button' %>
              </div>
            </div>

        <% end %>

      <p class="text-center"><%= link_to (image_tag('big.png')) %></p>
      <%= render 'shared/two_breaks' %>
    </div>
  </div>
</div>

Route.rb

Rails.application.routes.draw do

  mount Payola::Engine => '/payola', as: :payola
  mount RedactorRails::Engine => '/redactor_rails'
  root 'jobs#index'
  get 'new' => 'jobs#new', as: :new
  get 'jobs/:id/preview' => 'jobs#preview', as: :preview_job
  get 'payments/:permalink' => 'jobs#payment', as: :buy_ad
  get 'jobs/:id/' => 'jobs#show', as: :show_job
  post 'payments/:permalink' => 'payola/transactions#create'


  resources :jobs, except: :destroy do
    collection do
      get :search
    end
  end
end

Development Server log

Started POST "/payola/buy/job/farflung-f9418cfd37de67f5bd733b3d2ea39188" for 127.0.0.1 at 2016-04-16 13:25:55 +0100
Processing by Payola::TransactionsController#create as */*
  Parameters: {"stripeToken"=>"tok_180sWbCc1zXXaitaIc1ThbwS", "stripeEmail"=>"[email protected]", "authenticity_token"=>"5cocM2IsdmhayVDyQVPV7/LGh7LyWpFseu+/g1HjW9A=", "product_class"=
>"job", "permalink"=>"farflung-f9418cfd37de67f5bd733b3d2ea39188"}
  Payola::Affiliate Load (0.0ms)  SELECT  "payola_affiliates".* FROM "payola_affiliates"  WHERE (lower(code) = lower(NULL))  ORDER BY "payola_affiliates"."id" ASC LIMIT 1
  Job Load (1.0ms)  SELECT  "jobs".* FROM "jobs"  WHERE "jobs"."permalink" = 'farflung-f9418cfd37de67f5bd733b3d2ea39188' LIMIT 1
  Payola::Coupon Load (1.0ms)  SELECT  "payola_coupons".* FROM "payola_coupons"  WHERE (lower(code) = lower(NULL))  ORDER BY "payola_coupons"."id" ASC LIMIT 1
   (0.0ms)  begin transaction
  Payola::Sale Exists (0.0ms)  SELECT  1 AS one FROM "payola_sales"  WHERE "payola_sales"."guid" IS NULL LIMIT 1
  CACHE (0.0ms)  SELECT  1 AS one FROM "payola_sales"  WHERE "payola_sales"."guid" IS NULL LIMIT 1
  Payola::Sale Exists (0.0ms)  SELECT  1 AS one FROM "payola_sales"  WHERE "payola_sales"."guid" = '16j79c' LIMIT 1
  SQL (1.0ms)  INSERT INTO "payola_sales" ("amount", "created_at", "currency", "email", "guid", "product_id", "product_type", "state", "stripe_token", "updated_at") VALUES (?, ?, ?,
?, ?, ?, ?, ?, ?, ?)  [["amount", 20000], ["created_at", "2016-04-16 12:25:56.073909"], ["currency", "usd"], ["email", "[email protected]"], ["guid", "16j79c"], ["product_id", 6], ["pr
oduct_type", "Job"], ["state", "pending"], ["stripe_token", "tok_180sWbCc1zXXaitaIc1ThbwS"], ["updated_at", "2016-04-16 12:25:56.073909"]]
  SQL (0.0ms)  INSERT INTO "versions" ("created_at", "event", "item_id", "item_type") VALUES (?, ?, ?, ?)  [["created_at", "2016-04-16 12:25:56.073909"], ["event", "create"], ["item_
id", 8], ["item_type", "Payola::Sale"]]
   (163.7ms)  commit transaction
Completed 500 Internal Server Error in 539ms

I really don't know how to tackle this problem. Is it Stripe.js problem, or my compiled [jquery.self.js?body=1:9632] as seen on the error picture posted?

Please, and help will be appreciated.

1

There are 1 best solutions below

2
On BEST ANSWER

Note: As figured out by the "creator of Payola-Payments",

I am getting this error because:

My browser console indicates my server is dumping the error, as HTML and the JS is trying to parse it as JSON.

By investigating the result of that POST on the Browser's Network Tab, we see facts that it's a RuntimeError in Payola::TransactionsController#create saying:

No eligible background worker systems found.

To fix this problem

I need to set up a background worker system. https://github.com/peterkeen/payola/wiki/Configuration-options#background-jobs

I believe this should work fine if Background Worker set/configured properly.