diff --git a/.gitignore b/.gitignore index 7fdffeb..5e623af 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ Gemfile.lock .idea releases/ +*.gem \ No newline at end of file diff --git a/Rakefile b/Rakefile index 0e00744..ac4efa7 100644 --- a/Rakefile +++ b/Rakefile @@ -1,11 +1,24 @@ require 'bundler/gem_tasks' require 'rake/testtask' -Rake::TestTask.new(:test) do |t| +Rake::TestTask.new(:unit) do |t| + t.libs << 'test' + t.libs << 'lib' + t.test_files = FileList['test/**/unit_test.rb'] +end + +Rake::TestTask.new(:contract) do |t| + t.libs << 'test' + t.libs << 'lib' + t.test_files = FileList['test/contract/**/*_test.rb'] +end + +Rake::TestTask.new(:full) do |t| t.libs << 'test' t.libs << 'lib' t.test_files = FileList['test/**/*_test.rb'] end -task :default => :test + +task :default => :full diff --git a/compropago_sdk.gemspec b/compropago_sdk.gemspec index 0ed0154..d5d6ab3 100644 --- a/compropago_sdk.gemspec +++ b/compropago_sdk.gemspec @@ -12,7 +12,7 @@ Dir.glob('./lib/compropago_sdk/tools/*.rb').each do |archivo| li.push archivo en Gem::Specification.new do |spec| spec.name = 'compropago_sdk' - spec.version = '3.0.3' + spec.version = '3.0.4' spec.authors = ['Eduardo Aguilar'] spec.email = ['eduardo.aguilar@compropago.com'] diff --git a/lib/compropago_sdk.rb b/lib/compropago_sdk.rb index 48fc9a2..4decd20 100644 --- a/lib/compropago_sdk.rb +++ b/lib/compropago_sdk.rb @@ -22,6 +22,7 @@ require 'compropago_sdk/factory/models/instructions' require 'compropago_sdk/factory/models/new_order_info' require 'compropago_sdk/factory/models/order_info' +require 'compropago_sdk/factory/models/exchange' require 'compropago_sdk/factory/models/place_order_info' require 'compropago_sdk/factory/models/provider' require 'compropago_sdk/factory/models/sms_data' diff --git a/lib/compropago_sdk/client.rb b/lib/compropago_sdk/client.rb index e35ef79..d2c402d 100644 --- a/lib/compropago_sdk/client.rb +++ b/lib/compropago_sdk/client.rb @@ -3,7 +3,7 @@ class Client API_LIVE_URI = 'https://api.compropago.com/v1/' API_SANDBOX_URI = 'https://api.compropago.com/v1/' - VERSION = '3.0.3' + VERSION = '3.0.4' attr_accessor :publickey, :privatekey, :live, :deploy_uri, :api diff --git a/lib/compropago_sdk/factory/models/exchange.rb b/lib/compropago_sdk/factory/models/exchange.rb new file mode 100644 index 0000000..4b3c003 --- /dev/null +++ b/lib/compropago_sdk/factory/models/exchange.rb @@ -0,0 +1,11 @@ +class Exchange + + attr_accessor :rate + attr_accessor :request + attr_accessor :origin_amount + attr_accessor :final_amount + attr_accessor :origin_currency + attr_accessor :final_currency + attr_accessor :exchange_id + +end \ No newline at end of file diff --git a/lib/compropago_sdk/factory/models/fee_details.rb b/lib/compropago_sdk/factory/models/fee_details.rb index 3a60d24..90b6bbd 100644 --- a/lib/compropago_sdk/factory/models/fee_details.rb +++ b/lib/compropago_sdk/factory/models/fee_details.rb @@ -1,12 +1,11 @@ class FeeDetails attr_accessor :amount + attr_accessor :tax + attr_accessor :tax_percent attr_accessor :currency attr_accessor :type attr_accessor :description - attr_accessor :application - attr_accessor :tax_percent attr_accessor :amount_refunded - attr_accessor :tax end \ No newline at end of file diff --git a/lib/compropago_sdk/factory/models/instruction_details.rb b/lib/compropago_sdk/factory/models/instruction_details.rb index 27cffe7..5d1ca41 100644 --- a/lib/compropago_sdk/factory/models/instruction_details.rb +++ b/lib/compropago_sdk/factory/models/instruction_details.rb @@ -1,16 +1,16 @@ class InstructionDetails - attr_accessor :amount - attr_accessor :store attr_accessor :payment_amount attr_accessor :payment_store - attr_accessor :bank_account_holder_name + attr_accessor :amount + attr_accessor :store attr_accessor :bank_account_number - attr_accessor :bank_reference attr_accessor :company_reference_name attr_accessor :company_reference_number attr_accessor :company_bank_number attr_accessor :order_reference_number + attr_accessor :bank_account_holder_name + attr_accessor :bank_reference attr_accessor :bank_name end \ No newline at end of file diff --git a/lib/compropago_sdk/factory/models/new_order_info.rb b/lib/compropago_sdk/factory/models/new_order_info.rb index 569fe58..dfddbab 100644 --- a/lib/compropago_sdk/factory/models/new_order_info.rb +++ b/lib/compropago_sdk/factory/models/new_order_info.rb @@ -10,6 +10,7 @@ class NewOrderInfo attr_accessor :order_info attr_accessor :fee_details attr_accessor :instructions + attr_accessor :api_version def initialize @order_info = OrderInfo.new diff --git a/lib/compropago_sdk/factory/models/order_info.rb b/lib/compropago_sdk/factory/models/order_info.rb index aea2506..4ecf677 100644 --- a/lib/compropago_sdk/factory/models/order_info.rb +++ b/lib/compropago_sdk/factory/models/order_info.rb @@ -1,12 +1,12 @@ class OrderInfo attr_accessor :order_id - attr_accessor :order_price attr_accessor :order_name - attr_accessor :payment_method - attr_accessor :store - attr_accessor :country + attr_accessor :order_price attr_accessor :image_url - attr_accessor :success_url + attr_accessor :exchange + def initialize + @exchange = Exchange.new + end end \ No newline at end of file diff --git a/lib/compropago_sdk/factory/serialize.rb b/lib/compropago_sdk/factory/serialize.rb index 996e568..02237ec 100644 --- a/lib/compropago_sdk/factory/serialize.rb +++ b/lib/compropago_sdk/factory/serialize.rb @@ -39,7 +39,7 @@ def self.cp_order_info(data=nil) obj.fee_details = Serialize::fee_details(data['fee_details']) obj.order_info = Serialize::order_info(data['order_info']) - obj.customer.customer_name = Serialize::customer(data['customer']) + obj.customer = Serialize::customer(data['customer']) obj.captured = data['captured'] obj.failure_message = data['failure_message'] @@ -89,13 +89,12 @@ def self.fee_details(data=nil) obj = FeeDetails.new obj.amount = data['amount'] ? data['amount'] : nil + obj.tax = data['tax'] ? data['tax'] : nil + obj.tax_percent = data['tax_percent'] ? data['tax_percent'] : nil obj.currency = data['currency'] ? data['currency'] : nil obj.type = data['type'] ? data['type'] : nil obj.description = data['description'] ? data['description'] : nil - obj.application = data['application'] ? data['application'] : nil - obj.tax_percent = data['tax_percent'] ? data['tax_percent'] : nil obj.amount_refunded = data['amount_refunded'] ? data['amount_refunded'] : nil - obj.tax = data['tax'] ? data['tax'] : nil return obj end @@ -107,17 +106,17 @@ def self.instruction_details(data=nil) else obj = InstructionDetails.new - obj.amount = data['amount'] ? data['amount'] : nil - obj.store = data['store'] ? data['store'] : nil obj.payment_amount = data['payment_amount'] ? data['payment_amount'] : nil obj.payment_store = data['payment_store'] ? data['payment_store'] : nil - obj.bank_account_holder_name = data['bank_account_holder_name'] ? data['bank_account_holder_name'] : nil - obj.bank_account_number = data['bank_accont_number'] ? data['bank_accont_number'] : nil - obj.bank_reference = data['bank_reference'] ? data['bank_reference'] : nil + obj.amount = data['amount'] ? data['amount'] : nil + obj.store = data['store'] ? data['store'] : nil + obj.bank_account_number = data['bank_account_number'] ? data['bank_account_number'] : nil obj.company_reference_name = data['company_reference_name'] ? data['company_reference_name'] : nil obj.company_reference_number = data['company_reference_number'] ? data['company_reference_number'] : nil obj.company_bank_number = data['company_bank_number'] ? data['company_bank_number'] : nil obj.order_reference_number = data['order_reference_number'] ? data['order_reference_number'] : nil + obj.bank_account_holder_name = data['bank_account_holder_name'] ? data['bank_account_holder_name'] : nil + obj.bank_reference = data['bank_reference'] ? data['bank_reference'] : nil obj.bank_name = data['bank_name'] ? data['bank_name'] : nil return obj @@ -160,6 +159,7 @@ def self.new_order_info(data=nil) obj.order_info = Serialize::order_info(data['order_info']) obj.fee_details = Serialize::fee_details(data['fee_details']) obj.instructions = Serialize::instructions(data['instructions']) + obj.api_version = data['api_version'] return obj end @@ -172,13 +172,28 @@ def self.order_info(data=nil) obj = OrderInfo.new obj.order_id = data['order_id'] ? data['order_id'] : nil - obj.order_price = data['order_price'] ? data['order_price'] : nil obj.order_name = data['order_name'] ? data['order_name'] : nil - obj.payment_method = data['payment_method'] ? data['payment_method'] : nil - obj.store = data['store'] ? data['store'] : nil - obj.country = data['country'] ? data['country'] : nil + obj.order_price = data['order_price'] ? data['order_price'] : nil obj.image_url = data['image_url'] ? data['image_url'] : nil - obj.success_url = data['success_url'] ? data['success_url'] : nil + obj.exchange = Serialize::exchange(data['exchange']) + + return obj + end + end + + def self.exchange(data=nil) + if data.nil? + return Exchange.new + else + obj = Exchange.new + + obj.rate = data['rate'] ? data['rate'] : nil + obj.request = data['request'] ? data['request'] : nil + obj.origin_amount = data['origin_amount'] ? data['origin_amount'] : nil + obj.final_amount = data['final_amount'] ? data['final_amount'] : nil + obj.origin_currency = data['origin_currency'] ? data['origin_currency'] : nil + obj.final_currency = data['final_currency'] ? data['final_currency'] : nil + obj.exchange_id = data['exchange_id'] ? data['exchange_id'] : nil return obj end diff --git a/test/contract/neworder_test.rb b/test/contract/neworder_test.rb new file mode 100644 index 0000000..0b10828 --- /dev/null +++ b/test/contract/neworder_test.rb @@ -0,0 +1,87 @@ +require 'minitest/autorun' + +# Importar el archivo principal de la gema (lib/scafold_gem) +require 'compropago_sdk' + +class NeworderTest < MiniTest::Unit::TestCase + + def setup + @client = Client.new('pk_live_760db54141134cbcb0', 'sk_live_6432b0141302941f8b', true) + @order_info = { + :order_id => 1232, + :order_name => 'M4 Contract SDK ruby', + :order_price => 123, + :customer_name => 'Eduardo Aguilar Y', + :customer_email => 'eduardo.aguilar@compropago.com', + :payment_type => 'OXXO' + } + end + + def test_main + res = false + begin + order = Factory::get_instance_of('PlaceOrderInfo', @order_info) + new_order = @client.api.place_order(order) + + if (new_order.id =~ /^ch_([0-9a-z]*)-([0-9a-z]*)-([0-9a-z]*)-([0-9a-z]*)-([0-9a-z]*)$/) == nil + raise 'Error: id == '+new_order.id + end + if (new_order.short_id =~ /^[0-9a-z]{6}$/) == nil + raise 'Error: short_id == '+new_order.short_id + end + unless new_order.object == 'charge' + raise 'Error: object == '+new_order.object + end + unless new_order.created.is_a? Numeric + raise 'Error: created == '+new_order.created.class + end + unless new_order.exp_date.is_a? Numeric + raise 'Error: exp_date == '+new_order.exp_date.class + end + unless new_order.status.is_a? String || new_order.status != 'pending' + raise 'Error: status == '+new_order.status.class+'|'+new_order.status + end + unless !!new_order.live_mode == new_order.live_mode + raise 'Error: live_mode == '+new_order.live_mode.class + end + if new_order.api_version != '1.0' && new_order.api_version != '1.1' + raise 'Error: api_version == '+new_order.api_version + end + + validate_order_info new_order.order_info + + res = true + rescue => ex + puts ex.message + end + + assert res + end + + def validate_order_info(order_info) + if order_info.order_id == '' || order_info.order_id.nil? + raise 'Error: order_info.order_id == '+order_info.order_id + end + if order_info.order_name == '' || order_info.order_name.nil? + raise 'Error: order_info.order_name == '+order_info.order_name + end + if (order_info.order_price =~ /^[0-9]+(\.[0-9]+)*$/) == nil + raise 'Error: order_info.order_price == '+order_info.order_price+'|'+order_info.order_price.class + end + + validate_exchange order_info.exchange + end + + def validate_exchange(exchange) + unless exchange.origin_amount.is_a? Numeric + raise 'Error: exchange.origin_amount == '+exchange.origin_amount.class + end + if exchange.origin_currency != 'MXN' && exchange.origin_currency != 'USD' && exchange.origin_currency != 'GBP' && exchange.origin_currency != 'EUR' + raise 'Error: exchange.origin_currency == '+exchange.origin_currency + end + unless exchange.final_amount.is_a? Numeric + raise 'Error: exchange.final_amount == '+exchange.final_amount + end + end + +end \ No newline at end of file diff --git a/test/unit_test.rb b/test/unit_test.rb index 90134ce..4caebc8 100644 --- a/test/unit_test.rb +++ b/test/unit_test.rb @@ -18,7 +18,7 @@ def setup order_name: 'M4 unit ruby', order_price: 123.45, customer_name: 'Eduardo Aguilar', - customer_email: 'eduardo.aguilar@compropago.com' + customer_email: 'aguilar@compropago.com' } end @@ -28,7 +28,7 @@ def test_create_client obj = Client.new @publickey, @privatekey, @mode res = true rescue => ex - puts '====>>'+ex.message + puts 'Error: '+ex.message end assert res @@ -42,7 +42,7 @@ def test_eval_auth res = evl.is_a? EvalAuthInfo rescue => ex - puts '====>>'+ex.message + puts 'Error: '+ex.message end assert res @@ -56,7 +56,7 @@ def test_providers res = (providers.is_a?(Array) && providers[0].is_a?(Provider)) rescue => ex - puts '====>>'+ex.message + puts 'Error: '+ex.message end assert res @@ -75,7 +75,7 @@ def test_providers_limit end end rescue => ex - puts '====>>'+ex.message + puts 'Error: '+ex.message res = false end @@ -95,7 +95,7 @@ def test_providers_currency end end rescue => ex - puts '====>> '+ex.message + puts 'Error: '+ex.message res = false end @@ -111,7 +111,7 @@ def test_place_order res = response.is_a? NewOrderInfo rescue => ex - puts '====>> '+ex.message + puts 'Error: '+ex.message end assert res end @@ -123,13 +123,14 @@ def test_place_order_expdate time = Time.now.to_i + (6 * 60 * 60) @order_info[:expiration_time] = time + @order_info[:customer_email] = 'asd@asd.com' order = Factory::get_instance_of 'PlaceOrderInfo', @order_info response = obj.api.place_order order res = response.is_a?(NewOrderInfo) && (time == response.exp_date) rescue => ex - puts '====>> '+ex.message + puts 'Error: '+ex.message end assert res end @@ -138,6 +139,9 @@ def test_service_verify_order res = false begin obj = Client.new @publickey, @privatekey, @mode + + @order_info[:customer_email] = 'qwe@qwe.com' + order = Factory::get_instance_of 'PlaceOrderInfo', @order_info resp = obj.api.place_order order ver = obj.api.verify_order resp.id @@ -154,6 +158,9 @@ def test_service_send_sms res = false begin obj = Client.new @publickey, @privatekey, @mode + + @order_info[:customer_email] = 'ert@ert.com' + order = Factory::get_instance_of 'PlaceOrderInfo', @order_info resp = obj.api.place_order order sms = obj.api.send_sms_instructions @phone_number, resp.id @@ -174,7 +181,7 @@ def test_list_webhooks res = list.is_a?(Array) && list[0].is_a?(Webhook) rescue => ex - puts 'Eerror: '+ex.message + puts ex.message end assert res end @@ -187,7 +194,7 @@ def test_create_webhook res = webhook.is_a? Webhook rescue => ex - puts 'Eerror: '+ex.message + puts ex.message end assert res end @@ -201,7 +208,7 @@ def test_update_webhook res = webhook.is_a? Webhook rescue => ex - puts 'Eerror: '+ex.message + puts ex.message end assert res end @@ -215,7 +222,7 @@ def test_delete_webhook res = webhook.is_a? Webhook rescue => ex - puts 'Eerror: '+ex.message + puts ex.message end assert res end