diff --git a/CHANGELOG b/CHANGELOG index 5d4e634..a560f68 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -10,3 +10,9 @@ === 0.3.7 2013-01-27 * Add api_version= method. + +=== 0.4.0 2014-04-16 + +* Add payout and payee models with specs. +* Add webhook logs for events. +* Ameliorate convert_to_conekta_object logic. diff --git a/Gemfile b/Gemfile index 0c70739..e683ba3 100644 --- a/Gemfile +++ b/Gemfile @@ -3,5 +3,6 @@ gem 'sys-uname' gem 'rspec' gem 'faraday' gem 'json' +gem 'active_support' # Specify your gem's dependencies in conekta.gemspec gemspec diff --git a/lib/conekta.rb b/lib/conekta.rb index a65f219..38f985d 100644 --- a/lib/conekta.rb +++ b/lib/conekta.rb @@ -23,6 +23,11 @@ require "conekta/plan" require "conekta/token" require "conekta/event" +require "conekta/payee" +require "conekta/payout" +require "conekta/payout_method" +require "conekta/method" +require "conekta/webhook_log" module Conekta @api_base = 'https://api.conekta.io' diff --git a/lib/conekta/conekta_object.rb b/lib/conekta/conekta_object.rb index 8b7cd09..2042317 100644 --- a/lib/conekta/conekta_object.rb +++ b/lib/conekta/conekta_object.rb @@ -73,7 +73,7 @@ def create_method( name, &block ) end def load_from_enumerable(k,v) if v.respond_to? :each and !v.instance_of?(ConektaObject) - v = Conekta::Util.convert_to_conekta_object(v) + v = Conekta::Util.convert_to_conekta_object(k,v) end if self.instance_of?(ConektaObject) self[k] = v diff --git a/lib/conekta/method.rb b/lib/conekta/method.rb new file mode 100644 index 0000000..a375521 --- /dev/null +++ b/lib/conekta/method.rb @@ -0,0 +1,4 @@ +module Conekta + class Method < Resource + end +end diff --git a/lib/conekta/payee.rb b/lib/conekta/payee.rb new file mode 100644 index 0000000..12455ba --- /dev/null +++ b/lib/conekta/payee.rb @@ -0,0 +1,26 @@ +module Conekta + class Payee < Resource + include Conekta::Operations::Find + include Conekta::Operations::Where + include Conekta::Operations::Create + include Conekta::Operations::Delete + include Conekta::Operations::Update + include Conekta::Operations::CustomAction + include Conekta::Operations::CreateMember + def load_from(response=nil) + if response + super + end + payee = self + self.payout_methods.each do |k,v| + if !v.respond_to? :deleted or !v.deleted + v.create_attr('payee', payee) + self.payout_methods.set_val(k,v) + end + end + end + def create_payout_method(params) + self.create_member('payout_methods', params) + end + end +end diff --git a/lib/conekta/payout.rb b/lib/conekta/payout.rb new file mode 100644 index 0000000..0553b89 --- /dev/null +++ b/lib/conekta/payout.rb @@ -0,0 +1,7 @@ +module Conekta + class Payout < Resource + include Conekta::Operations::Find + include Conekta::Operations::Where + include Conekta::Operations::Create + end +end diff --git a/lib/conekta/payout_method.rb b/lib/conekta/payout_method.rb new file mode 100644 index 0000000..3316f87 --- /dev/null +++ b/lib/conekta/payout_method.rb @@ -0,0 +1,16 @@ +module Conekta + class PayoutMethod < Resource + include Conekta::Operations::Delete + include Conekta::Operations::Update + include Conekta::Operations::CustomAction + def url + if id == nil || id == "" + raise Error.new('Could not get the id of ' + self.class.class_name + ' instance.') + end + self.payee.url + self.class.url + "/" + id + end + def delete + self.delete_member('payee','payout_methods') + end + end +end diff --git a/lib/conekta/util.rb b/lib/conekta/util.rb index ab1d575..d64dda4 100644 --- a/lib/conekta/util.rb +++ b/lib/conekta/util.rb @@ -1,7 +1,13 @@ +require 'active_support/inflector' module Conekta module Util def self.types @types ||= { + 'webhook_log' => WebhookLog, + 'bank_transfer_payout_method' => Method, + 'payout' => Payout, + 'payee' => Payee, + 'payout_method' => PayoutMethod, 'bank_transfer_payment' => PaymentMethod, 'card_payment' => PaymentMethod, 'cash_payment' => PaymentMethod, @@ -14,15 +20,34 @@ def self.types 'event' => Event } end - def self.convert_to_conekta_object(resp) - if resp.kind_of?(Hash) and resp.has_key?('object') and types[resp['object']] - instance = types[resp['object']].new() - instance.load_from(resp) - return instance + def self.convert_to_conekta_object(name,resp) + if resp.kind_of?(Hash) + if resp.has_key?('object') and types[resp['object']] + instance = types[resp['object']].new() + instance.load_from(resp) + return instance + elsif name.instance_of? String + name = "event_data" if name.camelize == "Data" + name = "obj" if name.camelize == "Object" + if !Object.const_defined?(name.camelize) + instance = Object.const_set(name.camelize, Class.new(ConektaObject)).new + else + instance = name.camelize.constantize.new + end + instance.load_from(resp) + return instance + end end - if resp.respond_to? :each + if resp.kind_of?(Array) instance = ConektaObject.new instance.load_from(resp) + if !resp.empty? and resp.first.instance_of? Hash and !resp.first["object"] + resp.each_with_index do |r, i| + obj = convert_to_conekta_object(name,r) + instance.set_val(i,obj) + instance[i] = obj + end + end return instance end return instance diff --git a/lib/conekta/version.rb b/lib/conekta/version.rb index 17e02ae..e7fa6e9 100644 --- a/lib/conekta/version.rb +++ b/lib/conekta/version.rb @@ -1,3 +1,3 @@ module Conekta - VERSION = '0.3.7' + VERSION = '0.4.0' end diff --git a/lib/conekta/webhook_log.rb b/lib/conekta/webhook_log.rb new file mode 100644 index 0000000..e9a5635 --- /dev/null +++ b/lib/conekta/webhook_log.rb @@ -0,0 +1,4 @@ +module Conekta + class WebhookLog < Resource + end +end diff --git a/spec/conekta_spec.rb b/spec/conekta_spec.rb index 8f12175..02c3c8c 100644 --- a/spec/conekta_spec.rb +++ b/spec/conekta_spec.rb @@ -1,6 +1,66 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe :conekta_tests do Conekta.api_key = '1tv5yJp3xnVZ7eK67m4h' + describe :payouts_tests do + p "payout tests" + before :each do + @valid_payment_method = {amount: 2000, currency: 'mxn', description: 'Some desc'} + @invalid_payment_method = {amount: 10, currency: 'mxn', description: 'Some desc'} + @valid_visa_card = {card: 'tok_test_visa_4242'} + end + it "succesful get payout" do + payee = Conekta::Payee.create(name: "John Doe", + email: "j_d@radcorp.com", + phone: "555555555", + bank: { + account_number: '123456789012345678', + account_holder: 'J D - Radcorp', + bank: 'Banorte', + description: 'Conekta To JD', + statement_description: 'Conekta To JD 111111111', + statement_reference: '111111111' + }, + billing_address:{ + company_name: 'Rad Corp', + tax_id: 'tax121212abc', + street1: 'Guadalupe 73', + street2: 'Despacho 32', + street3: 'Condesa', + city: 'Cuauhtemoc', + state: 'DF', + country: 'MX', + zip: '06100' + }) + payee.class.class_name.should eq("Payee") + + payee.phone.should eq("555555555") + payee.payout_methods.first.account_number.should eq('123456789012345678') + payee.payout_methods.first.account_holder.should eq('J D - Radcorp') + payee.payout_methods.first.bank.should eq('Banorte') + payee.default_payout_method_id.should_not eq(nil) + + payee.payout_methods.first.description.should eq('Conekta To JD') + payee.payout_methods.first.statement_description.should eq('Conekta To JD 111111111') + payee.payout_methods.first.statement_reference.should eq('111111111') + + payee.billing_address.company_name.should eq('Rad Corp') + payee.billing_address.tax_id.should eq('tax121212abc') + payee.billing_address.zip.should eq('06100') + + payout = Conekta::Payout.create(amount: 5000, + currency: "MXN", + payee: payee.id) + payout.class.class_name.should eq("Payout") + payout.amount.should eq(5000) + payout.currency.should eq("MXN") + + payout.method.account_number.should eq('123456789012345678') + payout.method.account_holder.should eq('J D - Radcorp') + payout.method.bank.should eq('Banorte') +# payout.payout_transaction_id.should_not eq(nil) + payout.transactions.count.should eq(0) + end + end describe :charge_tests do p "charge tests" before :each do @@ -45,7 +105,7 @@ begin cpm = Conekta::Charge.create(pm.merge(card)) rescue Conekta::Error => e - e.message.should eq("The minimum purchase is 3 MXN pesos for card payments") + e.message.should eq("The minimum for card payments is 3 pesos. Check that the amount is in cents as explained in the documentation.") end end it "test susccesful refund" do @@ -117,7 +177,7 @@ :cards => ["tok_test_visa_4241"], }) rescue Conekta::Error => e - e.message.should eq("Token 'tok_test_visa_4241' could not be found.") + e.message.should eq("Object tok_test_visa_4241 could not be found.") end end it "add card to customer" do @@ -178,7 +238,7 @@ begin subscription = customer.create_subscription({plan: 'unexistent-plan'}) rescue Conekta::Error => e - e.message.should eq("Plan 'unexistent-plan' does not exist and cannot be used to create a new subsription.") + e.message.should eq("Object Plan unexistent-plan could not be found.") end end it "test succesful pause subscription" do @@ -279,14 +339,13 @@ events = Conekta::Event.where events.class_name.should eq("ConektaObject") events[0].class_name.should eq("Event") + if !events[0].webhook_logs.empty? + events[0].webhook_logs.first.class_name.should eq("WebhookLog") + end end end describe :token_tests do p "token tests" - it "test succesful where" do - token = Conekta::Token.find("tok_test_visa_4242") - token.class_name.should eq("Token") - end end describe :plan_tests do p "plan tests" @@ -304,7 +363,7 @@ it "test succesful create plan" do plans = Conekta::Plan.where plan = Conekta::Plan.create({ - id: "gold-plan#{plans.count}", + id: ((0...8).map { (65 + rand(26)).chr }.join), name: "Gold Plan", amount: 10000, currency: "MXN",