mondido 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +32 -0
- data/lib/mondido/base_model.rb +75 -0
- data/lib/mondido/config.rb +5 -0
- data/lib/mondido/credentials.rb +16 -0
- data/lib/mondido/credit_card/refund.rb +27 -0
- data/lib/mondido/credit_card/stored_card.rb +38 -0
- data/lib/mondido/credit_card/transaction.rb +72 -0
- data/lib/mondido/exceptions/api_exception.rb +6 -0
- data/lib/mondido/exceptions/exceptions.rb +6 -0
- data/lib/mondido/exceptions/validation_exception.rb +11 -0
- data/lib/mondido/rest_client.rb +48 -0
- data/lib/mondido/version.rb +3 -0
- data/lib/mondido.rb +19 -0
- data/spec/credit_card/refund_spec.rb +34 -0
- data/spec/credit_card/stored_card_spec.rb +75 -0
- data/spec/credit_card/transaction_spec.rb +193 -0
- data/spec/spec_helper.rb +19 -0
- data/spec/stubs/refund.json +10 -0
- data/spec/stubs/stored_card.json +16 -0
- data/spec/stubs/transaction.json +35 -0
- data/spec/stubs/transactions.json +70 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 38e6de19a52348345f2dbf4bd5d70c87e22a0c91
|
4
|
+
data.tar.gz: b1b872b2642b5e20137dee77339701658e03440a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cce179185b1a6636409286aa925daea01baa98ca5086b9bad3e0074337f63100f017fdfe1c703eab76a2a0503c3ddacd083eef07848f5756c7cfe5a130d67852
|
7
|
+
data.tar.gz: d70be22746fe80499c75767eaecee37b7437844dbb4ec814a3e0e3bb417d7cde4db629093b8b8288aa1ea843af3db95b26f572e4190b1019960e7836439ca354
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 Mondido Payments AB
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'MondidoSdk'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
Bundler::GemHelper.install_tasks
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
|
24
|
+
Rake::TestTask.new(:test) do |t|
|
25
|
+
t.libs << 'lib'
|
26
|
+
t.libs << 'test'
|
27
|
+
t.pattern = 'test/**/*_test.rb'
|
28
|
+
t.verbose = false
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
task default: :test
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module Mondido
|
2
|
+
class BaseModel
|
3
|
+
|
4
|
+
# @param attributes [Hash]
|
5
|
+
# @return [Mondido::*]
|
6
|
+
# Takes the JSON response from the API and sets white listed properties for the instance
|
7
|
+
def update_from_response(attributes)
|
8
|
+
attributes.each do |attribute, value|
|
9
|
+
attribute_symbol = "@#{attribute}".to_sym
|
10
|
+
setter_symbol = "#{attribute}=".to_sym
|
11
|
+
instance_variable_set attribute_symbol, value if respond_to?(setter_symbol)
|
12
|
+
end
|
13
|
+
|
14
|
+
return self
|
15
|
+
end
|
16
|
+
|
17
|
+
# @return [Hash]
|
18
|
+
# Returns a sanitized hash suitable for posting to the API
|
19
|
+
def api_params
|
20
|
+
excluded = %w(@errors @validation_context).map(&:to_sym)
|
21
|
+
hash = {}
|
22
|
+
instance_variables.reject{|o| excluded.include?(o) }.each do |var_sym|
|
23
|
+
var_sym_without_at = var_sym.to_s[1, var_sym.length-1].to_sym
|
24
|
+
hash[var_sym_without_at] = instance_variable_get(var_sym)
|
25
|
+
end
|
26
|
+
return hash
|
27
|
+
end
|
28
|
+
|
29
|
+
# Class Methods
|
30
|
+
|
31
|
+
# @param attributes [Hash]
|
32
|
+
# @return [Mondido::*]
|
33
|
+
# Creates a transaction and posts it to the API
|
34
|
+
def self.create(attributes)
|
35
|
+
object = self.new(attributes)
|
36
|
+
object.valid? # Will raise exception if validation fails
|
37
|
+
|
38
|
+
object.set_hash! if object.respond_to? :set_hash!
|
39
|
+
|
40
|
+
response = Mondido::RestClient.process(object)
|
41
|
+
|
42
|
+
object.update_from_response(JSON.parse(response.body))
|
43
|
+
end
|
44
|
+
|
45
|
+
# @param id [Integer]
|
46
|
+
# @return [Mondido::*]
|
47
|
+
# Retrieves an object from the API, by ID
|
48
|
+
def self.get(id)
|
49
|
+
response = Mondido::RestClient.get(pluralized, id)
|
50
|
+
object = self.new
|
51
|
+
object.update_from_response(JSON.parse(response.body))
|
52
|
+
end
|
53
|
+
|
54
|
+
# @param filter [Hash] :limit, :offset
|
55
|
+
# @return [Array]
|
56
|
+
# Retrieves a list of objects from the API
|
57
|
+
def self.all(filter={})
|
58
|
+
response = Mondido::RestClient.all(pluralized, filter)
|
59
|
+
JSON.parse(response.body).map do |attributes|
|
60
|
+
object = self.new
|
61
|
+
object.update_from_response(attributes)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
# @return [String]
|
68
|
+
# Returns a camel cased pluralized version of the class name
|
69
|
+
# e.g. converts Mondido::CreditCard::StoredCard to 'stored_cards'
|
70
|
+
def self.pluralized
|
71
|
+
self.name.split('::').last.underscore.pluralize
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Mondido
|
2
|
+
class Credentials
|
3
|
+
unless Rails.root.nil?
|
4
|
+
config_file = File.join(Rails.root, 'config', 'mondido.yml')
|
5
|
+
if File.exist?(config_file)
|
6
|
+
yaml = YAML.load(File.read(config_file))
|
7
|
+
else
|
8
|
+
raise 'Could not find app/mondido.yml'
|
9
|
+
end
|
10
|
+
|
11
|
+
MERCHANT_ID ||= yaml['merchant_id'] or raise 'Mondido merchant ID not set in config/mondido.yml'
|
12
|
+
SECRET ||= yaml['secret'] or raise 'Mondido secret not set in config/mondido.yml'
|
13
|
+
PASSWORD ||= yaml['password'] or raise 'Mondido password not set in config/mondido.yml'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Mondido
|
2
|
+
module CreditCard
|
3
|
+
class Refund < BaseModel
|
4
|
+
include ActiveModel::Model
|
5
|
+
|
6
|
+
attr_accessor :id,
|
7
|
+
:amount,
|
8
|
+
:reason,
|
9
|
+
:ref,
|
10
|
+
:created_at,
|
11
|
+
:transaction,
|
12
|
+
:transaction_id
|
13
|
+
|
14
|
+
validates :transaction_id,
|
15
|
+
presence: { message: 'errors.transaction_id.missing' },
|
16
|
+
strict: Mondido::Exceptions::ValidationException
|
17
|
+
|
18
|
+
validates :amount,
|
19
|
+
presence: { message: 'errors.amount.missing' },
|
20
|
+
strict: Mondido::Exceptions::ValidationException
|
21
|
+
|
22
|
+
validates :reason,
|
23
|
+
presence: { message: 'errors.reason.missing' },
|
24
|
+
strict: Mondido::Exceptions::ValidationException
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Mondido
|
2
|
+
module CreditCard
|
3
|
+
class StoredCard < BaseModel
|
4
|
+
include ActiveModel::Model
|
5
|
+
|
6
|
+
attr_accessor :id,
|
7
|
+
:currency,
|
8
|
+
:card_number,
|
9
|
+
:card_holder,
|
10
|
+
:card_cvv,
|
11
|
+
:card_expiry,
|
12
|
+
:card_type,
|
13
|
+
:metadata,
|
14
|
+
:created_at,
|
15
|
+
:merchant_id,
|
16
|
+
:test,
|
17
|
+
:status,
|
18
|
+
:customer,
|
19
|
+
:hash
|
20
|
+
|
21
|
+
validates :currency,
|
22
|
+
presence: { message: 'errors.currency.missing' }
|
23
|
+
|
24
|
+
validates :card_number,
|
25
|
+
presence: { message: 'errors.card_number.missing' }
|
26
|
+
|
27
|
+
validates :card_cvv,
|
28
|
+
presence: { message: 'errors.card_cvv.missing' }
|
29
|
+
|
30
|
+
validates :card_expiry,
|
31
|
+
presence: { message: 'errors.card_expiry.missing' }
|
32
|
+
|
33
|
+
validates :card_type,
|
34
|
+
presence: { message: 'errors.card_type.missing' }
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Mondido
|
2
|
+
module CreditCard
|
3
|
+
class Transaction < BaseModel
|
4
|
+
include ActiveModel::Model
|
5
|
+
|
6
|
+
attr_accessor :id,
|
7
|
+
:amount,
|
8
|
+
:currency,
|
9
|
+
:card_number,
|
10
|
+
:card_holder,
|
11
|
+
:card_cvv,
|
12
|
+
:card_expiry,
|
13
|
+
:card_type,
|
14
|
+
:payment_ref,
|
15
|
+
:metadata,
|
16
|
+
:items,
|
17
|
+
:created_at,
|
18
|
+
:merchant_id,
|
19
|
+
:vat_amount,
|
20
|
+
:test,
|
21
|
+
:status,
|
22
|
+
:transaction_type,
|
23
|
+
:cost,
|
24
|
+
:stored_card,
|
25
|
+
:customer,
|
26
|
+
:subscription,
|
27
|
+
:payment_details,
|
28
|
+
:hash
|
29
|
+
|
30
|
+
validates :currency,
|
31
|
+
presence: { message: 'errors.currency.missing' },
|
32
|
+
strict: Mondido::Exceptions::ValidationException
|
33
|
+
|
34
|
+
validates :amount,
|
35
|
+
presence: { message: 'errors.amount.missing' },
|
36
|
+
strict: Mondido::Exceptions::ValidationException
|
37
|
+
|
38
|
+
validates :card_number,
|
39
|
+
presence: { message: 'errors.card_number.missing' },
|
40
|
+
strict: Mondido::Exceptions::ValidationException
|
41
|
+
|
42
|
+
validates :card_cvv,
|
43
|
+
presence: { message: 'errors.card_cvv.missing' },
|
44
|
+
strict: Mondido::Exceptions::ValidationException
|
45
|
+
|
46
|
+
validates :card_expiry,
|
47
|
+
presence: { message: 'errors.card_expiry.missing' },
|
48
|
+
strict: Mondido::Exceptions::ValidationException
|
49
|
+
|
50
|
+
validates :card_type,
|
51
|
+
presence: { message: 'errors.card_type.missing' },
|
52
|
+
strict: Mondido::Exceptions::ValidationException
|
53
|
+
|
54
|
+
validates :payment_ref,
|
55
|
+
presence: { message: 'errors.payment_ref.missing' },
|
56
|
+
strict: Mondido::Exceptions::ValidationException
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
def initialize(attributes={})
|
62
|
+
super
|
63
|
+
end
|
64
|
+
|
65
|
+
def set_hash!
|
66
|
+
unhashed = [Mondido::Credentials::MERCHANT_ID, payment_ref, amount, currency, Mondido::Credentials::SECRET]
|
67
|
+
self.hash = Digest::MD5.hexdigest(unhashed.join)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Mondido
|
2
|
+
class RestClient
|
3
|
+
|
4
|
+
# @param instance [Mondido::*]
|
5
|
+
# @return [Mondido::*]
|
6
|
+
# Takes an instance of a Mondido object and calls the API to process the transaction
|
7
|
+
def self.process(instance)
|
8
|
+
uri_string = [Mondido::Config::URI, instance.class.pluralized].join('/')
|
9
|
+
call_api(uri: uri_string, data: instance.api_params, http_method: :post)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def self.call_api(args)
|
15
|
+
require 'net/https'
|
16
|
+
uri = URI.parse args[:uri]
|
17
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
18
|
+
# http.use_ssl = true
|
19
|
+
http_method = args[:http_method] || :get
|
20
|
+
case http_method
|
21
|
+
when :post
|
22
|
+
request = Net::HTTP::Post.new(uri.path)
|
23
|
+
when :get
|
24
|
+
request = Net::HTTP::Get.new(uri.path)
|
25
|
+
end
|
26
|
+
request.basic_auth(Mondido::Credentials::MERCHANT_ID, Mondido::Credentials::PASSWORD)
|
27
|
+
request.set_form_data(args[:data]) if args.has_key?(:data)
|
28
|
+
response = http.start { |http| http.request(request) }
|
29
|
+
unless (200..299).include?(response.code.to_i)
|
30
|
+
error_name = JSON.parse(response.body)['name'] rescue nil || 'errors.generic'
|
31
|
+
raise Mondido::Exceptions::ApiException.new(error_name)
|
32
|
+
end
|
33
|
+
|
34
|
+
return response
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.get(method, id=nil)
|
38
|
+
uri_string = [Mondido::Config::URI, method.to_s, id.to_s].join('/')
|
39
|
+
call_api(uri: uri_string)
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.all(method, filter)
|
43
|
+
uri_string = [Mondido::Config::URI, method.to_s].join('/')
|
44
|
+
call_api(uri: uri_string, data: filter)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
data/lib/mondido.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Mondido
|
2
|
+
require 'active_support'
|
3
|
+
require 'active_model'
|
4
|
+
require 'mondido/config'
|
5
|
+
require 'mondido/exceptions/exceptions'
|
6
|
+
require 'mondido/rest_client'
|
7
|
+
require 'mondido/base_model'
|
8
|
+
require 'mondido/credit_card/transaction'
|
9
|
+
require 'mondido/credit_card/refund'
|
10
|
+
require 'mondido/credit_card/stored_card'
|
11
|
+
|
12
|
+
class Railtie < Rails::Railtie
|
13
|
+
initializer 'require credentials after rails is initialized' do
|
14
|
+
require 'mondido/credentials'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mondido::CreditCard::Refund do
|
4
|
+
context 'create' do
|
5
|
+
context 'valid' do
|
6
|
+
before(:all) do
|
7
|
+
uri = URI.parse(Mondido::Config::URI + '/refunds')
|
8
|
+
uri.user = Mondido::Credentials::MERCHANT_ID.to_s
|
9
|
+
uri.password = Mondido::Credentials::PASSWORD.to_s
|
10
|
+
json_refund = File.read('spec/stubs/refund.json')
|
11
|
+
@refund_hash = JSON.parse(json_refund)
|
12
|
+
|
13
|
+
stub_request(:post, uri.to_s)
|
14
|
+
.to_return(status: 200, body: json_refund, headers: {})
|
15
|
+
|
16
|
+
@attributes = {
|
17
|
+
transaction_id: 200,
|
18
|
+
amount: '1.00',
|
19
|
+
reason: 'Because of test'
|
20
|
+
}
|
21
|
+
|
22
|
+
@refund = Mondido::CreditCard::Refund.create(@attributes)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'is a CreditCard::Refund' do
|
26
|
+
expect(@refund).to be_an_instance_of(Mondido::CreditCard::Refund)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'has the correct reason' do
|
30
|
+
expect(@refund.reason).to eq(@attributes[:reason])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mondido::CreditCard::StoredCard do
|
4
|
+
context '#get' do
|
5
|
+
context 'valid call' do
|
6
|
+
before(:all) do
|
7
|
+
uri = URI.parse(Mondido::Config::URI + '/stored_cards/300')
|
8
|
+
uri.user = Mondido::Credentials::MERCHANT_ID.to_s
|
9
|
+
uri.password = Mondido::Credentials::PASSWORD.to_s
|
10
|
+
json_stored_card = File.read('spec/stubs/stored_card.json')
|
11
|
+
@stored_card_hash = JSON.parse(json_stored_card)
|
12
|
+
stub_request(:get, uri.to_s)
|
13
|
+
.to_return(status: 200, body: json_stored_card, headers: {})
|
14
|
+
|
15
|
+
@stored_card = Mondido::CreditCard::StoredCard.get(300)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'returns a StoredCard' do
|
19
|
+
expect(@stored_card).to be_an_instance_of(Mondido::CreditCard::StoredCard)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'has the correct currency' do
|
23
|
+
expect(@stored_card.currency).to eq(@stored_card_hash['currency'])
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'has the correct card_type' do
|
27
|
+
expect(@stored_card.card_type).to eq(@stored_card_hash['card_type'])
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'invalid call' do
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context '#create' do
|
38
|
+
before(:all) do
|
39
|
+
uri = URI.parse(Mondido::Config::URI + '/stored_cards')
|
40
|
+
uri.user = Mondido::Credentials::MERCHANT_ID.to_s
|
41
|
+
uri.password = Mondido::Credentials::PASSWORD.to_s
|
42
|
+
json_transaction = File.read('spec/stubs/stored_card.json')
|
43
|
+
@transaction_hash = JSON.parse(json_transaction)
|
44
|
+
|
45
|
+
stub_request(:post, uri.to_s)
|
46
|
+
.to_return(status: 200, body: json_transaction, headers: {})
|
47
|
+
|
48
|
+
@attributes = {
|
49
|
+
currency: 'sek',
|
50
|
+
card_holder: 'Jane Doe',
|
51
|
+
card_number: '4111 1111 1111 1111',
|
52
|
+
card_cvv: 200,
|
53
|
+
card_expiry: '1224'
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'valid' do
|
58
|
+
before(:all) do
|
59
|
+
@stored_card = Mondido::CreditCard::StoredCard.create(@attributes)
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'is a Mondido::CreditCard::StoredCard' do
|
63
|
+
expect(@stored_card).to be_an_instance_of(Mondido::CreditCard::StoredCard)
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'has the correct card_holder' do
|
67
|
+
expect(@stored_card.card_holder).to eq(@attributes[:card_holder])
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'invalid' do
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,193 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mondido::CreditCard::Transaction do
|
4
|
+
|
5
|
+
context 'list transactions' do
|
6
|
+
before(:all) do
|
7
|
+
uri = URI.parse(Mondido::Config::URI + '/transactions')
|
8
|
+
uri.user = Mondido::Credentials::MERCHANT_ID.to_s
|
9
|
+
uri.password = Mondido::Credentials::PASSWORD.to_s
|
10
|
+
json_transactions = File.read('spec/stubs/transactions.json')
|
11
|
+
@transactions_array = JSON.parse(json_transactions)
|
12
|
+
|
13
|
+
stub_request(:get, uri.to_s)
|
14
|
+
.with(body: {limit: '2', offset: '1'})
|
15
|
+
.to_return(status: 200, body: json_transactions, headers: {})
|
16
|
+
|
17
|
+
@transactions = Mondido::CreditCard::Transaction.all(limit: 2, offset: 1)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'lists transactions' do
|
21
|
+
expect(@transactions.first).to be_an_instance_of(Mondido::CreditCard::Transaction)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'get transaction' do
|
26
|
+
before(:all) do
|
27
|
+
uri = URI.parse(Mondido::Config::URI + '/transactions/200')
|
28
|
+
uri.user = Mondido::Credentials::MERCHANT_ID.to_s
|
29
|
+
uri.password = Mondido::Credentials::PASSWORD.to_s
|
30
|
+
json_transaction = File.read('spec/stubs/transaction.json')
|
31
|
+
@transaction_hash = JSON.parse(json_transaction)
|
32
|
+
|
33
|
+
stub_request(:get, uri.to_s)
|
34
|
+
.to_return(status: 200, body: json_transaction, headers: {})
|
35
|
+
|
36
|
+
json_error = {
|
37
|
+
code: 128,
|
38
|
+
name: 'errors.transaction.not_found',
|
39
|
+
description: 'Transaction not found'
|
40
|
+
}
|
41
|
+
uri.path = '/transactions/201'
|
42
|
+
stub_request(:get, uri.to_s)
|
43
|
+
.to_return(status: 404, body: json_error, headers: {})
|
44
|
+
|
45
|
+
@transaction = Mondido::CreditCard::Transaction.get(200)
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'valid call' do
|
49
|
+
|
50
|
+
it 'returns a CreditCard::Transaction' do
|
51
|
+
expect(@transaction).to be_an_instance_of(Mondido::CreditCard::Transaction)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'has the correct amount' do
|
55
|
+
expect(@transaction.amount).to eq(@transaction_hash['amount'])
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'has the correct payment_ref' do
|
59
|
+
expect(@transaction.payment_ref).to eq(@transaction_hash['payment_ref'])
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'invalid call' do
|
65
|
+
before(:all) do
|
66
|
+
|
67
|
+
uri = URI.parse(Mondido::Config::URI + '/transactions/201')
|
68
|
+
uri.user = Mondido::Credentials::MERCHANT_ID.to_s
|
69
|
+
uri.password = Mondido::Credentials::PASSWORD.to_s
|
70
|
+
|
71
|
+
json_error = {
|
72
|
+
code: 128,
|
73
|
+
name: 'errors.transaction.not_found',
|
74
|
+
description: 'Transaction not found'
|
75
|
+
}.to_json
|
76
|
+
stub_request(:get, uri.to_s)
|
77
|
+
.to_return(status: 404, body: json_error, headers: {})
|
78
|
+
|
79
|
+
end
|
80
|
+
it 'raises ApiException errors.transaction.not_found' do
|
81
|
+
expect{
|
82
|
+
Mondido::CreditCard::Transaction.get(201)
|
83
|
+
}.to raise_error(Mondido::Exceptions::ApiException, 'errors.transaction.not_found')
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'create transaction' do
|
90
|
+
|
91
|
+
before(:all) do
|
92
|
+
@attributes = {
|
93
|
+
amount: '1.00',
|
94
|
+
currency: 'sek',
|
95
|
+
card_number: '4111 1111 1111 1111',
|
96
|
+
card_holder: 'Jane Doe',
|
97
|
+
card_cvv: '200',
|
98
|
+
card_expiry: '1120',
|
99
|
+
card_type: 'visa',
|
100
|
+
payment_ref: "#{Time.now.to_i.to_s(16)}#{Time.now.usec.to_s[0,3]}"
|
101
|
+
}
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'invalid transaction' do
|
105
|
+
|
106
|
+
it 'raises ValidationException errors.currency.missing' do
|
107
|
+
expect{
|
108
|
+
Mondido::CreditCard::Transaction.create(@attributes.except(:currency))
|
109
|
+
}.to raise_error(Mondido::Exceptions::ValidationException, 'errors.currency.missing')
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'raises ValidationException errors.amount.missing' do
|
113
|
+
expect{
|
114
|
+
Mondido::CreditCard::Transaction.create(@attributes.except(:amount))
|
115
|
+
}.to raise_error(Mondido::Exceptions::ValidationException, 'errors.amount.missing')
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'raises ValidationException errors.card_number.missing' do
|
119
|
+
expect{
|
120
|
+
Mondido::CreditCard::Transaction.create(@attributes.except(:card_number))
|
121
|
+
}.to raise_error(Mondido::Exceptions::ValidationException, 'errors.card_number.missing')
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'raises ValidationException errors.card_cvv.missing' do
|
125
|
+
expect{
|
126
|
+
Mondido::CreditCard::Transaction.create(@attributes.except(:card_cvv))
|
127
|
+
}.to raise_error(Mondido::Exceptions::ValidationException, 'errors.card_cvv.missing')
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'raises ValidationException errors.card_expiry.missing' do
|
131
|
+
expect{
|
132
|
+
Mondido::CreditCard::Transaction.create(@attributes.except(:card_expiry))
|
133
|
+
}.to raise_error(Mondido::Exceptions::ValidationException, 'errors.card_expiry.missing')
|
134
|
+
end
|
135
|
+
|
136
|
+
it 'raises ValidationException errors.card_type.missing' do
|
137
|
+
expect{
|
138
|
+
Mondido::CreditCard::Transaction.create(@attributes.except(:card_type))
|
139
|
+
}.to raise_error(Mondido::Exceptions::ValidationException, 'errors.card_type.missing')
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'raises ValidationException errors.payment_ref.missing' do
|
143
|
+
expect{
|
144
|
+
Mondido::CreditCard::Transaction.create(@attributes.except(:payment_ref))
|
145
|
+
}.to raise_error(Mondido::Exceptions::ValidationException, 'errors.payment_ref.missing')
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'raises ApiException errors.payment.declined' do
|
149
|
+
uri = URI.parse(Mondido::Config::URI + '/transactions')
|
150
|
+
uri.user = Mondido::Credentials::MERCHANT_ID.to_s
|
151
|
+
uri.password = Mondido::Credentials::PASSWORD.to_s
|
152
|
+
json_transaction = File.read('spec/stubs/transaction.json')
|
153
|
+
stub_request(:post, uri.to_s)
|
154
|
+
.with(body: hash_including({card_cvv: '201'}))
|
155
|
+
.to_return(status: 400, body: '{"name": "errors.payment.declined", "code": 129, "description": "Payment declined" }', headers: {})
|
156
|
+
|
157
|
+
attributes = @attributes.dup
|
158
|
+
attributes[:card_cvv] = '201'
|
159
|
+
|
160
|
+
expect{
|
161
|
+
Mondido::CreditCard::Transaction.create(attributes)
|
162
|
+
}.to raise_error(Mondido::Exceptions::ApiException, 'errors.payment.declined')
|
163
|
+
end
|
164
|
+
|
165
|
+
end
|
166
|
+
|
167
|
+
context 'valid transaction' do
|
168
|
+
|
169
|
+
before(:all) do
|
170
|
+
uri = URI.parse(Mondido::Config::URI + '/transactions')
|
171
|
+
uri.user = Mondido::Credentials::MERCHANT_ID.to_s
|
172
|
+
uri.password = Mondido::Credentials::PASSWORD.to_s
|
173
|
+
json_transaction = File.read('spec/stubs/transaction.json')
|
174
|
+
|
175
|
+
stub_request(:post, uri.to_s)
|
176
|
+
.with(body: hash_including({card_cvv: '200'}))
|
177
|
+
.to_return(status: 200, body: json_transaction, headers: {})
|
178
|
+
|
179
|
+
@transaction = Mondido::CreditCard::Transaction.create(@attributes)
|
180
|
+
end
|
181
|
+
|
182
|
+
it 'is approved' do
|
183
|
+
expect(@transaction.status).to eq('approved')
|
184
|
+
end
|
185
|
+
|
186
|
+
it 'has no errors' do
|
187
|
+
expect(@transaction.errors).to be_empty
|
188
|
+
end
|
189
|
+
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
|
3
|
+
Bundler.setup
|
4
|
+
|
5
|
+
require 'rails'
|
6
|
+
require 'mondido'
|
7
|
+
require 'webmock/rspec'
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
end
|
11
|
+
|
12
|
+
# Monkeymock the config class
|
13
|
+
module Mondido
|
14
|
+
class Credentials
|
15
|
+
MERCHANT_ID = 1
|
16
|
+
SECRET = 'secret'
|
17
|
+
PASSWORD = 'password'
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
{
|
2
|
+
"id" : 300,
|
3
|
+
"currency" : "SEK",
|
4
|
+
"customer" : {
|
5
|
+
"id" : 9
|
6
|
+
},
|
7
|
+
"card_holder" : "Jane Doe",
|
8
|
+
"created_at" : "2014-07-24T14:36:44Z",
|
9
|
+
"expires" : "2015-09-30T23:59:59Z",
|
10
|
+
"token" : "11406212604317313",
|
11
|
+
"merchant_id" : 1,
|
12
|
+
"card_number" : "411111******1111",
|
13
|
+
"ref" : null,
|
14
|
+
"status" : "active",
|
15
|
+
"test" : true
|
16
|
+
}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
{
|
2
|
+
"status" : "approved",
|
3
|
+
"metadata" : null,
|
4
|
+
"amount" : "1.0",
|
5
|
+
"transaction_type" : "credit_card",
|
6
|
+
"card_holder" : "Jane Doe",
|
7
|
+
"subscription" : null,
|
8
|
+
"refunds" : [],
|
9
|
+
"template_id" : null,
|
10
|
+
"currency" : "sek",
|
11
|
+
"stored_card" : null,
|
12
|
+
"merchant_id" : 1,
|
13
|
+
"vat_amount" : null,
|
14
|
+
"error_url" : null,
|
15
|
+
"card_type" : "visa",
|
16
|
+
"payment_ref" : "53e0a9b5177",
|
17
|
+
"id" : 200,
|
18
|
+
"card_number" : "411111******1111",
|
19
|
+
"success_url" : "http://api.localmondido.com:3000/success?locale=sv",
|
20
|
+
"payment_details" : {
|
21
|
+
"id" : 96
|
22
|
+
},
|
23
|
+
"error" : null,
|
24
|
+
"customer" : null,
|
25
|
+
"ref" : null,
|
26
|
+
"cost" : {
|
27
|
+
"percentual_exchange_fee" : "0.035",
|
28
|
+
"percentual_fee" : "0.025",
|
29
|
+
"fixed_fee" : "2.5",
|
30
|
+
"total" : "2.56"
|
31
|
+
},
|
32
|
+
"webhooks" : [],
|
33
|
+
"created_at" : "2014-08-05T09:53:57Z",
|
34
|
+
"test" : false
|
35
|
+
}
|
@@ -0,0 +1,70 @@
|
|
1
|
+
[{
|
2
|
+
"status" : "approved",
|
3
|
+
"metadata" : null,
|
4
|
+
"amount" : "1.0",
|
5
|
+
"transaction_type" : "credit_card",
|
6
|
+
"card_holder" : "Jane Doe",
|
7
|
+
"subscription" : null,
|
8
|
+
"refunds" : [],
|
9
|
+
"template_id" : null,
|
10
|
+
"currency" : "sek",
|
11
|
+
"stored_card" : null,
|
12
|
+
"merchant_id" : 1,
|
13
|
+
"vat_amount" : null,
|
14
|
+
"error_url" : null,
|
15
|
+
"card_type" : "visa",
|
16
|
+
"payment_ref" : "53e0a9b5178",
|
17
|
+
"id" : 201,
|
18
|
+
"card_number" : "411111******1111",
|
19
|
+
"success_url" : "http://api.localmondido.com:3000/success?locale=sv",
|
20
|
+
"payment_details" : {
|
21
|
+
"id" : 96
|
22
|
+
},
|
23
|
+
"error" : null,
|
24
|
+
"customer" : null,
|
25
|
+
"ref" : null,
|
26
|
+
"cost" : {
|
27
|
+
"percentual_exchange_fee" : "0.035",
|
28
|
+
"percentual_fee" : "0.025",
|
29
|
+
"fixed_fee" : "2.5",
|
30
|
+
"total" : "2.56"
|
31
|
+
},
|
32
|
+
"webhooks" : [],
|
33
|
+
"created_at" : "2014-08-05T09:53:57Z",
|
34
|
+
"test" : false
|
35
|
+
},
|
36
|
+
{
|
37
|
+
"status" : "approved",
|
38
|
+
"metadata" : null,
|
39
|
+
"amount" : "1.0",
|
40
|
+
"transaction_type" : "credit_card",
|
41
|
+
"card_holder" : "Jane Doe",
|
42
|
+
"subscription" : null,
|
43
|
+
"refunds" : [],
|
44
|
+
"template_id" : null,
|
45
|
+
"currency" : "sek",
|
46
|
+
"stored_card" : null,
|
47
|
+
"merchant_id" : 1,
|
48
|
+
"vat_amount" : null,
|
49
|
+
"error_url" : null,
|
50
|
+
"card_type" : "visa",
|
51
|
+
"payment_ref" : "53e0a9b5178",
|
52
|
+
"id" : 201,
|
53
|
+
"card_number" : "411111******1111",
|
54
|
+
"success_url" : "http://api.localmondido.com:3000/success?locale=sv",
|
55
|
+
"payment_details" : {
|
56
|
+
"id" : 96
|
57
|
+
},
|
58
|
+
"error" : null,
|
59
|
+
"customer" : null,
|
60
|
+
"ref" : null,
|
61
|
+
"cost" : {
|
62
|
+
"percentual_exchange_fee" : "0.035",
|
63
|
+
"percentual_fee" : "0.025",
|
64
|
+
"fixed_fee" : "2.5",
|
65
|
+
"total" : "2.56"
|
66
|
+
},
|
67
|
+
"webhooks" : [],
|
68
|
+
"created_at" : "2014-08-05T09:53:57Z",
|
69
|
+
"test" : false
|
70
|
+
}]
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mondido
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert Falkén
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
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: webmock
|
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
|
+
description: Library for making payments with Mondido, visit https://mondido.com to
|
56
|
+
sign up for an account.
|
57
|
+
email:
|
58
|
+
- falken@mondido.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- lib/mondido/base_model.rb
|
64
|
+
- lib/mondido/config.rb
|
65
|
+
- lib/mondido/credentials.rb
|
66
|
+
- lib/mondido/credit_card/refund.rb
|
67
|
+
- lib/mondido/credit_card/stored_card.rb
|
68
|
+
- lib/mondido/credit_card/transaction.rb
|
69
|
+
- lib/mondido/exceptions/api_exception.rb
|
70
|
+
- lib/mondido/exceptions/exceptions.rb
|
71
|
+
- lib/mondido/exceptions/validation_exception.rb
|
72
|
+
- lib/mondido/rest_client.rb
|
73
|
+
- lib/mondido/version.rb
|
74
|
+
- lib/mondido.rb
|
75
|
+
- MIT-LICENSE
|
76
|
+
- Rakefile
|
77
|
+
- spec/credit_card/refund_spec.rb
|
78
|
+
- spec/credit_card/stored_card_spec.rb
|
79
|
+
- spec/credit_card/transaction_spec.rb
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
- spec/stubs/refund.json
|
82
|
+
- spec/stubs/stored_card.json
|
83
|
+
- spec/stubs/transaction.json
|
84
|
+
- spec/stubs/transactions.json
|
85
|
+
homepage: https://github.com/Mondido/ruby-sdk
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.0.3
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: SDK for consuming the Mondido API
|
109
|
+
test_files:
|
110
|
+
- spec/credit_card/refund_spec.rb
|
111
|
+
- spec/credit_card/stored_card_spec.rb
|
112
|
+
- spec/credit_card/transaction_spec.rb
|
113
|
+
- spec/spec_helper.rb
|
114
|
+
- spec/stubs/refund.json
|
115
|
+
- spec/stubs/stored_card.json
|
116
|
+
- spec/stubs/transaction.json
|
117
|
+
- spec/stubs/transactions.json
|