Skip to content

Commit

Permalink
Add support for transactions api
Browse files Browse the repository at this point in the history
  • Loading branch information
xxswingxx committed Mar 24, 2023
1 parent 73001a7 commit c131f0f
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# byebug generated
.byebug_history

# rcov generated
coverage
coverage.data
Expand Down
4 changes: 3 additions & 1 deletion lib/quaderno-ruby.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Quaderno
end

Expand All @@ -10,4 +12,4 @@ class Quaderno
require 'quaderno-ruby/collection'

%w(block crud deliver payment retrieve).each { |filename| require "quaderno-ruby/behavior/#{filename}" }
%w(base contact item invoice receipt credit income estimate expense recurring document_item report report_request evidence payment webhook tax checkout_session).each { |filename| require "quaderno-ruby/#{ filename }" }
%w(base contact item transaction invoice receipt credit income estimate expense recurring document_item report report_request evidence payment webhook tax checkout_session).each { |filename| require "quaderno-ruby/#{filename}" }
2 changes: 2 additions & 0 deletions lib/quaderno-ruby/base.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'httparty'
require 'json'

Expand Down
8 changes: 5 additions & 3 deletions lib/quaderno-ruby/behavior/crud.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Quaderno::Behavior
module Crud

Expand Down Expand Up @@ -69,7 +71,7 @@ def find(id, options = {})

