pay_simple 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -0
  3. data/.rspec +1 -0
  4. data/Gemfile +12 -0
  5. data/Gemfile.lock +35 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +100 -0
  8. data/Rakefile +1 -0
  9. data/lib/ps.rb +32 -0
  10. data/lib/ps/api.rb +80 -0
  11. data/lib/ps/api/json.rb +78 -0
  12. data/lib/ps/base.rb +22 -0
  13. data/lib/ps/enumerations.rb +191 -0
  14. data/lib/ps/exceptions.rb +5 -0
  15. data/lib/ps/object.rb +48 -0
  16. data/lib/ps/objects/ach_account.rb +34 -0
  17. data/lib/ps/objects/credit_card_account.rb +38 -0
  18. data/lib/ps/objects/customer.rb +79 -0
  19. data/lib/ps/objects/customer_account.rb +25 -0
  20. data/lib/ps/objects/payment.rb +73 -0
  21. data/lib/ps/objects/payment_status_filter.rb +6 -0
  22. data/lib/ps/objects/recurring_payment.rb +60 -0
  23. data/lib/ps/objects/recurring_payment_filter.rb +26 -0
  24. data/lib/ps/objects/user.rb +10 -0
  25. data/lib/ps/response.rb +83 -0
  26. data/lib/ps/util.rb +28 -0
  27. data/lib/ps/util/hash.rb +17 -0
  28. data/lib/ps/util/state.rb +15 -0
  29. data/lib/ps/util/states.yml +263 -0
  30. data/lib/ps/util/string.rb +15 -0
  31. data/paysimple.gemspec +19 -0
  32. data/spec/config.yml.example +13 -0
  33. data/spec/factories/ach_account.rb +11 -0
  34. data/spec/factories/credit_card_accounts.rb +11 -0
  35. data/spec/factories/customer_accounts.rb +7 -0
  36. data/spec/factories/customers.rb +18 -0
  37. data/spec/factories/payment.rb +12 -0
  38. data/spec/factories/recurring_payment.rb +25 -0
  39. data/spec/ps/api/json_spec.rb +12 -0
  40. data/spec/ps/api_spec.rb +53 -0
  41. data/spec/ps/base_spec.rb +40 -0
  42. data/spec/ps/format_spec.rb +16 -0
  43. data/spec/ps/object_spec.rb +35 -0
  44. data/spec/ps/objects/ach_account_spec.rb +57 -0
  45. data/spec/ps/objects/credit_card_account_spec.rb +69 -0
  46. data/spec/ps/objects/customer_account_spec.rb +43 -0
  47. data/spec/ps/objects/customer_spec.rb +153 -0
  48. data/spec/ps/objects/payment_spec.rb +98 -0
  49. data/spec/ps/objects/recurring_payment_spec.rb +145 -0
  50. data/spec/ps/objects/user_spec.rb +14 -0
  51. data/spec/ps/response_spec.rb +39 -0
  52. data/spec/ps/util/hash_spec.rb +33 -0
  53. data/spec/ps/util/states_spec.rb +25 -0
  54. data/spec/ps/util/string_spec.rb +21 -0
  55. data/spec/ps/util_spec.rb +30 -0
  56. data/spec/spec_functions.rb +155 -0
  57. data/spec/spec_helper.rb +22 -0
  58. metadata +156 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2532957b6fb8a3d031363541f83eb3b0b562caa9
