mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2024-12-02 13:24:20 +01:00
handle wallet transaction w/ stripe subscription
This commit is contained in:
parent
f4f1464826
commit
fba5f57a1c
5
app/exceptions/invalid_subscription_error.rb
Normal file
5
app/exceptions/invalid_subscription_error.rb
Normal file
@ -0,0 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Raised when trying to create something based on a subscription but it does not exists or is expired
|
||||
class InvalidSubscriptionError < StandardError
|
||||
end
|
@ -18,9 +18,9 @@ class PaymentDocument < Footprintable
|
||||
end
|
||||
|
||||
def set_wallet_transaction(amount, transaction_id)
|
||||
raise InvalidFootprintError unless check_footprint
|
||||
|
||||
update_columns(wallet_amount: amount, wallet_transaction_id: transaction_id)
|
||||
chain_record
|
||||
self.wallet_amount = amount
|
||||
self.wallet_transaction_id = transaction_id
|
||||
end
|
||||
|
||||
def post_save(arg); end
|
||||
end
|
||||
|
@ -49,6 +49,12 @@ class PaymentSchedule < PaymentDocument
|
||||
payment_schedule_items.map(&:check_footprint).all? && footprint == compute_footprint
|
||||
end
|
||||
|
||||
def post_save(setup_intent_id)
|
||||
return unless payment_method == 'stripe'
|
||||
|
||||
StripeService.create_stripe_subscription(id, setup_intent_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def generate_and_send_document
|
||||
|
@ -143,18 +143,4 @@ class Subscription < ApplicationRecord
|
||||
def of_partner_plan?
|
||||
plan.is_a?(PartnerPlan)
|
||||
end
|
||||
|
||||
def get_wallet_amount_debit
|
||||
total = plan.amount
|
||||
total = CouponService.new.apply(total, @coupon, user.id) if @coupon
|
||||
wallet_amount = (user.wallet.amount * 100).to_i
|
||||
wallet_amount >= total ? total : wallet_amount
|
||||
end
|
||||
|
||||
def debit_user_wallet
|
||||
return if !@wallet_amount_debit.present? || @wallet_amount_debit.zero?
|
||||
|
||||
amount = @wallet_amount_debit / 100.0
|
||||
WalletService.new(user: user, wallet: user.wallet).debit(amount, self)
|
||||
end
|
||||
end
|
||||
|
@ -47,7 +47,8 @@ class PaymentScheduleService
|
||||
end
|
||||
|
||||
def create(subscription, total, coupon: nil, operator: nil, payment_method: nil, reservation: nil, user: nil, setup_intent_id: nil)
|
||||
subscription = reservation.generate_subscription if !subscription && reservation.plan_id
|
||||
subscription = reservation.generate_subscription if !subscription && reservation&.plan_id
|
||||
raise InvalidSubscriptionError unless subscription
|
||||
|
||||
schedule = compute(subscription.plan, total, coupon)
|
||||
ps = schedule[:payment_schedule]
|
||||
@ -58,13 +59,10 @@ class PaymentScheduleService
|
||||
ps.stp_setup_intent_id = setup_intent_id
|
||||
ps.operator_profile = operator.invoicing_profile
|
||||
ps.invoicing_profile = user.invoicing_profile
|
||||
ps.save!
|
||||
ps.payment_schedule_items = items
|
||||
items.each do |item|
|
||||
item.payment_schedule = ps
|
||||
item.save!
|
||||
end
|
||||
|
||||
StripeService.create_stripe_subscription(ps.id, subscription, reservation&.reservable&.stp_product_id, setup_intent_id) if payment_method == 'stripe'
|
||||
ps
|
||||
end
|
||||
end
|
||||
|
@ -30,9 +30,10 @@ class Reservations::Reserve
|
||||
else
|
||||
generate_invoice(reservation, operator_profile_id, payment_details, intent_id)
|
||||
end
|
||||
WalletService.debit_user_wallet(payment, user, reservation)
|
||||
payment.save
|
||||
reservation.save
|
||||
WalletService.debit_user_wallet(payment, user, reservation)
|
||||
payment.post_save(intent_id)
|
||||
reservation.post_save
|
||||
end
|
||||
true
|
||||
|
@ -5,11 +5,24 @@ class StripeService
|
||||
class << self
|
||||
|
||||
# Create the provided PaymentSchedule on Stripe, using the Subscription API
|
||||
def create_stripe_subscription(payment_schedule_id, subscription, reservable_stp_id, setup_intent_id)
|
||||
def create_stripe_subscription(payment_schedule_id, setup_intent_id)
|
||||
stripe_key = Setting.get('stripe_secret_key')
|
||||
payment_schedule = PaymentSchedule.find(payment_schedule_id)
|
||||
first_item = payment_schedule.ordered_items.first
|
||||
|
||||
case payment_schedule.scheduled_type
|
||||
when Reservation.name
|
||||
subscription = payment_schedule.scheduled.subscription
|
||||
reservable_stp_id = payment_schedule.scheduled.reservable&.stp_product_id
|
||||
when Subscription.name
|
||||
subscription = payment_schedule.scheduled
|
||||
reservable_stp_id = nil
|
||||
else
|
||||
raise InvalidSubscriptionError
|
||||
end
|
||||
|
||||
handle_wallet_transaction(payment_schedule)
|
||||
|
||||
# setup intent (associates the customer and the payment method)
|
||||
intent = Stripe::SetupIntent.retrieve(setup_intent_id, api_key: stripe_key)
|
||||
# subscription (recurring price)
|
||||
@ -71,5 +84,12 @@ class StripeService
|
||||
|
||||
Stripe::Price.create(params, api_key: Setting.get('stripe_secret_key'))
|
||||
end
|
||||
|
||||
def handle_wallet_transaction(payment_schedule)
|
||||
return unless payment_schedule.wallet_amount
|
||||
|
||||
customer_id = payment_schedule.invoicing_profile.user.stp_customer_id
|
||||
Stripe::Customer.update(customer_id, { balance: -payment_schedule.wallet_amount }, { api_key: Setting.get('stripe_secret_key') })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -35,8 +35,9 @@ class Subscriptions::Subscribe
|
||||
else
|
||||
generate_invoice(subscription, operator_profile_id, payment_details, intent_id)
|
||||
end
|
||||
payment.save
|
||||
WalletService.debit_user_wallet(payment, user, subscription)
|
||||
payment.save
|
||||
payment.post_save(intent_id)
|
||||
end
|
||||
true
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user