def create(params = {})
authentication = get_authentication(params.merge(api_model: api_model))
params.dup.delete_if { |k,v| %w(auth_token access_token api_url mode api_model').include? k.to_s }
params.dup.delete_if { |k, _| %w[auth_token access_token api_url mode api_model].include? k.to_s }

response = post("#{authentication[:url]}#{api_model.api_path}.json",
body: params.to_json,
Expand All @@ -91,7 +93,7 @@ def create(params = {})

def update(id, params = {})
authentication = get_authentication(params.merge(api_model: api_model))
params = params.dup.delete_if { |k,v| %w(auth_token access_token api_url mode api_model').include? k.to_s }
params = params.dup.delete_if { |k, _| %w[auth_token access_token api_url mode api_model].include? k.to_s }

response = put("#{authentication[:url]}#{api_model.api_path}/#{id}.json",
body: params.to_json,
Expand All @@ -114,7 +116,7 @@ def update(id, params = {})
def delete(id, options = {})
authentication = get_authentication(options.merge(api_model: api_model))

response = HTTParty.delete("#{authentication[:url]}#{ api_model.api_path }/#{ id }.json",
response = HTTParty.delete("#{authentication[:url]}#{api_model.api_path}/#{id}.json",
basic_auth: authentication[:basic_auth],
headers: default_headers.merge(authentication[:headers])
)
Expand Down
11 changes: 11 additions & 0 deletions lib/quaderno-ruby/transaction.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class Quaderno::Transaction < Quaderno::Base
api_model Quaderno::Transaction
api_path 'transactions'
is_a_document? true

class << self
undef :all, :find, :update, :delete
end
end
4 changes: 3 additions & 1 deletion lib/quaderno-ruby/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Quaderno
VERSION = "2.1.3"
VERSION = "2.2.0"
end
2 changes: 1 addition & 1 deletion quaderno.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
spec.add_dependency('httparty', '~> 0.21.0')
spec.add_development_dependency('rdoc', '>= 6.3.1')
spec.add_development_dependency('activesupport', '~> 4.2.0')
spec.add_development_dependency('webmock', '~> 1.22.6')
spec.add_development_dependency('webmock', '~> 3.18')
spec.add_development_dependency('vcr', '>= 0')
spec.add_development_dependency('bundler', '~> 2.2')
spec.add_development_dependency('rake', '>= 12.3.3')
Expand Down
3 changes: 2 additions & 1 deletion spec/helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
$LOAD_PATH.unshift(File.dirname(__FILE__))
Expand All @@ -17,4 +19,3 @@
TEST_KEY = 'sk_test_bMz9mJJ5bZnWPwWGuV8y'
TEST_OAUTH_ACCESS_TOKEN = 'afa16c7478f0ba3be222e627c2571d4dd5dca47924996b13a3af377feca47ff0'
OLDEST_SUPPORTED_API_VERSION = 20160602

97 changes: 97 additions & 0 deletions spec/unit/test_quaderno_transaction.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# frozen_string_literal: true

require 'helper'

describe Quaderno::Transaction do
context 'A user with an authentication token' do
before(:each) do
Quaderno::Base.configure do |config|
config.auth_token = TEST_KEY
config.url = TEST_URL
config.api_version = nil
end
end

it 'should create a sale transaction' do
sale_params = {
type: 'sale', customer: { first_name: 'John', last_name: 'Smith', email: '[email protected]' },
evidence: { billing_country: 'US', ip_address: '255.255.255.255', bank_country: 'US' },
currency: 'USD',
items: [
{
description: 'Awesome ebook', quantity: 1, amount: 19,
tax: {
city: nil, country: 'US', county: nil, currency: 'USD', name: nil, notes: nil,
notice: '', product_type: 'service', rate: 0.0, region: 'NY', tax_behavior: 'exclusive',
tax_code: 'ebook', taxable_part: nil, import: true, subtotal: nil, tax_amount: nil, total_amount: nil
}
},
{
description: 'Awesome SaaS', quantity: 1, amount: 89,
tax: {
city: 'NEW YORK', country: 'US', county: 'NEW YORK', currency: 'USD', name: 'Sales tax', notes: nil,
notice: '', product_type: 'service', rate: 8.875, region: 'NY', tax_behavior: 'exclusive',
tax_code: 'saas', taxable_part: 100.0, import: true, subtotal: nil, tax_amount: nil, total_amount: nil
}
}
],
payment: { method: 'credit_card', processor: 'processor12345', processor_id: 'txn_12345' },
processor: 'processor12345', processor_id: 'txn_12345', po_number: 'PO_12345',
tags: 'tag-a,tag-b,tag-c',
custom_metadata: { anything_you_want: 'extra info' }
}

VCR.use_cassette('new sale') do
@transaction = Quaderno::Transaction.create(sale_params)
end

expect(@transaction.is_a?(Quaderno::Transaction)).to be true
expect(@transaction.type).to eq 'Invoice'
expect(@transaction.currency).to eq sale_params[:currency]
expect(@transaction.items.count).to eq sale_params[:items].count
expect(@transaction.total_cents).to eq(sale_params[:items].sum { |item| item[:amount] * 100 })
expect(@transaction.contact.full_name).to eq "#{sale_params[:customer][:first_name]} #{sale_params[:customer][:last_name]}"
end

it 'should create a refund transaction' do
refund_params = {
type: 'refund', customer: { first_name: 'John', last_name: 'Smith', email: '[email protected]' },
evidence: { billing_country: 'US', ip_address: '255.255.255.255', bank_country: 'US' },
currency: 'USD',
items: [
{
description: 'Awesome ebook', quantity: 1, amount: 19,
tax: {
city: nil, country: 'US', county: nil, currency: 'USD', name: nil, notes: nil,
notice: '', product_type: 'service', rate: 0.0, region: 'NY', tax_behavior: 'exclusive',
tax_code: 'ebook', taxable_part: nil, import: true, subtotal: nil, tax_amount: nil, total_amount: nil
}
},
{
description: 'Awesome SaaS', quantity: 1, amount: 89,
tax: {
city: 'NEW YORK', country: 'US', county: 'NEW YORK', currency: 'USD', name: 'Sales tax', notes: nil,
notice: '', product_type: 'service', rate: 8.875, region: 'NY', tax_behavior: 'exclusive',
tax_code: 'saas', taxable_part: 100.0, import: true, subtotal: nil, tax_amount: nil, total_amount: nil
}
}
],
payment: { method: 'credit_card', processor: 'processor12345', processor_id: 'txn_12345' },
processor: 'processor12345', processor_id: 'txn_12345', po_number: 'PO_12345',
tags: 'tag-a,tag-b,tag-c',
custom_metadata: { anything_you_want: 'extra info' }
}

VCR.use_cassette('new refund') do
@transaction = Quaderno::Transaction.create(refund_params)
end

expect(@transaction.is_a?(Quaderno::Transaction)).to be true
expect(@transaction.type).to eq 'Credit'
expect(@transaction.currency).to eq refund_params[:currency]
expect(@transaction.items.count).to eq refund_params[:items].count
expect(@transaction.total_cents).to eq(refund_params[:items].sum { |item| item[:amount] * 100 })
expect(@transaction.contact.full_name).to eq "#{refund_params[:customer][:first_name]} #{refund_params[:customer][:last_name]}"
end
end
end

0 comments on commit c131f0f

Please sign in to comment.