Skip to content

Commit

Permalink
fix way that avoid turn into paid payments that are pending_refund
Browse files Browse the repository at this point in the history
  • Loading branch information
devton committed May 13, 2015
1 parent 2b988ad commit b44a37a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
9 changes: 2 additions & 7 deletions app/controllers/catarse_pagarme/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ def create
payment.payment_notifications.create(contribution: payment.contribution, extra_data: params.to_json)

if PagarMe::validate_fingerprint(payment.try(:gateway_id), params[:fingerprint])

if params[:current_status] == 'paid' && params[:desired_status] == 'refunded'
payment.try(:invalid_refund)
else
delegator.change_status_by_transaction(params[:current_status])
delegator.update_transaction
end
delegator.change_status_by_transaction(params[:current_status])
delegator.update_transaction

return render nothing: true, status: 200
end
Expand Down
6 changes: 5 additions & 1 deletion app/models/catarse_pagarme/payment_delegator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ def initialize(payment)
def change_status_by_transaction(transactin_status)
case transactin_status
when 'paid', 'authorized' then
self.payment.pay unless self.payment.paid?
if payment.refunded? || payment.pending_refund?
payment.invalid_refund
elsif !self.payment.paid?
self.payment.pay
end
when 'refunded' then
self.payment.refund unless self.payment.refunded?
when 'refused' then
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy/app/models/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def notification_template_for_failed_project
end
end

def invalid_refund
true
end

def refunded?
true
end
Expand Down
26 changes: 26 additions & 0 deletions spec/models/catarse_pagarme/payment_delegator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,33 @@

%w(paid authorized).each do |status|
context "when status is #{status}" do
context "when payment is refunded" do
before do
payment.stub(:paid?).and_return(false)
payment.stub(:refunded?).and_return(true)
payment.stub(:pending_refund?).and_return(false)
payment.should_receive(:invalid_refund)
end

it { delegator.change_status_by_transaction(status) }
end

context "when payment is pending_refund" do
before do
payment.stub(:paid?).and_return(false)
payment.stub(:refunded?).and_return(false)
payment.stub(:pending_refund?).and_return(true)
payment.should_receive(:invalid_refund)
end

it { delegator.change_status_by_transaction(status) }
end

context "and payment is already paid" do
before do
payment.stub(:paid?).and_return(true)
payment.stub(:refunded?).and_return(false)
payment.stub(:pending_refund?).and_return(false)
payment.should_not_receive(:pay)
end

Expand All @@ -116,6 +140,8 @@
context "and payment is not paid" do
before do
payment.stub(:paid?).and_return(false)
payment.stub(:refunded?).and_return(false)
payment.stub(:pending_refund?).and_return(false)
payment.should_receive(:pay)
end

Expand Down

0 comments on commit b44a37a

Please sign in to comment.