pagseguro-oficial 2.5.0 → 2.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (140) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -1
  3. data/CONTRIBUTING.md +39 -0
  4. data/Dockerfile +20 -0
  5. data/README.md +22 -0
  6. data/docker-compose.yml +5 -0
  7. data/examples/checkout/create_payment_request.rb +1 -1
  8. data/examples/direct/create_transaction_using_boleto.rb +1 -1
  9. data/examples/direct/create_transaction_using_credit_card.rb +1 -1
  10. data/examples/direct/create_transaction_using_online_debit.rb +1 -1
  11. data/examples/preapproval/cancel_preapproval.rb +26 -0
  12. data/examples/preapproval/change_preapproval_payment.rb +50 -0
  13. data/examples/preapproval/change_preapproval_status.rb +29 -0
  14. data/examples/preapproval/charge_preapproval.rb +36 -0
  15. data/examples/preapproval/create_preapproval.rb +68 -0
  16. data/examples/preapproval/create_preapproval_auto.rb +53 -0
  17. data/examples/preapproval/create_preapproval_discount.rb +30 -0
  18. data/examples/preapproval/create_preapproval_manual.rb +58 -0
  19. data/examples/preapproval/create_preapproval_transparent.rb +41 -0
  20. data/examples/preapproval/preapproval_retry.rb +29 -0
  21. data/examples/preapproval/search/search_by_date_interval.rb +45 -0
  22. data/examples/preapproval/search/search_by_days_interval.rb +46 -0
  23. data/examples/preapproval/search/search_by_notification_code.rb +50 -0
  24. data/examples/preapproval/search/search_by_subscription_code.rb +50 -0
  25. data/examples/preapproval/search/search_subscription_payment_orders.rb +53 -0
  26. data/examples/split_payment/create_payment_request.rb +1 -1
  27. data/examples/split_payment/create_transaction_using_boleto.rb +118 -0
  28. data/examples/split_payment/create_transaction_using_credit_card.rb +152 -0
  29. data/lib/pagseguro.rb +33 -1
  30. data/lib/pagseguro/credit_card_transaction_request.rb +1 -1
  31. data/lib/pagseguro/document.rb +8 -0
  32. data/lib/pagseguro/holder.rb +8 -0
  33. data/lib/pagseguro/manual_subscription_charger.rb +59 -0
  34. data/lib/pagseguro/manual_subscription_charger/request_serializer.rb +50 -0
  35. data/lib/pagseguro/manual_subscription_charger/response.rb +36 -0
  36. data/lib/pagseguro/manual_subscription_charger/response_serializer.rb +17 -0
  37. data/lib/pagseguro/payment_request/request_serializer.rb +25 -1
  38. data/lib/pagseguro/receiver.rb +3 -0
  39. data/lib/pagseguro/request.rb +32 -2
  40. data/lib/pagseguro/sender.rb +21 -3
  41. data/lib/pagseguro/subscription.rb +132 -0
  42. data/lib/pagseguro/subscription/request_serializer.rb +129 -0
  43. data/lib/pagseguro/subscription/response.rb +49 -0
  44. data/lib/pagseguro/subscription/response_serializer.rb +62 -0
  45. data/lib/pagseguro/subscription/subscription_search.rb +135 -0
  46. data/lib/pagseguro/subscription_canceller.rb +30 -0
  47. data/lib/pagseguro/subscription_canceller/response.rb +34 -0
  48. data/lib/pagseguro/subscription_change_payment.rb +46 -0
  49. data/lib/pagseguro/subscription_change_payment/request_serializer.rb +80 -0
  50. data/lib/pagseguro/subscription_change_payment/response.rb +26 -0
  51. data/lib/pagseguro/subscription_change_status.rb +50 -0
  52. data/lib/pagseguro/subscription_change_status/request_serializer.rb +28 -0
  53. data/lib/pagseguro/subscription_change_status/response.rb +26 -0
  54. data/lib/pagseguro/subscription_discount.rb +41 -0
  55. data/lib/pagseguro/subscription_discount/request_serializer.rb +34 -0
  56. data/lib/pagseguro/subscription_discount/response.rb +34 -0
  57. data/lib/pagseguro/subscription_payment_method.rb +21 -0
  58. data/lib/pagseguro/subscription_payment_order.rb +57 -0
  59. data/lib/pagseguro/subscription_payment_order/response.rb +37 -0
  60. data/lib/pagseguro/subscription_payment_order/response_serializer.rb +51 -0
  61. data/lib/pagseguro/subscription_plan.rb +69 -0
  62. data/lib/pagseguro/subscription_plan/request_serializer.rb +81 -0
  63. data/lib/pagseguro/subscription_plan/response.rb +36 -0
  64. data/lib/pagseguro/subscription_plan/response_serializer.rb +17 -0
  65. data/lib/pagseguro/subscription_retry.rb +35 -0
  66. data/lib/pagseguro/subscription_retry/response.rb +34 -0
  67. data/lib/pagseguro/subscription_search_payment_orders.rb +131 -0
  68. data/lib/pagseguro/subscription_transaction.rb +29 -0
  69. data/lib/pagseguro/subscription_transactions.rb +5 -0
  70. data/lib/pagseguro/transaction.rb +0 -10
  71. data/lib/pagseguro/transaction/serializer.rb +19 -2
  72. data/lib/pagseguro/transaction_request.rb +26 -1
  73. data/lib/pagseguro/transaction_request/request_serializer.rb +164 -1
  74. data/lib/pagseguro/version.rb +1 -1
  75. data/spec/fixtures/manual_subscription_charger/fail.xml +6 -0
  76. data/spec/fixtures/manual_subscription_charger/success.xml +4 -0
  77. data/spec/fixtures/subscription/fail.xml +6 -0
  78. data/spec/fixtures/subscription/find_success.xml +28 -0
  79. data/spec/fixtures/subscription/search_success.xml +18 -0
  80. data/spec/fixtures/subscription/success.xml +4 -0
  81. data/spec/fixtures/subscription_canceller/fail.xml +6 -0
  82. data/spec/fixtures/subscription_change_payment/fail.xml +7 -0
  83. data/spec/fixtures/subscription_change_status/fail.xml +7 -0
  84. data/spec/fixtures/subscription_discount/fail.xml +6 -0
  85. data/spec/fixtures/subscription_payment_order/success.xml +18 -0
  86. data/spec/fixtures/subscription_plan/fail.xml +6 -0
  87. data/spec/fixtures/subscription_plan/success.xml +4 -0
  88. data/spec/fixtures/subscription_retry/fail.xml +7 -0
  89. data/spec/fixtures/subscription_search_payment_orders/success.xml +43 -0
  90. data/spec/fixtures/transactions/without_status_and_type.xml +71 -0
  91. data/spec/pagseguro/credit_card_transaction_request_spec.rb +1 -1
  92. data/spec/pagseguro/document_spec.rb +15 -0
  93. data/spec/pagseguro/holder_spec.rb +1 -0
  94. data/spec/pagseguro/manual_subscription_charger/request_serializer_spec.rb +79 -0
  95. data/spec/pagseguro/manual_subscription_charger/response_serializer_spec.rb +10 -0
  96. data/spec/pagseguro/manual_subscription_charger/response_spec.rb +65 -0
  97. data/spec/pagseguro/manual_subscription_charger_spec.rb +68 -0
  98. data/spec/pagseguro/payment_request/request_serializer_spec.rb +54 -22
  99. data/spec/pagseguro/receiver_spec.rb +1 -0
  100. data/spec/pagseguro/sender_spec.rb +16 -2
  101. data/spec/pagseguro/subscription/request_serializer_spec.rb +233 -0
  102. data/spec/pagseguro/subscription/response_serializer_spec.rb +46 -0
  103. data/spec/pagseguro/subscription/response_spec.rb +79 -0
  104. data/spec/pagseguro/subscription/subscription_search_spec.rb +154 -0
  105. data/spec/pagseguro/subscription_canceller/response_spec.rb +58 -0
  106. data/spec/pagseguro/subscription_canceller_spec.rb +57 -0
  107. data/spec/pagseguro/subscription_change_payment/request_serializer_spec.rb +150 -0
  108. data/spec/pagseguro/subscription_change_payment/response_spec.rb +41 -0
  109. data/spec/pagseguro/subscription_change_payment_spec.rb +60 -0
  110. data/spec/pagseguro/subscription_change_status/request_serializer_spec.rb +19 -0
  111. data/spec/pagseguro/subscription_change_status/response_spec.rb +41 -0
  112. data/spec/pagseguro/subscription_change_status_spec.rb +53 -0
  113. data/spec/pagseguro/subscription_discount/request_serializer_spec.rb +27 -0
  114. data/spec/pagseguro/subscription_discount/response_spec.rb +58 -0
  115. data/spec/pagseguro/subscription_discount_spec.rb +60 -0
  116. data/spec/pagseguro/subscription_payment_method_spec.rb +10 -0
  117. data/spec/pagseguro/subscription_payment_order/response_serializer_spec.rb +24 -0
  118. data/spec/pagseguro/subscription_payment_order/response_spec.rb +55 -0
  119. data/spec/pagseguro/subscription_payment_order_spec.rb +27 -0
  120. data/spec/pagseguro/subscription_plan/request_serializer_spec.rb +253 -0
  121. data/spec/pagseguro/subscription_plan/response_serializer_spec.rb +10 -0
  122. data/spec/pagseguro/subscription_plan/response_spec.rb +65 -0
  123. data/spec/pagseguro/subscription_plan_spec.rb +118 -0
  124. data/spec/pagseguro/subscription_retry/response_spec.rb +58 -0
  125. data/spec/pagseguro/subscription_retry_spec.rb +62 -0
  126. data/spec/pagseguro/subscription_search_payment_order_spec.rb +129 -0
  127. data/spec/pagseguro/subscription_spec.rb +137 -0
  128. data/spec/pagseguro/subscription_transaction_spec.rb +24 -0
  129. data/spec/pagseguro/subscription_transactions_spec.rb +37 -0
  130. data/spec/pagseguro/transaction/response_spec.rb +0 -42
  131. data/spec/pagseguro/transaction/serializer_spec.rb +5 -6
  132. data/spec/pagseguro/transaction_request/request_serializer_spec.rb +623 -159
  133. data/spec/pagseguro/transaction_request_spec.rb +26 -0
  134. data/spec/pagseguro/transaction_spec.rb +0 -48
  135. data/spec/spec_helper.rb +1 -0
  136. metadata +154 -8
  137. data/examples/checkout/search/search_transaction_status_history.rb +0 -22
  138. data/lib/pagseguro/transaction/status_collection.rb +0 -20
  139. data/spec/fixtures/transactions/status_history.xml +0 -12
  140. data/spec/pagseguro/transaction/status_collection_spec.rb +0 -43
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe PagSeguro::SubscriptionPaymentOrder do
4
+ it_assigns_attribute :code
5
+ it_assigns_attribute :status
6
+ it_assigns_attribute :amount
7
+ it_assigns_attribute :gross_amount
8
+ it_assigns_attribute :last_event_date
9
+ it_assigns_attribute :scheduling_date
10
+
11
+ it_ensures_type PagSeguro::SubscriptionDiscount, :discount
12
+ it_ensures_collection_type PagSeguro::SubscriptionTransaction, :transactions, [{code: '1234'}]
13
+
14
+ it '#udpate_attributes' do
15
+ expect{ subject.update_attributes(code: '1234') }.to change{ subject.code }.to '1234'
16
+ end
17
+
18
+ context '#status_code' do
19
+ before do
20
+ subject.status = :scheduled
21
+ end
22
+
23
+ it 'must return the code related' do
24
+ expect(subject.status_code).to eq 1
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,253 @@
1
+ require 'spec_helper'
2
+
3
+ describe PagSeguro::SubscriptionPlan::RequestSerializer do
4
+ context '#to_xml_params' do
5
+ subject { described_class.new(plan) }
6
+
7
+ let(:plan) { PagSeguro::SubscriptionPlan.new }
8
+
9
+ it 'should serializer max users' do
10
+ plan.max_users = 300
11
+
12
+ expect(subject.to_xml_params).to match %r[
13
+ <preApprovalRequest>
14
+ .*<maxUsers>300
15
+ ]xm
16
+ end
17
+
18
+ it 'should serializer name' do
19
+ plan.name = 'Private channel access'
20
+
21
+ expect(subject.to_xml_params).to match %r[
22
+ <preApprovalRequest>
23
+ .*<preApproval>
24
+ .*<name>Private.channel.access
25
+ ]xm
26
+ end
27
+
28
+ it 'should serializer details' do
29
+ plan.details = 'To have access.'
30
+
31
+ expect(subject.to_xml_params).to match %r[
32
+ <preApprovalRequest>
33
+ .*<preApproval>
34
+ .*<details>To.have.access.
35
+ ]xm
36
+ end
37
+
38
+ it 'should serializer charge' do
39
+ plan.charge = 'AUTO'
40
+
41
+ expect(subject.to_xml_params).to match %r[
42
+ <preApprovalRequest>
43
+ .*<preApproval>
44
+ .*<charge>AUTO
45
+ ]xm
46
+ end
47
+
48
+ it 'should serializer period' do
49
+ plan.period = 'MONTHLY'
50
+
51
+ expect(subject.to_xml_params).to match %r[
52
+ <preApprovalRequest>
53
+ .*<preApproval>
54
+ .*<period>MONTHLY
55
+ ]xm
56
+ end
57
+
58
+ it 'should serializer amount' do
59
+ plan.amount = 80
60
+
61
+ expect(subject.to_xml_params).to match %r[
62
+ <preApprovalRequest>
63
+ .*<preApproval>
64
+ .*<amountPerPayment>80.00
65
+ ]xm
66
+ end
67
+
68
+ it 'should serializer max total amount' do
69
+ plan.max_total_amount = 35_000
70
+
71
+ expect(subject.to_xml_params).to match %r[
72
+ <preApprovalRequest>
73
+ .*<preApproval>
74
+ .*<maxTotalAmount>35000.00
75
+ ]xm
76
+ end
77
+
78
+ it 'should serializer max amount per payment' do
79
+ plan.max_amount_per_payment = 321
80
+
81
+ expect(subject.to_xml_params).to match %r[
82
+ <preApprovalRequest>
83
+ .*<preApproval>
84
+ .*<maxAmountPerPayment>321.00
85
+ ]xm
86
+ end
87
+
88
+ it 'should not serializer max payments per period if its value is nil' do
89
+ plan.max_payments_per_period = nil
90
+
91
+ expect(subject.to_xml_params).not_to match %r[.*<maxPaymentsPerPeriod>]xm
92
+ end
93
+
94
+ it 'should serializer max payments per period' do
95
+ plan.max_payments_per_period = 3
96
+
97
+ expect(subject.to_xml_params).to match %r[
98
+ <preApprovalRequest>
99
+ .*<preApproval>
100
+ .*<maxPaymentsPerPeriod>3
101
+ ]xm
102
+ end
103
+
104
+ it 'should not use floating number to serializer max payments per period' do
105
+ plan.max_payments_per_period = 3
106
+
107
+ expect(subject.to_xml_params).not_to match %r[
108
+ <preApprovalRequest>
109
+ .*<preApproval>
110
+ .*<maxPaymentsPerPeriod>3.00
111
+ ]xm
112
+ end
113
+
114
+ it 'should serializer max amount per period' do
115
+ plan.max_amount_per_period = 300
116
+
117
+ expect(subject.to_xml_params).to match %r[
118
+ <preApprovalRequest>
119
+ .*<preApproval>
120
+ .*<maxAmountPerPeriod>300.00
121
+ ]xm
122
+ end
123
+
124
+ it 'should serializer initial date' do
125
+ plan.initial_date = Time.new(2016, 1, 31, 18, 59)
126
+
127
+ expect(subject.to_xml_params).to match %r[
128
+ <preApprovalRequest>
129
+ .*<preApproval>
130
+ .*<initialDate>2016-01-31T.*
131
+ ]xm
132
+ end
133
+
134
+ it 'should serializer final date' do
135
+ plan.final_date = Time.new(2020, 12, 31, 18, 59)
136
+
137
+ expect(subject.to_xml_params).to match %r[
138
+ <preApprovalRequest>
139
+ .*<preApproval>
140
+ .*<finalDate>2020-12-31T.*
141
+ ]xm
142
+ end
143
+
144
+ it 'should serializer membership fee' do
145
+ plan.membership_fee = 150
146
+
147
+ expect(subject.to_xml_params).to match %r[
148
+ <preApprovalRequest>
149
+ .*<preApproval>
150
+ .*<membershipFee>150.00
151
+ ]xm
152
+ end
153
+
154
+ it 'should serializer trial duration' do
155
+ plan.trial_duration = 30.0
156
+
157
+ expect(subject.to_xml_params).to match %r[
158
+ <preApprovalRequest>
159
+ .*<preApproval>
160
+ .*<trialPeriodDuration>30
161
+ ]xm
162
+ end
163
+
164
+ it 'should serializer redirect url' do
165
+ plan.redirect_url = 'http://example.com/redirect'
166
+
167
+ expect(subject.to_xml_params).to match %r[
168
+ <preApprovalRequest>
169
+ .*<redirectURL>http://example.com/redirect
170
+ ]xm
171
+ end
172
+
173
+ it 'should serializer review url' do
174
+ plan.review_url = 'http://example.com/review'
175
+
176
+ expect(subject.to_xml_params).to match %r[
177
+ <preApprovalRequest>
178
+ .*<reviewURL>http://example.com/review
179
+ ]xm
180
+ end
181
+
182
+ it 'should serializer reference' do
183
+ plan.reference = 'ref1234'
184
+
185
+ expect(subject.to_xml_params).to match %r[
186
+ <preApprovalRequest>
187
+ .*<reference>ref1234
188
+ ]xm
189
+ end
190
+
191
+ context "should serializer sender's" do
192
+ before do
193
+ plan.sender = {
194
+ name: 'John',
195
+ email: 'john@example.com',
196
+ phone: { area_code: 11, number: 123 },
197
+ address: {
198
+ street: 'Park Street',
199
+ number: 12,
200
+ complement: 'Complement here',
201
+ district: 'Neighborhood',
202
+ postal_code: '123456',
203
+ city: 'Sao Paulo',
204
+ state: 'SP',
205
+ country: 'BRA'
206
+ }
207
+ }
208
+ end
209
+
210
+ it 'name' do
211
+ expect(subject.to_xml_params).to match %r[
212
+ <preApprovalRequest>
213
+ .*<sender>
214
+ .*<name>John
215
+ ]xm
216
+ end
217
+
218
+ it 'email' do
219
+ expect(subject.to_xml_params).to match %r[
220
+ <preApprovalRequest>
221
+ .*<sender>
222
+ .*<email>john@example.com
223
+ ]xm
224
+ end
225
+
226
+ it 'phone' do
227
+ expect(subject.to_xml_params).to match %r[
228
+ <preApprovalRequest>
229
+ .*<sender>
230
+ .*<phone>
231
+ .*<areaCode>11</areaCode>
232
+ .*<number>123
233
+ ]xm
234
+ end
235
+
236
+ it 'address' do
237
+ expect(subject.to_xml_params).to match %r[
238
+ <preApprovalRequest>
239
+ .*<sender>
240
+ .*<address>
241
+ .*<street>Park.Street</street>
242
+ .*<number>12</number>
243
+ .*<complement>Complement.here</complement>
244
+ .*<district>Neighborhood</district>
245
+ .*<postalCode>123456</postalCode>
246
+ .*<city>Sao.Paulo</city>
247
+ .*<state>SP</state>
248
+ .*<country>BRA</country>
249
+ ]xm
250
+ end
251
+ end
252
+ end
253
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe PagSeguro::SubscriptionPlan::ResponseSerializer do
4
+ let(:source) { File.read('./spec/fixtures/subscription_plan/success.xml') }
5
+ let(:xml) { Nokogiri::XML(source) }
6
+
7
+ subject { described_class.new(xml) }
8
+
9
+ it { expect(subject.serialize[:code]).to eql('12345') }
10
+ end
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe PagSeguro::SubscriptionPlan::Response do
4
+ subject { PagSeguro::SubscriptionPlan::Response.new(http_response, plan) }
5
+ let(:plan) { PagSeguro::SubscriptionPlan.new }
6
+
7
+ context '#success?' do
8
+ let(:http_response) do
9
+ double(:HttpResponse, xml?: true)
10
+ end
11
+
12
+ it 'delegate to response' do
13
+ allow(http_response).to receive(:success?).and_return(true)
14
+ expect(subject).to be_success
15
+
16
+ allow(http_response).to receive(:success?).and_return(false)
17
+ expect(subject).not_to be_success
18
+ end
19
+ end
20
+
21
+ describe '#serialize' do
22
+ let(:http_response) do
23
+ double(
24
+ :Request,
25
+ success?: true,
26
+ xml?: true,
27
+ data: xml_parsed,
28
+ body: raw_xml,
29
+ unauthorized?: false,
30
+ bad_request?: false,
31
+ not_found?: false
32
+ )
33
+ end
34
+
35
+ let(:xml_parsed) { Nokogiri::XML(raw_xml) }
36
+
37
+ context 'when request succeeds' do
38
+ let(:raw_xml) { File.read('./spec/fixtures/subscription_plan/success.xml') }
39
+
40
+ it 'returns a hash with serialized response data' do
41
+ expect { subject.serialize }.to change { plan.code }
42
+ end
43
+
44
+ it 'not change subscription plan errors' do
45
+ expect { subject.serialize }.not_to change { plan.errors.empty? }
46
+ end
47
+ end
48
+
49
+ context 'when request fails' do
50
+ before do
51
+ allow(http_response).to receive_messages(
52
+ success?: false,
53
+ error?: true,
54
+ error: Aitch::BadRequestError
55
+ )
56
+ end
57
+
58
+ let(:raw_xml) { File.read('./spec/fixtures/subscription_plan/fail.xml') }
59
+
60
+ it 'change subscription plan errors' do
61
+ expect { subject.serialize }.to change { plan.errors.empty? }
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,118 @@
1
+ require 'spec_helper'
2
+
3
+ describe PagSeguro::SubscriptionPlan do
4
+ it_assigns_attribute :max_users
5
+ it_assigns_attribute :name
6
+ it_assigns_attribute :charge
7
+ it_assigns_attribute :amount
8
+ it_assigns_attribute :max_total_amount
9
+ it_assigns_attribute :max_amount_per_payment
10
+ it_assigns_attribute :max_payments_per_period
11
+ it_assigns_attribute :max_amount_per_period
12
+ it_assigns_attribute :initial_date
13
+ it_assigns_attribute :final_date
14
+ it_assigns_attribute :membership_fee
15
+ it_assigns_attribute :trial_duration
16
+ it_assigns_attribute :period
17
+ it_assigns_attribute :redirect_url
18
+ it_assigns_attribute :review_url
19
+ it_assigns_attribute :reference
20
+ it_assigns_attribute :details
21
+
22
+ it_ensures_type PagSeguro::Sender, :sender
23
+
24
+ context 'errors attribute' do
25
+ it 'should start with errors' do
26
+ expect(subject.errors).to be_a PagSeguro::Errors
27
+ end
28
+
29
+ it 'should start with empty errors' do
30
+ expect(subject.errors).to be_empty
31
+ end
32
+ end
33
+
34
+ describe '#create' do
35
+ before do
36
+ subject.credentials = credentials
37
+
38
+ FakeWeb.register_uri(
39
+ :post,
40
+ 'https://ws.pagseguro.uol.com.br/v2/pre-approvals/request?email=user@example.com&token=TOKEN',
41
+ body: ''
42
+ )
43
+ end
44
+
45
+ let(:credentials) do
46
+ PagSeguro::AccountCredentials.new('user@example.com', 'TOKEN')
47
+ end
48
+
49
+ let(:request) do
50
+ double(
51
+ :Request,
52
+ success?: true,
53
+ xml?: true,
54
+ body: '<preApprovalRequest><code>1234</code></preApprovalRequest>'
55
+ )
56
+ end
57
+
58
+ it 'sends it to correct url' do
59
+ expect(PagSeguro).to receive(:api_url).with(
60
+ 'v2/pre-approvals/request?email=user@example.com&token=TOKEN'
61
+ ).and_return('https://ws.pagseguro.uol.com.br/v2/pre-approvals/request?email=user@example.com&token=TOKEN')
62
+
63
+ subject.create
64
+ end
65
+
66
+ context 'when parsing' do
67
+ it 'create a plan' do
68
+ expect(PagSeguro::Request).to receive(:post_xml).with(
69
+ 'pre-approvals/request', 'v2', credentials, a_string_matching(/<preApprovalRequest>/)
70
+ ).and_return(request)
71
+
72
+ subject.create
73
+ end
74
+
75
+ it 'update plan with the new code' do
76
+ allow(PagSeguro::Request).to receive(:post_xml).and_return(request)
77
+
78
+ expect { subject.create }.to change { subject.code }.to('1234')
79
+ end
80
+ end
81
+ end
82
+
83
+ context '#url' do
84
+ context 'when it is a manual subscription' do
85
+ before do
86
+ allow(subject).to receive(:charge).and_return('MANUAL')
87
+ end
88
+
89
+ context 'and it has code' do
90
+ before do
91
+ allow(subject).to receive(:code).and_return('12345')
92
+ end
93
+
94
+ context 'should return correct url with code' do
95
+ it 'to production' do
96
+ PagSeguro.configuration.environment = :production
97
+ expect(subject.url).to eq 'https://pagseguro.uol.com.br/v2/pre-approvals/request.html?code=12345'
98
+ end
99
+
100
+ it 'to sandbox' do
101
+ PagSeguro.configuration.environment = :sandbox
102
+ expect(subject.url).to eq 'https://sandbox.pagseguro.uol.com.br/v2/pre-approvals/request.html?code=12345'
103
+ end
104
+ end
105
+ end
106
+
107
+ context 'and it has no code' do
108
+ before do
109
+ allow(subject).to receive(:code).and_return(nil)
110
+ end
111
+
112
+ it 'should not return url' do
113
+ expect(subject.url).to eq nil
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end