【问题标题】:Ruby, stripe and javascript红宝石、条纹和javascript
【发布时间】:2023-04-03 03:24:01
【问题描述】:

如何将此脚本合并为 ruby​​ 格式,以便在不创建帖子的情况下不会通过条纹收费。

现在,如果我删除验证,则在收费通过时发布,如果收费未通过,则不发布。

一旦我添加验证并且缺少某些内容,通过在收费正确时单击付款,验证将停止以将帖子保存在数据库中,但收费仍在进行中。

妻子

<%= form_for @post do |f| %>


  <div>
    <%= f.label :name %>
    <%= f.text_field :name %>
  </div>

  <div>
    <%= f.label :email %>
    <%= f.text_field :email %>
  </div>

  <div>
    <%= f.label :phone_number %>
    <%= f.text_field :phone_number %>
  </div>

  <div>
    <%= f.label :post %>
    <%= f.text_area :post %>
  </div>

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


<% end %>

后控制器

class PostsController < ApplicationController

  before_action :authenticate_admin!, only: [:index]

  def index
    @posts = Post.all
  end

  def new
    @post = Post.new
  end

  def show

  end

  def create
    @post = Post.new(post_params)

    if @post.save
      redirect_to @post, notice: 'Your post was create'
    else
      render :new
    end

      # Amount in cents
      @amount = 150

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

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

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


  end

  private

  def post_params
    params.require(:post).permit(:name, :email, :phone_number, :post)
  end

end

发布模型

validates :name, :phone_number, :post, presence: true

  validates :email,
            presence: true,
            format: { with: /\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,4}\z/}

【问题讨论】:

    标签: javascript ruby-on-rails ruby ruby-on-rails-4 stripe-payments


    【解决方案1】:

    您需要在 try/catch 块中包含 beginend 以及 rescue,否则我不相信它会正常工作:

    begin
      charge = Stripe::Charge.create(
        :customer    => customer.id,
        :amount      => @amount,
        :description => 'Rails Stripe customer',
        :currency    => 'usd'
      )
    
    rescue Stripe::CardError => e
      flash[:error] = e.message
      redirect_to new_charge_path
    end
    

    在您尝试创建客户之前,您可能还想确认params[:stripeEmail]params[:stripeToken] 确实包含(预期的)内容。

    【讨论】:

    • def create 或多或少是一个隐含的begin,因此如果您希望rescue 应用于整个方法,则不需要显式的begin。跨度>
    • 是的,那东西没用。你有什么其他建议吗?
    • 如果验证未通过,我不希望该条带费用弹出窗口打开。
    【解决方案2】:

    您需要将您的 Stripe 交互放在 if 块中,用于 @post.save。像这样的:

    def create
      @post = Post.new(post_params)
    
      if @post.save
        # Amount in cents
        @amount = 150
    
        begin
          customer = Stripe::Customer.create(
            :email => params[:stripeEmail],
            :source  => params[:stripeToken]
          )
    
          charge = Stripe::Charge.create(
            :customer    => customer.id,
            :amount      => @amount,
            :description => 'Rails Stripe customer',
            :currency    => 'usd'
          )
    
          redirect_to @post, notice: 'Your post was created.'
        rescue Stripe::CardError => e
          flash[:error] = e.message
          redirect_to new_charge_path
        end
      else
        render :new
      end
    

    另外,我需要评论一下这并不真正属于控制器。我会构建一个类来处理 Stripe 交互。

    这样,在您的控制器中,您将拥有如下内容:

    if @post.save
      begin
        MyStripeStuff.create_and_charge_customer({
           email: params[:stripeEmail],
           token: params[:stripeToken],
           amount: 150,
           currency: 'usd',
           description: 'Rails Stripe customer'
        })
        redirect_to @post, notice: 'Your post was created.'
      rescue Stripe::CardError => e
        flash[:error] = e.message
        redirect_to new_charge_path
      end
    else
      render :new
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      相关资源
      最近更新 更多