Skip to content

Commit

Permalink
Merge insiders and premium (exercism#5862)
Browse files Browse the repository at this point in the history
* WIP

* WIP

* WIP

* More WIP

* Fix things

* Add custom input (exercism#5863)

* Add custom input

* Add conditionals

* WIP

* Fix things

* First working iteration

Adjust tests, update TS, add return links, refactor, etc

Sort out setup_future_usage issue

Remove unused bits, tailor other bits

Add appearance obj

Add different theme objects

DRY

Use card element for subscriptions, refactor things

Fix a wrong type

Update import

Adjust tests

Fix things, and paths

Fix footer form anomaly

Improve TS on application.tsx

Add stripe version to footer cache key

Fix test

Force dark colors on premium and insiders

Refactor things, make card element options adaptive

Format utils better

Pass amount from backend

Delete unused component

Lazy load stripe.js (exercism#5843)

Add styling for load error and loading

Tweak margins

Fix the world (exercism#5861)

* Fix the world

* Tidy up, optimise ExercismStripeElements

* Tidy up StripeForm

* add off_session on Stripe::Subscription.create

* Refactor Form, useStripeForm

* Leave things at a simple, working state

* Fix tests

* One more fix

---------

Co-authored-by: dem4ron <demaaron88@gmail.com>

Fix bad rebase

* Fix flow

* Tweak tweak tweak

* Add icons

* Show error on blur (exercism#5865)

* Improvements

* Fix missing import

* Fix mismatch

* Fix breaking StripeElementAmount

* Tweak insiders page copy

* Remove final references to Premium

---------

Co-authored-by: Aron Demeter <66035744+dem4ron@users.noreply.github.com>
Co-authored-by: dem4ron <demaaron88@gmail.com>
  • Loading branch information
3 people authored Aug 9, 2023
1 parent b60b95d commit 1578997
Show file tree
Hide file tree
Showing 249 changed files with 1,683 additions and 4,472 deletions.
9 changes: 2 additions & 7 deletions app/assemblers/assemble_active_subscription.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class AssembleActiveSubscription
include Mandate

initialize_with :user, :product
initialize_with :user

def call
return { subscription: nil } if user.blank? || subscription.blank?
Expand All @@ -18,11 +18,6 @@ def call
private
memoize
def subscription
case product.to_sym
when :donation
user.current_active_donation_subscription
when :premium
user.current_active_premium_subscription
end
user.current_active_subscription
end
end
2 changes: 1 addition & 1 deletion app/commands/payments/github/payment/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class Payments::Github::Payment::Create
initialize_with :user, :node_id, :amount_in_cents, subscription: nil

def call
Payments::Payment::Create.(user, :github, :donation, node_id, amount_in_cents, nil, subscription:)
Payments::Payment::Create.(user, :github, node_id, amount_in_cents, nil, subscription:)
end
end
2 changes: 1 addition & 1 deletion app/commands/payments/github/subscription/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class Payments::Github::Subscription::Create
initialize_with :user, :node_id, :amount_in_cents

def call
Payments::Subscription::Create.(user, :github, :donation, :month, node_id, amount_in_cents)
Payments::Subscription::Create.(user, :github, :month, node_id, amount_in_cents)
end
end
22 changes: 4 additions & 18 deletions app/commands/payments/payment/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,20 @@
class Payments::Payment::Create
include Mandate

initialize_with :user, :provider, :product, :external_id, :amount_in_cents, :external_receipt_url, subscription: nil
initialize_with :user, :provider, :external_id, :amount_in_cents, :external_receipt_url, subscription: nil

def call
Payments::Payment.create!(
user:,
provider:,
product:,
external_id:,
external_receipt_url:,
subscription:,
amount_in_cents:
).tap do |payment|
if product == :premium
User::Premium::Update.(user)
elsif product == :donation
User::UpdateTotalDonatedInCents.(user)
User::RegisterAsDonor.(user, Time.current)

if amount_in_cents >= Premium::LIFETIME_AMOUNT_IN_CENTS
# Immediately activate a user that donates 499 or more, as it's
# likely that the reason they're doing it is to immediately
# get a Premium subscription.
User::InsidersStatus::Activate.(user, force_lifetime: true)
else
User::InsidersStatus::Update.(user)
end
end

User::UpdateTotalDonatedInCents.(user)
User::RegisterAsDonor.(user, Time.current)
User::InsidersStatus::UpdateForPayment.(user)
Payments::Payment::SendEmail.defer(payment)
end
rescue ActiveRecord::RecordNotUnique
Expand Down
5 changes: 1 addition & 4 deletions app/commands/payments/payment/send_email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ class Payments::Payment::SendEmail

def call
User::SendEmail.(payment) do
mailer.with(payment:).payment_created.deliver_later
DonationsMailer.with(payment:).payment_created.deliver_later
end
end

private
def mailer = payment.premium? ? PremiumMailer : DonationsMailer
end
7 changes: 2 additions & 5 deletions app/commands/payments/payment/update_amount.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ def call

payment.update!(amount_in_cents:)

if payment.donation?
User::UpdateTotalDonatedInCents.(payment.user)
elsif payment.premium?
User::Premium::Update.(user)
end
User::UpdateTotalDonatedInCents.(payment.user)
User::InsidersStatus::UpdateForPayment.(payment.user)
end
end
4 changes: 2 additions & 2 deletions app/commands/payments/paypal/payment/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
class Payments::Paypal::Payment::Create
include Mandate

initialize_with :user, :id, :amount, :product, subscription: nil
initialize_with :user, :id, :amount, subscription: nil

def call = Payments::Payment::Create.(user, :paypal, product, id, amount_in_cents, nil, subscription:)
def call = Payments::Payment::Create.(user, :paypal, id, amount_in_cents, nil, subscription:)

private
def amount_in_cents = (amount * 100).to_i
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def user = Payments::Paypal::Customer::FindOrUpdate.(payer_id, payer_email, user
def handle_completed
return unless user

Payments::Paypal::Payment::Create.(user, external_id, amount, product)
Payments::Paypal::Payment::Create.(user, external_id, amount)
end

def handle_canceled_reversal = Payments::Paypal::Payment::UpdateAmount.(external_id, amount)
Expand All @@ -37,5 +37,4 @@ def payment_status = payload["payment_status"]
def payer_id = payload["payer_id"]
def payer_email = payload["payer_email"]
def user_id = payload["custom"]
def product = :donation
end

This file was deleted.

12 changes: 0 additions & 12 deletions app/commands/payments/paypal/subscription/create.rb

This file was deleted.

54 changes: 0 additions & 54 deletions app/commands/payments/paypal/subscription/create_for_premium.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ def call
user = Payments::Paypal::Customer::FindOrUpdate.(payer_id, payer_email, paypal_subscription_id: subscription_external_id)
return unless user

subscription = Payments::Paypal::Subscription::Create.(user, subscription_external_id, amount, product, interval)
subscription = Payments::Subscription.find_by(external_id: subscription_external_id, provider: :paypal)
return unless subscription

Payments::Paypal::Payment::Create.(user, payment_external_id, amount, product, subscription:)
Payments::Paypal::Payment::Create.(user, payment_external_id, amount, subscription:)
end

private
Expand All @@ -20,6 +20,5 @@ def subscription_external_id = payload["recurring_payment_id"]
def payment_external_id = payload["txn_id"]
def payer_id = payload["payer_id"]
def payer_email = payload["payer_email"]
def product = Payments::Paypal.product_from_name(payload["product_name"])
def interval = Payments::Paypal.interval_from_payment_cycle(payload["payment_cycle"])
end

This file was deleted.

9 changes: 1 addition & 8 deletions app/commands/payments/stripe/payment/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,14 @@ def initialize
initialize_with :user, :stripe_data, subscription: nil

def call
Payments::Payment::Create.(user, :stripe, product, external_id, amount_in_cents, external_receipt_url, subscription:)
Payments::Payment::Create.(user, :stripe, external_id, amount_in_cents, external_receipt_url, subscription:)
end

private
def external_id = stripe_data.id
def external_receipt_url = stripe_data.charges.first.receipt_url
def amount_in_cents = stripe_data.amount

def product
return subscription.product if subscription

# Premium payments are always linked to a subscription
:donation
end

memoize
def subscription
return @subscription if @subscription
Expand Down
4 changes: 0 additions & 4 deletions app/commands/payments/stripe/payment_intent/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ def call
Payments::Stripe::Customer::CreateForEmail.(user_or_email)

case type.to_sym
when :premium_monthly_subscription
Payments::Stripe::PaymentIntent::CreateForPremiumSubscription.(customer_id, :monthly)
when :premium_yearly_subscription
Payments::Stripe::PaymentIntent::CreateForPremiumSubscription.(customer_id, :yearly)
when :subscription
Payments::Stripe::PaymentIntent::CreateForSubscription.(customer_id, amount_in_cents)
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ class Payments::Stripe::PaymentIntent::CreateForPayment
initialize_with :customer_id, :amount_in_cents

def call
Stripe::PaymentIntent.create(
options = {
customer: customer_id,
amount: amount_in_cents,
currency: 'usd',
setup_future_usage: 'off_session'
)
automatic_payment_methods: {
enabled: true
}
}

Stripe::PaymentIntent.create(**options)
end
end

This file was deleted.

3 changes: 1 addition & 2 deletions app/commands/payments/stripe/subscription/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ class UnknownSubscriptionIntervalError < RuntimeError; end

initialize_with :user, :stripe_data

def call = Payments::Subscription::Create.(user, :stripe, product, interval, external_id, amount_in_cents)
def call = Payments::Subscription::Create.(user, :stripe, interval, external_id, amount_in_cents)

private
def external_id = stripe_data.id
def amount_in_cents = price.unit_amount
def product = Payments::Stripe.product_from_id(price.product)
def price = stripe_data.items.data[0].price

def interval
Expand Down
5 changes: 4 additions & 1 deletion app/commands/payments/subscription/activate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ class Payments::Subscription::Activate

initialize_with :subscription

def call = Payments::Subscription::UpdateStatus.(subscription, :active)
def call
subscription.update!(status: :active)
User::InsidersStatus::UpdateForPayment.(subscription.user)
end
end
5 changes: 4 additions & 1 deletion app/commands/payments/subscription/cancel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ class Payments::Subscription::Cancel

initialize_with :subscription

def call = Payments::Subscription::UpdateStatus.(subscription, :canceled)
def call
subscription.update!(status: :canceled)
User::InsidersStatus::Update.defer(subscription.user)
end
end
10 changes: 2 additions & 8 deletions app/commands/payments/subscription/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,18 @@
class Payments::Subscription::Create
include Mandate

initialize_with :user, :provider, :product, :interval, :external_id, :amount_in_cents, status: :active
initialize_with :user, :provider, :interval, :external_id, :amount_in_cents, status: :active

def call
Payments::Subscription.create!(
user:,
provider:,
product:,
interval:,
external_id:,
amount_in_cents:,
status:
).tap do
case product
when :donation
User::UpdateActiveDonationSubscription.(user)
when :premium
User::Premium::Update.(user)
end
User::InsidersStatus::UpdateForPayment.(user)
end
rescue ActiveRecord::RecordNotUnique
Payments::Subscription.find_by!(external_id:, provider:)
Expand Down
Loading

0 comments on commit 1578997

Please sign in to comment.