simplepay-rails4 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/CHANGELOG.md +34 -0
  4. data/Gemfile +8 -0
  5. data/Gemfile.lock +104 -0
  6. data/MIT-LICENSE +22 -0
  7. data/README.md +106 -0
  8. data/Rakefile +13 -0
  9. data/app/helpers/rails_helper.rb +38 -0
  10. data/lib/simplepay/authentication.rb +41 -0
  11. data/lib/simplepay/constants.rb +64 -0
  12. data/lib/simplepay/errors.rb +16 -0
  13. data/lib/simplepay/helpers/form_helper.rb +31 -0
  14. data/lib/simplepay/rails.rb +2 -0
  15. data/lib/simplepay/service.rb +133 -0
  16. data/lib/simplepay/services/donation.rb +90 -0
  17. data/lib/simplepay/services/marketplace.rb +88 -0
  18. data/lib/simplepay/services/marketplace_policy.rb +53 -0
  19. data/lib/simplepay/services/standard.rb +58 -0
  20. data/lib/simplepay/services/subscription.rb +95 -0
  21. data/lib/simplepay/support/amount.rb +55 -0
  22. data/lib/simplepay/support/billing_frequency.rb +14 -0
  23. data/lib/simplepay/support/boolean.rb +28 -0
  24. data/lib/simplepay/support/currency.rb +21 -0
  25. data/lib/simplepay/support/epoch.rb +39 -0
  26. data/lib/simplepay/support/field.rb +147 -0
  27. data/lib/simplepay/support/interval.rb +145 -0
  28. data/lib/simplepay/support/simple_amount.rb +37 -0
  29. data/lib/simplepay/support/subscription_period.rb +25 -0
  30. data/lib/simplepay/support.rb +16 -0
  31. data/lib/simplepay/validator.rb +59 -0
  32. data/lib/simplepay/version.rb +3 -0
  33. data/lib/simplepay.rb +29 -0
  34. data/simplepay.gemspec +27 -0
  35. data/test/simplepay/services/test_donation.rb +81 -0
  36. data/test/simplepay/services/test_marketplace.rb +81 -0
  37. data/test/simplepay/services/test_marketplace_policy.rb +48 -0
  38. data/test/simplepay/services/test_standard.rb +71 -0
  39. data/test/simplepay/services/test_subscription.rb +105 -0
  40. data/test/simplepay/support/test_amount.rb +46 -0
  41. data/test/simplepay/support/test_billing_frequency.rb +43 -0
  42. data/test/simplepay/support/test_boolean.rb +17 -0
  43. data/test/simplepay/support/test_epoch.rb +34 -0
  44. data/test/simplepay/support/test_field.rb +99 -0
  45. data/test/simplepay/support/test_interval.rb +92 -0
  46. data/test/simplepay/support/test_simple_amount.rb +28 -0
  47. data/test/simplepay/support/test_subscription_period.rb +49 -0
  48. data/test/simplepay/test_authentication.rb +25 -0
  49. data/test/simplepay/test_service.rb +118 -0
  50. data/test/simplepay/test_validator.rb +32 -0
  51. data/test/test_helper.rb +75 -0
  52. data/test/test_simplepay.rb +11 -0
  53. metadata +145 -0
