mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Allow credit owed on payments made via stripe to be refunded via the admin section
This commit is contained in:
committed by
Maikel Linke
parent
687612dba8
commit
c496d0f14d
@@ -1,3 +1,4 @@
|
||||
@import 'plugins/font-awesome';
|
||||
|
||||
.icon-refund:before { @extend .icon-ok:before }
|
||||
.icon-credit:before { @extend .icon-ok:before }
|
||||
|
||||
@@ -43,6 +43,11 @@ module Spree
|
||||
provider.void(response_code, gateway_options)
|
||||
end
|
||||
|
||||
def credit(money, _creditcard, response_code, gateway_options)
|
||||
gateway_options[:stripe_account] = stripe_account_id
|
||||
provider.refund(money, response_code, gateway_options)
|
||||
end
|
||||
|
||||
def create_profile(payment)
|
||||
return unless payment.source.gateway_customer_profile_id.nil?
|
||||
|
||||
|
||||
@@ -66,5 +66,59 @@ describe Spree::Admin::PaymentsController, type: :controller do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "requesting a partial credit on a payment" do
|
||||
let(:params) { { id: payment.id, order_id: order.number, e: :credit } }
|
||||
|
||||
# Required for the respond override in the controller decorator to work
|
||||
before { @request.env['HTTP_REFERER'] = spree.admin_order_payments_url(payment) }
|
||||
|
||||
context "that was processed by stripe" do
|
||||
let!(:payment_method) { create(:stripe_payment_method, distributors: [shop], preferred_enterprise_id: shop.id) }
|
||||
let!(:payment) { create(:payment, order: order, state: 'completed', payment_method: payment_method, response_code: 'ch_1a2b3c', amount: order.total + 5) }
|
||||
|
||||
|
||||
before do
|
||||
allow(Stripe).to receive(:api_key) { "sk_test_12345" }
|
||||
end
|
||||
|
||||
context "where the request succeeds" do
|
||||
before do
|
||||
stub_request(:post, "https://sk_test_12345:@api.stripe.com/v1/charges/ch_1a2b3c/refunds").
|
||||
to_return(:status => 200, :body => JSON.generate(id: 're_123', object: 'refund', status: 'succeeded') )
|
||||
end
|
||||
|
||||
it "partially refunds the payment" do
|
||||
order.reload
|
||||
expect(order.payment_total).to eq order.total + 5
|
||||
expect(order.outstanding_balance).to eq(-5)
|
||||
spree_put :fire, params
|
||||
expect(payment.reload.state).to eq 'completed'
|
||||
order.reload
|
||||
expect(order.payment_total).to eq order.total
|
||||
expect(order.outstanding_balance).to eq 0
|
||||
end
|
||||
end
|
||||
|
||||
context "where the request fails" do
|
||||
before do
|
||||
stub_request(:post, "https://sk_test_12345:@api.stripe.com/v1/charges/ch_1a2b3c/refunds").
|
||||
to_return(:status => 200, :body => JSON.generate(error: { message: "Bup-bow!"}) )
|
||||
end
|
||||
|
||||
it "does not void the payment" do
|
||||
order.reload
|
||||
expect(order.payment_total).to eq order.total + 5
|
||||
expect(order.outstanding_balance).to eq(-5)
|
||||
spree_put :fire, params
|
||||
expect(payment.reload.state).to eq 'completed'
|
||||
order.reload
|
||||
expect(order.payment_total).to eq order.total + 5
|
||||
expect(order.outstanding_balance).to eq -5
|
||||
expect(flash[:error]).to eq "Bup-bow!"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user