elucid-adaptive_pay 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. data/.gitignore +1 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README +186 -0
  4. data/Rakefile +39 -0
  5. data/VERSION +1 -0
  6. data/config/adaptive_pay.yml +19 -0
  7. data/elucid-adaptive_pay.gemspec +90 -0
  8. data/init.rb +1 -0
  9. data/install.rb +4 -0
  10. data/lib/adaptive_pay.rb +12 -0
  11. data/lib/adaptive_pay/abstract_payment_request.rb +44 -0
  12. data/lib/adaptive_pay/callback.rb +17 -0
  13. data/lib/adaptive_pay/cancel_preapproval_request.rb +16 -0
  14. data/lib/adaptive_pay/interface.rb +93 -0
  15. data/lib/adaptive_pay/parser.rb +43 -0
  16. data/lib/adaptive_pay/payment_request.rb +48 -0
  17. data/lib/adaptive_pay/preapproval_request.rb +24 -0
  18. data/lib/adaptive_pay/recipient.rb +17 -0
  19. data/lib/adaptive_pay/refund_request.rb +46 -0
  20. data/lib/adaptive_pay/request.rb +124 -0
  21. data/lib/adaptive_pay/response.rb +76 -0
  22. data/lib/adaptive_pay/sender.rb +13 -0
  23. data/spec/adaptive_pay/abstract_payment_request_spec.rb +41 -0
  24. data/spec/adaptive_pay/callback_spec.rb +17 -0
  25. data/spec/adaptive_pay/cancel_preapproval_request_spec.rb +22 -0
  26. data/spec/adaptive_pay/interface_spec.rb +184 -0
  27. data/spec/adaptive_pay/parser_spec.rb +47 -0
  28. data/spec/adaptive_pay/payment_request_spec.rb +53 -0
  29. data/spec/adaptive_pay/preapproval_request_spec.rb +9 -0
  30. data/spec/adaptive_pay/recipient_spec.rb +20 -0
  31. data/spec/adaptive_pay/refund_request_spec.rb +51 -0
  32. data/spec/adaptive_pay/request_spec.rb +178 -0
  33. data/spec/adaptive_pay/response_spec.rb +87 -0
  34. data/spec/adaptive_pay/sender_spec.rb +20 -0
  35. data/spec/fixtures/config/adaptive_pay.yml +16 -0
  36. data/spec/rails_mocks.rb +3 -0
  37. data/spec/spec_helper.rb +13 -0
  38. data/uninstall.rb +1 -0
  39. metadata +106 -0
