Created
March 21, 2019 15:56
-
-
Save bearded-avenger/ae651940dfb230b891f3d12b19df057a to your computer and use it in GitHub Desktop.
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
module Checkups::Notify | |
require 'slack-notify' | |
class Dispatch | |
def initialize(params) | |
@message = params[:message] | |
@room = params[:room] || '#web-dev_events' | |
@botname = params[:botname] || 'EDU Bot' | |
end | |
def call | |
client.notify(@message) if dispatchable? | |
end | |
private | |
attr_reader :message, :room, :botname | |
def client | |
SlackNotify::Client.new( | |
webhook_url:webhook_url, | |
channel:room, | |
username:botname, | |
icon_url: 'https://d34urnl45u363e.cloudfront.net/site-assets/logo-mark.png' | |
) | |
end | |
def dispatchable? | |
message.present? && webhook_url.present? | |
end | |
def webhook_url | |
ENV['SLACK_WEBHOOK_URL'] | |
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
module AppServices | |
class StripeSyncSubscriptionService | |
def initialize(params) | |
@subscription = params[:subscription] | |
end | |
def call | |
if sub_id.present? | |
Retriable.retriable on: Stripe::RateLimitError, tries: 3, base_interval: 10 do | |
reconcile_sub | |
end | |
else | |
notify_subscription_missing | |
Rails.logger.error "[Stripe Subscription Sync] No subscription_id present for local subscription with id #{@subscription.id}" | |
end | |
end | |
private | |
attr_reader :subscription | |
def reconcile_sub | |
sub = StripeServices::RetrieveSubscription.new({subscription_id:sub_id}).call | |
if sub && sub.success? | |
# sync status | |
subscription.update_attributes!(status:sub.payload.status) | |
# downgrade any users not active | |
if ['canceled','past_due','expired'].include?(sub.payload.status) | |
downgrade_subscription_and_user(sub.payload.status) | |
end | |
else | |
notify_subscription_mismatch | |
end | |
end | |
def sub_id | |
subscription.subscription_id | |
end | |
def downgrade_subscription_and_user(status) | |
subscription.user.info.update_attributes!(access_level_id:1) | |
subscription.update_attributes!(expiration:Date.today) | |
subscription.update_column(:subscription_id, nil) if ['canceled','expired'].include?(status) | |
end | |
def notify_subscription_missing | |
Checkups::Notify::Dispatch.new(message:"Stripe subscription_id missing for CGC Subscription ##{subscription.id}",room:'#community-support').call if Rails.env.production? | |
end | |
def notify_subscription_mismatch | |
Checkups::Notify::Dispatch.new(message:"CGC Subscription ##{subscription.id} with Stripe subscription_id #{sub_id} not found in Stripe.",room:'#community-support').call if Rails.env.production? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment