-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Controllers Stripe_Webhook Account_Manager
- Loading branch information
1 parent
5e54a63
commit 3e1e0be
Showing
12 changed files
with
112 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class AccountManagerController < ApplicationController | ||
before_action :authenticate_user! | ||
|
||
def index; end | ||
|
||
def create_portal_session | ||
session = Stripe::BillingPortal::Session.create({ | ||
customer: current_user.stripe_customer_ref, | ||
return_url: account_manager_url | ||
}) | ||
|
||
redirect_to session.url, status: 303, allow_other_host: true | ||
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,51 @@ | ||
class StripeWebhooksController < ApplicationController | ||
skip_before_action :verify_authenticity_token, only: :webhook | ||
|
||
def webhook | ||
payload = request.body.read | ||
sig_header = request.env['HTTP_STRIPE_SIGNATURE'] | ||
event = nil | ||
|
||
begin | ||
event = Stripe::Webhook.construct_event( | ||
payload, sig_header, ENV.fetch('STRIPE_WEBHOOK_SECRET') | ||
) | ||
rescue JSON::ParserError => e | ||
# Invalid payload | ||
Rails.logger.error "⚠️ Webhook JSON parse error: #{e.message}" | ||
rescue Stripe::SignatureVerificationError => e | ||
# Invalid signature | ||
Rails.logger.error "⚠️ Webhook signature verification error: #{e.message}" | ||
end | ||
|
||
# Handle the event | ||
case event.type | ||
when 'checkout.session.completed' | ||
session = event.data.object | ||
|
||
if session.client_reference_id.present? | ||
subscription = Subscription.find(session.client_reference_id) | ||
subscription.user.update(stripe_customer_ref: session.customer) | ||
subscription.update!(stripe_customer_ref: session.customer, stripe_subscription_ref: session.subscription) | ||
end | ||
when 'customer.subscription.created' | ||
stripe_subscription = event.data.object | ||
|
||
subscription = Subscription.find_by(stripe_subscription_ref: stripe_subscription.id) | ||
subscription.update!(status: stripe_subscription.status, | ||
paid_until: Time.at(stripe_subscription.current_period_end)) | ||
when 'customer.subscription.deleted' | ||
stripe_subscription = event.data.object | ||
subscription = Subscription.find_by(stripe_subscription_ref: stripe_subscription.id) | ||
subscription.update!(status: 'canceled', paid_until: nil) | ||
when 'customer.subscription.updated' | ||
stripe_subscription = event.data.object | ||
subscription = Subscription.find_by(stripe_subscription_ref: stripe_subscription.id) | ||
subscription.update!(status: stripe_subscription.status, | ||
paid_until: Time.at(stripe_subscription.current_period_end)) | ||
# ... handle other event types | ||
else | ||
puts "Unhandled event type: #{event.type}" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module AccountManagerHelper | ||
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,2 @@ | ||
module StripeWebhooksHelper | ||
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 |
---|---|---|
|
@@ -23,4 +23,6 @@ | |
# | ||
class Subscription < ApplicationRecord | ||
belongs_to :user | ||
|
||
scope :active, -> { where(status: "active") } | ||
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,14 @@ | ||
<div class="vh-100 d-flex justify-content-center align-items-center"> | ||
<div class='text-center'> | ||
<div class='card mb-2'> | ||
<div class='card-body'> | ||
<h5 class='card-title'>Your Subscription</h5> | ||
<p class='card-text'>You are currently subscribed and your account will renew on:</p> | ||
<p class='card-text'><%= current_user.subscriptions.active.first.paid_until.strftime('%B %d, %Y') %></p> | ||
</div> | ||
</div> | ||
<section> | ||
<%= button_to 'Manage Account', create_portal_session_path, data: {turbo: false}, class: 'btn btn-primary' %> | ||
</section> | ||
</div> | ||
</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
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,2 @@ | ||
<h1>StripeWebhooks#webhook</h1> | ||
<p>Find me in app/views/stripe_webhooks/webhook.html.erb</p> |
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,8 @@ | ||
require "test_helper" | ||
|
||
class AccountManagerControllerTest < ActionDispatch::IntegrationTest | ||
test "should get index" do | ||
get account_manager_index_url | ||
assert_response :success | ||
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,8 @@ | ||
require "test_helper" | ||
|
||
class StripeWebhooksControllerTest < ActionDispatch::IntegrationTest | ||
test "should get webhook" do | ||
get stripe_webhooks_webhook_url | ||
assert_response :success | ||
end | ||
end |