Created
June 3, 2025 19:18
-
-
Save Bodacious/7c09229eb59a52f5737163a3d053a2ca to your computer and use it in GitHub Desktop.
Another bad service object day
This file contains hidden or 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
class UserSubscriberService | |
PROMO_DISCOUNT_PERCENT = Rational(85, 100) # 15% OFF | |
PREMIUM_PLAN_NAME = 'premium_plan' | |
def self.call(user) | |
plan = Plan.find_by(name: PREMIUM_PLAN_NAME) | |
user.build_subscription | |
user.subscription.plan = plan | |
user.subscription.amount = plan.amount * PROMO_DISCOUNT_PERCENT | |
user.subscription.cadence_months = plan.cadence_months | |
unless user.subscription.save | |
return Result.new(:failure, "could not create subscription") | |
end | |
payment = subscription.payments.create(source: user.payment_card) | |
result = PaymentGateway.charge_payment!(payment) | |
if result.success? | |
user.subscription.status = 'active' | |
user.subscription.started_ = Time.now | |
user.subscription.save | |
user.account.increment!(:credit_balance, plan.monthly_credits) | |
return Result.new(:success) | |
else | |
return Result.new(:failure, "could not charge payment") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment