create a PaymentEngine::Interface to handle with missing methods on payment engines [# 67561316]

Esse commit está contido em:
Antônio Roberto Silva
2014-03-18 12:50:31 -03:00
commit 61766f2147
5 arquivos alterados com 63 adições e 31 exclusões
@@ -3,27 +3,18 @@ module Contribution::PaymentEngineHandler
included do
delegate :can_do_refund?, to: :payment_engine
def payment_engine
PaymentEngines.find_engine(self.payment_method) rescue nil
PaymentEngines.find_engine(self.payment_method) || PaymentEngines::Interface.new
end
def can_do_refund?
engine_handler { |engine| engine[:can_do_refund?] }
def review_path
payment_engine.review_path(self)
end
%i(review_path direct_refund).each do |method_name|
define_method method_name do
engine_handler { |engine| engine[method_name].try(:call, self) }
end
def direct_refund
payment_engine.direct_refund(self)
end
private
def engine_handler
if payment_engine
yield payment_engine
end
end
end
end
+2 -2
Ver Arquivo
@@ -3,7 +3,7 @@ class PaymentEngines
def self.find_engine name
@@engines.find do |engine|
engine[:name].downcase == name.downcase
engine.name.downcase == name.downcase
end
end
@@ -16,7 +16,7 @@ class PaymentEngines
end
def self.engines
@@engines.sort{|a,b| (a[:locale] == I18n.locale.to_s ? -1 : 1) }
@@engines.sort{|a,b| (a.locale == I18n.locale.to_s ? -1 : 1) }
end
def self.create_payment_notification attributes
+15
Ver Arquivo
@@ -0,0 +1,15 @@
class PaymentEngines
class Interface
def name; end
def locale; end
def review_path(contribution); end
def can_do_refund?; end
def direct_refund(contribution); end
end
end
+18 -5
Ver Arquivo
@@ -3,10 +3,23 @@
require 'spec_helper'
describe PaymentEngines do
let(:engine){ {name: 'test', review_path: ->(contribution){ "/#{contribution}" }, locale: 'en'} }
let(:engine_pt){ {name: 'test pt', review_path: ->(contribution){ "/#{contribution}" }, locale: 'pt'} }
let(:contribution){ FactoryGirl.create(:contribution) }
before{ PaymentEngines.clear }
let(:paypal_engine) { double }
let(:moip_engine) { double }
before do
PaymentEngines.clear
paypal_engine.stub(:name).and_return('PayPal')
paypal_engine.stub(:review_path).with(contribution).and_return("/#{contribution}")
paypal_engine.stub(:locale).and_return('en')
moip_engine.stub(:name).and_return('MoIP')
moip_engine.stub(:review_path).with(contribution).and_return("/#{contribution}")
moip_engine.stub(:locale).and_return('pt')
end
let(:engine){ paypal_engine }
let(:engine_pt){ moip_engine }
describe ".find_engine" do
before do
@@ -14,9 +27,9 @@ describe PaymentEngines do
PaymentEngines.register engine_pt
end
subject { PaymentEngines.find_engine('test pt') }
subject { PaymentEngines.find_engine('MoIP') }
it { should == engine_pt}
it { should == engine_pt }
end
describe ".register" do
@@ -1,15 +1,23 @@
require 'spec_helper'
describe Contribution::PaymentEngineHandler do
let(:engine){ { name: 'moip', review_path: ->(contribution){ "/#{contribution}" }, locale: 'en' } }
let(:contribution){ create(:contribution, payment_method: 'MoIP') }
let(:moip_engine) { double }
before do
Contribution.any_instance.unstub(:direct_refund)
PaymentEngines.clear
engine
moip_engine.stub(:name).and_return('MoIP')
moip_engine.stub(:review_path).and_return("/#{contribution}")
moip_engine.stub(:locale).and_return('pt')
moip_engine.stub(:can_do_refund?).and_return(false)
moip_engine.stub(:direct_refund).and_return(false)
end
let(:engine){ moip_engine }
describe "#payment_engine" do
subject { contribution.payment_engine }
@@ -20,7 +28,7 @@ describe Contribution::PaymentEngineHandler do
end
context "when contribution don't have a payment engine" do
it { should eq(nil) }
it { should be_a_kind_of(PaymentEngines::Interface) }
end
end
@@ -29,8 +37,8 @@ describe Contribution::PaymentEngineHandler do
context "when contribution has a payment engine with direct refund enabled" do
before do
PaymentEngines.register(engine.merge!({ can_do_refund?: true }))
contribution.stub(:direct_refund).and_return(true)
moip_engine.stub(:can_do_refund?).and_return(true)
PaymentEngines.register(engine)
end
it { should be_true }
@@ -50,7 +58,9 @@ describe Contribution::PaymentEngineHandler do
context "when contribution has a payment engine with direct refund enabled" do
before do
PaymentEngines.register(engine.merge!({ direct_refund: ->(contribution) { true } }))
moip_engine.stub(:can_do_refund?).and_return(true)
moip_engine.stub(:direct_refund).and_return(true)
PaymentEngines.register(engine)
end
it { should be_true }
@@ -69,9 +79,12 @@ describe Contribution::PaymentEngineHandler do
subject { contribution.review_path }
context "when contribution has a payment engine" do
before { PaymentEngines.register engine }
before do
contribution.stub(:payment_engine).and_return(engine)
PaymentEngines.register engine
end
it { should eq(engine[:review_path].call(contribution)) }
it { should eq(engine.review_path(contribution)) }
end
context "when contribution don't have a payment engine" do