-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from solidusio-contrib/aldesantis/churn-buster
Integrate Churn Buster
- Loading branch information
Showing
26 changed files
with
777 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
28 changes: 28 additions & 0 deletions
28
...lidus_subscriptions/spree/wallet_payment_source/report_default_change_to_subscriptions.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusSubscriptions | ||
module Spree | ||
module WalletPaymentSource | ||
module ReportDefaultChangeToSubscriptions | ||
def self.prepended(base) | ||
base.after_save :report_default_change_to_subscriptions | ||
end | ||
|
||
private | ||
|
||
def report_default_change_to_subscriptions | ||
return if !previous_changes.key?('default') || !default? | ||
|
||
user.subscriptions.with_default_payment_source.each do |subscription| | ||
::Spree::Event.fire( | ||
'solidus_subscriptions.subscription_payment_method_changed', | ||
subscription: subscription, | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
Spree::WalletPaymentSource.prepend(SolidusSubscriptions::Spree::WalletPaymentSource::ReportDefaultChangeToSubscriptions) |
This file contains 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
This file contains 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
This file contains 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
39 changes: 39 additions & 0 deletions
39
app/subscribers/solidus_subscriptions/churn_buster_subscriber.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusSubscriptions | ||
module ChurnBusterSubscriber | ||
include ::Spree::Event::Subscriber | ||
|
||
event_action :report_subscription_cancellation, event_name: 'solidus_subscriptions.subscription_canceled' | ||
event_action :report_subscription_ending, event_name: 'solidus_subscriptions.subscription_ended' | ||
event_action :report_payment_success, event_name: 'solidus_subscriptions.installments_succeeded' | ||
event_action :report_payment_failure, event_name: 'solidus_subscriptions.installments_failed_payment' | ||
event_action :report_payment_method_change, event_name: 'solidus_subscriptions.subscription_payment_method_changed' | ||
|
||
def report_subscription_cancellation(event) | ||
churn_buster&.report_subscription_cancellation(event.payload.fetch(:subscription)) | ||
end | ||
|
||
def report_subscription_ending(event) | ||
churn_buster&.report_subscription_cancellation(event.payload.fetch(:subscription)) | ||
end | ||
|
||
def report_payment_success(event) | ||
churn_buster&.report_successful_payment(event.payload.fetch(:order)) | ||
end | ||
|
||
def report_payment_failure(event) | ||
churn_buster&.report_failed_payment(event.payload.fetch(:order)) | ||
end | ||
|
||
def report_payment_method_change(event) | ||
churn_buster&.report_payment_method_change(event.payload.fetch(:subscription)) | ||
end | ||
|
||
private | ||
|
||
def churn_buster | ||
SolidusSubscriptions.churn_buster | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
Spree.config do |config| | ||
config.events.subscribers << 'SolidusSubscriptions::EventStorageSubscriber' | ||
if Spree.solidus_gem_version < Gem::Version.new('2.11.0') | ||
require SolidusSubscriptions::Engine.root.join('app/subscribers/solidus_subscriptions/event_storage_subscriber') | ||
require SolidusSubscriptions::Engine.root.join('app/subscribers/solidus_subscriptions/churn_buster_subscriber') | ||
|
||
SolidusSubscriptions::ChurnBusterSubscriber.subscribe! | ||
SolidusSubscriptions::EventStorageSubscriber.subscribe! | ||
end |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusSubscriptions | ||
module ChurnBuster | ||
class Client | ||
BASE_API_URL = 'https://api.churnbuster.io/v1' | ||
|
||
attr_reader :account_id, :api_key | ||
|
||
def initialize(account_id:, api_key:) | ||
@account_id = account_id | ||
@api_key = api_key | ||
end | ||
|
||
def report_failed_payment(order) | ||
post('/failed_payments', OrderSerializer.serialize(order)) | ||
end | ||
|
||
def report_successful_payment(order) | ||
post('/successful_payments', OrderSerializer.serialize(order)) | ||
end | ||
|
||
def report_subscription_cancellation(subscription) | ||
post('/cancellations', SubscriptionSerializer.serialize(subscription)) | ||
end | ||
|
||
def report_payment_method_change(subscription) | ||
post('/payment_methods', SubscriptionPaymentMethodSerializer.serialize(subscription)) | ||
end | ||
|
||
private | ||
|
||
def post(path, body) | ||
HTTParty.post( | ||
"#{BASE_API_URL}#{path}", | ||
body: body.to_json, | ||
headers: { | ||
'Content-Type' => 'application/json', | ||
}, | ||
basic_auth: { | ||
username: account_id, | ||
password: api_key, | ||
}, | ||
) | ||
end | ||
end | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
lib/solidus_subscriptions/churn_buster/order_serializer.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusSubscriptions | ||
module ChurnBuster | ||
class OrderSerializer < Serializer | ||
def to_h | ||
{ | ||
payment: { | ||
source: 'in_house', | ||
source_id: object.number, | ||
amount_in_cents: object.display_total.cents, | ||
currency: object.currency, | ||
}, | ||
customer: SubscriptionCustomerSerializer.serialize(object.subscription), | ||
} | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusSubscriptions | ||
module ChurnBuster | ||
class Serializer | ||
attr_reader :object | ||
|
||
class << self | ||
def serialize(object) | ||
new(object).to_h | ||
end | ||
end | ||
|
||
def initialize(object) | ||
@object = object | ||
end | ||
|
||
def to_h | ||
raise NotImplementedError | ||
end | ||
end | ||
end | ||
end |
19 changes: 19 additions & 0 deletions
19
lib/solidus_subscriptions/churn_buster/subscription_customer_serializer.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusSubscriptions | ||
module ChurnBuster | ||
class SubscriptionCustomerSerializer < Serializer | ||
def to_h | ||
{ | ||
source: 'in_house', | ||
source_id: object.id, | ||
email: object.user.email, | ||
properties: { | ||
first_name: object.shipping_address_to_use.firstname, | ||
last_name: object.shipping_address_to_use.lastname, | ||
}, | ||
} | ||
end | ||
end | ||
end | ||
end |
37 changes: 37 additions & 0 deletions
37
lib/solidus_subscriptions/churn_buster/subscription_payment_method_serializer.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusSubscriptions | ||
module ChurnBuster | ||
class SubscriptionPaymentMethodSerializer < Serializer | ||
def to_h | ||
{ | ||
payment_method: { | ||
source: 'in_house', | ||
source_id: [ | ||
object.payment_method_to_use&.id, | ||
object.payment_source_to_use&.id | ||
].compact.join('-'), | ||
type: 'card', | ||
properties: payment_source_properties, | ||
}, | ||
customer: SubscriptionCustomerSerializer.serialize(object), | ||
} | ||
end | ||
|
||
private | ||
|
||
def payment_source_properties | ||
if object.payment_source.is_a?(::Spree::CreditCard) | ||
{ | ||
brand: object.payment_source.cc_type, | ||
last4: object.payment_source.last_digits, | ||
exp_month: object.payment_source.month, | ||
exp_year: object.payment_source.year, | ||
} | ||
else | ||
{} | ||
end | ||
end | ||
end | ||
end | ||
end |
17 changes: 17 additions & 0 deletions
17
lib/solidus_subscriptions/churn_buster/subscription_serializer.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusSubscriptions | ||
module ChurnBuster | ||
class SubscriptionSerializer < Serializer | ||
def to_h | ||
{ | ||
subscription: { | ||
source: 'in_house', | ||
source_id: object.id | ||
}, | ||
customer: SubscriptionCustomerSerializer.serialize(object), | ||
} | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.