@@ -0,0 +1,13 @@
1
+ module AdaptivePay
2
+ class Sender
3
+
4
+ attr_accessor :email, :client_ip
5
+
6
+ def initialize(options={})
7
+ options.each do |k, v|
8
+ send "#{k}=", v
9
+ end
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,41 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe AdaptivePay::AbstractPaymentRequest do
4
+
5
+ before :each do
6
+ @payment_request = AdaptivePay::AbstractPaymentRequest.new
7
+ end
8
+
9
+ describe "sender" do
10
+
11
+ it "should save sender object" do
12
+ @payment_request.sender = AdaptivePay::Sender.new :email => "test@paypal.com", :client_ip => "12.23.34.45"
13
+ @payment_request.sender.should be_a(AdaptivePay::Sender)
14
+ @payment_request.sender.email.should == "test@paypal.com"
15
+ @payment_request.sender.client_ip.should == "12.23.34.45"
16
+ end
17
+
18
+ it "should save sender hash" do
19
+ @payment_request.sender = { :email => "test@paypal.com", :client_ip => "12.23.34.45" }
20
+ @payment_request.sender.should be_a(AdaptivePay::Sender)
21
+ @payment_request.sender.email.should == "test@paypal.com"
22
+ @payment_request.sender.client_ip.should == "12.23.34.45"
23
+ end
24
+
25
+ end
26
+
27
+ describe "serialize" do
28
+
29
+ it "should include sender details in serialization" do
30
+ request = AdaptivePay::AbstractPaymentRequest.new
31
+ request.sender = { :client_ip => "11.22.33.44", :email => "sender@paypal.com" }
32
+ decompose(request.serialize).should == {
33
+ "clientDetails.ipAddress" => "11.22.33.44",
34
+ "senderEmail" => "sender@paypal.com",
35
+ "requestEnvelope.errorLanguage" => "en_US"
36
+ }
37
+ end
38
+
39
+ end
40
+
41
+ end
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe AdaptivePay::Callback do
4
+
5
+ it "should parse params" do
6
+ callback = AdaptivePay::Callback.new "txn_id" => "3XC103945N720211C", "invoice" => "923204115", "payment_status" => "Completed"
7
+ callback.txn_id.should == "3XC103945N720211C"
8
+ callback.invoice.should == "923204115"
9
+ callback.payment_status.should == "Completed"
10
+ end
11
+
12
+ it "should identify Completed callback" do
13
+ callback = AdaptivePay::Callback.new "payment_status" => "Completed"
14
+ callback.should be_completed
15
+ end
16
+
17
+ end
@@ -0,0 +1,22 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe AdaptivePay::CancelPreapprovalRequest do
4
+
5
+ before :each do
6
+ @refund_request = AdaptivePay::CancelPreapprovalRequest.new
7
+ end
8
+
9
+ describe "serialize" do
10
+
11
+ it "should include recipient details in serialization" do
12
+ request = AdaptivePay::CancelPreapprovalRequest.new
13
+ request.preapproval_key = "PA-12345"
14
+ decompose(request.serialize).should == {
15
+ "preapprovalKey" => "PA-12345",
16
+ "requestEnvelope.errorLanguage" => "en_US"
17
+ }
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -0,0 +1,184 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe AdaptivePay::Interface do
4
+
5
+ before :each do
6
+ AdaptivePay::Interface.requests.clear
7
+ @interface = AdaptivePay::Interface.new false
8
+ @interface.base_url = "https://paypal.yumshare.com/"
9
+ end
10
+
11
+ describe "initialize" do
12
+
13
+ it "should load configuration from adaptive_pay.yml according to" do
14
+ Rails.stub!(:root).and_return(File.expand_path(File.dirname(__FILE__) + '/../fixtures'))
15
+ Rails.stub!(:env).and_return("development")
16
+ interface = AdaptivePay::Interface.new
17
+ interface.environment.should == :sandbox
18
+ interface.base_url.should == "https://svcs.sandbox.paypal.com/AdaptivePayments/"
19
+ interface.username.should == "my_username"
20
+ interface.password.should == "my_password"
21
+ interface.signature.should == "my_signature"
22
+ interface.application_id.should == "my_application_id"
23
+ interface.should_not be_retain_requests_for_test
24
+ end
25
+
26
+ it "should allow setting environment as a parameter" do
27
+ Rails.stub!(:root).and_return(File.expand_path(File.dirname(__FILE__) + '/../fixtures'))
28
+ Rails.stub!(:env).and_return("development")
29
+ interface = AdaptivePay::Interface.new :production
30
+ interface.environment.should == :production
31
+ interface.base_url.should == "https://svcs.paypal.com/AdaptivePayments/"
32
+ interface.username.should == "my_production_username"
33
+ interface.password.should == "my_production_password"
34
+ interface.signature.should == "my_production_signature"
35
+ interface.should_not be_retain_requests_for_test
36
+ end
37
+
38
+ it "should allow preventing the configuration loading" do
39
+ interface = AdaptivePay::Interface.new false
40
+ interface.environment.should be_blank
41
+ interface.base_url.should be_blank
42
+ interface.username.should be_blank
43
+ interface.password.should be_blank
44
+ interface.signature.should be_blank
45
+ end
46
+
47
+ end
48
+
49
+ describe "set_environment" do
50
+
51
+ it "should set production environment" do
52
+ @interface.set_environment :production
53
+ @interface.environment.should == :production
54
+ @interface.base_url.should == "https://svcs.paypal.com/AdaptivePayments/"
55
+ @interface.base_page_url.should == "https://www.paypal.com"
56
+ end
57
+
58
+ it "should set sandbox environment" do
59
+ @interface.set_environment :sandbox
60
+ @interface.environment.should == :sandbox
61
+ @interface.base_url.should == "https://svcs.sandbox.paypal.com/AdaptivePayments/"
62
+ @interface.base_page_url.should == "https://www.sandbox.paypal.com"
63
+ end
64
+
65
+ it "should set beta_sandbox environment" do
66
+ @interface.set_environment :beta_sandbox
67
+ @interface.environment.should == :beta_sandbox
68
+ @interface.base_url.should == "https://svcs.beta-sandbox.paypal.com/AdaptivePayments/"
69
+ @interface.base_page_url.should == "https://www.beta-sandbox.paypal.com"
70
+ end
71
+
72
+ end
73
+
74
+ describe "request_preapproval" do
75
+
76
+ before :each do
77
+ @interface.retain_requests_for_test = true
78
+ end
79
+
80
+ it "should enqueue request if retain_requests_for_test is set" do
81
+ request = nil
82
+ @interface.request_preapproval do |r|
83
+ request = r
84
+ end
85
+ AdaptivePay::Interface.requests.size.should == 1
86
+ AdaptivePay::Interface.requests.first.should == request
87
+ end
88
+
89
+ it "should allow providing pre canned responses" do
90
+ response = mock(:response)
91
+ AdaptivePay::Interface.test_response = response
92
+ @interface.request_preapproval { |r| }.should == response
93
+ end
94
+
95
+ it "should use passed block to build request" do
96
+ called = false
97
+ @interface.request_preapproval do |p|
98
+ called = true
99
+ p.should be_a(AdaptivePay::PreapprovalRequest)
100
+ end
101
+ called.should be_true
102
+ end
103
+
104
+ end
105
+
106
+ describe "request_payment" do
107
+
108
+ before :each do
109
+ @interface.retain_requests_for_test = true
110
+ end
111
+
112
+ it "should enqueue request if retain_requests_for_test is set" do
113
+ request = nil
114
+ @interface.request_payment do |r|
115
+ request = r
116
+ end
117
+ AdaptivePay::Interface.requests.size.should == 1
118
+ AdaptivePay::Interface.requests.first.should == request
119
+ end
120
+
121
+ it "should allow providing pre canned responses" do
122
+ response = mock(:response)
123
+ AdaptivePay::Interface.test_response = response
124
+ @interface.request_payment { |r| }.should == response
125
+ end
126
+
127
+ it "should use passed block to build request" do
128
+ called = false
129
+ @interface.request_payment do |p|
130
+ called = true
131
+ p.should be_a(AdaptivePay::PaymentRequest)
132
+ end
133
+ called.should be_true
134
+ end
135
+
136
+ end
137
+
138
+ describe "request_refund" do
139
+
140
+ before :each do
141
+ @interface.retain_requests_for_test = true
142
+ end
143
+
144
+ it "should enqueue request if retain_requests_for_test is set" do
145
+ request = nil
146
+ @interface.request_refund do |r|
147
+ request = r
148
+ end
149
+ AdaptivePay::Interface.requests.size.should == 1
150
+ AdaptivePay::Interface.requests.first.should == request
151
+ end
152
+
153
+ it "should allow providing pre canned responses" do
154
+ response = mock(:response)
155
+ AdaptivePay::Interface.test_response = response
156
+ @interface.request_refund { |r| }.should == response
157
+ end
158
+
159
+ it "should use passed block to build request" do
160
+ called = false
161
+ @interface.request_refund do |p|
162
+ called = true
163
+ p.should be_a(AdaptivePay::RefundRequest)
164
+ end
165
+ called.should be_true
166
+ end
167
+
168
+ end
169
+
170
+ describe "perform" do
171
+
172
+ before :each do
173
+ @interface.retain_requests_for_test = true
174
+ end
175
+
176
+ it "should allow providing pre canned responses" do
177
+ response = mock(:response)
178
+ AdaptivePay::Interface.test_response = response
179
+ @interface.perform(AdaptivePay::PreapprovalRequest.new).should == response
180
+ end
181
+
182
+ end
183
+
184
+ end
@@ -0,0 +1,47 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe AdaptivePay::Parser do
4
+ it "should parse simple NVP string" do
5
+ body = "foo=bar&baz=bat"
6
+ AdaptivePay::Parser.parse(body).should == {
7
+ "foo" => "bar",
8
+ "baz" => "bat",
9
+ }
10
+ end
11
+
12
+ it "should unescape URI-encoded values" do
13
+ body = "name=O%27Reilly"
14
+ AdaptivePay::Parser.parse(body).should == {
15
+ "name" => "O'Reilly",
16
+ }
17
+ end
18
+
19
+ it "should parse .-delimited key as namespace-attribute" do
20
+ body = "namespace.attribute=value"
21
+ AdaptivePay::Parser.parse(body).should == {
22
+ "namespace" => {"attribute" => "value"},
23
+ }
24
+ end
25
+
26
+ it "should parse (\d+)-suffixed keys as ordered list items" do
27
+ body = "listitem(0).key=value0&listitem(1).key=value1"
28
+ AdaptivePay::Parser.parse(body).should == {
29
+ "listitem" => [
30
+ {"key" => "value0"},
31
+ {"key" => "value1"},
32
+ ],
33
+ }
34
+ end
35
+
36
+ it "should parse combined .-delimited and (\d+)-suffixed keys correctly" do
37
+ body = "namespace.listitem(0).key=value0&namespace.listitem(1).key=value1"
38
+ AdaptivePay::Parser.parse(body).should == {
39
+ "namespace" => {
40
+ "listitem" => [
41
+ {"key" => "value0"},
42
+ {"key" => "value1"},
43
+ ],
44
+ }
45
+ }
46
+ end
47
+ end
@@ -0,0 +1,53 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe AdaptivePay::PaymentRequest do
4
+
5
+ before :each do
6
+ @payment_request = AdaptivePay::PaymentRequest.new
7
+ end
8
+
9
+ describe "serialize" do
10
+
11
+ it "should include recipient details in serialization" do
12
+ request = AdaptivePay::PaymentRequest.new
13
+ request.add_recipient :email => "acc@paypal.com", :amount => 100
14
+ request.add_recipient :email => "primary@paypal.com", :amount => 10, :primary => true, :invoice_id => 1234
15
+ decompose(request.serialize).should == {
16
+ "receiverList.receiver(0).email" => "acc@paypal.com",
17
+ "receiverList.receiver(0).amount" => "100.00",
18
+ "receiverList.receiver(0).primary" => "false",
19
+ "receiverList.receiver(1).email" => "primary@paypal.com",
20
+ "receiverList.receiver(1).amount" => "10.00",
21
+ "receiverList.receiver(1).primary" => "true",
22
+ "receiverList.receiver(1).invoiceId" => "1234",
23
+ "actionType" => "PAY",
24
+ "requestEnvelope.errorLanguage" => "en_US"
25
+ }
26
+ end
27
+
28
+ end
29
+
30
+ describe "add_recipient" do
31
+
32
+ it "should add a Recipient to recipients from Recipient" do
33
+ recipient = AdaptivePay::Recipient.new :email => "recipient@paypal.com",
34
+ :amount => BigDecimal.new("10.00"),
35
+ :primary => false
36
+ @payment_request.add_recipient recipient
37
+ @payment_request.recipients.size.should == 1
38
+ @payment_request.recipients.first.should == recipient
39
+ end
40
+
41
+ it "should add a Recipient to recipients from hash" do
42
+ @payment_request.add_recipient :email => "recipient@paypal.com",
43
+ :amount => BigDecimal.new("10.00"),
44
+ :primary => false
45
+ @payment_request.recipients.size.should == 1
46
+ @payment_request.recipients.first.email.should == "recipient@paypal.com"
47
+ @payment_request.recipients.first.amount.should == BigDecimal.new("10.00")
48
+ @payment_request.recipients.first.primary.should == false
49
+ end
50
+
51
+ end
52
+
53
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe AdaptivePay::PreapprovalRequest do
4
+
5
+ before :each do
6
+ @request = AdaptivePay::ApprovalRequest.new
7
+ end
8
+
9
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe AdaptivePay::Recipient do
4
+
5
+ before :each do
6
+ @recipient = AdaptivePay::Recipient.new
7
+ end
8
+
9
+ describe "attributes" do
10
+
11
+ %w{email amount primary}.each do |attribute|
12
+ it "should have #{attribute} attribute" do
13
+ @recipient.send("#{attribute}=", "test")
14
+ @recipient.send(attribute).should == "test"
15
+ end
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,51 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe AdaptivePay::RefundRequest do
4
+
5
+ before :each do
6
+ @refund_request = AdaptivePay::RefundRequest.new
7
+ end
8
+
9
+ describe "serialize" do
10
+
11
+ it "should include recipient details in serialization" do
12
+ request = AdaptivePay::RefundRequest.new
13
+ request.add_recipient :email => "acc@paypal.com", :amount => 100
14
+ request.add_recipient :email => "primary@paypal.com", :amount => 10, :primary => true
15
+ decompose(request.serialize).should == {
16
+ "receiverList.receiver(0).email" => "acc@paypal.com",
17
+ "receiverList.receiver(0).amount" => "100.00",
18
+ "receiverList.receiver(0).primary" => "false",
19
+ "receiverList.receiver(1).email" => "primary@paypal.com",
20
+ "receiverList.receiver(1).amount" => "10.00",
21
+ "receiverList.receiver(1).primary" => "true",
22
+ "requestEnvelope.errorLanguage" => "en_US"
23
+ }
24
+ end
25
+
26
+ end
27
+
28
+ describe "add_recipient" do
29
+
30
+ it "should add a Recipient to recipients from Recipient" do
31
+ recipient = AdaptivePay::Recipient.new :email => "recipient@paypal.com",
32
+ :amount => BigDecimal.new("10.00"),
33
+ :primary => false
34
+ @refund_request.add_recipient recipient
35
+ @refund_request.recipients.size.should == 1
36
+ @refund_request.recipients.first.should == recipient
37
+ end
38
+
39
+ it "should add a Recipient to recipients from hash" do
40
+ @refund_request.add_recipient :email => "recipient@paypal.com",
41
+ :amount => BigDecimal.new("10.00"),
42
+ :primary => false
43
+ @refund_request.recipients.size.should == 1
44
+ @refund_request.recipients.first.email.should == "recipient@paypal.com"
45
+ @refund_request.recipients.first.amount.should == BigDecimal.new("10.00")
46
+ @refund_request.recipients.first.primary.should == false
47
+ end
48
+
49
+ end
50
+
51
+ end