mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-08 22:56:06 +00:00
Add spec for StripeSCA payment gateway
We didn't actually have a unit test for this.
This commit is contained in:
52
spec/models/spree/gateway/stripe_sca_spec.rb
Normal file
52
spec/models/spree/gateway/stripe_sca_spec.rb
Normal file
@@ -0,0 +1,52 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe Spree::Gateway::StripeSCA, type: :model do
|
||||
before { allow(Stripe).to receive(:api_key) { "sk_test_12345" } }
|
||||
|
||||
describe "#purchase" do
|
||||
let(:order) { create(:order_with_totals_and_distribution) }
|
||||
let(:credit_card) { create(:credit_card) }
|
||||
let(:payment) {
|
||||
create(
|
||||
:payment,
|
||||
state: "checkout",
|
||||
order: order,
|
||||
amount: order.total,
|
||||
payment_method: subject,
|
||||
source: credit_card,
|
||||
)
|
||||
}
|
||||
let(:gateway_options) {
|
||||
{ order_id: order.number }
|
||||
}
|
||||
let(:payment_authorised) {
|
||||
payment_intent(payment.amount, "requires_capture")
|
||||
}
|
||||
let(:capture_successful) {
|
||||
payment_intent(payment.amount, "succeeded")
|
||||
}
|
||||
|
||||
it "captures the payment" do
|
||||
stub_request(:get, "https://api.stripe.com/v1/payment_intents/12345").
|
||||
to_return(status: 200, body: payment_authorised)
|
||||
stub_request(:post, "https://api.stripe.com/v1/payment_intents/12345/capture").
|
||||
with(body: {"amount_to_capture" => "10.0"}).
|
||||
to_return(status: 200, body: capture_successful)
|
||||
|
||||
response = subject.purchase(order.total, credit_card, gateway_options)
|
||||
|
||||
expect(response.success?).to eq true
|
||||
end
|
||||
end
|
||||
|
||||
def payment_intent(amount, status)
|
||||
JSON.generate(
|
||||
object: "payment_intent",
|
||||
amount: amount,
|
||||
status: status,
|
||||
charges: { data: [{ id: "ch_1234", amount: amount }] }
|
||||
)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user