@@ -0,0 +1,90 @@
1
+ module Simplepay
2
+ module Services
3
+
4
+ ##
5
+ # The Amazon Simple Pay Donation service is used to facilitate the collection
6
+ # of donations to your organization, or another's organization via the
7
+ # marketplace functionality. When used through the marketplace functionality,
8
+ # you may charge a commission fee for brokering the exchange.
9
+ #
10
+ # Note that donation recipients must accept your marketplace fee policy before
11
+ # the payment buttons for their donations may be generated. This can be
12
+ # accomplished using the +MarketplacePolicy+ service with the form helper
13
+ # (see examples).
14
+ #
15
+ # === Simple Pay Donation Fields
16
+ #
17
+ # ==== Required Fields
18
+ #
19
+ # The following attributes are required when generating a Simple Pay Donation
20
+ # form (in addition to those listed in +SimplePay::Service+):
21
+ #
22
+ # amount:: The dollar value you'd like to collect
23
+ #
24
+ # ==== Optional Fields
25
+ #
26
+ # immediate_return:: Immediately returns the customer to your +return_url+ directly after payment.
27
+ # ipn_url:: Fully-qualified URL to which Amazon will POST instant payment notifications.
28
+ # process_immediately:: Instructs Amazon to immediately process the payment.
29
+ # reference_id:: A custom string your can set to identify this transaction, it will be returned with the IPNs and other returned data.
30
+ # return_url:: Fully-qualified URL for where to send your customer following payment.
31
+ #
32
+ # ===== Marketplace Fields
33
+ #
34
+ # recipient_email:: The email address of the donation recipient (important and must be correct)
35
+ # fixed_marketplace_fee:: The fixed marketplace fee to add to each transaction
36
+ # variable_marketplace_fee:: The variable marketplace fee to add to each transaction
37
+ #
38
+ # === Example
39
+ #
40
+ # (in your view, sellers need to accept the marketplace fee policy using the form helper)
41
+ #
42
+ # <%= simplepay_form_for(:marketplace_policy, {
43
+ # :max_fixed_fee => 10.00,
44
+ # :max_variable_fee => 5,
45
+ # :return_url => 'http://yourservice.com'
46
+ # }) %>
47
+ #
48
+ # (in your view, payment form generated for end users using the form helper)
49
+ #
50
+ # <%= simplepay_form_for(:donation, {
51
+ # :amount => 34.95,
52
+ # :description => "Mutual profit!",
53
+ # :recipient_email => 'seller@gmail.com',
54
+ # :fixed_marketplace_fee => 10.00,
55
+ # :variable_marketplace_fee => 5
56
+ # }) %>
57
+ #
58
+ class Donation < Service
59
+
60
+ required_field :access_key
61
+ required_field :signature
62
+ required_field :signature_method, :value => 'HmacSHA256'
63
+ required_field :signature_version, :value => '2'
64
+
65
+
66
+ required_field :description
67
+ required_field :amount, :class => Support::Amount
68
+ required_field :cobranding_style, :value => 'logo'
69
+ required_field :donation_widget, :as => :is_donation_widget,
70
+ :value => '1'
71
+
72
+ field :reference_id
73
+ field :immediate_return, :class => Support::Boolean
74
+ field :collect_shipping_address, :class => Support::Boolean
75
+ field :process_immediately, :class => Support::Boolean,
76
+ :as => :process_immediate
77
+
78
+ field :return_url
79
+ field :ipn_url
80
+ field :abandon_url
81
+
82
+ # Marketplace inputs (recipient email required when using)
83
+ field :fixed_marketplace_fee, :class => Support::Amount
84
+ field :variable_marketplace_fee
85
+ field :recipient_email
86
+
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,88 @@
1
+ module Simplepay
2
+ module Services
3
+
4
+ ##
5
+ # The Amazon Simple Pay Marketplace service is used to facilitate payments for others.
6
+ # Use it to charge a commission fee for brokering the exchange between buyers and sellers.
7
+ #
8
+ # Note that sellers must accept your marketplace fee policy before the payment buttons
9
+ # for their products can be generated. This can be accomplished using the +MarketplacePolicy+
10
+ # service with the form helper (see examples).
11
+ #
12
+ # === Simple Pay Marketplace Fields
13
+ #
14
+ # ==== Required Fields
15
+ #
16
+ # The following attributes are required when creating a Simple Pay
17
+ # Marketplace form (in addition to those listed in +Simplepay::Service+):
18
+ #
19
+ # amount:: The dollar value you'd like to collect.
20
+ # description:: A summary of the reason for the payment, this is displayed to your customer during checkout.
21
+ # recipient_email:: The e-mail address of the seller (important and must be correct).
22
+ # fixed_marketplace_fee:: The fixed marketplace fee to add to each transaction.
23
+ # variable_marketplace_fee:: The variable percentage fee to add to each transaction.
24
+ #
25
+ # ==== Optional Fields
26
+ #
27
+ # abandon_url:: The fully-qualified URL to send your custom if they cancel during payment.
28
+ # cobranding_style:: Defines the type of cobranding to use during the checkout process.
29
+ # collect_shipping_address:: Tells Amazon whether or not to ask for shipping address and contact information.
30
+ # immediate_return:: Immediately returns the customer to your +return_url+ directly after payment.
31
+ # ipn_url:: Fully-qualified URL to which Amazon will POST instant payment notifications.
32
+ # process_immediately:: Instructs Amazon to immediately process the payment.
33
+ # reference_id:: A custom string your can set to identify this transaction, it will be returned with the IPNs and other returned data.
34
+ # return_url:: Fully-qualified URL for where to send your customer following payment.
35
+ #
36
+ # === Example
37
+ #
38
+ # (in your view, sellers need to accept the marketplace fee policy using the form helper)
39
+ #
40
+ # <%= simplepay_form_for(:marketplace_policy, {
41
+ # :max_fixed_fee => 10.00,
42
+ # :max_variable_fee => 5,
43
+ # :return_url => 'http://yourservice.com'
44
+ # }) %>
45
+ #
46
+ # (in your view, payment form generated for end users using the form helper)
47
+ #
48
+ # <%= simplepay_form_for(:standard, {
49
+ # :amount => 34.95,
50
+ # :description => "Mutual profit!",
51
+ # :recipient_email => 'seller@gmail.com',
52
+ # :fixed_marketplace_fee => 10.00,
53
+ # :variable_marketplace_fee => 5
54
+ # }) %>
55
+ #
56
+ class Marketplace < Service
57
+
58
+ required_field :access_key
59
+ required_field :signature
60
+ required_field :signature_method, :value => 'HmacSHA256'
61
+ required_field :signature_version, :value => '2'
62
+
63
+
64
+ required_field :description
65
+ required_field :amount, :class => Support::Amount
66
+ required_field :fixed_marketplace_fee, :class => Support::Amount
67
+ required_field :variable_marketplace_fee
68
+ required_field :cobranding_style, :value => 'logo'
69
+ required_field :recipient_email
70
+
71
+ field :reference_id
72
+ field :immediate_return, :class => Support::Boolean
73
+ field :collect_shipping_address, :class => Support::Boolean
74
+ field :process_immediately, :class => Support::Boolean,
75
+ :as => :process_immediate
76
+
77
+ field :return_url
78
+ field :ipn_url
79
+ field :abandon_url
80
+
81
+ # These fields are not currently utilized by the service
82
+ field :donation_widget, :as => :is_donation_widget,
83
+ :value => '0'
84
+
85
+ end
86
+
87
+ end
88
+ end
@@ -0,0 +1,53 @@
1
+ module Simplepay
2
+ module Services
3
+
4
+ ##
5
+ # The Amazon Simple Pay Marketplace Policy service is used to allow sellers to acknowledge marketplace policy fees.
6
+ # Only once a set policy has been agreed to will marketplace transactions be able to proceed.
7
+ #
8
+ # === Simple Pay Marketplace Policy Fields
9
+ #
10
+ # ==== Required Fields
11
+ #
12
+ # The following attributes are required when creating a Simple Pay Marketplace policy fee acceptance form
13
+ # (in addition to those listed in +Simplepay::Service+):
14
+ #
15
+ # max_fixed_fee:: The maximum fixed fee that will be appended to transactions.
16
+ # max_variable_fee:: The maximum variable fee (%) that will be calculated and added to transactions.
17
+ # return_url:: Fully-qualified URL for where to send they buyer following payment.
18
+ # reference_id:: A custom string used to identify this transaction, it will be returned with return data.
19
+ #
20
+ # === Example
21
+ #
22
+ # (in your view, using the form helper)
23
+ #
24
+ # <%= simplepay_form_for(:marketplace_policy, {
25
+ # :max_fixed_fee => 10.00,
26
+ # :max_variable_fee => 5,
27
+ # :return_url => 'http://yourservice.com',
28
+ # :reference_id => '123456789'
29
+ # }) %>
30
+ #
31
+ class MarketplacePolicy < Service
32
+
33
+ required_field :access_key, :as => :caller_key
34
+ required_field :signature, :as => :signature
35
+ required_field :signature_method, :value => 'HmacSHA256'
36
+ required_field :signature_version, :value => '2'
37
+
38
+
39
+ required_field :max_fixed_fee
40
+ required_field :max_variable_fee
41
+ required_field :return_url
42
+
43
+ required_field :reference_id, :as => :caller_reference
44
+ required_field :collect_email_address, :value => 'True'
45
+ required_field :pipeline_name, :value => 'Recipient'
46
+
47
+ # These fields are not currently utilized by the service
48
+ required_field :recipient_pays_fee, :value => 'True'
49
+
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,58 @@
1
+ module Simplepay
2
+ module Services
3
+
4
+ ##
5
+ # The Amazon Simple Pay Standard service is used for one-time payments.
6
+ #
7
+ # === Simple Pay Standard Fields
8
+ #
9
+ # ==== Required Fields
10
+ #
11
+ # The following attributes are required when creating a Simple Pay
12
+ # Standard form (in addition to those listed in +Simplepay::Service+):
13
+ #
14
+ # amount:: The dollar value you'd like to collect.
15
+ # description:: A summary of the reason for the payment, this is displayed to your customer during checkout.
16
+ #
17
+ # ==== Optional Fields
18
+ #
19
+ # abandon_url:: The fully-qualified URL to send your custom if they cancel during payment.
20
+ # cobranding_style:: Defines the type of cobranding to use during the checkout process.
21
+ # collect_shipping_address:: Tells Amazon whether or not to ask for shipping address and contact information.
22
+ # immediate_return:: Immediately returns the customer to your +return_url+ directly after payment.
23
+ # ipn_url:: Fully-qualified URL to which Amazon will POST instant payment notifications.
24
+ # process_immediately:: Instructs Amazon to immediately process the payment.
25
+ # reference_id:: A custom string your can set to identify this transaction, it will be returned with the IPNs and other returned data.
26
+ # return_url:: Fully-qualified URL for where to send your customer following payment.
27
+ #
28
+ # === Example
29
+ #
30
+ # (in your view, using the form helper)
31
+ #
32
+ # <%= simplepay_form_for(:standard, {
33
+ # :amount => 34.95,
34
+ # :description => "I want my money NOW!"
35
+ # }) %>
36
+ #
37
+ class Standard < Service
38
+
39
+ required_field :access_key
40
+ required_field :amount
41
+ required_field :description
42
+ required_field :signature
43
+ required_field :signature_method, :value => 'HmacSHA256'
44
+ required_field :signature_version, :value => '2'
45
+
46
+ field :abandon_url
47
+ field :cobranding_style, :value => 'logo'
48
+ field :collect_shipping_address, :class => Support::Boolean
49
+ field :immediate_return, :class => Support::Boolean
50
+ field :ipn_url
51
+ field :process_immediate, :class => Support::Boolean
52
+ field :reference_id
53
+ field :return_url
54
+
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,95 @@
1
+ module Simplepay
2
+ module Services
3
+
4
+ ##
5
+ # A Simple Pay Subscription is an automatically recurring payment which is
6
+ # charged every interval (Simplepay::Support::BillingFrequency) until
7
+ # a limiting period (Simplepay::Support::SubscriptionPeriod) is met.
8
+ #
9
+ # With this type of payment, for example, you may charge your customer:
10
+ #
11
+ # $10.00 every 3 days until 9 days.
12
+ # $9.95 every 1 month until forever.
13
+ #
14
+ # === Simple Pay Subscription Fields
15
+ #
16
+ # === Required Fields
17
+ #
18
+ # The following attributes are required when creating a Simple Pay
19
+ # Subscription form (in addition to those listed in +Simplepay::Service+):
20
+ #
21
+ # amount:: The dollar value you'd like to collect.
22
+ # description:: A summary of the reason for the payment, this is displayed to your customer during checkout.
23
+ # recurring_frequency:: Defines how often to charge your customer (ex. "1 month")
24
+ #
25
+ # ==== Optional Fields
26
+ #
27
+ # abandon_url:: The fully-qualified URL to send your custom if they cancel during payment.
28
+ # auto_renew:: Instructs Amazon to automatically renew the subscription after the +subscription_period+ ends.
29
+ # cobranding_style:: Defines the type of cobranding to use during the checkout process.
30
+ # collect_shipping_address:: Tells Amazon whether or not to ask for shipping address and contact information.
31
+ # immediate_return:: Immediately returns the customer to your +return_url+ directly after payment.
32
+ # ipn_url:: Fully-qualified URL to which Amazon will POST instant payment notifications.
33
+ # process_immediately:: Instructs Amazon to immediately process the payment.
34
+ # reference_id:: A custom string your can set to identify this transaction, it will be returned with the IPNs and other returned data.
35
+ # return_url:: Fully-qualified URL for where to send your customer following payment.
36
+ # start_date:: Instructs Amazon with the timestamp to start the recurring subscription charges.
37
+ # subscription_period:: Defines the expiration window of the subscription (i.e. charge +amount+ every +recurring_frequency+ for "36 months")
38
+ #
39
+ # === Example
40
+ #
41
+ # (in your view, using the form helper)
42
+ #
43
+ # <%= simplepay_form_for(:subscription, {
44
+ # :amount => 12.95,
45
+ # :recurring_frequency => "1 year",
46
+ # :description => "My.Url Yearly Dues"
47
+ # }) %>
48
+ #
49
+ class Subscription < Service
50
+
51
+ required_field :access_key
52
+ required_field :signature
53
+ required_field :signature_method, :value => 'HmacSHA256'
54
+ required_field :signature_version, :value => '2'
55
+
56
+
57
+
58
+ required_field :recurring_frequency, :class => Support::BillingFrequency
59
+ required_field :description
60
+ required_field :amount, :class => Support::Amount
61
+ required_field :cobranding_style, :value => 'logo'
62
+
63
+ field :subscription_period, :class => Support::SubscriptionPeriod
64
+ field :reference_id
65
+ field :immediate_return, :class => Support::Boolean
66
+
67
+ field :start_date, :class => Support::Epoch,
68
+ :as => :recurring_start_date
69
+
70
+ field :collect_shipping_address, :class => Support::Boolean
71
+
72
+ field :process_immediately, :class => Support::Boolean,
73
+ :as => :process_immediate
74
+
75
+ field :auto_renew, :class => Support::Boolean,
76
+ :as => :is_auto_renewal
77
+
78
+ field :return_url
79
+ field :ipn_url
80
+ field :abandon_url
81
+
82
+ # Used for trial periods
83
+ field :promotion_amount, :class => Support::Amount
84
+ field :number_of_promotion_transactions, :as => :no_of_promotion_transactions
85
+
86
+ # These fields are not currently utilized by the service
87
+ field :variable_marketplace_fee, :value => ''
88
+ field :donation_widget, :as => :is_donation_widget,
89
+ :value => '0'
90
+ field :fixed_marketplace_fee, :value => ''
91
+
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,55 @@
1
+ require 'bigdecimal'
2
+
3
+ module Simplepay
4
+ module Support
5
+
6
+ ##
7
+ # Amazon often represents dollar values as a combination of a value and a
8
+ # currency. In several types of requests, the combination is required for
9
+ # communication.
10
+ #
11
+ # At the present time, Amazon only uses USD.
12
+ #
13
+ class Amount
14
+
15
+ attr_reader :amount
16
+ attr_reader :currency
17
+
18
+ def initialize(amount, currency = Simplepay::Currency::USD)
19
+ self.amount = amount
20
+ self.currency = currency
21
+ end
22
+
23
+
24
+ ##
25
+ # Sets the amount of the currency value, such as "1" for 1 USD. This
26
+ # amount cannot be negative.
27
+ #
28
+ def amount=(amount)
29
+ raise(ArgumentError, "Amount cannot be nil") unless amount
30
+ raise(ArgumentError, "Amount cannot be negative") if amount < 0
31
+ @amount = BigDecimal.new(amount.to_s)
32
+ end
33
+
34
+ ##
35
+ # Sets the type of currency to use in the transaction. The parameter
36
+ # can either be a known currency code (see Simplepay::Currency) or a
37
+ # custom Simplepay::Currency::Currency instance.
38
+ #
39
+ def currency=(currency)
40
+ raise(ArgumentError, "Invalid currency, expected Simplepay::Support::Currency or currency code string.") unless currency.kind_of?(Simplepay::Support::Currency) || currency.kind_of?(String)
41
+ if currency.kind_of?(String)
42
+ currency = Simplepay::Currencies.detect { |c| c.code == currency } ||
43
+ raise(ArgumentError, "Invalid currency, '#{currency}'. Must be one of: #{Simplepay::Currencies.join(', ')}")
44
+ end
45
+ @currency = currency
46
+ end
47
+
48
+ def to_s
49
+ "#{currency.code} #{currency.format % amount}"
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,14 @@
1
+ require 'simplepay/support/interval'
2
+
3
+ module Simplepay
4
+ module Support
5
+
6
+ class BillingFrequency < Interval
7
+ ALLOWED_INTERVALS = Simplepay::Intervals
8
+
9
+ # Limited to 2 digits.
10
+ ALLOWED_QUANTITY_RANGE = (1...100)
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ module Simplepay
2
+ module Support
3
+
4
+ ##
5
+ # Acts as a delegator for Simplepay::Support::Field <tt>:class</tt>.
6
+ #
7
+ # This class acts as a helper for sending boolean values to Amazon. In
8
+ # their forms, booleans are expected to be either "0" or "1", for false or
9
+ # true, respectively.
10
+ #
11
+ class Boolean
12
+
13
+ def initialize(value)
14
+ @value = value
15
+ end
16
+
17
+ ##
18
+ # Returns "1" if the boolean is true, "0" otherwise.
19
+ #
20
+ def to_s
21
+ return '' if @value.nil?
22
+ @value ? Simplepay::Boolean::True : Simplepay::Boolean::False
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,21 @@
1
+ module Simplepay
2
+ module Support
3
+
4
+ ##
5
+ # Contains a name, recognized code, and basic formatting for a particular
6
+ # international monetary currency.
7
+ #
8
+ class Currency
9
+ attr_reader :name, :code, :format
10
+
11
+ def initialize(name, code, format)
12
+ @name, @code, @format = name, code, format
13
+ end
14
+
15
+ def to_s
16
+ "#{code}"
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,39 @@
1
+ module Simplepay
2
+ module Support
3
+
4
+ ##
5
+ # Acts as a Simplepay::Support::Field <tt>:class</tt> delegator.
6
+ #
7
+ # This class provides a means to have Time values returned as an integer
8
+ # since epoch (January 1, 1970).
9
+ #
10
+ class Epoch
11
+
12
+ def initialize(time)
13
+ @value = time ? parse(time) : Time.now
14
+ end
15
+
16
+ ##
17
+ # Returns a String of Integers, representing seconds since epoch.
18
+ #
19
+ def to_s
20
+ @value.to_i.to_s
21
+ end
22
+
23
+
24
+ private
25
+
26
+
27
+ def parse(time)
28
+ case time
29
+ when Time, Date, DateTime
30
+ time
31
+ else
32
+ Time.parse(time)
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+ end