Buy local products (product save on our on server) through stripe with rails

Umeriqbal
2 min readDec 4, 2023

--

Stripe integration in Rails

Step 1:
add product table in your database like below

class CreateProducts < ActiveRecord::Migration[7.0]
def change
create_table :products do |t|
t.string :name
t.integer :price
t.integer :sales_count

t.timestamps
end
end
end

here is how your product controller index method like

class ProductsController < ApplicationController
before_action :set_product, only: %i[ show edit update destroy ]

# GET /products or /products.json
def index
@products = Product.all
end
end

and index.html.erb

<p style="color: green"><%= notice %></p>

<h1>Products</h1>

<div id="products">
<% @products.each do |product| %>
<%#= render product %>

<table>
<thead>
<tr>
<td class="p-3">Name</td>
<td class="p-3">Price</td>
<td class="p-3">Payment</td>
<td class="p-3">Sales</td>
<td class="p-3">edit Product</td>
<td class="p-3">show</td>
</tr>
</thead>
<tbody>
<tr class="p-2">
<td class="p-3"><%= product.name%> </td>
<td class="p-3"><%= number_to_currency(product.price) %> </td>
<td class="p-3"><%= button_to 'Buy now', checkout_create_path(id: product.id), remote: true%> </td>
<td class="p-3"><%= product.sales_count%> </td>
<td class="p-3"><%= link_to "Edit", edit_product_path(product) %></td>
<td class="p-3"><%= link_to "Show", product %></td>
</tr>
</tbody>
</table>

<% end %>
</div>

<%= link_to "New product", new_product_path %>

one note able thing here is button_to which create post request other wise we need to mention method: :post by our-self and we also .
<%= button_to ‘Buy now’, checkout_create_path(id: product.id), remote: true%>

is here is our route file

  root "products#index"
post "checkout/create", to: "checkout#create"
resources :webhooks, only: [:create]

when user click on buy button we create stripe checkout session like below

class CheckoutController < ApplicationController

def create
product = Product.find(params[:id])

@session = Stripe::Checkout::Session.create({
payment_method_types: ['card'],
line_items: [{
price_data: {
currency: "usd",
unit_amount: product.price * 100,
product_data: {
name: product.name
},
},
quantity: 1
}],
mode: 'payment',
success_url: root_url,
cancel_url: root_url,
})

respond_to do |format|
format.js
end
end
end

and as we have js request we create.js.erb in checkout view to show the stripe checkout form for payment
views/checkout/create.js.erb here how it look like

const stripe = Stripe('pk_test_51OCcupHZwOGN0GX7axcRxfeRmVAHLgZl38H0gB3Veoj1DbhqpgVJXNXXDRZaGwQuzV7z84H9ryUlpczZlV8S9e5O002LWdZYwk');

stripe.redirectToCheckout({
sessionId: '<%= @session.id %>'
})

this is stripe public key

const stripe = Stripe('pk_test_51OCcupHZwOGN0GX7axcRxfeRmVAHLgZl38H0gB3Veoj1DbhqpgVJXNXXDRZaGwQuzV7z84H9ryUlpczZlV8S9e5O002LWdZYwk');

after this stripe checkout form will show here are the views

--

--

Umeriqbal

I am a full stack software engineer specializing in Ruby on Rails, I have extensive experience in developing and deploying web applications.