0

私はRailsとStripeを使って支払いを処理するイベントアプリを構築しています。私は予約ページにjavascriptを使用して、一度に1つだけではなく複数のスペースを予約して支払うことができます。私はこの問題を解決するにはどうすればよいRails/Stripe - 複数の支払いを受け取ります

Stripe payment

- しかし

Booking page view

は私のストライプのダッシュボード上の唯一の支払いが1つのスペースのために撮影されている - あなたがここに見ることができるように予約ページには、このすることができます予約ビューに表示された全額の支払いが必要ですか?

は、ここに私のコントローラとビューのコードだ -

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(quantity: params[:quantity]) 
     # 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 

     Booking.transaction do 

      @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 


    private 

    def booking_params 
     params.require(:booking).permit(:stripe_token, :quantity) 
    end 



end 

booking.new.html.erb

<div class="col-md-6 col-md-offset-3" id="eventshow"> 
    <div class="row"> 
    <div class="panel panel-default"> 
     <div class="panel-heading"> 
      <h2>Confirm Your Booking</h2> 
     </div> 
        <div class="calculate-total"> 
           <p> 
            Confirm number of spaces you wish to book here: 
            <input type="number" placeholder="1" min="1" value="1" class="num-spaces"> 
           </p> 
           <p> 
            Total Amount 
            £<span class="total" data-unit-cost="<%= @event.price %>">0</span> 
           </p> 
          </div> 





       <%= simple_form_for [@event, @booking], id: "new_booking" do |form| %> 



       <span class="payment-errors"></span> 

       <div class="form-row"> 
        <label> 
         <span>Card Number</span> 
         <input type="text" size="20" data-stripe="number"/> 
        </label> 
       </div> 

       <div class="form-row"> 
        <label> 
        <span>CVC</span> 
        <input type="text" size="4" data-stripe="cvc"/> 
        </label> 
       </div> 

       <div class="form-row"> 
        <label> 
         <span>Expiration (MM/YYYY)</span> 
         <input type="text" size="2" data-stripe="exp-month"/> 
        </label> 
        <span>/</span> 
        <input type="text" size="4" data-stripe="exp-year"/> 
       </div> 
      </div> 
      <div class="panel-footer">  

       <%= form.button :submit %> 


      </div> 

<% end %> 
<% end %> 

     </div> 
    </div> 
</div> 

<script type="text/javascript"> 
    $('.calculate-total input').on('keyup', calculateBookingPrice); 

function calculateBookingPrice() { 
    var unitCost = parseFloat($('.calculate-total .total').data('unit-cost')), 
     numSpaces = parseInt($('.calculate-total .num-spaces').val()), 
     total = (numSpaces * unitCost).toFixed(2); 

    if (isNaN(total)) { 
    total = 0; 
    } 

    $('.calculate-total span.total').text(total); 
} 

    $(document).ready(calculateBookingPrice) 

</script> 



<script type="text/javascript" src="https://js.stripe.com/v2/"></script> 

<script type="text/javascript"> 
    Stripe.setPublishableKey('<%= STRIPE_PUBLIC_KEY %>'); 
    var stripeResponseHandler = function(status, response) { 
    var $form = $('#new_booking'); 

    if (response.error) { 
    // Show the errors on the form 
    $form.find('.payment-errors').text(response.error.message); 
    $form.find('input[type=submit]').prop('disabled', false); 
    } else { 
    // token contains id, last4, and card type 
    var token = response.id; 
    // Insert the token into the form so it gets submitted to the server 
    $form.append($('<input type="hidden" name="booking[stripe_token]"  />').val(token)); 
    // and submit 
    $form.get(0).submit(); 
    } 
    }; 

    // jQuery(function($) { - changed to the line below 
    $(document).on("ready page:load", function() { 

    $('#new_booking').submit(function(event) { 
     var $form = $(this); 

     // Disable the submit button to prevent repeated clicks 
     $form.find('input[type=submit]').prop('disabled', true); 

     Stripe.card.createToken($form, stripeResponseHandler); 

     // Prevent the form from submitting with the default action 
     return false; 
    }); 
    }); 
</script> 

私はbooking_paramsに '価格' を追加する必要がありますか?私はすでにそこに「量」を持っていますか?

