user upgrades: add ability to refund upgrades.
This commit is contained in:
@@ -27,6 +27,14 @@ class UserUpgradesController < ApplicationController
|
||||
respond_with(@user_upgrade)
|
||||
end
|
||||
|
||||
def refund
|
||||
@user_upgrade = authorize UserUpgrade.find(params[:id])
|
||||
@user_upgrade.refund!
|
||||
flash[:notice] = "Upgrade refunded"
|
||||
|
||||
respond_with(@user_upgrade)
|
||||
end
|
||||
|
||||
def receipt
|
||||
@user_upgrade = authorize UserUpgrade.find(params[:id])
|
||||
redirect_to @user_upgrade.receipt_url
|
||||
|
||||
@@ -11,7 +11,8 @@ class UserUpgrade < ApplicationRecord
|
||||
enum status: {
|
||||
pending: 0,
|
||||
processing: 10,
|
||||
complete: 20
|
||||
complete: 20,
|
||||
refunded: 30,
|
||||
}
|
||||
|
||||
scope :gifted, -> { where("recipient_id != purchaser_id") }
|
||||
@@ -62,6 +63,19 @@ class UserUpgrade < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def previous_level
|
||||
case upgrade_type
|
||||
when "gold"
|
||||
User::Levels::MEMBER
|
||||
when "platinum"
|
||||
User::Levels::MEMBER
|
||||
when "gold_to_platinum"
|
||||
User::Levels::GOLD
|
||||
else
|
||||
raise NotImplementedError
|
||||
end
|
||||
end
|
||||
|
||||
def upgrade_price
|
||||
case upgrade_type
|
||||
when "gold"
|
||||
@@ -120,7 +134,7 @@ class UserUpgrade < ApplicationRecord
|
||||
concerning :UpgradeMethods do
|
||||
def process_upgrade!(payment_status)
|
||||
recipient.with_lock do
|
||||
return if status == "complete"
|
||||
return unless pending? || processing?
|
||||
|
||||
if payment_status == "paid"
|
||||
upgrade_recipient!
|
||||
@@ -198,24 +212,38 @@ class UserUpgrade < ApplicationRecord
|
||||
checkout
|
||||
end
|
||||
|
||||
def refund!(reason: nil)
|
||||
with_lock do
|
||||
return if refunded?
|
||||
|
||||
Stripe::Refund.create(payment_intent: payment_intent.id, reason: reason)
|
||||
recipient.update!(level: previous_level)
|
||||
update!(status: "refunded")
|
||||
end
|
||||
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 checkout_session
|
||||
@checkout_session ||= Stripe::Checkout::Session.retrieve(stripe_id)
|
||||
end
|
||||
|
||||
def payment_intent
|
||||
@payment_intent ||= Stripe::PaymentIntent.retrieve(checkout_session.payment_intent)
|
||||
end
|
||||
|
||||
def charge
|
||||
payment_intent.charges.data.first
|
||||
end
|
||||
|
||||
def has_receipt?
|
||||
!pending?
|
||||
end
|
||||
|
||||
@@ -11,6 +11,10 @@ class UserUpgradePolicy < ApplicationPolicy
|
||||
record.recipient == user || record.purchaser == user || user.is_owner?
|
||||
end
|
||||
|
||||
def refund?
|
||||
user.is_owner? && record.complete?
|
||||
end
|
||||
|
||||
def receipt?
|
||||
(record.purchaser == user || user.is_owner?) && record.has_receipt?
|
||||
end
|
||||
|
||||
@@ -40,6 +40,9 @@
|
||||
<% if policy(user_upgrade).payment? %>
|
||||
| <%= link_to "Payment", payment_user_upgrade_path(user_upgrade), target: "_blank" %>
|
||||
<% end %>
|
||||
<% if policy(user_upgrade).refund? %>
|
||||
| <%= link_to "Refund", refund_user_upgrade_path(user_upgrade), remote: true, method: :put, "data-confirm": "Are you sure you want to refund this payment?" %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
|
||||
|
||||
1
app/views/user_upgrades/refund.js.erb
Normal file
1
app/views/user_upgrades/refund.js.erb
Normal file
@@ -0,0 +1 @@
|
||||
location.reload();
|
||||
Reference in New Issue
Block a user