Permalink
Browse files

Add ability to credit a payment through the API

  • Loading branch information...
radar committed Apr 17, 2012
1 parent 22e8533 commit 320599ad495b246aad98bb3491cd28cc82d1be2b
@@ -37,6 +37,14 @@ def void
perform_payment_action(:void_transaction)
end
def credit
if params[:amount].to_f > @payment.credit_allowed
render "spree/api/v1/payments/credit_over_limit", :status => 422
else
perform_payment_action(:credit, params[:amount])
end
end
private
def find_order
@@ -48,11 +56,11 @@ def find_payment
@payment = @order.payments.find(params[:id])
end
def perform_payment_action(action)
def perform_payment_action(action, *args)
authorize! action, Payment
begin
@payment.send("#{action}!")
@payment.send("#{action}!", *args)
render :show
rescue Spree::Core::GatewayError => e
@error = e.message
@@ -0,0 +1,2 @@
object false
node(:error) { I18n.t(:credit_over_limit, :limit => @payment.credit_allowed, :scope => 'spree.api') }
@@ -7,6 +7,7 @@ en:
invalid_resource: "Invalid resource. Please fix errors and try again."
resource_not_found: "The resource you were looking for could not be found."
gateway_error: "There was a problem with the payment gateway: %{text}"
credit_over_limit: "This payment can only be credited up to %{limit}. Please specify an amount less than or equal to this number."
order:
could_not_transition: "The order could not be transitioned. Please fix the errors and try again."
@@ -123,6 +123,37 @@ module Spree
payment.reload
payment.state.should == "pending"
end
context "crediting" do
before do
payment.purchase!
end
it "can credit" do
api_put :credit, :id => payment.to_param
response.status.should == 200
payment.reload
payment.state.should == "completed"
# Ensur that a credit payment was created, and it has correct credit amount
credit_payment = Payment.where(:source_type => 'Spree::Payment', :source_id => payment.id).last
credit_payment.amount.to_f.should == -45.75
end
it "returns a 422 status when crediting fails" do
fake_response = stub(:success? => false, :to_s => "NO CREDIT FOR YOU")
Spree::Gateway::Bogus.any_instance.should_receive(:credit).and_return(fake_response)
api_put :credit, :id => payment.to_param
response.status.should == 422
json_response["error"].should == "There was a problem with the payment gateway: NO CREDIT FOR YOU"
end
it "cannot credit over credit_allowed limit" do
api_put :credit, :id => payment.to_param, :amount => 1000000
response.status.should == 422
json_response["error"].should == "This payment can only be credited up to 45.75. Please specify an amount less than or equal to this number."
end
end
end
end

0 comments on commit 320599a

Please sign in to comment.