here is how you can configure stripe in rails
Create product and price on stripe using ruby on rails
Add customer and payment method on Stripe with ruby on rails
we will discuss two method to buy product in this article are
- Buy product through invoice
- Buy product through Payment Intent
Buy product through invoice:
for this first we need to create product, price and customer and payment method on stripe that we discuss in these articles Create product and price on stripe using ruby on rails and Add customer and payment method on Stripe with ruby on rails and after that we can buy product using the following steps
Create pay_package method in payment controller like below
class PaymentsController < ApplicationController
def pay_package
package = Package.find(params[:package_id])
payment_method_id = Payment.find(params[:payment_id]).payment_method["id"]
if package.present? && payment_method_id.present?
customer_id = current_user.customer_id
invoice = Stripe::Invoice.create({customer: customer_id})
Stripe::InvoiceItem.create({
customer: customer_id,
price: package.stripe_price_id,
invoice: invoice.id,
})
finalize_invoice = Stripe::Invoice.finalize_invoice(invoice.id)
Stripe::PaymentIntent.update(finalize_invoice.payment_intent, payment_method: payment_method_id)
invoice_pay = invoice.pay
receipt_url = "#{Stripe::Charge.retrieve(invoice_pay.charge).receipt_url.split('?').first}/pdf"
if current_user.transaction_histories.create(invoice_id: invoice_pay.id, receipt_url: receipt_url)
current_user.update(credits: current_user.credits + package.credits + package.free_credits)
render json: "Credits added successfully"
else
render json: "transaction failed", status: :unprocessable_entity
end
else
render json: "unable to perform transaction", status: :unprocessable_entity
end
end
end
what we are doing in this method?
fist we create invoice using the customer_id that is id of the customer created on stripe that we discuss in this article (Add customer and payment method on Stripe with ruby on rails). then we create invoiceItem using the save customer_id and stripe_price_id which is the id of the price that is created on stripe that we discuss in this article(Create product and price on stripe using ruby on rails) after that we finilize the invoice which will add PaymentIntent in the invoice object after that invoice.pay will deduct the amount specified in price object from the default payment method of the specified customer whose id we give on invoiceItem.create.
at that point we done have successful payment. below lines are just creating receipt url to download receipt pdf
Buy product through Payment Intent:
We don’t need to create products and prices on stripe if we are buying product through payment intent we just need to maintain product and price info locally.
pi = Stripe::PaymentIntent.create({
amount: 1000*100,
currency: 'usd',
payment_method: "pm_1OH...",
customer: "cus_P5...",
payment_method_types: ['card'],
confirm: true
})
we need payment_method id and customer id from that we have on stripe that we discuss in this article(Add customer and payment method on Stripe with ruby on rails) here is the payment intent that we received in return with status success after successful $1000 payment and you can save its id for future reference.
3.2.2 :072 > pi
=>
#<Stripe::PaymentIntent:0x59bb4 id=pi_3OJZojHZwOGN0GX70vcCpbBh> JSON: {
"id": "pi_3OJ...",
"object": "payment_intent",
"amount": 10000,
"amount_capturable": 0,
"amount_details": {"tip":{}},
"amount_received": 10000,
"application": null,
"application_fee_amount": null,
"automatic_payment_methods": null,
"canceled_at": null,
"cancellation_reason": null,
"capture_method": "automatic",
"client_secret": "pi_3OJZojHZwOGN0GX70vcCpbBh_secret_Nm6D3PSPwS3cNy7r63sdDTKoz",
"confirmation_method": "automatic",
"created": 1701687361,
"currency": "usd",
"customer": "cus_P5...",
"description": null,
"invoice": null,
"last_payment_error": null,
"latest_charge": "ch_3OJ...",
"livemode": false,
"metadata": {},
"next_action": null,
"on_behalf_of": null,
"payment_method": "pm_1OHg...",
"payment_method_configuration_details": null,
"payment_method_options": {"card":{"installments":null,"mandate_options":null,"network":null,"request_three_d_secure":"automatic"}},
"payment_method_types": [
"card"
],
"processing": null,
"receipt_email": null,
"review": null,
"setup_future_usage": null,
"shipping": null,
"source": null,
"statement_descriptor": null,
"statement_descriptor_suffix": null,
"status": "succeeded",
"transfer_data": null,
"transfer_group": null
}