Skip to content

Instantly share code, notes, and snippets.

@abhianair
Created May 18, 2020 08:52
Show Gist options
  • Save abhianair/b9aeef2d0b0c5f0928f5337efc9357df to your computer and use it in GitHub Desktop.
Save abhianair/b9aeef2d0b0c5f0928f5337efc9357df to your computer and use it in GitHub Desktop.
Stripe payment integration in ruby on rails
<script src="https://js.stripe.com/v3/"></script>
def make_payment_stripe
@stripe_session = make_payment()
@stripe_session_id = @stripe_session['id']
respond_to do |format|
format.js
end
end
def make_payment()
Stripe.api_key = Rails.application.secrets.stripe_sk
session = Stripe::Checkout::Session.create(
payment_method_types: ['card'],
line_items: [{
name: "#{name}",
description: "Payment",
amount: "#{amount * 100}",
currency: 'usd',
quantity: 1,
}],
mode: 'payment',
success_url: "#{return_path}?invoice=#{invoice.id}&session_id={CHECKOUT_SESSION_ID}",
cancel_url: cancel_url,
)
return session
end
var stripe = Stripe('<%= Rails.application.secrets.stripe_pk %>');
stripe.redirectToCheckout({
// Make the id field from the Checkout Session creation API response
// available to this file, so you can provide it as parameter here
// instead of the {{CHECKOUT_SESSION_ID}} placeholder.
sessionId: '<%= @stripe_session_id %>'
}).then(function (result) {
// If `redirectToCheckout` fails due to a browser or network
// error, display the localized error message to your customer
// using `result.error.message`.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment