Skip to content

Commit

Permalink
add payout and payees and webhook logs with specs
Browse files Browse the repository at this point in the history
  • Loading branch information
MauricioMurga committed Apr 17, 2014
1 parent f86f51b commit 7d11415
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions lib/conekta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion lib/conekta/conekta_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/conekta/method.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Conekta
class Method < Resource
end
end
26 changes: 26 additions & 0 deletions lib/conekta/payee.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions lib/conekta/payout.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Conekta
class Payout < Resource
include Conekta::Operations::Find
include Conekta::Operations::Where
include Conekta::Operations::Create
end
end
16 changes: 16 additions & 0 deletions lib/conekta/payout_method.rb
Original file line number Diff line number Diff line change
@@ -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
37 changes: 31 additions & 6 deletions lib/conekta/util.rb
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/conekta/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Conekta
VERSION = '0.3.7'
VERSION = '0.4.0'
end
4 changes: 4 additions & 0 deletions lib/conekta/webhook_log.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Conekta
class WebhookLog < Resource
end
end
75 changes: 67 additions & 8 deletions spec/conekta_spec.rb
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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",
Expand Down

0 comments on commit 7d11415

Please sign in to comment.