Skip to content

Commit

Permalink
Merge branch 'narocorporation-feature/allow-custom-mailer'
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Jun 7, 2022
2 parents 57f9171 + 6c19dbc commit 225642f
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 9 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
### Unreleased

* Update `refund!` method in `stripe/charge.rb` to handle multiple refunds on the same charge. - @cjilbert504 @kyleschmolze
* Add configuration options for mailer - @excid3 @le-doude @cjilbert504

```ruby
Pay.setup do |config|
# Change parent mailer for Pay::UserMailer
config.parent_mailer = "MyCustomMailer"

# Change the mailer for Pay
config.mailer = "MyCustomMailer"
```

### 4.0.0

Expand Down
2 changes: 1 addition & 1 deletion app/mailers/pay/user_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Pay
class UserMailer < ApplicationMailer
class UserMailer < Pay.parent_mailer.constantize
def receipt
if params[:pay_charge].respond_to? :receipt
attachments[params[:pay_charge].filename] = params[:pay_charge].receipt
Expand Down
16 changes: 16 additions & 0 deletions lib/pay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require "pay/errors"
require "pay/adapter"

require "active_support/dependencies"

module Pay
autoload :Attributes, "pay/attributes"
autoload :Env, "pay/env"
Expand Down Expand Up @@ -60,6 +62,20 @@ module Billable
(price&.type == "recurring") && (price.recurring&.interval == "year")
}

@@mailer = "Pay::UserMailer"

def self.mailer=(value)
@@mailer = value
@@mailer_ref = nil
end

def self.mailer
@@mailer_ref ||= @@mailer&.constantize
end

mattr_accessor :parent_mailer
@@parent_mailer = "Pay::ApplicationMailer"

def self.setup
yield self
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def call(event)
pay_charge = Pay::Braintree::Billable.new(pay_customer).save_transaction(subscription.transactions.first)

if pay_charge && Pay.send_email?(:receipt, pay_charge)
Pay::UserMailer.with(pay_customer: pay_customer, pay_charge: pay_charge).receipt.deliver_later
Pay.mailer.with(pay_customer: pay_customer, pay_charge: pay_charge).receipt.deliver_later
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def call(event)
# pay_charge = Pay::Braintree::Billable.new(pay_customer).save_transaction(subscription.transactions.first)

# if Pay.send_emails
# Pay::UserMailer.with(pay_customer: pay_charge.customer, charge: pay_charge).receipt.deliver_later
# Pay.mailer.with(pay_customer: pay_charge.customer, charge: pay_charge).receipt.deliver_later
# end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pay/paddle/webhooks/subscription_payment_refunded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def call(event)
pay_charge.update!(amount_refunded: (event.gross_refund.to_f * 100).to_i)

if Pay.send_email?(:refund, pay_charge)
Pay::UserMailer.with(pay_customer: pay_charge.customer, pay_charge: pay_charge).refund.deliver_later
Pay.mailer.with(pay_customer: pay_charge.customer, pay_charge: pay_charge).refund.deliver_later
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pay/paddle/webhooks/subscription_payment_succeeded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def create_charge(pay_customer, event)

def notify_user(pay_charge)
if Pay.send_email?(:receipt, pay_charge)
Pay::UserMailer.with(pay_customer: pay_charge.customer, pay_charge: pay_charge).receipt.deliver_later
Pay.mailer.with(pay_customer: pay_charge.customer, pay_charge: pay_charge).receipt.deliver_later
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pay/stripe/webhooks/charge_refunded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def call(event)
pay_charge = Pay::Stripe::Charge.sync(event.data.object.id, stripe_account: event.try(:account))

if pay_charge && Pay.send_email?(:refund, pay_charge)
Pay::UserMailer.with(pay_customer: pay_charge.customer, pay_charge: pay_charge).refund.deliver_later
Pay.mailer.with(pay_customer: pay_charge.customer, pay_charge: pay_charge).refund.deliver_later
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pay/stripe/webhooks/charge_succeeded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def call(event)
pay_charge = Pay::Stripe::Charge.sync(event.data.object.id, stripe_account: event.try(:account))

if pay_charge && Pay.send_email?(:receipt, pay_charge)
Pay::UserMailer.with(pay_customer: pay_charge.customer, pay_charge: pay_charge).receipt.deliver_later
Pay.mailer.with(pay_customer: pay_charge.customer, pay_charge: pay_charge).receipt.deliver_later
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pay/stripe/webhooks/payment_action_required.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def call(event)
return if pay_subscription.nil?

if Pay.send_email?(:payment_action_required, pay_subscription)
Pay::UserMailer.with(
Pay.mailer.with(
pay_customer: pay_subscription.customer,
payment_intent_id: event.data.object.payment_intent,
pay_subscription: pay_subscription
Expand Down
2 changes: 1 addition & 1 deletion lib/pay/stripe/webhooks/subscription_renewing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def call(event)
price = event.data.object.lines.data.first.price

if Pay.send_email?(:subscription_renewing, pay_subscription, price)
Pay::UserMailer.with(
Pay.mailer.with(
pay_customer: pay_subscription.customer,
pay_subscription: pay_subscription,
date: Time.zone.at(event.data.object.next_payment_attempt)
Expand Down
28 changes: 28 additions & 0 deletions test/pay_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ class Pay::Test < ActiveSupport::TestCase
assert Pay.respond_to?(:enabled_processors=)
end

test "parent_mailer config" do
assert Pay.respond_to?(:parent_mailer=)
assert Pay.respond_to?(:parent_mailer)
assert_equal "Pay::ApplicationMailer", Pay.parent_mailer
end

test "mailer config" do
assert Pay.respond_to?(:mailer=)
assert Pay.respond_to?(:mailer)

Pay.mailer = "Pay::ApplicationMailer"
assert_equal Pay::ApplicationMailer, Pay.mailer

Pay.mailer = "Pay::UserMailer"
assert_equal Pay::UserMailer, Pay.mailer
end

test "can enable and disable the stripe processor" do
original = Pay.enabled_processors

Expand Down Expand Up @@ -104,4 +121,15 @@ class Pay::Test < ActiveSupport::TestCase
Pay.send_email?(:subscription_renewing, pay_subscription)
end
end

test "can retrieve Pay::UserMail as default mailer" do
assert_equal(Pay.mailer, Pay::UserMailer)
end

test "can configure mailer and retrieve correct class" do
Pay.mailer = "ApplicationMailer"
assert_equal(Pay.mailer, ApplicationMailer)

Pay.mailer = "Pay::UserMailer" # clean up for other tests
end
end

0 comments on commit 225642f

Please sign in to comment.