mercado_pago_rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 91eb158bd85a06341dba4cdfd9c38eb89a1913f5
4
+ data.tar.gz: cc4f479c7da12ea865afc86c90e73a516bd5f5fb
5
+ SHA512:
6
+ metadata.gz: 72ad17a9e962baed5696bdcaf23dc7ad569378a48e417702f4df35f19787fefaf2c5221142dba9d9c0775179807fd771712ecf0f5ff33ab9ecb8340a4192a0cf
7
+ data.tar.gz: 432b123bb09465b3c4f88e4f0279e41455ce781236019f0b003542fd1987ce6209520ac5e4a0b45ea74d43d5cd8c02d224ff04734016b5739b9051dc889b1960
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0-p247
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - ruby-head
6
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mercado_pago_rails.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,191 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction, and
10
+ distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by the copyright
13
+ owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all other entities
16
+ that control, are controlled by, or are under common control with that entity.
17
+ For the purposes of this definition, "control" means (i) the power, direct or
18
+ indirect, to cause the direction or management of such entity, whether by
19
+ contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
20
+ outstanding shares, or (iii) beneficial ownership of such entity.
21
+
22
+ "You" (or "Your") shall mean an individual or Legal Entity exercising
23
+ permissions granted by this License.
24
+
25
+ "Source" form shall mean the preferred form for making modifications, including
26
+ but not limited to software source code, documentation source, and configuration
27
+ files.
28
+
29
+ "Object" form shall mean any form resulting from mechanical transformation or
30
+ translation of a Source form, including but not limited to compiled object code,
31
+ generated documentation, and conversions to other media types.
32
+
33
+ "Work" shall mean the work of authorship, whether in Source or Object form, made
34
+ available under the License, as indicated by a copyright notice that is included
35
+ in or attached to the work (an example is provided in the Appendix below).
36
+
37
+ "Derivative Works" shall mean any work, whether in Source or Object form, that
38
+ is based on (or derived from) the Work and for which the editorial revisions,
39
+ annotations, elaborations, or other modifications represent, as a whole, an
40
+ original work of authorship. For the purposes of this License, Derivative Works
41
+ shall not include works that remain separable from, or merely link (or bind by
42
+ name) to the interfaces of, the Work and Derivative Works thereof.
43
+
44
+ "Contribution" shall mean any work of authorship, including the original version
45
+ of the Work and any modifications or additions to that Work or Derivative Works
46
+ thereof, that is intentionally submitted to Licensor for inclusion in the Work
47
+ by the copyright owner or by an individual or Legal Entity authorized to submit
48
+ on behalf of the copyright owner. For the purposes of this definition,
49
+ "submitted" means any form of electronic, verbal, or written communication sent
50
+ to the Licensor or its representatives, including but not limited to
51
+ communication on electronic mailing lists, source code control systems, and
52
+ issue tracking systems that are managed by, or on behalf of, the Licensor for
53
+ the purpose of discussing and improving the Work, but excluding communication
54
+ that is conspicuously marked or otherwise designated in writing by the copyright
55
+ owner as "Not a Contribution."
56
+
57
+ "Contributor" shall mean Licensor and any individual or Legal Entity on behalf
58
+ of whom a Contribution has been received by Licensor and subsequently
59
+ incorporated within the Work.
60
+
61
+ 2. Grant of Copyright License.
62
+
63
+ Subject to the terms and conditions of this License, each Contributor hereby
64
+ grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
65
+ irrevocable copyright license to reproduce, prepare Derivative Works of,
66
+ publicly display, publicly perform, sublicense, and distribute the Work and such
67
+ Derivative Works in Source or Object form.
68
+
69
+ 3. Grant of Patent License.
70
+
71
+ Subject to the terms and conditions of this License, each Contributor hereby
72
+ grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
73
+ irrevocable (except as stated in this section) patent license to make, have
74
+ made, use, offer to sell, sell, import, and otherwise transfer the Work, where
75
+ such license applies only to those patent claims licensable by such Contributor
76
+ that are necessarily infringed by their Contribution(s) alone or by combination
77
+ of their Contribution(s) with the Work to which such Contribution(s) was
78
+ submitted. If You institute patent litigation against any entity (including a
79
+ cross-claim or counterclaim in a lawsuit) alleging that the Work or a
80
+ Contribution incorporated within the Work constitutes direct or contributory
81
+ patent infringement, then any patent licenses granted to You under this License
82
+ for that Work shall terminate as of the date such litigation is filed.
83
+
84
+ 4. Redistribution.
85
+
86
+ You may reproduce and distribute copies of the Work or Derivative Works thereof
87
+ in any medium, with or without modifications, and in Source or Object form,
88
+ provided that You meet the following conditions:
89
+
90
+ You must give any other recipients of the Work or Derivative Works a copy of
91
+ this License; and
92
+ You must cause any modified files to carry prominent notices stating that You
93
+ changed the files; and
94
+ You must retain, in the Source form of any Derivative Works that You distribute,
95
+ all copyright, patent, trademark, and attribution notices from the Source form
96
+ of the Work, excluding those notices that do not pertain to any part of the
97
+ Derivative Works; and
98
+ If the Work includes a "NOTICE" text file as part of its distribution, then any
99
+ Derivative Works that You distribute must include a readable copy of the
100
+ attribution notices contained within such NOTICE file, excluding those notices
101
+ that do not pertain to any part of the Derivative Works, in at least one of the
102
+ following places: within a NOTICE text file distributed as part of the
103
+ Derivative Works; within the Source form or documentation, if provided along
104
+ with the Derivative Works; or, within a display generated by the Derivative
105
+ Works, if and wherever such third-party notices normally appear. The contents of
106
+ the NOTICE file are for informational purposes only and do not modify the
107
+ License. You may add Your own attribution notices within Derivative Works that
108
+ You distribute, alongside or as an addendum to the NOTICE text from the Work,
109
+ provided that such additional attribution notices cannot be construed as
110
+ modifying the License.
111
+ You may add Your own copyright statement to Your modifications and may provide
112
+ additional or different license terms and conditions for use, reproduction, or
113
+ distribution of Your modifications, or for any such Derivative Works as a whole,
114
+ provided Your use, reproduction, and distribution of the Work otherwise complies
115
+ with the conditions stated in this License.
116
+
117
+ 5. Submission of Contributions.
118
+
119
+ Unless You explicitly state otherwise, any Contribution intentionally submitted
120
+ for inclusion in the Work by You to the Licensor shall be under the terms and
121
+ conditions of this License, without any additional terms or conditions.
122
+ Notwithstanding the above, nothing herein shall supersede or modify the terms of
123
+ any separate license agreement you may have executed with Licensor regarding
124
+ such Contributions.
125
+
126
+ 6. Trademarks.
127
+
128
+ This License does not grant permission to use the trade names, trademarks,
129
+ service marks, or product names of the Licensor, except as required for
130
+ reasonable and customary use in describing the origin of the Work and
131
+ reproducing the content of the NOTICE file.
132
+
133
+ 7. Disclaimer of Warranty.
134
+
135
+ Unless required by applicable law or agreed to in writing, Licensor provides the
136
+ Work (and each Contributor provides its Contributions) on an "AS IS" BASIS,
137
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
138
+ including, without limitation, any warranties or conditions of TITLE,
139
+ NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
140
+ solely responsible for determining the appropriateness of using or
141
+ redistributing the Work and assume any risks associated with Your exercise of
142
+ permissions under this License.
143
+
144
+ 8. Limitation of Liability.
145
+
146
+ In no event and under no legal theory, whether in tort (including negligence),
147
+ contract, or otherwise, unless required by applicable law (such as deliberate
148
+ and grossly negligent acts) or agreed to in writing, shall any Contributor be
149
+ liable to You for damages, including any direct, indirect, special, incidental,
150
+ or consequential damages of any character arising as a result of this License or
151
+ out of the use or inability to use the Work (including but not limited to
152
+ damages for loss of goodwill, work stoppage, computer failure or malfunction, or
153
+ any and all other commercial damages or losses), even if such Contributor has
154
+ been advised of the possibility of such damages.
155
+
156
+ 9. Accepting Warranty or Additional Liability.
157
+
158
+ While redistributing the Work or Derivative Works thereof, You may choose to
159
+ offer, and charge a fee for, acceptance of support, warranty, indemnity, or
160
+ other liability obligations and/or rights consistent with this License. However,
161
+ in accepting such obligations, You may act only on Your own behalf and on Your
162
+ sole responsibility, not on behalf of any other Contributor, and only if You
163
+ agree to indemnify, defend, and hold each Contributor harmless for any liability
164
+ incurred by, or claims asserted against, such Contributor by reason of your
165
+ accepting any such warranty or additional liability.
166
+
167
+ END OF TERMS AND CONDITIONS
168
+
169
+ APPENDIX: How to apply the Apache License to your work
170
+
171
+ To apply the Apache License to your work, attach the following boilerplate
172
+ notice, with the fields enclosed by brackets "[]" replaced with your own
173
+ identifying information. (Don't include the brackets!) The text should be
174
+ enclosed in the appropriate comment syntax for the file format. We also
175
+ recommend that a file or class name and description of purpose be included on
176
+ the same "printed page" as the copyright notice for easier identification within
177
+ third-party archives.
178
+
179
+ Copyright 2013 Wolox S.A.
180
+
181
+ Licensed under the Apache License, Version 2.0 (the "License");
182
+ you may not use this file except in compliance with the License.
183
+ You may obtain a copy of the License at
184
+
185
+ http://www.apache.org/licenses/LICENSE-2.0
186
+
187
+ Unless required by applicable law or agreed to in writing, software
188
+ distributed under the License is distributed on an "AS IS" BASIS,
189
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190
+ See the License for the specific language governing permissions and
191
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,124 @@
1
+ Mercadopago-Rails
2
+ =================
3
+ **MercadoPago-Rails** is a very simple gem that helps you interact with mercadopago api. It uses [meracado pago gem](https://github.com/kauplus/mercadopago) and [mercado pago sdk](https://github.com/mercadopago/sdk-ruby) to work internally.
4
+ For the moment, this gem only supports buying only ONE type of product at a time.
5
+
6
+ ## Usage
7
+
8
+ ### MercadoPagoModule
9
+ To start communicating with mercado pago, you need to include **MercadoPagoModule**. This module adds the method generate_purchase that receives two parameters:
10
+ * An object that holds the information of the purchase to be made
11
+ * A hash containing three urls: success_url, failure_url and pending_url
12
+
13
+ The object needs to respond to the following methods:
14
+ * **external_reference**: this will and id to be used by MercadoPago to refer to the object (it can be your database ID or whatever reference you wish).
15
+ * **quantity**: amount of products to be bought.
16
+ * **unit_price**: unit price of the product you are buying (hence, the total amount to be paid is quantity * unit_price).
17
+ * **currency**: the currency in which the payment must be made (for instance, "ARS". For more information about this, refer [here](http://developers.mercadopago.com/documentacion/api/preferences).
18
+ * **picture_url**: url for the picture of the product. This is shown by MercadoPago at the time of the payment (this is optional)
19
+ * **payer_name**: payer's name
20
+ * **payer_surname**: payer's surname
21
+ * **payer_email**: payer's email
22
+
23
+ The following object is an example of the mentioned above:
24
+
25
+ ```ruby
26
+ class MercadoPagoVoucherAdapter
27
+
28
+ def initialize(voucher)
29
+ @voucher = voucher
30
+ end
31
+
32
+ def quantity
33
+ 1
34
+ end
35
+
36
+ def unit_price
37
+ @voucher.paid
38
+ end
39
+
40
+ def currency
41
+ "ARS"
42
+ end
43
+
44
+ def picture_url
45
+ @voucher.brand.logo.url
46
+ end
47
+
48
+ def payer_namer
49
+ @voucher.user.name
50
+ end
51
+
52
+ def payer_surname
53
+ @voucher.user.name
54
+ end
55
+
56
+ def payer_email
57
+ @voucher.user.email
58
+ end
59
+
60
+ def external_reference
61
+ @voucher.id
62
+ end
63
+
64
+ def title
65
+ @voucher.brand.name
66
+ end
67
+
68
+ def new_status(status)
69
+ @voucher.status = status
70
+ @voucher.save
71
+ end
72
+ end
73
+
74
+ ```
75
+
76
+ The urls passed as a hash are used by MercadoPago to create links back to your application once the user has finished with the payment.
77
+
78
+ When the method returns, it creates an instance of MercadoPagoResponse. To check if there was an error while just invoke `mercado_pago_response.success?`. If there was an error, you can call
79
+ `mercado_pago_response.error_msg` to get the error message. Otherwise, you can call `mercado_pago_response.redirect_url` and it will return the url where the user must go to complete the
80
+ payment.
81
+
82
+
83
+ ```ruby
84
+ mercado_pago_response = generate_purchase(MercadoPagoVoucherAdapter.new(@voucher), success_url: root_url, pending_url: root_url, failure_url: root_url)
85
+ if !mercado_pago_response.success?
86
+ flash[:error] = I18n.t('voucher.confirm.mercadopago.error')
87
+ render action: "new", brand_id: params[:voucher][:brand_id]
88
+ else
89
+ redirect_to mercado_pago_response.redirect_url
90
+ end
91
+
92
+ ```
93
+
94
+ ### MercadoPagoHelper
95
+ This class contains the object in charge of communicating with MercadoPago. To use it, you must include [AppConfiguration](https://github.com/guidomb/app_configuration). Usign this gem, **MercadoPagoHelper** will load
96
+ the client_id and client_secret that you where provided to interact with MercadoPago from the file .mercadopago.yml (or environment variable, just check the gem's documentation).
97
+ This class also contains constants representing the different status that a purchase can adquire: **APPROVED**, **PENDING**, **IN_PROCESS**, **REJECTED**, **REFUNDED**, **CANCELLED**, **IN_MEDIATION**.
98
+
99
+ If you want, you can set handlers for the different status that a purchase might adquire. Just add the following lines in an initializer.
100
+ ```ruby
101
+ MercadoPagoHelper.set_handlers(approved: SuccessfulPurchaseContext, pending: PendingPurchaseContext, in_process: InProcessPurchaseContext,
102
+ in_mediation: InMediationPurchaseContext, rejected: RejectedPurchaseContext, cancelled: CancelledPurchaseContext,
103
+ refunded: RefundedPurchaseContext)
104
+ ```
105
+ This handlers will be instanciated when `get_context(response, obj)` is called. The method checks the purchase's status instantiates the corresponding handler. All this handlers must receive in their initializer
106
+ the response and the object that want to modify. This can be used in the following way: `MercadoPagoHelper.get_context(info, voucher).handle`
107
+
108
+ ## License ##
109
+
110
+ Copyright 2013 Wolox S.A.
111
+
112
+ Licensed under the Apache License, Version 2.0 (the "License");
113
+ you may not use this file except in compliance with the License.
114
+ You may obtain a copy of the License at
115
+
116
+ http://www.apache.org/licenses/LICENSE-2.0
117
+
118
+ Unless required by applicable law or agreed to in writing, software
119
+ distributed under the License is distributed on an "AS IS" BASIS,
120
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121
+ See the License for the specific language governing permissions and
122
+ limitations under the License.
123
+
124
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,34 @@
1
+ require 'i18n'
2
+
3
+ class MercadoPagoCheckout
4
+
5
+ def initialize(model)
6
+ @model = model
7
+ end
8
+
9
+ def generate(success_url, pending_url, failure_url)
10
+ { :external_reference => @model.external_reference,
11
+ :items => get_items,
12
+ :payer => get_payer,
13
+ :back_urls => { pending: pending_url, success: success_url, failure: failure_url }
14
+ }
15
+ end
16
+
17
+ private
18
+ def get_items
19
+ [{ :id => '',
20
+ :title => "#{I18n.t('mercadopago.preference.title', brand: @model.title)}",
21
+ :quantity => @model.quantity,
22
+ :unit_price => @model.unit_price,
23
+ :currency_id => @model.currency,
24
+ :picture_url => @model.picture_url
25
+ }]
26
+ end
27
+
28
+ def get_payer
29
+ { :name => @model.payer_namer,
30
+ :surname => @model.payer_surname,
31
+ :email => @model.payer_email
32
+ }
33
+ end
34
+ end
@@ -0,0 +1,39 @@
1
+ require 'mercadopago'
2
+ require 'app_configuration'
3
+
4
+ class MercadoPagoHelper
5
+
6
+ APPROVED = :approved
7
+ PENDING = :pending
8
+ IN_PROCESS = :in_process
9
+ REJECTED = :rejected
10
+ REFUNDED = :refunded
11
+ CANCELLED = :cancelled
12
+ IN_MEDIATION = :in_mediation
13
+
14
+ class << self
15
+
16
+ @@mp = MercadoPago.new(AppConfiguration.for(:mercadopago).client_id, AppConfiguration.for(:mercadopago).client_secret)
17
+
18
+ def mp
19
+ @@mp
20
+ end
21
+
22
+ def set_handlers(status_handlers = {})
23
+ validate_presence(status_handlers)
24
+ @contexts = status_handlers.dup
25
+ end
26
+
27
+ def get_context(response, obj)
28
+ status = response['response']['collection']['status'].to_sym
29
+ @contexts[status].new(response, obj)
30
+ end
31
+
32
+ private
33
+ def validate_presence(status_handlers)
34
+ if status_handlers[APPROVED].nil? || status_handlers[PENDING].nil? || status_handlers[IN_PROCESS].nil? || status_handlers[REJECTED].nil? || status_handlers[REFUNDED].nil? || status_handlers[CANCELLED].nil? || status_handlers[IN_MEDIATION].nil?
35
+ raise "MissingStatusHandler"
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,21 @@
1
+ class MercadoPagoResponse
2
+
3
+ # SANDBOX_INIT_POINT = 'sandbox_init_point'
4
+ INIT_POINT = 'init_point'
5
+
6
+ def initialize(response)
7
+ @response = response
8
+ end
9
+
10
+ def success?
11
+ return @response['error'].nil?
12
+ end
13
+
14
+ def redirect_url
15
+ @response['response'][INIT_POINT]
16
+ end
17
+
18
+ def error_msg
19
+ @response['error']
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module MercadoPagoRails
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,18 @@
1
+ require "mercado_pago_rails/mercado_pago_checkout"
2
+ require "mercado_pago_rails/mercado_pago_response"
3
+ require "mercado_pago_rails/version"
4
+
5
+ module MercadoPagoRails
6
+
7
+ def generate_purchase(mercado_pago_object, urls = {})
8
+ purchase = MercadoPagoHelper.mp.create_preference(get_data(mercado_pago_object, urls))
9
+ mercado_pago_object.new_status(purchase['response']['error'] ? "error" : "pending")
10
+ MercadoPagoResponse.new(purchase)
11
+ end
12
+
13
+ private
14
+ def get_data(mercado_pago_object, urls)
15
+ role = MercadoPagoCheckout.new(mercado_pago_object)
16
+ role.generate(urls[:success_url], urls[:pending_url], urls[:failure_url])
17
+ end
18
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mercado_pago_rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mercado_pago_rails"
8
+ spec.version = MercadoPagoRails::VERSION
9
+ spec.authors = ["Matias De Santi"]
10
+ spec.email = ["matias.desanti@wolox.com.ar"]
11
+ spec.description = %q{This gem makes communication with mercado pago easy at the time of generating a preference}
12
+ spec.summary = %q{This gem makes communication with mercado pago easy at the time of generating a preference}
13
+ spec.homepage = ""
14
+ spec.license = "Apache2"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+
25
+ spec.add_dependency 'mercadopago'
26
+ spec.add_dependency 'mercadopago-sdk'
27
+ spec.add_dependency "app_configuration"
28
+ spec.add_dependency "i18n"
29
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+ require 'mercado_pago_rails/mercado_pago_checkout'
3
+ require_relative 'mercado_pago_test_model'
4
+
5
+ describe MercadoPagoCheckout do
6
+
7
+ let(:model) { MercadoPagoTestModel.new }
8
+ subject(:mercado_pago_checkout) { MercadoPagoCheckout.new(model) }
9
+
10
+ describe "#generate" do
11
+
12
+ context "when the preference wants to be created" do
13
+
14
+ it "it returns a hash with the correct data" do
15
+ mercado_pago_checkout.generate("root_url", "root_url", "root_url").should eq get_preference
16
+ end
17
+ end
18
+ end
19
+
20
+ def get_preference
21
+ {:external_reference=>1, :items=>[{:id=>"", :title=>"translation missing: en.mercadopago.preference.title", :quantity=>3, :unit_price=>12, :currency_id=>"ARS", :picture_url=>"/test/url"}], :payer=>{:name=>"John", :surname=>"Smith", :email=>"johnsmith@example.com"}, :back_urls=>{:pending=>"root_url", :success=>"root_url", :failure=>"root_url"}}
22
+ end
23
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+ require 'mercado_pago_rails/mercado_pago_helper'
3
+ require_relative 'test_contexts'
4
+
5
+ describe MercadoPagoHelper do
6
+
7
+ describe "#set_handlers" do
8
+
9
+ context "when not all handlers are set" do
10
+
11
+ it "it raises an exception" do
12
+ expect { MercadoPagoHelper.set_handlers(approved: TestContexts::ApprovedContext, pending: TestContexts::PendingContext, in_process: TestContexts::InProcessContext,
13
+ in_mediation: TestContexts::InMediationContext, rejected: TestContexts::RejectedContext, cancelled: TestContexts::CancelledContext) }.to raise_error
14
+ end
15
+ end
16
+
17
+ context "when all handlers are set" do
18
+ before do
19
+ MercadoPagoHelper.set_handlers(approved: TestContexts::ApprovedContext, pending: TestContexts::PendingContext, in_process: TestContexts::InProcessContext,
20
+ in_mediation: TestContexts::InMediationContext, rejected: TestContexts::RejectedContext, cancelled: TestContexts::CancelledContext, refunded: TestContexts::RefundedContext)
21
+ end
22
+
23
+ it "it returns the right context for approved status" do
24
+ MercadoPagoHelper.get_context(get_response(MercadoPagoHelper::APPROVED.to_s), nil).class.should eq TestContexts::ApprovedContext
25
+ end
26
+
27
+ it "it returns the right context for pending status" do
28
+ MercadoPagoHelper.get_context(get_response(MercadoPagoHelper::PENDING.to_s), nil).class.should eq TestContexts::PendingContext
29
+ end
30
+
31
+ it "it returns the right context for in_process status" do
32
+ MercadoPagoHelper.get_context(get_response(MercadoPagoHelper::IN_PROCESS.to_s), nil).class.should eq TestContexts::InProcessContext
33
+ end
34
+
35
+ it "it returns the right context for in mediation status" do
36
+ MercadoPagoHelper.get_context(get_response(MercadoPagoHelper::IN_MEDIATION.to_s), nil).class.should eq TestContexts::InMediationContext
37
+ end
38
+
39
+ it "it returns the right context for rejected status" do
40
+ MercadoPagoHelper.get_context(get_response(MercadoPagoHelper::REJECTED.to_s), nil).class.should eq TestContexts::RejectedContext
41
+ end
42
+
43
+ it "it returns the right context for cancelled status" do
44
+ MercadoPagoHelper.get_context(get_response(MercadoPagoHelper::CANCELLED.to_s), nil).class.should eq TestContexts::CancelledContext
45
+ end
46
+
47
+ it "it returns the right context for refunded status" do
48
+ MercadoPagoHelper.get_context(get_response(MercadoPagoHelper::REFUNDED.to_s), nil).class.should eq TestContexts::RefundedContext
49
+ end
50
+ end
51
+ end
52
+
53
+ def get_response(status)
54
+ {'response' => { 'collection' => { 'status' => status } } }
55
+ end
56
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+ require 'mercado_pago_rails/mercado_pago_response'
3
+
4
+ describe MercadoPagoResponse do
5
+
6
+ let(:successful_response) { MercadoPagoResponse.new(get_success_response) }
7
+
8
+ describe "#success?" do
9
+
10
+ context "when consulted if there was an error" do
11
+
12
+ it "it returns false" do
13
+ successful_response.success?.should be_true
14
+ end
15
+ end
16
+ end
17
+
18
+ describe "#redirect_url" do
19
+ context "when consulted for the redirect url"
20
+
21
+ it "it returns the correct url" do
22
+ successful_response.redirect_url.should eq "http://checkout.url.com"
23
+ end
24
+ end
25
+
26
+ def get_success_response
27
+ { "response" =>
28
+ {
29
+ "external_reference"=> "Reference_1234",
30
+ "items"=> [{
31
+ "id"=> "Código",
32
+ "title"=> "Título de lo que estás pagando",
33
+ "description"=> "Descripción",
34
+ "quantity"=> 1,
35
+ "unit_price"=> 50.5,
36
+ "currency_id"=> "Tipo de moneda",
37
+ "picture_url"=> "https=>//www.mercadopago.com/org-img/MP3/home/logomp3.gif"
38
+ }],
39
+ "date_created"=> "2011-08-16T21=>28=>42.606-04=>00",
40
+ "id"=> "identificador_de_la_preferencia",
41
+ "collector_id"=> "tu_identificador_como_vendedor",
42
+ "init_point"=> "http://checkout.url.com",
43
+ "payer"=> {
44
+ "email"=> "payer@email.com",
45
+ "name"=> "payer-name",
46
+ "surname"=> "payer-surname"
47
+ },
48
+ "back_urls"=> {
49
+ "success"=> "https=>//www.success.com",
50
+ "failure"=> "http=>//www.failure.com",
51
+ "pending"=> "http=>//www.pending.com"
52
+ },
53
+ "payment_methods"=> {
54
+ "excluded_payment_methods"=> [
55
+ {
56
+ "id"=> "amex"
57
+ }
58
+ ],
59
+ "excluded_payment_types"=> [
60
+ {
61
+ "id"=> "ticket"
62
+ }
63
+ ],
64
+ "installments"=> 12
65
+ }
66
+ }
67
+ }
68
+ end
69
+ end
@@ -0,0 +1,42 @@
1
+ class MercadoPagoTestModel
2
+
3
+ def quantity
4
+ 3
5
+ end
6
+
7
+ def unit_price
8
+ 12
9
+ end
10
+
11
+ def currency
12
+ "ARS"
13
+ end
14
+
15
+ def picture_url
16
+ "/test/url"
17
+ end
18
+
19
+ def payer_namer
20
+ "John"
21
+ end
22
+
23
+ def payer_surname
24
+ "Smith"
25
+ end
26
+
27
+ def payer_email
28
+ "johnsmith@example.com"
29
+ end
30
+
31
+ def external_reference
32
+ 1
33
+ end
34
+
35
+ def title
36
+ "John Smith's purchase"
37
+ end
38
+
39
+ def new_status(status)
40
+
41
+ end
42
+ end
@@ -0,0 +1,15 @@
1
+ require 'bundler/setup'
2
+
3
+ module Helpers
4
+
5
+ def fixture_path
6
+ File.expand_path(File.join('.', 'spec', 'fixtures'))
7
+ end
8
+
9
+ end
10
+
11
+ RSpec.configure do |config|
12
+
13
+ config.include(Helpers)
14
+
15
+ end
@@ -0,0 +1,71 @@
1
+ module TestContexts
2
+ class ApprovedContext
3
+ def initialize(response, obj)
4
+ # do nothing, just testing
5
+ end
6
+
7
+ def handle
8
+ # do nothing, just testing
9
+ end
10
+ end
11
+
12
+ class PendingContext
13
+ def initialize(response, obj)
14
+ # do nothing, just testing
15
+ end
16
+
17
+ def handle
18
+ # do nothing, just testing
19
+ end
20
+ end
21
+
22
+ class InProcessContext
23
+ def initialize(response, obj)
24
+ # do nothing, just testing
25
+ end
26
+
27
+ def handle
28
+ # do nothing, just testing
29
+ end
30
+ end
31
+
32
+ class InMediationContext
33
+ def initialize(response, obj)
34
+ # do nothing, just testing
35
+ end
36
+
37
+ def handle
38
+ # do nothing, just testing
39
+ end
40
+ end
41
+
42
+ class RejectedContext
43
+ def initialize(response, obj)
44
+ # do nothing, just testing
45
+ end
46
+
47
+ def handle
48
+ # do nothing, just testing
49
+ end
50
+ end
51
+
52
+ class RefundedContext
53
+ def initialize(response, obj)
54
+ # do nothing, just testing
55
+ end
56
+
57
+ def handle
58
+ # do nothing, just testing
59
+ end
60
+ end
61
+
62
+ class CancelledContext
63
+ def initialize(response, obj)
64
+ # do nothing, just testing
65
+ end
66
+
67
+ def handle
68
+ # do nothing, just testing
69
+ end
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mercado_pago_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matias De Santi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-09 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mercadopago
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
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: mercadopago-sdk
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
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: app_configuration
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
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: i18n
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
+ description: This gem makes communication with mercado pago easy at the time of generating
112
+ a preference
113
+ email:
114
+ - matias.desanti@wolox.com.ar
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - .ruby-version
121
+ - .travis.yml
122
+ - Gemfile
123
+ - LICENSE
124
+ - README.md
125
+ - Rakefile
126
+ - lib/mercado_pago_rails.rb
127
+ - lib/mercado_pago_rails/mercado_pago_checkout.rb
128
+ - lib/mercado_pago_rails/mercado_pago_helper.rb
129
+ - lib/mercado_pago_rails/mercado_pago_response.rb
130
+ - lib/mercado_pago_rails/version.rb
131
+ - mercado_pago_rails.gemspec
132
+ - spec/mercado_pago_checkout_spec.rb
133
+ - spec/mercado_pago_helper_spec.rb
134
+ - spec/mercado_pago_response_spec.rb
135
+ - spec/mercado_pago_test_model.rb
136
+ - spec/spec_helper.rb
137
+ - spec/test_contexts.rb
138
+ homepage: ''
139
+ licenses:
140
+ - Apache2
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - '>='
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - '>='
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.0.3
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: This gem makes communication with mercado pago easy at the time of generating
162
+ a preference
163
+ test_files:
164
+ - spec/mercado_pago_checkout_spec.rb
165
+ - spec/mercado_pago_helper_spec.rb
166
+ - spec/mercado_pago_response_spec.rb
167
+ - spec/mercado_pago_test_model.rb
168
+ - spec/spec_helper.rb
169
+ - spec/test_contexts.rb