条纹收费未定的杂项“游戏”

时间:2019-07-16 23:13:02

标签: ruby-on-rails ruby

我使用的条纹卡效果很好,但是我使用的是市场上的条纹连接,根据用户的商户ID,它向不同的用户发送付款。我遇到“游戏”错误。

Iv'e还尝试定义了无用游戏= Game.find(params [:game_id])  但出现错误“没有ID找不到游戏”

这是我的条纹方法:

class LineItemsController < ApplicationController
    def charge
        price = params[:amount].to_f*100
        Stripe::Charge.create({
            amount: price.to_i,
            currency: 'usd',
            source: params[:stripeToken], # obtained with Stripe.js
            description: 'plan',
            :destination => {
                :amount => price * 94, 
                :account => game.user.merchant_id
              }
          })

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

line_items属于cart,我有一个has_many,并且属于gameline_items之间的关联。

class LineItem < ApplicationRecord
  belongs_to :game
  belongs_to :cart

  def total_price
    game.price.to_i * quantity.to_i
  end
end

这是我的游戏模型:

class Game < ApplicationRecord
  belongs_to :user
  has_many :line_items
end

这是我的carts / show.html.erb文件

<p id="notice"><%= notice %></p>

<%= link_to 'Keep Shopping', games_path %>

<div>
    <%= render(@cart.line_items) %>
</div>
<div>
    <%= link_to 'Empty Cart', @cart, method: :delete, data: { confirm: "Are you sure?" } %>
</div>

<div>
    Total: <%= number_to_currency(@cart.total_price) %> 
</div>



<script src="https://js.stripe.com/v3/"></script>
<form action="/charge" method="post" id="payment-form">
  <div id="content" class="section-padding">
    <div class="container">
      <div class="row">
          <div class="col-md-12 col-xs-12">
            <%= hidden_field_tag :cart_id , nil, value: @cart.id %>
            <%= hidden_field_tag :amount , nil, id: "amount", value: @line_item.total_price %>
            <input 
                type="submit" 
                value="Pay with Card"
                data-key="<%= ENV['STRIPE_PUBLISHABLE_KEY'] %>"
                data-currency="usd"
                class="btn btn-primary"
            />
          </div>
        </div>
      </div>
  </div>
</form>

<script src="https://checkout.stripe.com/v2/checkout.js"></script>
<script>
$(document).ready(function() {
    $(':submit').on('click', function(event) {
        event.preventDefault();
        var $button = $(this),
            $form = $button.parents('form');
        var opts = $.extend({}, $button.data(), {
            token: function(result) {
                $form.append($('<input>').attr({ type: 'hidden', name: 'stripeToken', value: result.id })).submit();
            }
        });
        StripeCheckout.open(opts);
    });
});
</script>

那么游戏中有has_many多个订单项。订单项属于游戏和购物车。购物车只有很多订单项

1 个答案:

答案 0 :(得分:0)

您需要先声明game变量,然后才能在LineItemsController#charge上使用它

game = Game.find(params[:game_id])之前像Stripe::Charge.create...的东西