-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stripe checkout for invoices (#325)
- Loading branch information
1 parent
a7c7912
commit 644dea8
Showing
15 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
class Invoices::PaymentsController < ApplicationController | ||
skip_before_action :authenticate_user! | ||
skip_after_action :verify_authorized | ||
before_action :load_invoice | ||
before_action :ensure_invoice_unpaid, only: [:new] | ||
|
||
def new | ||
session = @invoice.create_checkout_session!( | ||
success_url: success_invoice_payments_url(@invoice), | ||
cancel_url: cancel_invoice_payments_url(@invoice) | ||
) | ||
|
||
redirect_to session.url, allow_other_host: true | ||
end | ||
|
||
def success | ||
@invoice.paid! | ||
end | ||
|
||
def cancel | ||
render | ||
end | ||
|
||
private | ||
|
||
def load_invoice | ||
@invoice = Invoice.includes(client: :company).find(params[:invoice_id]) | ||
end | ||
|
||
def ensure_invoice_unpaid | ||
if @invoice.paid? | ||
redirect_to success_invoice_payments_url(@invoice.id) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationService | ||
def self.process(*args, &block) | ||
new(*args, &block).process | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
module InvoicePayment | ||
class Checkout < ApplicationService | ||
def initialize(params) | ||
@invoice = params[:invoice] | ||
@company = invoice.client.company | ||
@client = invoice.client | ||
@success_url = params[:success_url] | ||
@cancel_url = params[:cancel_url] | ||
end | ||
|
||
def process | ||
Invoice.transaction do | ||
ensure_client_registered! | ||
checkout! | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :invoice, :company, :client, :success_url, :cancel_url | ||
|
||
def ensure_client_registered! | ||
return if client.stripe_id? | ||
|
||
client.register_on_stripe! | ||
end | ||
|
||
def description | ||
"Invoice from #{company.name} for #{currency} #{invoice.amount} due on #{invoice.due_date}" | ||
end | ||
|
||
def currency | ||
company.base_currency | ||
end | ||
|
||
def checkout! | ||
Stripe::Checkout::Session.create( | ||
{ | ||
line_items: [{ | ||
price_data: { | ||
currency: company.base_currency.downcase, | ||
product_data: { | ||
name: invoice.invoice_number, | ||
description: | ||
}, | ||
unit_amount: invoice.amount.to_i | ||
}, | ||
quantity: 1 | ||
}], | ||
mode: "payment", | ||
customer: client.reload.stripe_id, | ||
success_url:, | ||
cancel_url: | ||
}) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<div class="flex flex-col min-h-full pt-16 pb-12 my-auto"> | ||
<main class="flex flex-col self-center justify-center flex-grow w-full px-4 mx-auto max-w-7xl sm:px-6 lg:px-8"> | ||
<div class="flex justify-center flex-shrink-0"> | ||
<svg class="w-auto h-16 text-red-400 bg-red-200 rounded-full shadow-sm" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"> | ||
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd" /> | ||
</svg> | ||
</div> | ||
|
||
<div class="py-16"> | ||
<div class="text-center"> | ||
<p class="text-sm font-semibold tracking-wide uppercase text-miru-han-purple-600">Invoice #<%= @invoice.invoice_number %></p> | ||
<h1 class="mt-2 text-4xl font-extrabold tracking-tight text-gray-900 sm:text-5xl">Payment was cancelled.</h1> | ||
<p class="mt-2 text-base text-gray-500">Didn't cancel the payment?</p> | ||
<div class="mt-6 group"> | ||
<%= link_to "Try again", new_invoice_payment_url(@invoice), class: "text-base font-medium text-miru-han-purple-600 group-hover:text-miru-han-purple-400", target: "_blank", rel: "nofollow" %> | ||
<span aria-hidden="true" class="text-base font-medium text-miru-han-purple-600 group-hover:text-miru-han-purple-400"> →</span> | ||
</div> | ||
</div> | ||
</div> | ||
</main> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<div class="flex flex-col min-h-full pt-16 pb-12 my-auto"> | ||
<main class="flex flex-col self-center justify-center flex-grow w-full px-4 mx-auto max-w-7xl sm:px-6 lg:px-8"> | ||
<div class="flex justify-center flex-shrink-0"> | ||
<svg class="w-auto h-16 text-green-400 bg-green-200 rounded-full shadow-sm" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"> | ||
<path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd" /> | ||
</svg> | ||
</div> | ||
|
||
<div class="py-16"> | ||
<div class="text-center"> | ||
<p class="text-sm font-semibold tracking-wide text-indigo-600 uppercase">Invoice #<%= @invoice.invoice_number %></p> | ||
<h1 class="mt-2 text-4xl font-extrabold tracking-tight text-gray-900 sm:text-5xl">Payment was successful. 🎉</h1> | ||
<p class="mt-2 text-base text-gray-500">We have received your payment.</p> | ||
</div> | ||
</div> | ||
</main> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
Stripe.api_key = ENV["STRIPE_SECRET_KEY"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddStripeIdToClients < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :clients, :stripe_id, :string, default: nil | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
RSpec.describe "Invoices::Payments", type: :request do | ||
describe "GET /index" do | ||
pending "add some examples (or delete) #{__FILE__}" | ||
end | ||
end |