4
+ data.tar.gz: 5525f4e0701481d83433b63c87a81ffe8ff4a859
5
+ SHA512:
6
+ metadata.gz: 22546392eccc615b4a0f879061d8395ef3ae4a17c4573dea6b1e126a2cf624065cc2075d34472fe3d85881bac0fa5960aa88e82c4c54f19eb1dc5428c5176652
7
+ data.tar.gz: ea1bb1450e5066982e06d0505711f71a4fbc4d7d80cc5d6d64b52efd378476770ab84c29e4f53b8c04fdb4b08031983d217871e83cebd6910848f256beb79e31
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ ./spec/config.yml
2
+ spec/concept_testing.rb
3
+ spec/config.yml
4
+ *.swp
5
+ *.swo
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in paysimple.gemspec
4
+ gemspec
5
+
6
+ gem 'httparty'
7
+ gem 'rspec'
8
+
9
+ group :test do
10
+ gem 'factory_girl'
11
+ gem 'simplecov'
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ PS (0.0.0)
5
+ httparty
6
+ json
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ diff-lcs (1.2.4)
12
+ hooks (0.3.1)
13
+ httparty (0.11.0)
14
+ multi_json (~> 1.0)
15
+ multi_xml (>= 0.5.2)
16
+ json (1.8.0)
17
+ multi_json (1.7.9)
18
+ multi_xml (0.5.5)
19
+ rspec (2.14.1)
20
+ rspec-core (~> 2.14.0)
21
+ rspec-expectations (~> 2.14.0)
22
+ rspec-mocks (~> 2.14.0)
23
+ rspec-core (2.14.5)
24
+ rspec-expectations (2.14.2)
25
+ diff-lcs (>= 1.1.3, < 2.0)
26
+ rspec-mocks (2.14.3)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ PS!
33
+ hooks
34
+ httparty
35
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 CTA
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # Paysimple
2
+
3
+ A multiformat integration with paysimple's version 3 api.
4
+
5
+ We hope to replace the already existing paysimple gem that hasn't been updated in years.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'paysimple'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install paysimple
20
+
21
+ ## Usage
22
+ please note that the documention, and the gem are still works in progress. Clone this repo for usage at your own risk.
23
+
24
+ ### Connection to paysimple
25
+ require 'paysimple'
26
+ PS::Base.establish_connection(
27
+ :format => "JSON",
28
+ :apikey => your_api_key,
29
+ :userkey => the_user_key,
30
+ :company_name => your_company_name
31
+ )
32
+ ### Creating a customer
33
+ require 'paysimple'
34
+ PS::Base.establish_connection( ... )
35
+
36
+ customer_params = {
37
+ :first_name => "test",
38
+ :middle_name => "e",
39
+ :last_name => "name",
40
+ :email => "example@test.com",
41
+ :phone => "0000000000",
42
+ :billing_address1 => "1600 Pennsylvania Ave NW",
43
+ :billing_city => "Washington",
44
+ :billing_state => 8,
45
+ :billing_postal_code => 20500,
46
+ :billing_country_code => "USA",
47
+ :shipping_same_as_billing => 1
48
+ }
49
+
50
+ customer = PS::Customer.create(customer_params)
51
+ p customer
52
+ # =>
53
+ #<PS::Customer ps_reference_id: 1, billing_address1: '1600 Pennsylvania Ave NW', billing_city: 'Washington', billing_postal_code: '20500', billing_state: 8, email: 'example@test.com', first_name: 'test', last_name: 'e', phone: '0000000000', shipping_same_as_billing: 1>
54
+
55
+ ### Making a payment
56
+ customer = PS::Customer.find(1)
57
+ cc = PS::CreditCardAccount.create({
58
+ :account_number => "4111111111111111",
59
+ :c_c_expiry => "2014/12",
60
+ :c_c_type => CreditCardIssuer::VISA,
61
+ :customer_id => customer.ps_reference_id
62
+ })
63
+ payment = Payment.make(customer.ps_reference_id, 100000, cc.ps_reference_id)
64
+ ### Alternative method of making a payment
65
+ customer = {
66
+ :first_name => "test",
67
+ :middle_name => "e",
68
+ :last_name => "name",
69
+ :email => "example@test.com",
70
+ :phone => "0000000000",
71
+ :billing_address1 => "1600 Pennsylvania Ave NW",
72
+ :billing_city => "Washington",
73
+ :billing_state => 8,
74
+ :billing_postal_code => 20500,
75
+ :billing_country_code => "USA",
76
+ :shipping_same_as_billing => 1
77
+ }
78
+ customer_account = {
79
+ :account_number => "4111111111111111",
80
+ :c_c_expiry => "2014/12",
81
+ :c_c_type => CreditCardIssuer::VISA,
82
+ }
83
+ amount = 100
84
+
85
+ response = PS::Customer.create_and_make_cc_payment(
86
+ customer,
87
+ customer_account,
88
+ amount
89
+ )
90
+
91
+ response.map(&:class) #=> [ PS::Customer, PS::CreditCardAccount, PS::Payment ]
92
+
93
+
94
+ ## Contributing
95
+
96
+ 1. Fork it
97
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
98
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
99
+ 4. Push to the branch (`git push origin my-new-feature`)
100
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/ps.rb ADDED
@@ -0,0 +1,32 @@
1
+ # gems
2
+ require 'httparty'
3
+ require 'json'
4
+
5
+ # core classes
6
+ require 'ps/exceptions'
7
+ require 'ps/api'
8
+ require 'ps/base'
9
+
10
+ # ps objects
11
+ require 'ps/object'
12
+ require 'ps/objects/customer'
13
+ require 'ps/objects/customer_account'
14
+ require 'ps/objects/credit_card_account'
15
+ require 'ps/objects/ach_account'
16
+ require 'ps/objects/payment'
17
+ require 'ps/objects/recurring_payment'
18
+ require 'ps/objects/user'
19
+
20
+
21
+ require 'ps/response'
22
+ # enumerables
23
+ require 'ps/enumerations'
24
+
25
+ # util
26
+ require 'ps/util.rb'
27
+ require 'ps/util/string'
28
+ require 'ps/util/hash'
29
+ require 'ps/util/state'
30
+
31
+ module PS
32
+ end
data/lib/ps/api.rb ADDED
@@ -0,0 +1,80 @@
1
+ module PS
2
+ module Api
3
+ ##
4
+ # Establishes the connection to the appropriate paysimple v3 api.
5
+ # Defaults to JSON
6
+ def connect(format)
7
+ format ||= "JSON"
8
+ begin
9
+ require "ps/api/#{format.downcase}"
10
+ rescue LoadError
11
+ raise ConnectionError, "#{format} is not a supported format"
12
+ end
13
+ $api = PS::Api.const_get(format.downcase.capitalize).new
14
+ end
15
+
16
+ def validate_and_assign(params)
17
+ required_attributes().each do |key|
18
+ if params.key?(key) then
19
+ $api.instance_variable_set("@#{key.to_s}", params[key])
20
+ else
21
+ raise ArgumentError, "Missing required attribute: #{key}"
22
+ end
23
+ end
24
+ end
25
+
26
+ def required_attributes
27
+ $api.instance_variables.map do |instance_variable|
28
+ instance_variable.to_s[1..-1].to_sym
29
+ end
30
+ end
31
+
32
+ def connection_hash
33
+ {
34
+ :apikey => $api.apikey,
35
+ :userkey => $api.userkey,
36
+ :company_name => $api.company_name,
37
+ :host => host()
38
+ }
39
+ end
40
+
41
+ def request(method, params={}, &block)
42
+ response = Response.new($api.request(method, camel_case_request(params)))
43
+ block[response] if block
44
+ end
45
+
46
+ def env
47
+ $api.env
48
+ end
49
+
50
+ #TODO: make constants
51
+ def host
52
+ if env() == "development" then
53
+ "https://sandbox-api.paysimple.com/3.00/paysimpleapi"
54
+ elsif env() == "production" then
55
+ "https://api.paysimple.com/3.00/paysimpleapi"
56
+ end
57
+ end
58
+
59
+ #Paysimple expects the attribute names to be in CamelCase, but that isn't
60
+ #very ruby-esque; so, in an effort to be more ruby-esque, we define and
61
+ #work with the attributes in snake case. The method bellow converts from
62
+ #the ruby-esque snake case to PS' CamelCase.
63
+ def camel_case_request(params)
64
+ params.each { |key, value| value.camel_case_keys if value.class == Hash }
65
+ end
66
+
67
+ ##
68
+ # returns the conditions for detecting if something is a date format for
69
+ # the current format class
70
+ def date?(object)
71
+ $api.date?(object)
72
+ end
73
+
74
+ ##
75
+ # How to parse dates for thi current format
76
+ def parse_date(date)
77
+ $api.parse_date(date)
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,78 @@
1
+ # encoding: utf-8
2
+ module PS
3
+ module Api
4
+ class Json
5
+ include HTTParty
6
+ attr_reader :apikey, :userkey, :company_name, :env
7
+
8
+ def initialize
9
+ @apikey = nil
10
+ @userkey = nil
11
+ @company_name = nil
12
+ @env = "production"
13
+ end
14
+
15
+ def request(method, params={})
16
+ post_response = self.class.post(request_url(method), options_hash(params)).parsed_response
17
+ post_response['d'] || post_response
18
+ end
19
+
20
+ def date?(object)
21
+ object.instance_of?(String) && object.include?("Date")
22
+ end
23
+
24
+ #do some conversion for the ASP.net json dates
25
+ def parse_date(str)
26
+ Time.at(str[/([0-9]+)-([0-9]+)/,1].to_i/1000)
27
+ end
28
+
29
+ private
30
+ #TODO refactor
31
+ def format_request_dates(request)
32
+ request.each do |request_key,request_value|
33
+ if request_value.instance_of? Hash then
34
+ request_value.each do |obj_key, obj_value|
35
+ if obj_value.instance_of? Time then
36
+ request[request_key][obj_key] = format_date(obj_value)
37
+ end
38
+ end
39
+ elsif request_value.instance_of? Time then
40
+ request[request_key] = format_date(request_value)
41
+ end
42
+ end
43
+ end
44
+
45
+ #format http://msdn.microsoft.com/en-us/library/bb299886.aspx#intro_to_json_sidebarb
46
+ def format_date(date)
47
+ "/Date(#{(date.to_i*1000)}-0600)/"
48
+ end
49
+
50
+ def options_hash(post_data)
51
+ post_data[:apikey] = @apikey
52
+ post_data[:userkey] = @userkey
53
+ post_data = format_request_dates(post_data).to_json
54
+ {
55
+ :body => post_data,
56
+ :headers => header(post_data.to_json.length.to_s)
57
+ }
58
+ end
59
+
60
+ def request_url(method)
61
+ "#{Base.host()}/#{name()}/#{method}"
62
+ end
63
+
64
+ def name
65
+ self.class.name.split('::').last.downcase
66
+ end
67
+
68
+ def header(content_length)
69
+ {
70
+ 'Content-Type' => "application/json;charset=utf-8",
71
+ 'Accept' => "application/json",
72
+ 'User-Agent' => @company_name,
73
+ 'Content-Length' => content_length
74
+ }
75
+ end
76
+ end
77
+ end
78
+ end
data/lib/ps/base.rb ADDED
@@ -0,0 +1,22 @@
1
+ module PS
2
+ class Base
3
+ #hmm...
4
+ extend Api
5
+ include Api
6
+
7
+ ## params.keys -> [
8
+ # :apikey,
9
+ # :userkey,
10
+ # :company_name
11
+ ##]
12
+ def self.establish_connection(params={})
13
+ connect(params.delete(:format))
14
+ params[:env] ||= "development"
15
+ validate_and_assign(params)
16
+ end
17
+
18
+ def self.current_connection
19
+ puts connection_hash().inspect
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,191 @@
1
+ class CreditCardIssuer
2
+ VISA = 1
3
+ MASTER = 2
4
+ AMEX = 3
5
+ DISCOVER = 4
6
+ end
7
+
8
+ class BillingFrequencyType
9
+ DAILY = 1
10
+ WEEKLY = 2
11
+ BI_WEEKLY = 3
12
+ FIRST_OF_MONTH = 4
13
+ SPECIFIC_DAY_OF_MONTH = 5
14
+ LAST_OF_MONTH = 6
15
+ QUARTERLY = 7
16
+ SEMI_ANNUALLY = 8
17
+ ANNUALLY = 9
18
+ end
19
+
20
+ class RecurringScheduleType
21
+ PAYMENT_PLAN = 1
22
+ BILL_PAYMENT = 2
23
+ end
24
+
25
+ class ErrorCodeType
26
+ NONE = 0
27
+ end
28
+
29
+ class PaymentType
30
+ CC = 1
31
+ ACH = 2
32
+ end
33
+
34
+ class CreditCardPaymentType
35
+ MOTO = 11
36
+ SWIPE = 10
37
+ end
38
+
39
+ class AchPaymentType
40
+ ARC = 3
41
+ WEB = 4
42
+ TEL = 5
43
+ PPD = 6
44
+ CCD = 7
45
+ BOC = 8
46
+ RCK = 9
47
+ ACHD = 16
48
+ end
49
+
50
+ class Sort
51
+ ASC = 0
52
+ DESC = 1
53
+ end
54
+
55
+ class PaymentStatus
56
+ ALL = -1
57
+ PENDING = 0
58
+ POSTED = 1
59
+ SETTLED = 2
60
+ FAILED = 3
61
+ RESUBMITTED = 4
62
+ VOIDED = 5
63
+ REVERSED = 6
64
+ SAVED = 7
65
+ SCHEDULED = 8
66
+ REVERSEPOSTED = 9
67
+ CHARGEBACK = 10
68
+ CLOSECHARGEBACK = 11
69
+ AUTHORIZED = 12
70
+ RETURNED = 13
71
+ REVERSECHARGEBACK = 14
72
+ REVERSENSF = 15
73
+ REVERSERETURN = 16
74
+ REFUNDSETTLED = 17
75
+ end
76
+
77
+ module PS
78
+ class InvoiceScheduleStatus
79
+ EXPIRED = 1
80
+ SUSPENDED = 2
81
+ DRAFT = 3
82
+ ACTIVE = 4
83
+ end
84
+
85
+ class InvoiceStatus
86
+ PAID = 0
87
+ UNPAID = 1
88
+ DRAFT = 2
89
+ CANCELLED = 3
90
+ OVERDUE = 4
91
+ PAIDPARTIALLY = 5
92
+ end
93
+
94
+ class ScheduleStatus
95
+ DISABLE = 0
96
+ ACTIVE = 1
97
+ PAUSE_UNTIL = 2
98
+ EXPIRED = 3
99
+ SUSPENDED = 4
100
+ end
101
+
102
+ class PaymentDateRangeType
103
+ PAYMENT_DATE = 0
104
+ SETTLED_DATE = 1
105
+ end
106
+
107
+ class Country
108
+ MERICA = 1 # :D
109
+ AMERICA = 1
110
+ CANADA = 3
111
+ end
112
+
113
+ class States
114
+ AP = 74
115
+ WI = 49
116
+ GA = 11
117
+ MD = 21
118
+ ME = 22
119
+ GU = 68
120
+ CA = 5
121
+ VT = 47
122
+ AR = 3
123
+ ID = 14
124
+ VA = 46
125
+ AS = 71
126
+ CT = 7
127
+ FL = 10
128
+ OH = 36
129
+ AA = 76
130
+ FM = 75
131
+ MH = 70
132
+ KY = 18
133
+ PR = 52
134
+ SK = 64
135
+ MI = 23
136
+ PA = 39
137
+ AB = 53
138
+ OK = 37
139
+ NS = 59
140
+ LA = 19
141
+ NB = 56
142
+ NT = 58
143
+ AE = 72
144
+ NC = 28
145
+ NU = 60
146
+ TX = 44
147
+ PW = 73
148
+ PE = 62
149
+ ON = 61
150
+ NV = 34
151
+ ND = 29
152
+ VI = 69
153
+ YT = 65
154
+ AZ = 4
155
+ NE = 30
156
+ WA = 48
157
+ IL = 15
158
+ MN = 24
159
+ UT = 45
160
+ MO = 25
161
+ RI = 40
162
+ NY = 35
163
+ MP = 67
164
+ DC = 8
165
+ WV = 50
166
+ IN = 16
167
+ NH = 31
168
+ AK = 1
169
+ OR = 38
170
+ OTHER = 66
171
+ NJ = 32
172
+ SC = 41
173
+ BC = 54
174
+ AL = 2
175
+ MA = 20
176
+ QC = 63
177
+ MS = 26
178
+ SD = 42
179
+ TN = 43
180
+ DE = 9
181
+ MB = 55
182
+ KS = 17
183
+ NL = 57
184
+ MT = 27
185
+ WY = 51
186
+ CO = 6
187
+ NM = 33
188
+ IA = 13
189
+ HI = 12
190
+ end
191
+ end