user upgrades: add links to Stripe payment & receipt page.

Add links to the Stripe payment page and the Stripe receipt page on
completed user upgrades.

The Stripe payment link is a link to the payment details on the Stripe
dashboard and is only visible to the owner.
This commit is contained in:
evazion
2020-12-28 22:25:09 -06:00
parent e29e2da8be
commit 87af02f689
8 changed files with 154 additions and 6 deletions

View File

@@ -27,6 +27,16 @@ class UserUpgradesController < ApplicationController
respond_with(@user_upgrade)
end
def receipt
@user_upgrade = authorize UserUpgrade.find(params[:id])
redirect_to @user_upgrade.receipt_url
end
def payment
@user_upgrade = authorize UserUpgrade.find(params[:id])
redirect_to @user_upgrade.payment_url
end
private
def recipient

View File

@@ -198,6 +198,32 @@ class UserUpgrade < ApplicationRecord
checkout
end
def receipt_url
return nil if pending? || stripe_id.nil?
checkout_session = Stripe::Checkout::Session.retrieve(stripe_id)
payment_intent = Stripe::PaymentIntent.retrieve(checkout_session.payment_intent)
charge = payment_intent.charges.data.first
charge.receipt_url
end
def payment_url
return nil if pending? || stripe_id.nil?
checkout_session = Stripe::Checkout::Session.retrieve(stripe_id)
payment_intent = Stripe::PaymentIntent.retrieve(checkout_session.payment_intent)
"https://dashboard.stripe.com/payments/#{payment_intent.id}"
end
def has_receipt?
!pending?
end
def has_payment?
!pending?
end
class_methods do
def register_webhook
webhook = Stripe::WebhookEndpoint.create({

View File

@@ -10,4 +10,12 @@ class UserUpgradePolicy < ApplicationPolicy
def show?
record.recipient == user || record.purchaser == user || user.is_owner?
end
def receipt?
(record.purchaser == user || user.is_owner?) && record.has_receipt?
end
def payment?
user.is_owner? && record.has_payment?
end
end

View File

@@ -28,8 +28,18 @@
<% t.column :status %>
<% t.column "Updated" do |artist| %>
<%= time_ago_in_words_tagged(artist.updated_at) %>
<% t.column "Updated" do |user_upgrade| %>
<%= time_ago_in_words_tagged(user_upgrade.updated_at) %>
<% end %>
<% t.column column: "control" do |user_upgrade| %>
<%= link_to "Show", user_upgrade %>
<% if policy(user_upgrade).receipt? %>
| <%= link_to "Receipt", receipt_user_upgrade_path(user_upgrade), target: "_blank" %>
<% end %>
<% if policy(user_upgrade).payment? %>
| <%= link_to "Payment", payment_user_upgrade_path(user_upgrade), target: "_blank" %>
<% end %>
<% end %>
<% end %>

View File

@@ -30,12 +30,18 @@
<% if @user_upgrade.is_gift? && CurrentUser.user == @user_upgrade.recipient %>
<p><%= link_to_user @user_upgrade.purchaser %> has upgraded your account to <%= @user_upgrade.level_string %>. Enjoy your new account!</p>
<% elsif @user_upgrade.is_gift? && CurrentUser.user == @user_upgrade.purchaser %>
<p><%= link_to_user @user_upgrade.recipient %> is now a <%= @user_upgrade.level_string %> user. Thanks for supporting the site!</p>
<p><%= link_to_user @user_upgrade.recipient %> is now a <%= @user_upgrade.level_string %> user. Thanks for supporting the site! A receipt has been sent to your email.</p>
<% else %>
<p>You are now a <%= @user_upgrade.level_string %> user. Thanks for supporting the site!</p>
<p>You are now a <%= @user_upgrade.level_string %> user. Thanks for supporting the site! A receipt has been sent to your email.</p>
<% end %>
<p><%= link_to "Go back to #{Danbooru.config.canonical_app_name}", "https://danbooru.donmai.us" %> to continue using the site.</p>
<% if policy(@user_upgrade).receipt? %>
<%= link_to "View Receipt", receipt_user_upgrade_path(@user_upgrade), target: "_blank" %>
<% end %>
<% if policy(@user_upgrade).payment? %>
| <%= link_to "View Payment", payment_user_upgrade_path(@user_upgrade), target: "_blank" %>
<% end %>
<% else %>
<%= content_for :html_header do %>
<meta http-equiv="refresh" content="5">