From 6d19d215c6b057503f1cd06da7b6f380a2f62e1b Mon Sep 17 00:00:00 2001 From: glorieux Date: Thu, 25 Jul 2013 11:04:01 +0200 Subject: [PATCH 01/36] First draft. Add basic core features: * Authentication: Will use OAuth by deafult. The user will only need to do an setup process to get his client_passphrase. * Users: implemented the methods CREATE, UPDATE, FETCH * Test: add specs for the Users and Clients I will now start implementing all the other methods. --- .gitignore | 5 ++ Gemfile | 2 + lib/mangopay.rb | 120 +++++++++++++++++++++++++++++++ lib/mangopay/bank_detail.rb | 4 ++ lib/mangopay/client.rb | 5 ++ lib/mangopay/errors.rb | 4 ++ lib/mangopay/http_calls.rb | 39 ++++++++++ lib/mangopay/json.rb | 21 ++++++ lib/mangopay/legal_user.rb | 11 +++ lib/mangopay/mangopay_object.rb | 12 ++++ lib/mangopay/natural_user.rb | 11 +++ lib/mangopay/payin.rb | 4 ++ lib/mangopay/payout.rb | 4 ++ lib/mangopay/resource.rb | 18 +++++ lib/mangopay/transaction.rb | 4 ++ lib/mangopay/user.rb | 7 ++ lib/mangopay/version.rb | 3 + lib/mangopay/wallet.rb | 4 ++ mangopay.gemspec | 31 ++++++++ spec/lib/mangopay/client_spec.rb | 23 ++++++ spec/lib/mangopay/user_spec.rb | 91 +++++++++++++++++++++++ spec/spec_helper.rb | 7 ++ 22 files changed, 430 insertions(+) create mode 100644 Gemfile create mode 100644 lib/mangopay.rb create mode 100644 lib/mangopay/bank_detail.rb create mode 100644 lib/mangopay/client.rb create mode 100644 lib/mangopay/errors.rb create mode 100644 lib/mangopay/http_calls.rb create mode 100644 lib/mangopay/json.rb create mode 100644 lib/mangopay/legal_user.rb create mode 100644 lib/mangopay/mangopay_object.rb create mode 100644 lib/mangopay/natural_user.rb create mode 100644 lib/mangopay/payin.rb create mode 100644 lib/mangopay/payout.rb create mode 100644 lib/mangopay/resource.rb create mode 100644 lib/mangopay/transaction.rb create mode 100644 lib/mangopay/user.rb create mode 100644 lib/mangopay/version.rb create mode 100644 lib/mangopay/wallet.rb create mode 100644 mangopay.gemspec create mode 100644 spec/lib/mangopay/client_spec.rb create mode 100644 spec/lib/mangopay/user_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.gitignore b/.gitignore index 560d1a6..d3bf535 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,8 @@ tmp .yardoc _yardoc doc/ + +.DS_Store +.rvmrc +/Gemfile.lock +/mangopay-*.gem diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..3be9c3c --- /dev/null +++ b/Gemfile @@ -0,0 +1,2 @@ +source "https://rubygems.org" +gemspec diff --git a/lib/mangopay.rb b/lib/mangopay.rb new file mode 100644 index 0000000..612ed91 --- /dev/null +++ b/lib/mangopay.rb @@ -0,0 +1,120 @@ +require 'net/http' +require 'rest_client' +require 'multi_json' + +# Version +require 'mangopay/version' + +# JSON +require 'mangopay/json' + +# Resources +require 'mangopay/http_calls' +require 'mangopay/resource' +require 'mangopay/client' +require 'mangopay/user' +require 'mangopay/natural_user' +require 'mangopay/legal_user' +require 'mangopay/payin' +require 'mangopay/payout' +require 'mangopay/transaction' +require 'mangopay/wallet' +require 'mangopay/bank_detail' + +# Errors +require 'mangopay/errors' + +module MangoPay + + class Configuration + attr_accessor :root_url, :client_id, :client_passphrase, :preproduction + + def preproduction + @preproduction || false + end + + def root_url + @root_url || (@preproduction == true ? "https://mangopay-api-inte.leetchi.com" : "https://api.leetchi.com") + end + end + + class << self + attr_accessor :configuration + end + + def self.configure + self.configuration ||= Configuration.new + yield configuration + end + + def self.api_uri(url='') + URI(configuration.root_url + url) + end + + def self.request(method, url, params={}, headers={}) + uri = api_uri(url) + + res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| + request = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, request_headers) + puts request.uri + puts request.path + request.body = MangoPay::JSON.dump(params) + http.request request + end + puts res.body + puts MangoPay::JSON.load(res.body) + MangoPay::JSON.load(res.body) + # # RestClient::Request.execute(opts) + # rescue RestClient::ExceptionWithResponse => e + # puts e.http_code + # puts e.http_body + # end + end + + def self.user_agent + @uname ||= get_uname + + { + bindings_version: MangoPay::VERSION, + lang: 'ruby', + lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})", + platform: RUBY_PLATFORM, + uname: @uname + } + end + + def self.get_uname + `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i + rescue Errno::ENOMEM => ex + "uname lookup failed" + end + + def self.get_oauth_token + uri = api_uri('/api/oauth/token') + res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| + req = Net::HTTP::Post.new(uri.request_uri) + req.basic_auth configuration.client_id, configuration.client_passphrase + req.body = 'grant_type=client_credentials' + http.request req + end + MangoPay::JSON.load(res.body) + end + + def self.oauth_token + oauth = get_oauth_token + "#{oauth['token_type']} #{oauth['access_token']}" + end + + def self.request_headers + headers = { + 'user_agent' => "MangoPay V1 RubyBindings/#{MangoPay::VERSION}", + 'Authorization' => oauth_token, + 'Content-Type' => 'application/json' + } + begin + headers.update('x_mangopay_client_user_agent' => MangoPay::JSON.dump(user_agent)) + rescue => e + headers.update('x_mangopay_client_raw_user_agent' => user_agent.inspect, error: "#{e} (#{e.class})") + end + end +end diff --git a/lib/mangopay/bank_detail.rb b/lib/mangopay/bank_detail.rb new file mode 100644 index 0000000..2720cc5 --- /dev/null +++ b/lib/mangopay/bank_detail.rb @@ -0,0 +1,4 @@ +module MangoPay + class BankDetail < Resource + end +end diff --git a/lib/mangopay/client.rb b/lib/mangopay/client.rb new file mode 100644 index 0000000..d379dfc --- /dev/null +++ b/lib/mangopay/client.rb @@ -0,0 +1,5 @@ +module MangoPay + class Client < Resource + include MangoPay::HTTPCalls::Create + end +end diff --git a/lib/mangopay/errors.rb b/lib/mangopay/errors.rb new file mode 100644 index 0000000..bfa135e --- /dev/null +++ b/lib/mangopay/errors.rb @@ -0,0 +1,4 @@ +module MangoPay + class Errors + end +end diff --git a/lib/mangopay/http_calls.rb b/lib/mangopay/http_calls.rb new file mode 100644 index 0000000..2477257 --- /dev/null +++ b/lib/mangopay/http_calls.rb @@ -0,0 +1,39 @@ +module MangoPay + module HTTPCalls + module Create + module ClassMethods + def create(params={}) + response = MangoPay.request(:post, self.url, params) + end + end + + def self.included(base) + base.extend(ClassMethods) + end + end + + module Update + module ClassMethods + def update(id, params = {}) + response = MangoPay.request(:put, url(id), params) + end + end + + def self.included(base) + base.extend(ClassMethods) + end + end + + module Fetch + module ClassMethods + def fetch(id = nil) + response = MangoPay.request(:get, url(id)) + end + end + + def self.included(base) + base.extend(ClassMethods) + end + end + end +end diff --git a/lib/mangopay/json.rb b/lib/mangopay/json.rb new file mode 100644 index 0000000..7cffe8e --- /dev/null +++ b/lib/mangopay/json.rb @@ -0,0 +1,21 @@ +module MangoPay + module JSON + if MultiJson.respond_to?(:dump) + def self.dump(*args) + MultiJson.dump(*args) + end + + def self.load(*args) + MultiJson.load(*args) + end + else + def self.dump(*args) + MultiJson.encode(*args) + end + + def self.load(*args) + MultiJson.decode(*args) + end + end + end +end diff --git a/lib/mangopay/legal_user.rb b/lib/mangopay/legal_user.rb new file mode 100644 index 0000000..e0c2e57 --- /dev/null +++ b/lib/mangopay/legal_user.rb @@ -0,0 +1,11 @@ +module MangoPay + class LegalUser < User + def self.url(id = nil) + if id + "/v2/#{MangoPay.configuration.client_id}/users/legals/#{CGI.escape(id)}" + else + "/v2/#{MangoPay.configuration.client_id}/users/legals" + end + end + end +end diff --git a/lib/mangopay/mangopay_object.rb b/lib/mangopay/mangopay_object.rb new file mode 100644 index 0000000..5d8e844 --- /dev/null +++ b/lib/mangopay/mangopay_object.rb @@ -0,0 +1,12 @@ +module Mangopay + class MangoPayObject + include Enumerable + + def initialize + end + + def metaclass + class << self; self; end + end + end +end diff --git a/lib/mangopay/natural_user.rb b/lib/mangopay/natural_user.rb new file mode 100644 index 0000000..ffec613 --- /dev/null +++ b/lib/mangopay/natural_user.rb @@ -0,0 +1,11 @@ +module MangoPay + class NaturalUser < User + def self.url(id = nil) + if id + "/v2/#{MangoPay.configuration.client_id}/users/naturals/#{CGI.escape(id)}" + else + "/v2/#{MangoPay.configuration.client_id}/users/naturals" + end + end + end +end diff --git a/lib/mangopay/payin.rb b/lib/mangopay/payin.rb new file mode 100644 index 0000000..719bbf4 --- /dev/null +++ b/lib/mangopay/payin.rb @@ -0,0 +1,4 @@ +module MangoPay + class PayIn < Resource + end +end diff --git a/lib/mangopay/payout.rb b/lib/mangopay/payout.rb new file mode 100644 index 0000000..75b5816 --- /dev/null +++ b/lib/mangopay/payout.rb @@ -0,0 +1,4 @@ +module MangoPay + class PayOut < Resource + end +end diff --git a/lib/mangopay/resource.rb b/lib/mangopay/resource.rb new file mode 100644 index 0000000..f69eb4c --- /dev/null +++ b/lib/mangopay/resource.rb @@ -0,0 +1,18 @@ +module MangoPay + class Resource + def self.class_name + self.name.split('::')[-1] + end + + def self.url(id = nil) + if self == Resource + raise NotImplementedError.new('Resource is an abstract class. Do not use it directly.') + end + if id + "/v2/#{MangoPay.configuration.client_id}/#{CGI.escape(class_name.downcase)}s/#{CGI.escape(id)}" + else + "/v2/#{MangoPay.configuration.client_id}/#{CGI.escape(class_name.downcase)}s" + end + end + end +end diff --git a/lib/mangopay/transaction.rb b/lib/mangopay/transaction.rb new file mode 100644 index 0000000..e332921 --- /dev/null +++ b/lib/mangopay/transaction.rb @@ -0,0 +1,4 @@ +module MangoPay + class Transaction < Resource + end +end diff --git a/lib/mangopay/user.rb b/lib/mangopay/user.rb new file mode 100644 index 0000000..0f14b32 --- /dev/null +++ b/lib/mangopay/user.rb @@ -0,0 +1,7 @@ +module MangoPay + class User < Resource + include MangoPay::HTTPCalls::Create + include MangoPay::HTTPCalls::Update + include MangoPay::HTTPCalls::Fetch + end +end diff --git a/lib/mangopay/version.rb b/lib/mangopay/version.rb new file mode 100644 index 0000000..b411005 --- /dev/null +++ b/lib/mangopay/version.rb @@ -0,0 +1,3 @@ +module MangoPay + VERSION = '3.0.0' +end diff --git a/lib/mangopay/wallet.rb b/lib/mangopay/wallet.rb new file mode 100644 index 0000000..dc1622f --- /dev/null +++ b/lib/mangopay/wallet.rb @@ -0,0 +1,4 @@ +module MangoPay + class Wallet < Resource + end +end diff --git a/mangopay.gemspec b/mangopay.gemspec new file mode 100644 index 0000000..6e8aa64 --- /dev/null +++ b/mangopay.gemspec @@ -0,0 +1,31 @@ +$:.unshift(File.join(File.dirname(__FILE__), 'lib')) + +require 'mangopay/version' + +Gem::Specification.new do |s| + s.name = 'mangopay' + s.version = MangoPay::VERSION + s.summary = "Ruby bindings for the version 2 of the MangoPay API" + s.description = <<-EOF + The mangopay Gem makes interacting with MangoPay Services much easier. + For any questions regarding the use of MangoPay's Services feel free to contact us at http://www.mangopay.com/contact-us/ + You can find more documentation about MangoPay Services at http://www.mangopay.com/ + EOF + s.authors = ['Geoffroy Lorieux'] + s.email = 'it-support@mangopay.com' + s.homepage = 'http://www.mangopay.com/' + s.license = 'MIT' + + s.required_ruby_version = '>= 1.9.2' + + s.add_dependency('multi_json', '~> 1.7.7') + s.add_dependency('rest-client', '~> 1.6.7') + + s.add_development_dependency('rake', '~> 10.1.0') + s.add_development_dependency('rspec', '~> 2.14.1') + + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- spec/*`.split("\n") + s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + s.require_paths = ['lib'] +end diff --git a/spec/lib/mangopay/client_spec.rb b/spec/lib/mangopay/client_spec.rb new file mode 100644 index 0000000..7de36c8 --- /dev/null +++ b/spec/lib/mangopay/client_spec.rb @@ -0,0 +1,23 @@ +require_relative '../../spec_helper' + +describe MangoPay::Client do + + let(:new_client) { + MangoPay::Client.create({ + 'ClientID' => MangoPay.configuration.client_id, + 'Name' => 'What a nice name' + }) + } + + describe 'CREATE' do + it 'creates a new client' do + expect(new_client['ClientID']).to eq(MangoPay.configuration.client_id) + expect(new_client['Passphrase']).not_to be_nil + end + + it "ClientID_already_exist" do + expect(new_client['Type']).to eq('ClientID_already_exist') + expect(new_client['Message']).to eq('A partner with this ClientID already exist') + end + end +end diff --git a/spec/lib/mangopay/user_spec.rb b/spec/lib/mangopay/user_spec.rb new file mode 100644 index 0000000..f10ebab --- /dev/null +++ b/spec/lib/mangopay/user_spec.rb @@ -0,0 +1,91 @@ +require_relative '../../spec_helper' + +describe MangoPay::User do + + let(:new_natural_user) { + MangoPay::NaturalUser.create({ + Tag: 'test', + Email: 'my@email.com', + FirstName: 'John', + LastName: 'Doe', + Address: 'Here', + Birthday: '', + Birthplace: 'Paris', + Nationality: 'FR', + CountryOfResidence: 'FR', + Occupation: 'Worker', + IncomeRange: 1 + }) + } + + let(:new_legal_user) { + MangoPay::LegalUser.create({ + Name: 'Super', + LegalPersonType: 'BUSINESS', + HeadquartersAddress: 'Here', + LegalRepresentativeFirstName: 'John', + LegalRepresentativeLastName: 'Doe', + LegalRepresentativeAdress: 'Here', + LegalRepresentativeEmail: 'john@doe.com', + LegalRepresentativeBirthday: '', + LegalRepresentativeNationality: 'FR', + LegalRepresentativeCountryOfResidence: 'FR', + Statute: '', + ProofOfRegistration: '', + ShareholderDeclaration: '' + }) + } + + describe 'CREATE' do + it 'creates a new natural user' do + expect(new_natural_user["FirstName"]).to eq('John') + end + + it 'creates a new legal user' do + expect(new_legal_user["LegalRepresentativeFirstName"]).to eq('John') + end + end + + describe 'UPDATE' do + it 'updates a natural user' do + updated_user = MangoPay::NaturalUser.update(new_natural_user['Id'] ,{ + FirstName: 'Jack' + }) + expect(updated_user['FirstName']).to eq('Jack') + end + + it 'updates a legal user' do + updated_user = MangoPay::LegalUser.update(new_legal_user['Id'], { + LegalRepresentativeFirstName: 'Jack' + }) + expect(updated_user['LegalRepresentativeFirstName']).to eq('Jack') + end + end + + describe 'FETCH' do + it 'fetches all the users' do + users = MangoPay::User.fetch() + expect(users).to be_kind_of(Array) + end + + it 'fetches a legal user using the User module' do + legal_user = MangoPay::User.fetch(new_legal_user['Id']) + expect(legal_user['Id']).to eq(new_legal_user['Id']) + end + + it 'fetches a natural user using the User module' do + natural_user = MangoPay::User.fetch(new_natural_user['Id']) + expect(natural_user['Id']).to eq(new_natural_user['Id']) + end + + it 'fetches a legal user' do + user = MangoPay::LegalUser.fetch(new_legal_user['Id']) + expect(user['Id']).to eq(new_legal_user['Id']) + end + + it 'fetches a natural user' do + user = MangoPay::NaturalUser.fetch(new_natural_user['Id']) + expect(user['Id']).to eq(new_natural_user['Id']) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..4345bd4 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,7 @@ +require_relative '../lib/mangopay' + +MangoPay.configure do |c| + c.preproduction = true + c.client_id = 'example' + c.client_passphrase = 'uyWsmnwMQyTnqKgi8Y35A3eVB7bGhqrebYqA1tL6x2vYNpGPiY' +end From 3c6ceb3b80c44597f7e9b8439a8e7428503d5b80 Mon Sep 17 00:00:00 2001 From: glorieux Date: Thu, 25 Jul 2013 11:36:37 +0200 Subject: [PATCH 02/36] Add Wallet. Add Wallets specs and resources. --- lib/mangopay/wallet.rb | 7 ++-- spec/lib/mangopay/wallet_spec.rb | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 spec/lib/mangopay/wallet_spec.rb diff --git a/lib/mangopay/wallet.rb b/lib/mangopay/wallet.rb index dc1622f..07c7ed1 100644 --- a/lib/mangopay/wallet.rb +++ b/lib/mangopay/wallet.rb @@ -1,4 +1,7 @@ module MangoPay - class Wallet < Resource - end + class Wallet < Resource + include MangoPay::HTTPCalls::Create + include MangoPay::HTTPCalls::Update + include MangoPay::HTTPCalls::Fetch + end end diff --git a/spec/lib/mangopay/wallet_spec.rb b/spec/lib/mangopay/wallet_spec.rb new file mode 100644 index 0000000..e6cb69d --- /dev/null +++ b/spec/lib/mangopay/wallet_spec.rb @@ -0,0 +1,55 @@ +require_relative '../../spec_helper' + +describe MangoPay::Wallet do + + let(:new_natural_user) { + MangoPay::NaturalUser.create({ + Tag: 'test', + Email: 'my@email.com', + FirstName: 'John', + LastName: 'Doe', + Address: 'Here', + Birthday: '', + Birthplace: 'Paris', + Nationality: 'FR', + CountryOfResidence: 'FR', + Occupation: 'Worker', + IncomeRange: 1 + }) + } + + let(:new_wallet) { + MangoPay::Wallet.create({ + Owners: [new_natural_user['Id']], + Description: 'A test wallet', + Currency: 'EUR', + Tag: 'Test Time' + }) + } + + describe 'CREATE' do + it 'creates a wallet' do + expect(new_wallet['Id']).to_not be_nil + expect(new_wallet['Balance']['Currency']).to eq('EUR') + expect(new_wallet['Balance']['Amount']).to eq(0) + end + end + + describe 'UPDATE' do + it 'updates a wallet' do + updated_wallet = MangoPay::Wallet.update(new_wallet['Id'], { + Description: 'Updated Description', + Tag: 'Updated Tag' + }) + expect(updated_wallet['Description']).to eq('Updated Description') + expect(updated_wallet['Tag']).to eq('Updated Tag') + end + end + + describe 'FETCH' do + it 'fetches a wallet' do + wallet = MangoPay::Wallet.fetch(new_wallet['Id']) + expect(wallet['Id']).to eq(new_wallet['Id']) + end + end +end From 2e7768d3f41941cdd5ad20f045a54cc091da6835 Mon Sep 17 00:00:00 2001 From: glorieux Date: Thu, 25 Jul 2013 12:24:43 +0200 Subject: [PATCH 03/36] Transaction support + refactoring Remove RestClient references Add filters to http_calls Add Transaction resources and tests Refactor the rspec lets and move them to a shared set of resources --- lib/mangopay.rb | 5 --- lib/mangopay/http_calls.rb | 4 +- lib/mangopay/transaction.rb | 9 ++++- spec/lib/mangopay/client_spec.rb | 8 +--- spec/lib/mangopay/shared_resources.rb | 58 +++++++++++++++++++++++++++ spec/lib/mangopay/transaction_spec.rb | 14 +++++++ spec/lib/mangopay/user_spec.rb | 35 +--------------- spec/lib/mangopay/wallet_spec.rb | 27 +------------ spec/spec_helper.rb | 1 + 9 files changed, 86 insertions(+), 75 deletions(-) create mode 100644 spec/lib/mangopay/shared_resources.rb create mode 100644 spec/lib/mangopay/transaction_spec.rb diff --git a/lib/mangopay.rb b/lib/mangopay.rb index 612ed91..4659996 100644 --- a/lib/mangopay.rb +++ b/lib/mangopay.rb @@ -64,11 +64,6 @@ def self.request(method, url, params={}, headers={}) puts res.body puts MangoPay::JSON.load(res.body) MangoPay::JSON.load(res.body) - # # RestClient::Request.execute(opts) - # rescue RestClient::ExceptionWithResponse => e - # puts e.http_code - # puts e.http_body - # end end def self.user_agent diff --git a/lib/mangopay/http_calls.rb b/lib/mangopay/http_calls.rb index 2477257..a773202 100644 --- a/lib/mangopay/http_calls.rb +++ b/lib/mangopay/http_calls.rb @@ -26,8 +26,8 @@ def self.included(base) module Fetch module ClassMethods - def fetch(id = nil) - response = MangoPay.request(:get, url(id)) + def fetch(id = nil, filters = {}) + response = MangoPay.request(:get, url(id), filters) end end diff --git a/lib/mangopay/transaction.rb b/lib/mangopay/transaction.rb index e332921..b4423c3 100644 --- a/lib/mangopay/transaction.rb +++ b/lib/mangopay/transaction.rb @@ -1,4 +1,9 @@ module MangoPay - class Transaction < Resource - end + class Transaction < Resource + include MangoPay::HTTPCalls::Fetch + + def self.url(id) + "/v2/#{MangoPay.configuration.client_id}/wallets/#{CGI.escape(id)}/transactions" + end + end end diff --git a/spec/lib/mangopay/client_spec.rb b/spec/lib/mangopay/client_spec.rb index 7de36c8..e5f661c 100644 --- a/spec/lib/mangopay/client_spec.rb +++ b/spec/lib/mangopay/client_spec.rb @@ -1,13 +1,7 @@ require_relative '../../spec_helper' describe MangoPay::Client do - - let(:new_client) { - MangoPay::Client.create({ - 'ClientID' => MangoPay.configuration.client_id, - 'Name' => 'What a nice name' - }) - } + include_context 'clients' describe 'CREATE' do it 'creates a new client' do diff --git a/spec/lib/mangopay/shared_resources.rb b/spec/lib/mangopay/shared_resources.rb new file mode 100644 index 0000000..398d77a --- /dev/null +++ b/spec/lib/mangopay/shared_resources.rb @@ -0,0 +1,58 @@ +shared_context 'clients' do + + let(:new_client) { + MangoPay::Client.create({ + 'ClientID' => MangoPay.configuration.client_id, + 'Name' => 'What a nice name' + }) + } +end + +shared_context 'users' do + + let(:new_natural_user) { + MangoPay::NaturalUser.create({ + Tag: 'test', + Email: 'my@email.com', + FirstName: 'John', + LastName: 'Doe', + Address: 'Here', + Birthday: '', + Birthplace: 'Paris', + Nationality: 'FR', + CountryOfResidence: 'FR', + Occupation: 'Worker', + IncomeRange: 1 + }) + } + + let(:new_legal_user) { + MangoPay::LegalUser.create({ + Name: 'Super', + LegalPersonType: 'BUSINESS', + HeadquartersAddress: 'Here', + LegalRepresentativeFirstName: 'John', + LegalRepresentativeLastName: 'Doe', + LegalRepresentativeAdress: 'Here', + LegalRepresentativeEmail: 'john@doe.com', + LegalRepresentativeBirthday: '', + LegalRepresentativeNationality: 'FR', + LegalRepresentativeCountryOfResidence: 'FR', + Statute: '', + ProofOfRegistration: '', + ShareholderDeclaration: '' + }) + } +end + +shared_context 'wallets' do + + let(:new_wallet) { + MangoPay::Wallet.create({ + Owners: [new_natural_user['Id']], + Description: 'A test wallet', + Currency: 'EUR', + Tag: 'Test Time' + }) + } +end diff --git a/spec/lib/mangopay/transaction_spec.rb b/spec/lib/mangopay/transaction_spec.rb new file mode 100644 index 0000000..aa61ee8 --- /dev/null +++ b/spec/lib/mangopay/transaction_spec.rb @@ -0,0 +1,14 @@ +require_relative '../../spec_helper' + +describe MangoPay::Transaction do + include_context 'users' + include_context 'wallets' + + describe 'FETCH' do + it 'fetches the an empty list of transactions' do + transactions = MangoPay::Transaction.fetch(new_wallet['Id']) + expect(transactions).to be_kind_of(Array) + expect(transactions).to be_empty + end + end +end diff --git a/spec/lib/mangopay/user_spec.rb b/spec/lib/mangopay/user_spec.rb index f10ebab..c567ec9 100644 --- a/spec/lib/mangopay/user_spec.rb +++ b/spec/lib/mangopay/user_spec.rb @@ -1,40 +1,7 @@ require_relative '../../spec_helper' describe MangoPay::User do - - let(:new_natural_user) { - MangoPay::NaturalUser.create({ - Tag: 'test', - Email: 'my@email.com', - FirstName: 'John', - LastName: 'Doe', - Address: 'Here', - Birthday: '', - Birthplace: 'Paris', - Nationality: 'FR', - CountryOfResidence: 'FR', - Occupation: 'Worker', - IncomeRange: 1 - }) - } - - let(:new_legal_user) { - MangoPay::LegalUser.create({ - Name: 'Super', - LegalPersonType: 'BUSINESS', - HeadquartersAddress: 'Here', - LegalRepresentativeFirstName: 'John', - LegalRepresentativeLastName: 'Doe', - LegalRepresentativeAdress: 'Here', - LegalRepresentativeEmail: 'john@doe.com', - LegalRepresentativeBirthday: '', - LegalRepresentativeNationality: 'FR', - LegalRepresentativeCountryOfResidence: 'FR', - Statute: '', - ProofOfRegistration: '', - ShareholderDeclaration: '' - }) - } + include_context 'users' describe 'CREATE' do it 'creates a new natural user' do diff --git a/spec/lib/mangopay/wallet_spec.rb b/spec/lib/mangopay/wallet_spec.rb index e6cb69d..a8fe7c9 100644 --- a/spec/lib/mangopay/wallet_spec.rb +++ b/spec/lib/mangopay/wallet_spec.rb @@ -1,31 +1,8 @@ require_relative '../../spec_helper' describe MangoPay::Wallet do - - let(:new_natural_user) { - MangoPay::NaturalUser.create({ - Tag: 'test', - Email: 'my@email.com', - FirstName: 'John', - LastName: 'Doe', - Address: 'Here', - Birthday: '', - Birthplace: 'Paris', - Nationality: 'FR', - CountryOfResidence: 'FR', - Occupation: 'Worker', - IncomeRange: 1 - }) - } - - let(:new_wallet) { - MangoPay::Wallet.create({ - Owners: [new_natural_user['Id']], - Description: 'A test wallet', - Currency: 'EUR', - Tag: 'Test Time' - }) - } + include_context 'users' + include_context 'wallets' describe 'CREATE' do it 'creates a wallet' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4345bd4..c8da341 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,5 @@ require_relative '../lib/mangopay' +require_relative './lib/mangopay/shared_resources' MangoPay.configure do |c| c.preproduction = true From 3dbcb4ecd19436fdc789f3f0e230b97f6d1fc63e Mon Sep 17 00:00:00 2001 From: glorieux Date: Thu, 25 Jul 2013 14:45:01 +0200 Subject: [PATCH 04/36] Add Bank Details Add BankDetails resource and test. Update the create http calls to accept ids. --- lib/mangopay.rb | 1 + lib/mangopay/bank_detail.rb | 19 +++++++++++++++++-- lib/mangopay/http_calls.rb | 8 +++++--- spec/lib/mangopay/bank_detail_spec.rb | 26 ++++++++++++++++++++++++++ spec/lib/mangopay/shared_resources.rb | 14 ++++++++++++++ 5 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 spec/lib/mangopay/bank_detail_spec.rb diff --git a/lib/mangopay.rb b/lib/mangopay.rb index 4659996..516a0b5 100644 --- a/lib/mangopay.rb +++ b/lib/mangopay.rb @@ -56,6 +56,7 @@ def self.request(method, url, params={}, headers={}) res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| request = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, request_headers) + puts method puts request.uri puts request.path request.body = MangoPay::JSON.dump(params) diff --git a/lib/mangopay/bank_detail.rb b/lib/mangopay/bank_detail.rb index 2720cc5..bc63c3e 100644 --- a/lib/mangopay/bank_detail.rb +++ b/lib/mangopay/bank_detail.rb @@ -1,4 +1,19 @@ module MangoPay - class BankDetail < Resource - end + class BankDetail < Resource + include MangoPay::HTTPCalls::Create + include MangoPay::HTTPCalls::Fetch + + def self.fetch(*ids) + url = ids.length == 1 ? url(ids[0]) : url(ids[0], ids[1]) + response = MangoPay.request(:get, url) + end + + def self.url(*id) + if id.length == 1 + "/v2/#{MangoPay.configuration.client_id}/users/#{CGI.escape(id[0])}/bank-details" + else + "/v2/#{MangoPay.configuration.client_id}/users/#{CGI.escape(id[0])}/bank-details/#{CGI.escape(id[1])}" + end + end + end end diff --git a/lib/mangopay/http_calls.rb b/lib/mangopay/http_calls.rb index a773202..f489c7d 100644 --- a/lib/mangopay/http_calls.rb +++ b/lib/mangopay/http_calls.rb @@ -2,8 +2,10 @@ module MangoPay module HTTPCalls module Create module ClassMethods - def create(params={}) - response = MangoPay.request(:post, self.url, params) + + def create(*id, params) + id = id.empty? ? nil : id[0] + response = MangoPay.request(:post, self.url(id), params) end end @@ -14,7 +16,7 @@ def self.included(base) module Update module ClassMethods - def update(id, params = {}) + def update(id = nil, params = {}) response = MangoPay.request(:put, url(id), params) end end diff --git a/spec/lib/mangopay/bank_detail_spec.rb b/spec/lib/mangopay/bank_detail_spec.rb new file mode 100644 index 0000000..4575d70 --- /dev/null +++ b/spec/lib/mangopay/bank_detail_spec.rb @@ -0,0 +1,26 @@ +require_relative '../../spec_helper' + +describe MangoPay::BankDetail do + include_context 'users' + include_context 'bank_details' + + describe 'CREATE' do + it 'creates a new bank detail' do + expect(new_iban_bank_detail['Id']).not_to be_nil + end + end + + describe 'FETCH' do + + it 'fetches all the bank details' do + bank_details = MangoPay::BankDetail.fetch(new_iban_bank_detail['UserId']) + expect(bank_details).to be_kind_of(Array) + expect(bank_details[0]['Id']).to eq(new_iban_bank_detail['Id']) + end + + it 'fetches one bank detail' do + bank_detail = MangoPay::BankDetail.fetch(new_iban_bank_detail['UserId'], new_iban_bank_detail['Id']) + expect(bank_detail['Id']).to eq(new_iban_bank_detail['Id']) + end + end +end diff --git a/spec/lib/mangopay/shared_resources.rb b/spec/lib/mangopay/shared_resources.rb index 398d77a..3cc5e62 100644 --- a/spec/lib/mangopay/shared_resources.rb +++ b/spec/lib/mangopay/shared_resources.rb @@ -56,3 +56,17 @@ }) } end + +shared_context 'bank_details' do + + let(:new_iban_bank_detail) { + MangoPay::BankDetail.create(new_natural_user['Id'], { + Type: 'IBAN', + OwnerName: 'John', + OwnerAddress: 'Here', + IBAN: 'FR76 1790 6000 3200 0833 5232 973', + BIC: 'AGRIFRPP879', + Tag: 'Test Time' + }) + } +end From ec6c3593938fc93259035ec26ef6854b90f08a46 Mon Sep 17 00:00:00 2001 From: glorieux Date: Thu, 25 Jul 2013 15:58:03 +0200 Subject: [PATCH 05/36] Add PayIn + Refactoring Add PayIn resources + tests Remove debug flags Remove unnecessary call to self --- lib/mangopay.rb | 1 - lib/mangopay/http_calls.rb | 2 +- lib/mangopay/payin.rb | 15 +++++++++++-- spec/lib/mangopay/payin_spec.rb | 20 +++++++++++++++++ spec/lib/mangopay/shared_resources.rb | 32 +++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 spec/lib/mangopay/payin_spec.rb diff --git a/lib/mangopay.rb b/lib/mangopay.rb index 516a0b5..bfe3640 100644 --- a/lib/mangopay.rb +++ b/lib/mangopay.rb @@ -62,7 +62,6 @@ def self.request(method, url, params={}, headers={}) request.body = MangoPay::JSON.dump(params) http.request request end - puts res.body puts MangoPay::JSON.load(res.body) MangoPay::JSON.load(res.body) end diff --git a/lib/mangopay/http_calls.rb b/lib/mangopay/http_calls.rb index f489c7d..5574013 100644 --- a/lib/mangopay/http_calls.rb +++ b/lib/mangopay/http_calls.rb @@ -5,7 +5,7 @@ module ClassMethods def create(*id, params) id = id.empty? ? nil : id[0] - response = MangoPay.request(:post, self.url(id), params) + response = MangoPay.request(:post, url(id), params) end end diff --git a/lib/mangopay/payin.rb b/lib/mangopay/payin.rb index 719bbf4..9b53bf8 100644 --- a/lib/mangopay/payin.rb +++ b/lib/mangopay/payin.rb @@ -1,4 +1,15 @@ module MangoPay - class PayIn < Resource - end + class PayIn < Resource + include MangoPay::HTTPCalls::Fetch + + module Card + class Web < Resource + include MangoPay::HTTPCalls::Create + + def self.url(id = nil) + "/v2/#{MangoPay.configuration.client_id}/payins/card/#{CGI.escape(class_name.downcase)}" + end + end + end + end end diff --git a/spec/lib/mangopay/payin_spec.rb b/spec/lib/mangopay/payin_spec.rb new file mode 100644 index 0000000..4789b43 --- /dev/null +++ b/spec/lib/mangopay/payin_spec.rb @@ -0,0 +1,20 @@ +require_relative '../../spec_helper' + +describe MangoPay::PayIn::Card::Web do + include_context 'users' + include_context 'wallets' + include_context 'cards' + + describe 'CREATE' do + it 'creates a card' do + expect(new_web_card['RedirectURL']).not_to be_empty + end + end + + describe 'FETCH' do + it 'fetches a payin' do + payin = MangoPay::PayIn.fetch(new_web_card['Id']) + expect(payin['Id']).to eq(new_web_card['Id']) + end + end +end diff --git a/spec/lib/mangopay/shared_resources.rb b/spec/lib/mangopay/shared_resources.rb index 3cc5e62..5a174b4 100644 --- a/spec/lib/mangopay/shared_resources.rb +++ b/spec/lib/mangopay/shared_resources.rb @@ -70,3 +70,35 @@ }) } end + +shared_context 'cards' do + + let(:new_web_card) { + puts new_natural_user + puts new_wallet + puts "Hey" + puts({ + AuthorId: new_natural_user['Id'], + CreditedUserId: new_wallet['Owners'][0], + DebitedFunds: { Currency: 'EUR', Amount: 1000 }, + Fees: { Currency: 'EUR', Amount: 0}, + CreditedWalletId: new_wallet['Id'], + ReturnURL: 'http://dev.leetchi.com', + CardType: 'CB_VISA_MASTERCARD', + Culture: 'FR', + Tag: 'Test Card' + }) + puts "bye" + MangoPay::PayIn::Card::Web.create({ + AuthorId: new_natural_user['Id'], + CreditedUserId: new_wallet['Owners'][0], + DebitedFunds: { Currency: 'EUR', Amount: 1000 }, + Fees: { Currency: 'EUR', Amount: 0}, + CreditedWalletId: new_wallet['Id'], + ReturnURL: 'http://dev.leetchi.com', + CardType: 'CB_VISA_MASTERCARD', + Culture: 'FR', + Tag: 'Test Card' + }) + } +end From f18e83ff8d6be3ae2fd9711a4ca3df9179051da0 Mon Sep 17 00:00:00 2001 From: glorieux Date: Thu, 25 Jul 2013 16:18:06 +0200 Subject: [PATCH 06/36] Improve Client test suite The client test suite now test for the following cases: - Create a new client (using a random client id) - ClientID too long - Existing ClientID --- spec/lib/mangopay/client_spec.rb | 15 ++++++++++++--- spec/lib/mangopay/shared_resources.rb | 17 ++++++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/spec/lib/mangopay/client_spec.rb b/spec/lib/mangopay/client_spec.rb index e5f661c..f7c2a00 100644 --- a/spec/lib/mangopay/client_spec.rb +++ b/spec/lib/mangopay/client_spec.rb @@ -1,17 +1,26 @@ require_relative '../../spec_helper' +require 'securerandom' describe MangoPay::Client do include_context 'clients' describe 'CREATE' do it 'creates a new client' do - expect(new_client['ClientID']).to eq(MangoPay.configuration.client_id) + expect(new_client['ClientID']).to eq(client_id) expect(new_client['Passphrase']).not_to be_nil end + it 'refuses the client id' do + expect(wrong_client['errors']).not_to be_nil + end + it "ClientID_already_exist" do - expect(new_client['Type']).to eq('ClientID_already_exist') - expect(new_client['Message']).to eq('A partner with this ClientID already exist') + existing_client = MangoPay::Client.create({ + 'ClientID' => new_client['ClientID'], + 'Name' => 'What a nice name' + }) + expect(existing_client['Type']).to eq('ClientID_already_exist') + expect(existing_client['Message']).to eq('A partner with this ClientID already exist') end end end diff --git a/spec/lib/mangopay/shared_resources.rb b/spec/lib/mangopay/shared_resources.rb index 5a174b4..80e9872 100644 --- a/spec/lib/mangopay/shared_resources.rb +++ b/spec/lib/mangopay/shared_resources.rb @@ -1,8 +1,23 @@ shared_context 'clients' do + let(:client_id) { + SecureRandom.hex(10) + } + + let(:wrong_client_id) { + SecureRandom.hex(20) + } + + let(:wrong_client) { + MangoPay::Client.create({ + 'ClientID' => wrong_client_id, + 'Name' => 'What a nice name' + }) + } + let(:new_client) { MangoPay::Client.create({ - 'ClientID' => MangoPay.configuration.client_id, + 'ClientID' => client_id, 'Name' => 'What a nice name' }) } From c6b9527d274ea18c3fc4a9687c92895325682280 Mon Sep 17 00:00:00 2001 From: glorieux Date: Thu, 25 Jul 2013 17:04:21 +0200 Subject: [PATCH 07/36] Add Payout + Refactoring - Add Payout Ressource + partial tests - Some minor refactorings --- lib/mangopay/payin.rb | 1 - lib/mangopay/payout.rb | 12 +++++++++-- spec/lib/mangopay/payout_spec.rb | 22 ++++++++++++++++++++ spec/lib/mangopay/shared_resources.rb | 30 +++++++++++++-------------- 4 files changed, 46 insertions(+), 19 deletions(-) create mode 100644 spec/lib/mangopay/payout_spec.rb diff --git a/lib/mangopay/payin.rb b/lib/mangopay/payin.rb index 9b53bf8..1a692c7 100644 --- a/lib/mangopay/payin.rb +++ b/lib/mangopay/payin.rb @@ -1,7 +1,6 @@ module MangoPay class PayIn < Resource include MangoPay::HTTPCalls::Fetch - module Card class Web < Resource include MangoPay::HTTPCalls::Create diff --git a/lib/mangopay/payout.rb b/lib/mangopay/payout.rb index 75b5816..62be22d 100644 --- a/lib/mangopay/payout.rb +++ b/lib/mangopay/payout.rb @@ -1,4 +1,12 @@ module MangoPay - class PayOut < Resource - end + class PayOut < Resource + include MangoPay::HTTPCalls::Fetch + class BankWire < Resource + include MangoPay::HTTPCalls::Create + + def self.url(id = nil) + "/v2/#{MangoPay.configuration.client_id}/payouts/bank-wire" + end + end + end end diff --git a/spec/lib/mangopay/payout_spec.rb b/spec/lib/mangopay/payout_spec.rb new file mode 100644 index 0000000..ff873e5 --- /dev/null +++ b/spec/lib/mangopay/payout_spec.rb @@ -0,0 +1,22 @@ +require_relative '../../spec_helper' + +describe MangoPay::PayOut::BankWire do + include_context 'users' + include_context 'wallets' + include_context 'bank_details' + include_context 'bank_wires' + + describe 'CREATE' do + it 'creates a bank wire payout' do + expect(new_bank_wire['Message']).to be_nil + expect(new_bank_wire['Id']).not_to be_nil + end + end + + describe 'FETCH' do + it 'fetches a payout' do + bank_wire = MangoPay::PayOut.fetch(new_bank_wire['Id']) + expect(bank_wire['Id']).to eq(new_bank_wire['Id']) + end + end +end diff --git a/spec/lib/mangopay/shared_resources.rb b/spec/lib/mangopay/shared_resources.rb index 80e9872..97b88cc 100644 --- a/spec/lib/mangopay/shared_resources.rb +++ b/spec/lib/mangopay/shared_resources.rb @@ -87,23 +87,7 @@ end shared_context 'cards' do - let(:new_web_card) { - puts new_natural_user - puts new_wallet - puts "Hey" - puts({ - AuthorId: new_natural_user['Id'], - CreditedUserId: new_wallet['Owners'][0], - DebitedFunds: { Currency: 'EUR', Amount: 1000 }, - Fees: { Currency: 'EUR', Amount: 0}, - CreditedWalletId: new_wallet['Id'], - ReturnURL: 'http://dev.leetchi.com', - CardType: 'CB_VISA_MASTERCARD', - Culture: 'FR', - Tag: 'Test Card' - }) - puts "bye" MangoPay::PayIn::Card::Web.create({ AuthorId: new_natural_user['Id'], CreditedUserId: new_wallet['Owners'][0], @@ -117,3 +101,17 @@ }) } end + +shared_context 'bank_wires' do + let(:new_bank_wire){ + MangoPay::PayOut::BankWire.create({ + AuthorId: new_wallet['Owners'][0], + DebitedFunds: { Currency: 'EUR', Amount: 500 }, + Fees: { Currency: 'EUR', Amount: 0 }, + DebitedWalletId: new_wallet['Id'], + BankDetailsId: new_iban_bank_detail['Id'], + Communication: 'This is a test', + Tag: 'Test Bank Wire' + }) + } +end From b51a023b4898bcfd04ca79f0cb6ba0e7f0aec070 Mon Sep 17 00:00:00 2001 From: glorieux Date: Thu, 25 Jul 2013 17:34:47 +0200 Subject: [PATCH 08/36] Add Transfers. Add Transfers resources and tests. --- lib/mangopay.rb | 1 + lib/mangopay/transfer.rb | 6 +++++ spec/lib/mangopay/shared_resources.rb | 32 +++++++++++++++++++++++++++ spec/lib/mangopay/transfer_spec.rb | 20 +++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 lib/mangopay/transfer.rb create mode 100644 spec/lib/mangopay/transfer_spec.rb diff --git a/lib/mangopay.rb b/lib/mangopay.rb index bfe3640..dc1c6ea 100644 --- a/lib/mangopay.rb +++ b/lib/mangopay.rb @@ -17,6 +17,7 @@ require 'mangopay/legal_user' require 'mangopay/payin' require 'mangopay/payout' +require 'mangopay/transfer' require 'mangopay/transaction' require 'mangopay/wallet' require 'mangopay/bank_detail' diff --git a/lib/mangopay/transfer.rb b/lib/mangopay/transfer.rb new file mode 100644 index 0000000..78dc2d9 --- /dev/null +++ b/lib/mangopay/transfer.rb @@ -0,0 +1,6 @@ +module MangoPay + class Transfer < Resource + include MangoPay::HTTPCalls::Create + include MangoPay::HTTPCalls::Fetch + end +end diff --git a/spec/lib/mangopay/shared_resources.rb b/spec/lib/mangopay/shared_resources.rb index 97b88cc..9803e53 100644 --- a/spec/lib/mangopay/shared_resources.rb +++ b/spec/lib/mangopay/shared_resources.rb @@ -115,3 +115,35 @@ }) } end + +shared_context 'transfer' do + let(:credited_wallet) { + MangoPay::Wallet.create({ + Owners: [new_natural_user['Id']], + Description: 'A test wallet', + Currency: 'EUR', + Tag: 'Test Time' + }) + } + + let(:debited_wallet) { + MangoPay::Wallet.create({ + Owners: [new_legal_user['Id']], + Description: 'A test wallet', + Currency: 'EUR', + Tag: 'Test Time' + }) + } + + let(:new_transfer) { + MangoPay::Transfer.create({ + AuthorId: debited_wallet['Owners'][0], + CreditedUserId: credited_wallet['Owners'][0], + DebitedFunds: { Currency: 'EUR', Amount: 500}, + Fees: { Currency: 'EUR', Amout: 0}, + DebitedWalletId: debited_wallet['Id'], + CreditedWalletId: credited_wallet['Id'], + Tag: 'Test Transfer' + }) + } +end diff --git a/spec/lib/mangopay/transfer_spec.rb b/spec/lib/mangopay/transfer_spec.rb new file mode 100644 index 0000000..81d18a0 --- /dev/null +++ b/spec/lib/mangopay/transfer_spec.rb @@ -0,0 +1,20 @@ +require_relative '../../spec_helper' + +describe MangoPay::Transfer do + include_context 'users' + include_context 'wallets' + include_context 'transfer' + + describe 'CREATE' do + it 'creates a new Transfer' do + expect(new_transfer['Id']).not_to be_nil + end + end + + describe 'FETCH' do + it 'fetches a Transfer' do + transfer = MangoPay::Transfer.fetch(new_transfer['Id']) + expect(transfer['Id']).to eq(new_transfer['Id']) + end + end +end From b9453fbb619323cb7fcdbfe87ec1a4c848b27d5f Mon Sep 17 00:00:00 2001 From: glorieux Date: Thu, 25 Jul 2013 17:57:19 +0200 Subject: [PATCH 09/36] Remove rest_client. Remove the rest_client gem since it's not required anymore. --- lib/mangopay.rb | 1 - mangopay.gemspec | 1 - 2 files changed, 2 deletions(-) diff --git a/lib/mangopay.rb b/lib/mangopay.rb index dc1c6ea..d708e62 100644 --- a/lib/mangopay.rb +++ b/lib/mangopay.rb @@ -1,5 +1,4 @@ require 'net/http' -require 'rest_client' require 'multi_json' # Version diff --git a/mangopay.gemspec b/mangopay.gemspec index 6e8aa64..7d6e6de 100644 --- a/mangopay.gemspec +++ b/mangopay.gemspec @@ -19,7 +19,6 @@ Gem::Specification.new do |s| s.required_ruby_version = '>= 1.9.2' s.add_dependency('multi_json', '~> 1.7.7') - s.add_dependency('rest-client', '~> 1.6.7') s.add_development_dependency('rake', '~> 10.1.0') s.add_development_dependency('rspec', '~> 2.14.1') From ba6cee27f33b3c96c63a6c6db51744fc818c7c30 Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 26 Jul 2013 10:39:42 +0200 Subject: [PATCH 10/36] PayIn Web Card are now fully tested. - Add capybara and capybara-webkit for headless testing of the payin web card - Add headless testing to the payin web card to test the filling process of the payline form --- mangopay.gemspec | 2 ++ spec/lib/mangopay/payin_spec.rb | 5 +++-- spec/lib/mangopay/shared_resources.rb | 15 ++++++++++++--- spec/spec_helper.rb | 5 +++++ 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/mangopay.gemspec b/mangopay.gemspec index 7d6e6de..042d9cb 100644 --- a/mangopay.gemspec +++ b/mangopay.gemspec @@ -22,6 +22,8 @@ Gem::Specification.new do |s| s.add_development_dependency('rake', '~> 10.1.0') s.add_development_dependency('rspec', '~> 2.14.1') + s.add_development_dependency('capybara', '~> 2.1.0') + s.add_development_dependency('capybara-webkit', '~> 1.0.0') s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- spec/*`.split("\n") diff --git a/spec/lib/mangopay/payin_spec.rb b/spec/lib/mangopay/payin_spec.rb index 4789b43..7d63860 100644 --- a/spec/lib/mangopay/payin_spec.rb +++ b/spec/lib/mangopay/payin_spec.rb @@ -1,13 +1,14 @@ require_relative '../../spec_helper' -describe MangoPay::PayIn::Card::Web do +describe MangoPay::PayIn::Card::Web, type: :feature do include_context 'users' include_context 'wallets' include_context 'cards' describe 'CREATE' do it 'creates a card' do - expect(new_web_card['RedirectURL']).not_to be_empty + expect(new_web_card['Id']).not_to be_nil + expect(new_web_card['Status']).to eq('SUCCEEDED') end end diff --git a/spec/lib/mangopay/shared_resources.rb b/spec/lib/mangopay/shared_resources.rb index 9803e53..fb2ed6d 100644 --- a/spec/lib/mangopay/shared_resources.rb +++ b/spec/lib/mangopay/shared_resources.rb @@ -88,17 +88,26 @@ shared_context 'cards' do let(:new_web_card) { - MangoPay::PayIn::Card::Web.create({ + card = MangoPay::PayIn::Card::Web.create({ AuthorId: new_natural_user['Id'], CreditedUserId: new_wallet['Owners'][0], DebitedFunds: { Currency: 'EUR', Amount: 1000 }, - Fees: { Currency: 'EUR', Amount: 0}, + Fees: { Currency: 'EUR', Amount: 0 }, CreditedWalletId: new_wallet['Id'], - ReturnURL: 'http://dev.leetchi.com', + ReturnURL: MangoPay.configuration.root_url, CardType: 'CB_VISA_MASTERCARD', Culture: 'FR', Tag: 'Test Card' }) + visit(card['RedirectURL']) + fill_in('number', with: '4970100000000154') + fill_in('cvv', with: '123') + click_button('paybutton') + card = MangoPay::PayIn.fetch(card['Id']) + while card["Status"] == 'CREATED' do + card = MangoPay::PayIn.fetch(card['Id']) + end + card } end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c8da341..8c0ab31 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,11 @@ require_relative '../lib/mangopay' require_relative './lib/mangopay/shared_resources' +require 'capybara/rspec' +require 'capybara-webkit' + +Capybara.default_driver = :webkit + MangoPay.configure do |c| c.preproduction = true c.client_id = 'example' From fd334f0af8ad6c4799d61c56755d7d159b80d3be Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 26 Jul 2013 11:06:38 +0200 Subject: [PATCH 11/36] Update Payout spec to test on a wallet with actual funds. Thanks to the previous commit the Payout spec has been updated to use a wallet with actual funds on it. --- spec/lib/mangopay/payout_spec.rb | 6 ++++-- spec/lib/mangopay/shared_resources.rb | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/spec/lib/mangopay/payout_spec.rb b/spec/lib/mangopay/payout_spec.rb index ff873e5..c0d9a11 100644 --- a/spec/lib/mangopay/payout_spec.rb +++ b/spec/lib/mangopay/payout_spec.rb @@ -1,15 +1,16 @@ require_relative '../../spec_helper' -describe MangoPay::PayOut::BankWire do +describe MangoPay::PayOut::BankWire, type: :feature do include_context 'users' include_context 'wallets' include_context 'bank_details' + include_context 'cards' include_context 'bank_wires' describe 'CREATE' do it 'creates a bank wire payout' do - expect(new_bank_wire['Message']).to be_nil expect(new_bank_wire['Id']).not_to be_nil + expect(new_bank_wire['Status']).to eq('CREATED') end end @@ -17,6 +18,7 @@ it 'fetches a payout' do bank_wire = MangoPay::PayOut.fetch(new_bank_wire['Id']) expect(bank_wire['Id']).to eq(new_bank_wire['Id']) + expect(new_bank_wire['Status']).to eq('CREATED') end end end diff --git a/spec/lib/mangopay/shared_resources.rb b/spec/lib/mangopay/shared_resources.rb index fb2ed6d..3e0b5b8 100644 --- a/spec/lib/mangopay/shared_resources.rb +++ b/spec/lib/mangopay/shared_resources.rb @@ -113,11 +113,14 @@ shared_context 'bank_wires' do let(:new_bank_wire){ + # TODO: Once the issue with the CreditedWalletID being uppercased is solved + # change DebitedWalletId: new_web_card['CreditedWalletID'] to + # DebitedWalletId: new_web_card['CreditedWalletId'] MangoPay::PayOut::BankWire.create({ - AuthorId: new_wallet['Owners'][0], + AuthorId: new_web_card['CreditedUserId'], DebitedFunds: { Currency: 'EUR', Amount: 500 }, Fees: { Currency: 'EUR', Amount: 0 }, - DebitedWalletId: new_wallet['Id'], + DebitedWalletId: new_web_card['CreditedWalletID'], BankDetailsId: new_iban_bank_detail['Id'], Communication: 'This is a test', Tag: 'Test Bank Wire' From a80841ef0f33f2b629df71e20d2a56fab654aac0 Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 26 Jul 2013 11:15:02 +0200 Subject: [PATCH 12/36] Update Transfer spec to test on a wallet with actual funds. Thanks to the commit ba6cee27f33b3c96c63a6c6db51744fc818c7c30 the Transfer spec has been updated to use a wallet with actual funds on it. --- spec/lib/mangopay/shared_resources.rb | 30 +++++++++++++++++++++++++-- spec/lib/mangopay/transfer_spec.rb | 4 +++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/spec/lib/mangopay/shared_resources.rb b/spec/lib/mangopay/shared_resources.rb index 3e0b5b8..4a66a78 100644 --- a/spec/lib/mangopay/shared_resources.rb +++ b/spec/lib/mangopay/shared_resources.rb @@ -147,13 +147,39 @@ }) } + let(:web_card_contribution) { + card = MangoPay::PayIn::Card::Web.create({ + AuthorId: debited_wallet['Owners'][0], + CreditedUserId: debited_wallet['Owners'][0], + DebitedFunds: { Currency: 'EUR', Amount: 1000 }, + Fees: { Currency: 'EUR', Amount: 0 }, + CreditedWalletId: debited_wallet['Id'], + ReturnURL: MangoPay.configuration.root_url, + CardType: 'CB_VISA_MASTERCARD', + Culture: 'FR', + Tag: 'Test Card' + }) + visit(card['RedirectURL']) + fill_in('number', with: '4970100000000154') + fill_in('cvv', with: '123') + click_button('paybutton') + card = MangoPay::PayIn.fetch(card['Id']) + while card["Status"] == 'CREATED' do + card = MangoPay::PayIn.fetch(card['Id']) + end + card + } + + # TODO: Once the issue with the CreditedWalletID being uppercased is solved + # change DebitedWalletId: new_web_card['CreditedWalletID'] to + # DebitedWalletId: new_web_card['CreditedWalletId'] let(:new_transfer) { MangoPay::Transfer.create({ - AuthorId: debited_wallet['Owners'][0], + AuthorId: web_card_contribution['CreditedUserId'], CreditedUserId: credited_wallet['Owners'][0], DebitedFunds: { Currency: 'EUR', Amount: 500}, Fees: { Currency: 'EUR', Amout: 0}, - DebitedWalletId: debited_wallet['Id'], + DebitedWalletId: web_card_contribution['CreditedWalletID'], CreditedWalletId: credited_wallet['Id'], Tag: 'Test Transfer' }) diff --git a/spec/lib/mangopay/transfer_spec.rb b/spec/lib/mangopay/transfer_spec.rb index 81d18a0..e3b4501 100644 --- a/spec/lib/mangopay/transfer_spec.rb +++ b/spec/lib/mangopay/transfer_spec.rb @@ -1,6 +1,6 @@ require_relative '../../spec_helper' -describe MangoPay::Transfer do +describe MangoPay::Transfer, type: :feature do include_context 'users' include_context 'wallets' include_context 'transfer' @@ -8,6 +8,7 @@ describe 'CREATE' do it 'creates a new Transfer' do expect(new_transfer['Id']).not_to be_nil + expect(new_transfer['Status']).to eq('SUCCEEDED') end end @@ -15,6 +16,7 @@ it 'fetches a Transfer' do transfer = MangoPay::Transfer.fetch(new_transfer['Id']) expect(transfer['Id']).to eq(new_transfer['Id']) + expect(new_transfer['Status']).to eq('SUCCEEDED') end end end From b9322a98c2c909579146bdf016f68bbad75f3c21 Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 26 Jul 2013 16:12:10 +0200 Subject: [PATCH 13/36] Bump version to 3.0.0.beta1 --- lib/mangopay/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mangopay/version.rb b/lib/mangopay/version.rb index b411005..7c8ee5c 100644 --- a/lib/mangopay/version.rb +++ b/lib/mangopay/version.rb @@ -1,3 +1,3 @@ module MangoPay - VERSION = '3.0.0' + VERSION = '3.0.0.beta1' end From 90850504e4639759912d0322385f010bf6d1b5aa Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 26 Jul 2013 16:12:51 +0200 Subject: [PATCH 14/36] Add mininal rails generator Add a rails generator that create a config file from the client_id and client_name inputed --- lib/generators/mangopay/install_generator.rb | 29 ++++++++++++++++++++ lib/generators/templates/mangopay.rb | 5 ++++ mangopay.gemspec | 1 + 3 files changed, 35 insertions(+) create mode 100644 lib/generators/mangopay/install_generator.rb create mode 100644 lib/generators/templates/mangopay.rb diff --git a/lib/generators/mangopay/install_generator.rb b/lib/generators/mangopay/install_generator.rb new file mode 100644 index 0000000..0e13193 --- /dev/null +++ b/lib/generators/mangopay/install_generator.rb @@ -0,0 +1,29 @@ +require 'mangopay' + +module MangoPay + module Generators + class InstallGenerator < Rails::Generators::Base + namespace 'mangopay' + source_root File.expand_path('./templates', __FILE__) + argument :client_id, type: :string, + desc: 'The id you want to use to query the MangoPay API (must match with the regex ^[a-z0-9_-]{4,20}$)' + argument :client_name, type: :string, desc: "Full name of you're organization" + class_option :preproduction, type: :boolean, default: true + + desc 'Installs all the basic configuration of the mangopay gem' + def install_mangopay + template 'mangopay.rb', 'config/initializers/mangopay.rb' + end + + protected + + def client_passphrase + client = MangoPay::Client.create({ + ClientID: client_id, + Name: client_name + }) + client['Passphrase'] + end + end + end +end diff --git a/lib/generators/templates/mangopay.rb b/lib/generators/templates/mangopay.rb new file mode 100644 index 0000000..9063a9a --- /dev/null +++ b/lib/generators/templates/mangopay.rb @@ -0,0 +1,5 @@ +MangoPay.configure do |c| + c.preproduction = <%= preproduction %> + c.client_id = <%= client_id %> + c.client_passphrase = <%= client_passphrase %> +end diff --git a/mangopay.gemspec b/mangopay.gemspec index 042d9cb..393e579 100644 --- a/mangopay.gemspec +++ b/mangopay.gemspec @@ -24,6 +24,7 @@ Gem::Specification.new do |s| s.add_development_dependency('rspec', '~> 2.14.1') s.add_development_dependency('capybara', '~> 2.1.0') s.add_development_dependency('capybara-webkit', '~> 1.0.0') + s.add_development_dependency('rails', '~> 4.0.0') s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- spec/*`.split("\n") From 74b24e006a78cb76d3b8b32e5a34c72f6b095a6f Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 26 Jul 2013 16:23:23 +0200 Subject: [PATCH 15/36] moved to Mangopay in the module name --- lib/generators/mangopay/install_generator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/generators/mangopay/install_generator.rb b/lib/generators/mangopay/install_generator.rb index 0e13193..c397217 100644 --- a/lib/generators/mangopay/install_generator.rb +++ b/lib/generators/mangopay/install_generator.rb @@ -1,10 +1,10 @@ require 'mangopay' -module MangoPay +module Mangopay module Generators class InstallGenerator < Rails::Generators::Base namespace 'mangopay' - source_root File.expand_path('./templates', __FILE__) + source_root File.expand_path('../templates', __FILE__) argument :client_id, type: :string, desc: 'The id you want to use to query the MangoPay API (must match with the regex ^[a-z0-9_-]{4,20}$)' argument :client_name, type: :string, desc: "Full name of you're organization" From a6e45ac4ad07d6f46c395cda26cc3bd0dc4fda84 Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 26 Jul 2013 16:26:13 +0200 Subject: [PATCH 16/36] Require rails generators --- lib/generators/mangopay/install_generator.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/generators/mangopay/install_generator.rb b/lib/generators/mangopay/install_generator.rb index c397217..9c245ec 100644 --- a/lib/generators/mangopay/install_generator.rb +++ b/lib/generators/mangopay/install_generator.rb @@ -1,3 +1,4 @@ +require 'rails/generators/base' require 'mangopay' module Mangopay From 4f5a5219be8aae94c00c27003d75a0eda0fdf231 Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 26 Jul 2013 16:29:40 +0200 Subject: [PATCH 17/36] remove the namespace and renamte the method to setup --- lib/generators/mangopay/install_generator.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/generators/mangopay/install_generator.rb b/lib/generators/mangopay/install_generator.rb index 9c245ec..4d0605d 100644 --- a/lib/generators/mangopay/install_generator.rb +++ b/lib/generators/mangopay/install_generator.rb @@ -4,7 +4,6 @@ module Mangopay module Generators class InstallGenerator < Rails::Generators::Base - namespace 'mangopay' source_root File.expand_path('../templates', __FILE__) argument :client_id, type: :string, desc: 'The id you want to use to query the MangoPay API (must match with the regex ^[a-z0-9_-]{4,20}$)' @@ -12,7 +11,7 @@ class InstallGenerator < Rails::Generators::Base class_option :preproduction, type: :boolean, default: true desc 'Installs all the basic configuration of the mangopay gem' - def install_mangopay + def setup template 'mangopay.rb', 'config/initializers/mangopay.rb' end From f05d1f70820e0ccde6030b3a836a1aa72bb6ca0e Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 26 Jul 2013 16:38:44 +0200 Subject: [PATCH 18/36] Update preproduction description. chenge the source_root --- lib/generators/mangopay/install_generator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/generators/mangopay/install_generator.rb b/lib/generators/mangopay/install_generator.rb index 4d0605d..6f35b16 100644 --- a/lib/generators/mangopay/install_generator.rb +++ b/lib/generators/mangopay/install_generator.rb @@ -4,11 +4,11 @@ module Mangopay module Generators class InstallGenerator < Rails::Generators::Base - source_root File.expand_path('../templates', __FILE__) + source_root File.expand_path('../../templates', __FILE__) argument :client_id, type: :string, desc: 'The id you want to use to query the MangoPay API (must match with the regex ^[a-z0-9_-]{4,20}$)' argument :client_name, type: :string, desc: "Full name of you're organization" - class_option :preproduction, type: :boolean, default: true + class_option :preproduction, type: :boolean, default: true, desc: 'Whether or not use the preproduction environment' desc 'Installs all the basic configuration of the mangopay gem' def setup From ffe9ddec128813256ae4d9821b05c401f1b703e9 Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 26 Jul 2013 16:41:39 +0200 Subject: [PATCH 19/36] Update template variable --- lib/generators/templates/mangopay.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/templates/mangopay.rb b/lib/generators/templates/mangopay.rb index 9063a9a..6e4e1cb 100644 --- a/lib/generators/templates/mangopay.rb +++ b/lib/generators/templates/mangopay.rb @@ -1,5 +1,5 @@ MangoPay.configure do |c| - c.preproduction = <%= preproduction %> + c.preproduction = <%= options[:preproduction] %> c.client_id = <%= client_id %> c.client_passphrase = <%= client_passphrase %> end From db27f9612687da7f4f9e75a588fb6515be844f52 Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 26 Jul 2013 16:45:19 +0200 Subject: [PATCH 20/36] Add client_id method --- lib/generators/mangopay/install_generator.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/generators/mangopay/install_generator.rb b/lib/generators/mangopay/install_generator.rb index 6f35b16..1a70d51 100644 --- a/lib/generators/mangopay/install_generator.rb +++ b/lib/generators/mangopay/install_generator.rb @@ -17,6 +17,10 @@ def setup protected + def client_id + client_id + end + def client_passphrase client = MangoPay::Client.create({ ClientID: client_id, From d2aefb3a345373e3f9f0a71d7b4fae19983505c4 Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 26 Jul 2013 16:51:56 +0200 Subject: [PATCH 21/36] Add client_id --- lib/generators/mangopay/install_generator.rb | 5 +---- lib/generators/templates/mangopay.rb | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/generators/mangopay/install_generator.rb b/lib/generators/mangopay/install_generator.rb index 1a70d51..5443348 100644 --- a/lib/generators/mangopay/install_generator.rb +++ b/lib/generators/mangopay/install_generator.rb @@ -12,15 +12,12 @@ class InstallGenerator < Rails::Generators::Base desc 'Installs all the basic configuration of the mangopay gem' def setup + @client_id = client_id template 'mangopay.rb', 'config/initializers/mangopay.rb' end protected - def client_id - client_id - end - def client_passphrase client = MangoPay::Client.create({ ClientID: client_id, diff --git a/lib/generators/templates/mangopay.rb b/lib/generators/templates/mangopay.rb index 6e4e1cb..2eca247 100644 --- a/lib/generators/templates/mangopay.rb +++ b/lib/generators/templates/mangopay.rb @@ -1,5 +1,5 @@ MangoPay.configure do |c| c.preproduction = <%= options[:preproduction] %> - c.client_id = <%= client_id %> + c.client_id = <%= @client_id %> c.client_passphrase = <%= client_passphrase %> end From 1d139749ea96f42cf8647d69ca428bb7c5350774 Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 26 Jul 2013 17:29:09 +0200 Subject: [PATCH 22/36] Now use an outside method to create the client account --- lib/mangopay/client.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/mangopay/client.rb b/lib/mangopay/client.rb index d379dfc..c52cb4c 100644 --- a/lib/mangopay/client.rb +++ b/lib/mangopay/client.rb @@ -1,5 +1,17 @@ module MangoPay class Client < Resource - include MangoPay::HTTPCalls::Create + def self.create(params) + uri = URI(MangoPay.configuration.root_url + '/api/clients/') + res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| + puts uri.request_uri + request = Net::HTTP::Post.new(uri.request_uri, { + 'user_agent' => "MangoPay V1 RubyBindings/#{MangoPay::VERSION}", + 'Content-Type' => 'application/json' + }) + request.body = MangoPay::JSON.dump(params) + http.request request + end + MangoPay::JSON.load(res.body) + end end end From b147e6cc58496c7a2efb1190c3b2e456a8c931d3 Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 26 Jul 2013 17:53:33 +0200 Subject: [PATCH 23/36] Add the mangopay generator. Add the client_passphrase to the template --- lib/generators/mangopay/install_generator.rb | 5 +++++ lib/generators/templates/mangopay.rb | 2 +- lib/mangopay/client.rb | 1 - 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/generators/mangopay/install_generator.rb b/lib/generators/mangopay/install_generator.rb index 5443348..56b7b71 100644 --- a/lib/generators/mangopay/install_generator.rb +++ b/lib/generators/mangopay/install_generator.rb @@ -13,16 +13,21 @@ class InstallGenerator < Rails::Generators::Base desc 'Installs all the basic configuration of the mangopay gem' def setup @client_id = client_id + @client_passphrase = client_passphrase template 'mangopay.rb', 'config/initializers/mangopay.rb' end protected def client_passphrase + MangoPay.configure do |c| + c.preproduction = options[:preproduction] + end client = MangoPay::Client.create({ ClientID: client_id, Name: client_name }) + # TODO: Check for existing id client['Passphrase'] end end diff --git a/lib/generators/templates/mangopay.rb b/lib/generators/templates/mangopay.rb index 2eca247..6ce4e15 100644 --- a/lib/generators/templates/mangopay.rb +++ b/lib/generators/templates/mangopay.rb @@ -1,5 +1,5 @@ MangoPay.configure do |c| c.preproduction = <%= options[:preproduction] %> c.client_id = <%= @client_id %> - c.client_passphrase = <%= client_passphrase %> + c.client_passphrase = <%= @client_passphrase %> end diff --git a/lib/mangopay/client.rb b/lib/mangopay/client.rb index c52cb4c..a574ac1 100644 --- a/lib/mangopay/client.rb +++ b/lib/mangopay/client.rb @@ -3,7 +3,6 @@ class Client < Resource def self.create(params) uri = URI(MangoPay.configuration.root_url + '/api/clients/') res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| - puts uri.request_uri request = Net::HTTP::Post.new(uri.request_uri, { 'user_agent' => "MangoPay V1 RubyBindings/#{MangoPay::VERSION}", 'Content-Type' => 'application/json' From 82efdd410555c7ff47a74bc798c859a419ec2737 Mon Sep 17 00:00:00 2001 From: glorieux Date: Mon, 29 Jul 2013 11:33:53 +0200 Subject: [PATCH 24/36] Add a first draft of the README. --- README.md | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 49e4577..cb32de3 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,66 @@ -mangopay2-ruby-sdk -================== +# MangoPay2 Ruby SDK -Ruby Gem for Mangopay apiV2 +The gem for interacting with the version 2 of the MangoPay API. +See the [API documentation]() for more details on the API. + +Tested on the following versions of Ruby: 1.9.2, 1.9.3, 2.0.0 + +## Code Status +[![Build Status]()]() +[![Code Climate]()]() +[![Dependency Status]()]() + +## NEWS + +### Version 3.0.0 +** BREAKING CHANGES ** +This version of the gem is targeting the MangoPay API Version 2. +It has a brand new structure to make the api calls easier to use + +## Usage + +### Install +* Install the gem by either running ```gem install mangopay``` or by adding it to your Gemfile ```gem 'mangopay'``` + +* The Rails users will be happy to know that there is a new generator script that will help you configure your access to the MangoPay API version 2. +Simply run ``rails generate mangopay:install CLIENT_ID CLIENT_NAME`` where CLIENT_ID is the id you will use to connect to the api and CLIENT_NAME is a full name that will be use to identify all communications between you and the MangoPay Team. + +### Examples + + +### Tests +Make sure that you have run: ```bundle install``` +Then you just have to run rspec ```rspec``` to run all the test suite. +Feel free to report any test failure by creating an issue on the [Gem's Github](https://github.com/MangoPay/mangopay2-ruby-sdk/issues) + +## Contributing + +1. Fork the repo. + +2. Run the tests. We only take pull requests with passing tests, and it's great +to know that you have a clean slate: `bundle && bundle exec rake` + +3. Add a test for your change. Only refactoring and documentation changes +require no new tests. If you are adding functionality or fixing a bug, we need +a test! + +4. Make the test pass. + +5. Push to your fork and submit a pull request. + +At this point you're waiting on us. We like to at least comment on, if not +accept, pull requests within three business days (and, typically, one business +day). We may suggest some changes or improvements or alternatives. + +Syntax: + +* Two spaces, no tabs. +* No trailing whitespace. Blank lines should not have any space. +* Prefer &&/|| over and/or. +* MyClass.my_method(my_arg) not my_method( my_arg ) or my_method my_arg. +* a = b and not a=b. +* Follow the conventions you see used in the source already. + +A contribution can also be as simple as a +1 on issues tickets to show us what you would like to see in this gem. + +That's it for now. Good Hacking... From e02b30468ea51c30539e8c2ee7f7c2c3906a5d97 Mon Sep 17 00:00:00 2001 From: glorieux Date: Mon, 29 Jul 2013 12:38:44 +0200 Subject: [PATCH 25/36] Update the rails generator. The user is now able to generate has much account has needed without confilcts with his current configuration. Move the client_is and client_passphrase to strings instead of variable.(Must have been a mistake) Now removing the old initializer every time the user runs the generator. --- lib/generators/mangopay/install_generator.rb | 1 + lib/generators/templates/mangopay.rb | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/generators/mangopay/install_generator.rb b/lib/generators/mangopay/install_generator.rb index 56b7b71..fc5970c 100644 --- a/lib/generators/mangopay/install_generator.rb +++ b/lib/generators/mangopay/install_generator.rb @@ -12,6 +12,7 @@ class InstallGenerator < Rails::Generators::Base desc 'Installs all the basic configuration of the mangopay gem' def setup + remove_file 'config/initializers/mangopay.rb' @client_id = client_id @client_passphrase = client_passphrase template 'mangopay.rb', 'config/initializers/mangopay.rb' diff --git a/lib/generators/templates/mangopay.rb b/lib/generators/templates/mangopay.rb index 6ce4e15..ba1b605 100644 --- a/lib/generators/templates/mangopay.rb +++ b/lib/generators/templates/mangopay.rb @@ -1,5 +1,5 @@ MangoPay.configure do |c| c.preproduction = <%= options[:preproduction] %> - c.client_id = <%= @client_id %> - c.client_passphrase = <%= @client_passphrase %> + c.client_id = '<%= @client_id %>' + c.client_passphrase = '<%= @client_passphrase %>' end From fc014d6cd34afeba63d0183184f6fb550fb1a3da Mon Sep 17 00:00:00 2001 From: glorieux Date: Mon, 29 Jul 2013 13:54:31 +0200 Subject: [PATCH 26/36] Add .rspec for colors and profiling on specs. --- .rspec | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .rspec diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..7eb1831 --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--profile From 5a81bc63d94e634fc5be71e8d5a8ebaf03ceeced Mon Sep 17 00:00:00 2001 From: glorieux Date: Mon, 29 Jul 2013 13:56:13 +0200 Subject: [PATCH 27/36] Add auth token exipration management. Add auth token expiration management. Remove unnecesary debug. --- lib/mangopay.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/mangopay.rb b/lib/mangopay.rb index d708e62..32e8e9e 100644 --- a/lib/mangopay.rb +++ b/lib/mangopay.rb @@ -56,13 +56,9 @@ def self.request(method, url, params={}, headers={}) res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| request = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, request_headers) - puts method - puts request.uri - puts request.path request.body = MangoPay::JSON.dump(params) http.request request end - puts MangoPay::JSON.load(res.body) MangoPay::JSON.load(res.body) end @@ -85,14 +81,18 @@ def self.get_uname end def self.get_oauth_token - uri = api_uri('/api/oauth/token') - res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| - req = Net::HTTP::Post.new(uri.request_uri) - req.basic_auth configuration.client_id, configuration.client_passphrase - req.body = 'grant_type=client_credentials' - http.request req + if @auth_timestamp.nil? || @auth_timestamp <= Time.now || @auth_token.nil? + uri = api_uri('/api/oauth/token') + res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| + req = Net::HTTP::Post.new(uri.request_uri) + req.basic_auth configuration.client_id, configuration.client_passphrase + req.body = 'grant_type=client_credentials' + http.request req + end + @auth_token = MangoPay::JSON.load(res.body) + @auth_timestamp = Time.now + @auth_token['expires_in'].to_i end - MangoPay::JSON.load(res.body) + @auth_token end def self.oauth_token From 04317d37f76f54fb6cd3a7839b7062620c667dbb Mon Sep 17 00:00:00 2001 From: glorieux Date: Mon, 29 Jul 2013 15:58:20 +0200 Subject: [PATCH 28/36] Add an executable to the gem. The user can now start using the gem right away using the console. No need to start a big project to start playing with it. --- bin/mangopay | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100755 bin/mangopay diff --git a/bin/mangopay b/bin/mangopay new file mode 100755 index 0000000..ce46896 --- /dev/null +++ b/bin/mangopay @@ -0,0 +1,9 @@ +#!/usr/bin/env ruby + +irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb' + +libs = " -r irb/completion" +libs << " -r #{File.dirname(__FILE__) + '/../lib/mangopay'}" + +puts "Loading MangoPay gem" +exec "#{irb} #{libs}" From 451e90f4584115233bc2662e33933a0d0b5060d0 Mon Sep 17 00:00:00 2001 From: glorieux Date: Tue, 30 Jul 2013 13:34:44 +0200 Subject: [PATCH 29/36] Refactoring Refactoring on methods privacies. --- lib/mangopay/bank_detail.rb | 2 ++ lib/mangopay/client.rb | 1 + lib/mangopay/legal_user.rb | 3 +++ lib/mangopay/natural_user.rb | 3 +++ lib/mangopay/payin.rb | 3 +++ lib/mangopay/payout.rb | 3 +++ lib/mangopay/resource.rb | 4 ++++ lib/mangopay/transaction.rb | 2 ++ 8 files changed, 21 insertions(+) diff --git a/lib/mangopay/bank_detail.rb b/lib/mangopay/bank_detail.rb index bc63c3e..1847915 100644 --- a/lib/mangopay/bank_detail.rb +++ b/lib/mangopay/bank_detail.rb @@ -8,6 +8,8 @@ def self.fetch(*ids) response = MangoPay.request(:get, url) end + private + def self.url(*id) if id.length == 1 "/v2/#{MangoPay.configuration.client_id}/users/#{CGI.escape(id[0])}/bank-details" diff --git a/lib/mangopay/client.rb b/lib/mangopay/client.rb index a574ac1..b75c03b 100644 --- a/lib/mangopay/client.rb +++ b/lib/mangopay/client.rb @@ -1,5 +1,6 @@ module MangoPay class Client < Resource + def self.create(params) uri = URI(MangoPay.configuration.root_url + '/api/clients/') res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| diff --git a/lib/mangopay/legal_user.rb b/lib/mangopay/legal_user.rb index e0c2e57..f89d3bb 100644 --- a/lib/mangopay/legal_user.rb +++ b/lib/mangopay/legal_user.rb @@ -1,5 +1,8 @@ module MangoPay class LegalUser < User + + private + def self.url(id = nil) if id "/v2/#{MangoPay.configuration.client_id}/users/legals/#{CGI.escape(id)}" diff --git a/lib/mangopay/natural_user.rb b/lib/mangopay/natural_user.rb index ffec613..b2bddb7 100644 --- a/lib/mangopay/natural_user.rb +++ b/lib/mangopay/natural_user.rb @@ -1,5 +1,8 @@ module MangoPay class NaturalUser < User + + private + def self.url(id = nil) if id "/v2/#{MangoPay.configuration.client_id}/users/naturals/#{CGI.escape(id)}" diff --git a/lib/mangopay/payin.rb b/lib/mangopay/payin.rb index 1a692c7..2ad2883 100644 --- a/lib/mangopay/payin.rb +++ b/lib/mangopay/payin.rb @@ -1,10 +1,13 @@ module MangoPay class PayIn < Resource include MangoPay::HTTPCalls::Fetch + module Card class Web < Resource include MangoPay::HTTPCalls::Create + private + def self.url(id = nil) "/v2/#{MangoPay.configuration.client_id}/payins/card/#{CGI.escape(class_name.downcase)}" end diff --git a/lib/mangopay/payout.rb b/lib/mangopay/payout.rb index 62be22d..6735acb 100644 --- a/lib/mangopay/payout.rb +++ b/lib/mangopay/payout.rb @@ -1,9 +1,12 @@ module MangoPay class PayOut < Resource include MangoPay::HTTPCalls::Fetch + class BankWire < Resource include MangoPay::HTTPCalls::Create + private + def self.url(id = nil) "/v2/#{MangoPay.configuration.client_id}/payouts/bank-wire" end diff --git a/lib/mangopay/resource.rb b/lib/mangopay/resource.rb index f69eb4c..d1ad101 100644 --- a/lib/mangopay/resource.rb +++ b/lib/mangopay/resource.rb @@ -1,5 +1,9 @@ module MangoPay + # @abstract class Resource + + protected + def self.class_name self.name.split('::')[-1] end diff --git a/lib/mangopay/transaction.rb b/lib/mangopay/transaction.rb index b4423c3..fe72baa 100644 --- a/lib/mangopay/transaction.rb +++ b/lib/mangopay/transaction.rb @@ -2,6 +2,8 @@ module MangoPay class Transaction < Resource include MangoPay::HTTPCalls::Fetch + private + def self.url(id) "/v2/#{MangoPay.configuration.client_id}/wallets/#{CGI.escape(id)}/transactions" end From 320ec66fa0959e5cf96b9f37bda3ea088a79cfe4 Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 2 Aug 2013 12:10:10 +0200 Subject: [PATCH 30/36] Update the gem reflecting the changes on the API. Move bank details to bankaccounts Remove s from the legal and natural users urls Move bank-wire to bankwire Update all the ID's to Id Move CreditedWalletID to CreditedWalletId Rename BankDetail to BankAccount --- .gitignore | 1 + lib/mangopay.rb | 2 +- lib/mangopay/{bank_detail.rb => bank_account.rb} | 8 ++++---- lib/mangopay/legal_user.rb | 4 ++-- lib/mangopay/natural_user.rb | 4 ++-- lib/mangopay/payout.rb | 2 +- .../{bank_detail_spec.rb => bank_account_spec.rb} | 6 +++--- spec/lib/mangopay/client_spec.rb | 10 +++++----- spec/lib/mangopay/shared_resources.rb | 14 ++++---------- 9 files changed, 23 insertions(+), 28 deletions(-) rename lib/mangopay/{bank_detail.rb => bank_account.rb} (73%) rename spec/lib/mangopay/{bank_detail_spec.rb => bank_account_spec.rb} (70%) diff --git a/.gitignore b/.gitignore index d3bf535..5b0ac14 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ spec/reports test/tmp test/version_tmp tmp +tags # YARD artifacts .yardoc diff --git a/lib/mangopay.rb b/lib/mangopay.rb index 32e8e9e..c9499c2 100644 --- a/lib/mangopay.rb +++ b/lib/mangopay.rb @@ -19,7 +19,7 @@ require 'mangopay/transfer' require 'mangopay/transaction' require 'mangopay/wallet' -require 'mangopay/bank_detail' +require 'mangopay/bank_account' # Errors require 'mangopay/errors' diff --git a/lib/mangopay/bank_detail.rb b/lib/mangopay/bank_account.rb similarity index 73% rename from lib/mangopay/bank_detail.rb rename to lib/mangopay/bank_account.rb index 1847915..a623f82 100644 --- a/lib/mangopay/bank_detail.rb +++ b/lib/mangopay/bank_account.rb @@ -1,20 +1,20 @@ module MangoPay - class BankDetail < Resource + class BankAccount < Resource include MangoPay::HTTPCalls::Create include MangoPay::HTTPCalls::Fetch def self.fetch(*ids) url = ids.length == 1 ? url(ids[0]) : url(ids[0], ids[1]) - response = MangoPay.request(:get, url) + MangoPay.request(:get, url) end private def self.url(*id) if id.length == 1 - "/v2/#{MangoPay.configuration.client_id}/users/#{CGI.escape(id[0])}/bank-details" + "/v2/#{MangoPay.configuration.client_id}/users/#{CGI.escape(id[0])}/bankaccounts" else - "/v2/#{MangoPay.configuration.client_id}/users/#{CGI.escape(id[0])}/bank-details/#{CGI.escape(id[1])}" + "/v2/#{MangoPay.configuration.client_id}/users/#{CGI.escape(id[0])}/bankaccounts/#{CGI.escape(id[1])}" end end end diff --git a/lib/mangopay/legal_user.rb b/lib/mangopay/legal_user.rb index f89d3bb..08a1667 100644 --- a/lib/mangopay/legal_user.rb +++ b/lib/mangopay/legal_user.rb @@ -5,9 +5,9 @@ class LegalUser < User def self.url(id = nil) if id - "/v2/#{MangoPay.configuration.client_id}/users/legals/#{CGI.escape(id)}" + "/v2/#{MangoPay.configuration.client_id}/users/legal/#{CGI.escape(id)}" else - "/v2/#{MangoPay.configuration.client_id}/users/legals" + "/v2/#{MangoPay.configuration.client_id}/users/legal" end end end diff --git a/lib/mangopay/natural_user.rb b/lib/mangopay/natural_user.rb index b2bddb7..d799000 100644 --- a/lib/mangopay/natural_user.rb +++ b/lib/mangopay/natural_user.rb @@ -5,9 +5,9 @@ class NaturalUser < User def self.url(id = nil) if id - "/v2/#{MangoPay.configuration.client_id}/users/naturals/#{CGI.escape(id)}" + "/v2/#{MangoPay.configuration.client_id}/users/natural/#{CGI.escape(id)}" else - "/v2/#{MangoPay.configuration.client_id}/users/naturals" + "/v2/#{MangoPay.configuration.client_id}/users/natural" end end end diff --git a/lib/mangopay/payout.rb b/lib/mangopay/payout.rb index 6735acb..389a253 100644 --- a/lib/mangopay/payout.rb +++ b/lib/mangopay/payout.rb @@ -8,7 +8,7 @@ class BankWire < Resource private def self.url(id = nil) - "/v2/#{MangoPay.configuration.client_id}/payouts/bank-wire" + "/v2/#{MangoPay.configuration.client_id}/payouts/bankwire" end end end diff --git a/spec/lib/mangopay/bank_detail_spec.rb b/spec/lib/mangopay/bank_account_spec.rb similarity index 70% rename from spec/lib/mangopay/bank_detail_spec.rb rename to spec/lib/mangopay/bank_account_spec.rb index 4575d70..6e7f627 100644 --- a/spec/lib/mangopay/bank_detail_spec.rb +++ b/spec/lib/mangopay/bank_account_spec.rb @@ -1,6 +1,6 @@ require_relative '../../spec_helper' -describe MangoPay::BankDetail do +describe MangoPay::BankAccount do include_context 'users' include_context 'bank_details' @@ -13,13 +13,13 @@ describe 'FETCH' do it 'fetches all the bank details' do - bank_details = MangoPay::BankDetail.fetch(new_iban_bank_detail['UserId']) + bank_details = MangoPay::BankAccount.fetch(new_iban_bank_detail['UserId']) expect(bank_details).to be_kind_of(Array) expect(bank_details[0]['Id']).to eq(new_iban_bank_detail['Id']) end it 'fetches one bank detail' do - bank_detail = MangoPay::BankDetail.fetch(new_iban_bank_detail['UserId'], new_iban_bank_detail['Id']) + bank_detail = MangoPay::BankAccount.fetch(new_iban_bank_detail['UserId'], new_iban_bank_detail['Id']) expect(bank_detail['Id']).to eq(new_iban_bank_detail['Id']) end end diff --git a/spec/lib/mangopay/client_spec.rb b/spec/lib/mangopay/client_spec.rb index f7c2a00..f7530b4 100644 --- a/spec/lib/mangopay/client_spec.rb +++ b/spec/lib/mangopay/client_spec.rb @@ -6,7 +6,7 @@ describe 'CREATE' do it 'creates a new client' do - expect(new_client['ClientID']).to eq(client_id) + expect(new_client['ClientId']).to eq(client_id) expect(new_client['Passphrase']).not_to be_nil end @@ -14,13 +14,13 @@ expect(wrong_client['errors']).not_to be_nil end - it "ClientID_already_exist" do + it "ClientId_already_exist" do existing_client = MangoPay::Client.create({ - 'ClientID' => new_client['ClientID'], + 'ClientId' => new_client['ClientId'], 'Name' => 'What a nice name' }) - expect(existing_client['Type']).to eq('ClientID_already_exist') - expect(existing_client['Message']).to eq('A partner with this ClientID already exist') + expect(existing_client['Type']).to eq('ClientId_already_exist') + expect(existing_client['Message']).to eq('A partner with this ClientId already exist') end end end diff --git a/spec/lib/mangopay/shared_resources.rb b/spec/lib/mangopay/shared_resources.rb index 4a66a78..45ef865 100644 --- a/spec/lib/mangopay/shared_resources.rb +++ b/spec/lib/mangopay/shared_resources.rb @@ -75,7 +75,7 @@ shared_context 'bank_details' do let(:new_iban_bank_detail) { - MangoPay::BankDetail.create(new_natural_user['Id'], { + MangoPay::BankAccount.create(new_natural_user['Id'], { Type: 'IBAN', OwnerName: 'John', OwnerAddress: 'Here', @@ -113,15 +113,12 @@ shared_context 'bank_wires' do let(:new_bank_wire){ - # TODO: Once the issue with the CreditedWalletID being uppercased is solved - # change DebitedWalletId: new_web_card['CreditedWalletID'] to - # DebitedWalletId: new_web_card['CreditedWalletId'] MangoPay::PayOut::BankWire.create({ AuthorId: new_web_card['CreditedUserId'], DebitedFunds: { Currency: 'EUR', Amount: 500 }, Fees: { Currency: 'EUR', Amount: 0 }, - DebitedWalletId: new_web_card['CreditedWalletID'], - BankDetailsId: new_iban_bank_detail['Id'], + DebitedWalletId: new_web_card['CreditedWalletId'], + BankAccountId: new_iban_bank_detail['Id'], Communication: 'This is a test', Tag: 'Test Bank Wire' }) @@ -170,16 +167,13 @@ card } - # TODO: Once the issue with the CreditedWalletID being uppercased is solved - # change DebitedWalletId: new_web_card['CreditedWalletID'] to - # DebitedWalletId: new_web_card['CreditedWalletId'] let(:new_transfer) { MangoPay::Transfer.create({ AuthorId: web_card_contribution['CreditedUserId'], CreditedUserId: credited_wallet['Owners'][0], DebitedFunds: { Currency: 'EUR', Amount: 500}, Fees: { Currency: 'EUR', Amout: 0}, - DebitedWalletId: web_card_contribution['CreditedWalletID'], + DebitedWalletId: web_card_contribution['CreditedWalletId'], CreditedWalletId: credited_wallet['Id'], Tag: 'Test Transfer' }) From f4d54ded0892ccd990f1fc3ed38bc14acf9b0299 Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 2 Aug 2013 15:59:35 +0200 Subject: [PATCH 31/36] Add Refunds Add refund methods on Payin and Transfer Add the corresponding tests --- lib/mangopay/http_calls.rb | 12 ++++++++++++ lib/mangopay/payin.rb | 2 +- lib/mangopay/transfer.rb | 1 + spec/lib/mangopay/payin_spec.rb | 10 ++++++++++ spec/lib/mangopay/transfer_spec.rb | 10 ++++++++++ 5 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/mangopay/http_calls.rb b/lib/mangopay/http_calls.rb index 5574013..b263780 100644 --- a/lib/mangopay/http_calls.rb +++ b/lib/mangopay/http_calls.rb @@ -37,5 +37,17 @@ def self.included(base) base.extend(ClassMethods) end end + + module Refund + module ClassMethods + def refund(id = nil, params = {}) + MangoPay.request(:post, url(id) + '/refunds', params) + end + end + + def self.included(base) + base.extend(ClassMethods) + end + end end end diff --git a/lib/mangopay/payin.rb b/lib/mangopay/payin.rb index 2ad2883..af05892 100644 --- a/lib/mangopay/payin.rb +++ b/lib/mangopay/payin.rb @@ -1,7 +1,7 @@ module MangoPay class PayIn < Resource include MangoPay::HTTPCalls::Fetch - + include MangoPay::HTTPCalls::Refund module Card class Web < Resource include MangoPay::HTTPCalls::Create diff --git a/lib/mangopay/transfer.rb b/lib/mangopay/transfer.rb index 78dc2d9..8470216 100644 --- a/lib/mangopay/transfer.rb +++ b/lib/mangopay/transfer.rb @@ -2,5 +2,6 @@ module MangoPay class Transfer < Resource include MangoPay::HTTPCalls::Create include MangoPay::HTTPCalls::Fetch + include MangoPay::HTTPCalls::Refund end end diff --git a/spec/lib/mangopay/payin_spec.rb b/spec/lib/mangopay/payin_spec.rb index 7d63860..2c78297 100644 --- a/spec/lib/mangopay/payin_spec.rb +++ b/spec/lib/mangopay/payin_spec.rb @@ -18,4 +18,14 @@ expect(payin['Id']).to eq(new_web_card['Id']) end end + + describe 'REFUND' do + it 'refunds a payin' do + payin_refund = MangoPay::PayIn.refund(new_web_card['Id'], { + AuthorId: new_web_card['AuthorId'] + }) + expect(payin_refund['Id']).not_to be_nil + expect(payin_refund['Status']).to eq('SUCCEEDED') + end + end end diff --git a/spec/lib/mangopay/transfer_spec.rb b/spec/lib/mangopay/transfer_spec.rb index e3b4501..5c0ef2f 100644 --- a/spec/lib/mangopay/transfer_spec.rb +++ b/spec/lib/mangopay/transfer_spec.rb @@ -19,4 +19,14 @@ expect(new_transfer['Status']).to eq('SUCCEEDED') end end + + describe 'REFUND' do + it 'refunds a transfer' do + transfer_refund = MangoPay::Transfer.refund(new_transfer['Id'], { + AuthorId: new_transfer['AuthorId'] + }) + expect(transfer_refund['Id']).not_to be_nil + expect(transfer_refund['Status']).to eq('SUCCEEDED') + end + end end From a7f8c78c863775ac4353171d4eef41f6e93bdb14 Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 2 Aug 2013 16:44:53 +0200 Subject: [PATCH 32/36] Remove the MangoPay Object --- lib/mangopay/mangopay_object.rb | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 lib/mangopay/mangopay_object.rb diff --git a/lib/mangopay/mangopay_object.rb b/lib/mangopay/mangopay_object.rb deleted file mode 100644 index 5d8e844..0000000 --- a/lib/mangopay/mangopay_object.rb +++ /dev/null @@ -1,12 +0,0 @@ -module Mangopay - class MangoPayObject - include Enumerable - - def initialize - end - - def metaclass - class << self; self; end - end - end -end From d327f9a2a602386c1621ec34a81cc917511f67c7 Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 2 Aug 2013 16:45:25 +0200 Subject: [PATCH 33/36] Now checking the client_id with the regexp ^[a-z0-9_-]{4,20}$ before making the request. --- lib/generators/mangopay/install_generator.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/generators/mangopay/install_generator.rb b/lib/generators/mangopay/install_generator.rb index fc5970c..a38cec2 100644 --- a/lib/generators/mangopay/install_generator.rb +++ b/lib/generators/mangopay/install_generator.rb @@ -12,14 +12,22 @@ class InstallGenerator < Rails::Generators::Base desc 'Installs all the basic configuration of the mangopay gem' def setup - remove_file 'config/initializers/mangopay.rb' - @client_id = client_id - @client_passphrase = client_passphrase - template 'mangopay.rb', 'config/initializers/mangopay.rb' + if check_client_id + remove_file 'config/initializers/mangopay.rb' + @client_id = client_id + @client_passphrase = client_passphrase + template 'mangopay.rb', 'config/initializers/mangopay.rb' + else + puts "client_id must match the regexp ^[a-z0-9_-]{4,20}$" + end end protected + def check_client_id + (/^[a-z0-9_-]{4,20}$/ =~ client_id).nil? ? false : true + end + def client_passphrase MangoPay.configure do |c| c.preproduction = options[:preproduction] From fa07b42a6e176a5b290ab86a34f1b16635017797 Mon Sep 17 00:00:00 2001 From: glorieux Date: Fri, 2 Aug 2013 17:27:22 +0200 Subject: [PATCH 34/36] Better generator. The generator now checks the client_id validity before it gets submited. It also alert the user if the client_id is already taken. Refactoring of the methods to be simpler. --- lib/generators/mangopay/install_generator.rb | 34 ++++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/generators/mangopay/install_generator.rb b/lib/generators/mangopay/install_generator.rb index a38cec2..dce6a46 100644 --- a/lib/generators/mangopay/install_generator.rb +++ b/lib/generators/mangopay/install_generator.rb @@ -12,32 +12,46 @@ class InstallGenerator < Rails::Generators::Base desc 'Installs all the basic configuration of the mangopay gem' def setup - if check_client_id + begin + client = client_id_valid? remove_file 'config/initializers/mangopay.rb' @client_id = client_id - @client_passphrase = client_passphrase + @client_passphrase = client['Passphrase'] template 'mangopay.rb', 'config/initializers/mangopay.rb' - else - puts "client_id must match the regexp ^[a-z0-9_-]{4,20}$" + rescue => e + puts e.message end end protected - def check_client_id - (/^[a-z0-9_-]{4,20}$/ =~ client_id).nil? ? false : true + def client_id_valid? + check_client_id_validity + check_client_id_availablility end - def client_passphrase + def check_client_id_validity + if (/^[a-z0-9_-]{4,20}$/ =~ client_id).nil? + raise "The client_id must match the regexp ^[a-z0-9_-]{4,20}$" + end + end + + def check_client_id_availablility + client = create_client + if client['Type'] == 'ClientID_already_exist' + raise client['Message'] + end + client + end + + def create_client MangoPay.configure do |c| c.preproduction = options[:preproduction] end - client = MangoPay::Client.create({ + MangoPay::Client.create({ ClientID: client_id, Name: client_name }) - # TODO: Check for existing id - client['Passphrase'] end end end From 4e3dbc5e7d613d9f30d930257a8fbc40dc8409bb Mon Sep 17 00:00:00 2001 From: glorieux Date: Tue, 6 Aug 2013 11:31:34 +0200 Subject: [PATCH 35/36] Refactoring. --- lib/mangopay.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mangopay.rb b/lib/mangopay.rb index c9499c2..ac0b2d7 100644 --- a/lib/mangopay.rb +++ b/lib/mangopay.rb @@ -76,8 +76,8 @@ def self.user_agent def self.get_uname `uname -a 2>/dev/null`.strip if RUBY_PLATFORM =~ /linux|darwin/i - rescue Errno::ENOMEM => ex - "uname lookup failed" + rescue Errno::ENOMEM + 'uname lookup failed' end def self.get_oauth_token From b11819d98e3958bcade761d46dc85b1307114f30 Mon Sep 17 00:00:00 2001 From: glorieux Date: Thu, 29 Aug 2013 11:21:40 +0200 Subject: [PATCH 36/36] Bump version to 3.0. Add the email field on the client. --- lib/generators/mangopay/install_generator.rb | 4 +++- lib/mangopay/version.rb | 2 +- spec/lib/mangopay/client_spec.rb | 1 + spec/lib/mangopay/shared_resources.rb | 6 ++++-- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/generators/mangopay/install_generator.rb b/lib/generators/mangopay/install_generator.rb index dce6a46..0413f8b 100644 --- a/lib/generators/mangopay/install_generator.rb +++ b/lib/generators/mangopay/install_generator.rb @@ -8,6 +8,7 @@ class InstallGenerator < Rails::Generators::Base argument :client_id, type: :string, desc: 'The id you want to use to query the MangoPay API (must match with the regex ^[a-z0-9_-]{4,20}$)' argument :client_name, type: :string, desc: "Full name of you're organization" + argument :client_email, type: :string, desc: "An email for future contacts" class_option :preproduction, type: :boolean, default: true, desc: 'Whether or not use the preproduction environment' desc 'Installs all the basic configuration of the mangopay gem' @@ -50,7 +51,8 @@ def create_client end MangoPay::Client.create({ ClientID: client_id, - Name: client_name + Name: client_name, + Email: client_email }) end end diff --git a/lib/mangopay/version.rb b/lib/mangopay/version.rb index 7c8ee5c..b411005 100644 --- a/lib/mangopay/version.rb +++ b/lib/mangopay/version.rb @@ -1,3 +1,3 @@ module MangoPay - VERSION = '3.0.0.beta1' + VERSION = '3.0.0' end diff --git a/spec/lib/mangopay/client_spec.rb b/spec/lib/mangopay/client_spec.rb index f7530b4..c6d1311 100644 --- a/spec/lib/mangopay/client_spec.rb +++ b/spec/lib/mangopay/client_spec.rb @@ -7,6 +7,7 @@ describe 'CREATE' do it 'creates a new client' do expect(new_client['ClientId']).to eq(client_id) + expect(new_client['Email']).not_to be_nil expect(new_client['Passphrase']).not_to be_nil end diff --git a/spec/lib/mangopay/shared_resources.rb b/spec/lib/mangopay/shared_resources.rb index 45ef865..885fe8e 100644 --- a/spec/lib/mangopay/shared_resources.rb +++ b/spec/lib/mangopay/shared_resources.rb @@ -11,14 +11,16 @@ let(:wrong_client) { MangoPay::Client.create({ 'ClientID' => wrong_client_id, - 'Name' => 'What a nice name' + 'Name' => 'What a nice name', + 'Email' => 'clientemail@email.com' }) } let(:new_client) { MangoPay::Client.create({ 'ClientID' => client_id, - 'Name' => 'What a nice name' + 'Name' => 'What a nice name', + 'Email' => 'clientemail@email.com' }) } end