【问题标题】:Stripe checkout on wizard page via wicked gem gives error on loading, how to resolve?通过 wicked gem 在向导页面上进行条纹结帐会导致加载错误,如何解决?
【发布时间】:2017-10-28 13:17:20
【问题描述】:

现在我在 submit.html.erb 加载时收到此错误消息:Cannot charge a customer that has no active card

我想了解如何改进我的控制器。目标是在提交页面加载时不显示错误消息。但前提是有实际用户提交虚假信息。

class SubmissionsController < ApplicationController
include Wicked::Wizard
steps :name, :details, :comments, :submit, :thankyou

def show
  @company = Company.find(params[:company_id])
  render_wizard
end

def update
  @company = Company.find(params[:company_id])

  # one time checkout
  @amount = 50
  @description = 'Premium Package'

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

  Stripe::Charge.create(
    :customer    => customer.id,
    :amount      => @amount,
    :description => @description,
    :currency    => 'eur'
  )

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

  params[:company][:status] = step.to_s
  params[:company][:status] = 'active' if step == steps.last
  @company.update_attributes(company_params)
  render_wizard @company
end

private ...

这里是 submit.html.erb

<%= form_for @company, url: wizard_path, method: :put do |f| %>
  <article>
    <% if flash[:error].present? %>
      <div id="error_explanation">
        <p><%= flash[:error] %></p>
      </div>
    <% end %>
    <label class="amount">
      <span>Amount: €0.50</span>
    </label>
  </article>

  <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
          data-description=<% @description %>
          data-amount=<% @amount %>
          data-locale="auto"></script>
<% end %>

如果我可以提供更多上下文,请提供建议。

【问题讨论】:

    标签: ruby-on-rails stripe-payments wicked-gem


    【解决方案1】:

    重构为这个,现在可以工作了:

    class SubmissionsController < ApplicationController
      include Wicked::Wizard
      steps :name, :details, :comments, :submit, :thankyou
    
      def show
        get_company
        set_package
        render_wizard
      end
    
      def update
        case step
        when :thankyou
          get_company
          render_wizard @company
        when :submit
          get_company
          set_package
          if request.put?
            create_customer
            do_payment
            render_wizard @company
          end
        else
          get_company
          params[:company][:status] = 'active' if step == steps.last
          @company.update_attributes(company_params)
          render_wizard @company
        end
      end
    
      def get_company
        @company = Company.find(params[:company_id])
      end
    
      def create_customer
        @customer = Stripe::Customer.create(
          :email => params[:stripeEmail],
          :source  => params[:stripeToken]
        )
      end
    
      def set_package
        @amount = 50
        @amount_in_decimals = @amount / 100.00
        @description = "Premium Package"
      end
    
      def do_payment
        Stripe::Charge.create(
          :customer    => @customer.id,
          :amount      => @amount,
          :description => @description,
          :currency    => "eur"
        )
    
        rescue Stripe::CardError => e
          flash[:error] = e.message
      end
    

    为了视图

        <label class="amount">
          <span>Amount: <%= number_to_currency(@amount_in_decimals, :precision => 2, :locale => :nl) %></span>
        </label>
      </article>
    
      <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
              data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
              data-name="RailsApp!"
              data-description='<%= @description %>'
              data-amount=<%= @amount %>
              data-locale="auto"
              data-currency="eur"></script>
    <% end %>
    

    【讨论】:

      猜你喜欢
      • 2017-06-21
      • 1970-01-01
      • 2017-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      相关资源
      最近更新 更多