amdirent_stripe 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3bc259a404935d0acd93fee2ace2eb77f32a91f1
4
+ data.tar.gz: 19995a3fab597f90e25cfb7a530066f0231db4a3
5
+ SHA512:
6
+ metadata.gz: '091354a9a4113faa12dd6a4f8c82f2e51bf8a96d2c8c58db3436af748bb564f3bfc2c4587d9b7a5d4d331f3ba6d143f24a53d2fad59ed90f000732b766132545'
7
+ data.tar.gz: f9ded5ea88f33167809e9e7602d00e5634f5dc826619153401e5e681969b058218c2fd43373a8c57d49a5e3104d902cfb45a579c69d0529c34229393e1649bbc
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ .env
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in amdirent_stripe.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Jordan Prince
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ # AmdirentStripe
2
+
3
+ Contains a mixin for Customer AR models that expects the model to have a stripe_key and an email.
4
+ Defines helper methods for charging cards, saving cards, doing charges and subscriptions, etc.
5
+ See stripe_mixin.rb for detail.
6
+
7
+ To use:
8
+ `require 'amdirent_stripe/stripe_mixin'
9
+
10
+
11
+ `class Customer < ActiveRecord::Base
12
+ include AmdirentStripe::StripeMixin
13
+ end`
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'amdirent_stripe'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install amdirent_stripe
30
+
31
+ ## Usage
32
+
33
+ TODO: Write usage instructions here
34
+
35
+ ## Development
36
+
37
+ `rspec` to run tests, but make sure you create postgres DB amdirent_stripe_test first, and install a .env file
38
+ at the top level containing STRIPE_API_KEY env.
39
+
40
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+
42
+ ## Contributing
43
+
44
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/amdirent_stripe.
45
+
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
50
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'amdirent_stripe/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "amdirent_stripe"
8
+ spec.version = AmdirentStripe::VERSION
9
+ spec.authors = ["Jordan Prince"]
10
+ spec.email = ["jordanmprince@gmail.com"]
11
+
12
+ spec.summary = %q{Stripe Helper methods for models with a stripe_key column.}
13
+ spec.description = %q{Helpers for Stripe}
14
+ spec.homepage = "https://www.github.com/amdirent/amdirent_stripe"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
21
+ f.match(%r{^(test|spec|features)/})
22
+ end
23
+
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.14"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "pry"
32
+ spec.add_development_dependency "pg"
33
+ spec.add_development_dependency "dotenv"
34
+ spec.add_dependency "stripe"
35
+ spec.add_dependency "activerecord"
36
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "amdirent_stripe"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ require "amdirent_stripe/version"
2
+
3
+ module AmdirentStripe
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,29 @@
1
+ class NoCardError < Exception
2
+ def message
3
+ "No card present!"
4
+ end
5
+ end
6
+
7
+ class NoCustomerError < Exception
8
+ def message
9
+ "No stripe customer present!"
10
+ end
11
+ end
12
+
13
+ class NoSuchCardError < Exception
14
+ def message
15
+ "Not a valid card token!"
16
+ end
17
+ end
18
+
19
+ class NoSuchCustomerError < Exception
20
+ def message
21
+ "No such stripe customer!"
22
+ end
23
+ end
24
+
25
+ class NoSuchSubscriptionError < Exception
26
+ def message
27
+ "No such stripe subscription!"
28
+ end
29
+ end
@@ -0,0 +1,98 @@
1
+ require 'stripe'
2
+ require_relative 'errors'
3
+ module AmdirentStripe::StripeMixin
4
+ extend ActiveSupport::Concern
5
+ included do
6
+
7
+ before_create do
8
+ self.stripe_key = ::Stripe::Customer.create(email: email).id
9
+ end
10
+ end
11
+
12
+ def charges
13
+ raise NoCustomerError.new unless stripe_customer
14
+ ::Stripe::Charge.list(customer: stripe_key)
15
+ end
16
+
17
+ def plans
18
+ ::Stripe::Plan.list()
19
+ end
20
+
21
+ def subscriptions
22
+ raise NoCustomerError.new unless stripe_customer
23
+ ::Stripe::Subscription.list(customer: stripe_key)
24
+ end
25
+
26
+
27
+ def subscribe_to!(plan)
28
+ raise NoCustomerError.new unless stripe_customer
29
+ raise NoCardError.new unless stripe_customer.default_source
30
+
31
+ ::Stripe::Subscription.create(
32
+ :customer => stripe_key,
33
+ :plan => plan.is_a?(String) ? plan : plan.id
34
+ )
35
+ end
36
+
37
+ def stripe_customer
38
+ if stripe_key
39
+ begin
40
+ @stripe_customer ||= ::Stripe::Customer.retrieve(stripe_key)
41
+
42
+ if(@stripe_customer.deleted?)
43
+ raise NoSuchCustomerError.new
44
+ end
45
+
46
+ @stripe_customer
47
+ rescue ::Stripe::InvalidRequestError => e
48
+ if(e.message.match(/No such customer/))
49
+ raise NoSuchCustomerError.new
50
+ end
51
+ raise e
52
+ end
53
+ end
54
+ end
55
+
56
+ def cancel_subscription!(id)
57
+ begin
58
+ stripe_customer.subscriptions.retrieve(id).delete
59
+ rescue ::Stripe::InvalidRequestError => e
60
+ if(e.message.match(/does not have a subscription/))
61
+ raise NoSuchSubscriptionError.new
62
+ end
63
+ raise e
64
+ end
65
+ end
66
+
67
+ def has_active_subs?
68
+ subscriptions.data.any? { |sub| !sub.ended_at and !sub.canceled_at }
69
+ end
70
+
71
+ def save_card!(tok)
72
+ raise NoCustomerError.new unless stripe_customer
73
+ begin
74
+ stripe_customer.source = tok
75
+ stripe_customer.save
76
+ rescue ::Stripe::InvalidRequestError => e
77
+ if e.message.match(/No such source/) or e.message.match(/No such token/)
78
+ raise NoSuchCardError
79
+ end
80
+ raise e
81
+ end
82
+ end
83
+
84
+ private
85
+ def charge!(amt, desc)
86
+ raise NoCustomerError.new unless stripe_customer
87
+ raise NoCardError.new unless stripe_customer.default_source
88
+
89
+ ::Stripe::Charge.create(
90
+ :amount => amt,
91
+ :currency => "usd",
92
+ :customer => stripe_key,
93
+ :description => "Charge for #{email} for #{desc}"
94
+ )
95
+ end
96
+
97
+ end
98
+
@@ -0,0 +1,3 @@
1
+ module AmdirentStripe
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: amdirent_stripe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jordan Prince
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: dotenv
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: stripe
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: activerecord
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Helpers for Stripe
126
+ email:
127
+ - jordanmprince@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - Gemfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - amdirent_stripe.gemspec
140
+ - bin/console
141
+ - bin/setup
142
+ - lib/amdirent_stripe.rb
143
+ - lib/amdirent_stripe/errors.rb
144
+ - lib/amdirent_stripe/stripe_mixin.rb
145
+ - lib/amdirent_stripe/version.rb
146
+ homepage: https://www.github.com/amdirent/amdirent_stripe
147
+ licenses:
148
+ - MIT
149
+ metadata: {}
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 2.6.11
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: Stripe Helper methods for models with a stripe_key column.
170
+ test_files: []