Reputation: 307
I have integrated Stripe.js in my rails app. The app produces "Pay with Card" button. I would like to know how can I call a controller function for calling Stripe APIs for receiving payment.
show.html.erb:
<div class="col-xs-12">
<div class="btn-group pull-right" role="group" aria-label="..." style="margin-top:10px;">
<%= link_to 'Back to Products', products_path, class: "btn btn-default btn-info" %>
<%= button_to 'Pay with Card',
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="A month's subscription"
data-amount= "<%= @shopping_cart.total*100 %>"
data-locale="auto"></script> %>
<!--
<button type="button" class="btn btn-default btn-success">
<span class="fa fa-shopping-cart"></span>
Checkout
</button> -->
</div>
</div>
shopping_carts_controller.rb
def checkout
@shopping_cart.process_payment stripe_params
redirect_to products_path
flash[:notice] = "Succefully made payment."
end
How can I call the checkout method?
Upvotes: 0
Views: 151
Reputation: 8737
You'll need to make sure that your Checkout HTML snippet is inside a <form>
tag with the action
set to the URL of your shopping_carts_controller
.
Upvotes: 0
Reputation: 1413
Charging a payment in stripe is done by the following steps
1) Getting the card details from the user
2) Submitting the card details to stripe
3) Stripe returns you a token
4) Use the stripe token for charging the user through stripe charge API
This can be the view for getting the card details from the user
<%= form_tag charge_amount_path do %>
<article>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
</div>
<% end %>
<label class="amount">
<span>Amount: $5.00</span>
</label>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="some-key"
data-description="A month's subscription"
data-amount="500"
data-locale="auto"></script>
<% end %>
When a user submits this form, stripe returns you stripe token, you can use this token to create a charge.
Have a controller method, this will be the method that will be called after submitting the form
def charge_amount
charge = Stripe::Charge.create({
:amount => 100,
:card => params[:stripeToken],
:description => "blah-blah,
:currency => 'usd'
})
end
the params to this method from stripe will be like
Parameters: {"utf8"=>"✓", "authenticity_token"=>"rhYPiRCDLQJ4F9/B09TKgiyS11YGvP34hI5/Bi9n2nB9BbiBQXAMZWxdwkjVEkiy5+PJaKMnqraWSeu+a0Fn8Q==", "stripeToken"=>"tok_198sjvLeNQnnrmxRqgbTq12n", "stripeTokenType"=>"card", "stripeEmail"=>"[email protected]"}
Upvotes: 1