私の予約テーブルに 'total_amount'列を追加する必要がありますか?それともコントローラのストライプアクションとその量に関係していますか?

何か助けていただければ幸いです。

答えて

0

Stripe.orderを作成し、items: []を渡す必要があります。quantityに渡すことができます。あなたの場合、それは1つのアイテムになります。ステップガイドによって

ここで更新

を詳しく

ためStripe APIをチェックしてみて下さいステップ:Stripeアカウントへ

  1. ログイン、productを作成し、それactiveします。これはあなたの予約です。それを保存。
  2. 製品の内部には、Inventoryが表示されます。 + Add SKUを探します。クリックして。
  3. このようなポップアップウィンドウが表示されますenter image description here
  4. 必要な情報を入力してください。あなたのケースではcurrency,priceinventory、おそらくimageです。 IDが自動的に生成されます。プレスAdd SKU
  5. productにはSKUがあります。すでに推測しているように、同じSKUのSKUは、productpriceが異なる場合があります。
  6. コピーを新たに今、あなたはこのようなStripe::Orderを作成することができます
  7. SKU idを作成しました:

     
    response = Stripe::Order.create(
        currency: 'usd', 
        items: [ 
        { 
         type: 'sku', 
         parent: 'sku_9AJ4OfKbdRuoGi', # SKU id 
         quantity: 2 
        } 
        ], 
        customer: nil, # replace with customer id if needed 
        email: '[email protected]' 
    ) 
    

これはあなたのresponseは次のようになります方法です:調べるとき

#<Stripe::Order:0x3ff36d37e2b8 id=or_18ryyuBq3wgBfJrxa44Jzm3T> JSON: { 
    "id": "or_18ryyuBq3wgBfJrxa44Jzm3T", 
    "object": "order", 
    "amount": 19998, 
    "amount_returned": null, 
    "application": null, 
    "application_fee": null, 
    "charge": null, 
    "created": 1473465868, 
    "currency": "usd", 
    "customer": null, 
    "email": "[email protected]", 
    "items": [ 
    {"object":"order_item","amount":19998,"currency":"usd","description":"booking","parent":"sku_9AJ4OfKbdRuoGi","quantity":2,"type":"sku"}, 
    {"object":"order_item","amount":0,"currency":"usd","description":"Taxes (included)","parent":null,"quantity":null,"type":"tax"}, 
    {"object":"order_item","amount":0,"currency":"usd","description":"Free shipping","parent":"ship_free-shipping","quantity":null,"type":"shipping"} 
    ], 
    "livemode": false, 
    "metadata": {}, 
    "returns": {"object":"list","data":[],"has_more":false,"total_count":0,"url":"/v1/order_returns?order=or_18ryyuBq3wgBfJrxa44Jzm3T"}, 
    "selected_shipping_method": "ship_free-shipping", 
    "shipping": null, 
    "shipping_methods": [ 
    {"id":"ship_free-shipping","amount":0,"currency":"usd","delivery_estimate":null,"description":"Free shipping"} 
    ], 
    "status": "created", 
    "status_transitions": null, 
    "updated": 1473465868 
} 

任意のAPIコールまたはウェブフックの応答nの場合は、Orderオブジェクトのstatus属性を使用して、注文の現在のステータスを確認し、それに応じて応答します。注文が最初に要求された場合

  • 、得られた順序オブジェクトはcreatedの 初期statusを有しています。
  • ご注文の場合、statuspaidに変更されます。注文は になります。
  • 注文を完了した後、Orderオブジェクトを変更してstatus からfulfilledに変更します。 paid州の注文は fulfilledとマークすることができます。
  • お客様が になる前に注文をキャンセルした場合、お客様の注文により( )statusからcanceledに更新され、 の支払いが払い戻されます。
  • ご注文が完了し、お客様が 購入商品を返品した場合、statusreturnedに更新すると、 の支払いが払い戻されます。

詳細はorder guideをお読みください。

は今、あなたはStripe's dashboard

に新しい注文を確認する必要があり、この情報がお役に立てば幸いです。

+0

コントローラーには入っていますか? –

+0

はい、 'Stripe :: Charge.create'の代わりに、' Stripe :: Order.create'はapi docsのようにparamsを変更します。 – Pav31

+0

配列として入力する必要がありますか? –

関連する問題