Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
marckohlbrugge committed Jun 7, 2024
0 parents commit d27a189
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 3.3.0
10 changes: 10 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

source "https://rubygems.org"

# gem "rails"

gem "thor", "~> 1.3"
gem "stripe", "~> 11.6"

gem "debug", "~> 1.9"
31 changes: 31 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
GEM
remote: https://rubygems.org/
specs:
debug (1.9.2)
irb (~> 1.10)
reline (>= 0.3.8)
io-console (0.7.2)
irb (1.13.1)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
psych (5.1.2)
stringio
rdoc (6.7.0)
psych (>= 4.0.0)
reline (0.5.8)
io-console (~> 0.5)
stringio (3.1.0)
stripe (11.6.0)
thor (1.3.1)

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES
debug (~> 1.9)
stripe (~> 11.6)
thor (~> 1.3)

BUNDLED WITH
2.5.5
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Simple Ruby CLI to see a breakdown of Stripe subscription cancellations and their reasons.

### Usage

1. Get your Stripe API key here: https://dashboard.stripe.com/apikeys
2. `git clone https://github.com/marckohlbrugge/stripe-cancellation-reasons.git`
3. `cd stripe-cancellation-reasons`
4. `bundle install`
5. `bundle exec ruby app.rb`

### Example

```sh
bundle exec ruby app.rb cancellations
Reason: unused - Count: 14 (36.84%)
Reason: too_expensive - Count: 8 (21.05%)
Comment: It wasn’t working very disappointed
Reason: payment_failed - Count: 6 (15.79%)
Reason: cancellation_requested - Count: 5 (13.16%)
Reason: other - Count: 3 (7.89%)
Comment: I want picture stories
Comment: schlechte Qualität.
Comment: I paid for the subscription, but every time I wanted to make a story, they gave me an error. In the end, I didn't do anything
Reason: switched_service - Count: 2 (5.26%)
```
48 changes: 48 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "thor"
require "stripe"
require "io/console"

class StripeCLI < Thor
desc "cancellations", "Fetch and display grouped cancellation reasons for all canceled subscriptions"
method_option :api_key, aliases: "-k", type: :string, desc: "Stripe API Key"

def cancellations
api_key = options[:api_key] || ENV["STRIPE_API_KEY"] || prompt_for_api_key
Stripe.api_key = api_key

reasons_count = Hash.new { |hash, key| hash[key] = {count: 0, comments: []} }
total_cancellations = 0

Stripe::Subscription.list(status: "canceled").auto_paging_each do |subscription|
detail = subscription.cancellation_details || {}
comment = detail["comment"]
feedback = detail["feedback"] || detail["reason"] || "No reason provided"

reasons_count[feedback][:count] += 1
reasons_count[feedback][:comments] << comment if comment
total_cancellations += 1
end

sorted_reasons = reasons_count.sort_by { |_, data| -data[:count] }

sorted_reasons.each do |reason, data|
percentage = (data[:count].to_f / total_cancellations * 100).round(2)
puts "Reason: #{reason} - Count: #{data[:count]} (#{percentage}%)"

unless data[:comments].empty?
data[:comments].each do |comment|
puts " Comment: #{comment}"
end
end
end
end

private

def prompt_for_api_key
print "Please enter your Stripe API Key: "
STDIN.noecho(&:gets).chomp
end
end

StripeCLI.start(ARGV)

0 comments on commit d27a189

Please sign in to comment.