mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Extract Stripe payment helper methods to a service
This commit is contained in:
@@ -153,25 +153,6 @@ module Spree
|
||||
I18n.t('payment_method_fee')
|
||||
end
|
||||
|
||||
# Returns the current payment status from a live call to the Stripe API.
|
||||
# Returns nil if the payment is not a Stripe payment or does not have a payment intent.
|
||||
# If the payment requires authorization the status will be "requires_action".
|
||||
# If the payment has been captured the status will be "succeeded".
|
||||
# https://stripe.com/docs/api/payment_intents/object#payment_intent_object-status
|
||||
def stripe_status
|
||||
return if response_code.blank?
|
||||
|
||||
Stripe::PaymentIntentValidator.new.call(self).status
|
||||
rescue Stripe::StripeError
|
||||
# The Stripe::PaymentIntentValidator will raise an error if the Stripe API call indicates
|
||||
# the last attempted action on the payment intent failed.
|
||||
"failed"
|
||||
end
|
||||
|
||||
def stripe_captured?
|
||||
stripe_status == "succeeded"
|
||||
end
|
||||
|
||||
def clear_authorization_url
|
||||
update_attribute(:cvv_response_message, nil)
|
||||
end
|
||||
|
||||
32
app/services/stripe_payment_status.rb
Normal file
32
app/services/stripe_payment_status.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class StripePaymentStatus
|
||||
def initialize(payment)
|
||||
@payment = payment
|
||||
end
|
||||
|
||||
# Returns the current payment status from a live call to the Stripe API.
|
||||
# Returns nil if the payment is not a Stripe payment or does not have a payment intent.
|
||||
# If the payment requires authorization the status will be "requires_action".
|
||||
# If the payment has been captured the status will be "succeeded".
|
||||
# Docs: https://stripe.com/docs/api/payment_intents/object#payment_intent_object-status
|
||||
def stripe_status
|
||||
return if payment.response_code.blank?
|
||||
|
||||
Stripe::PaymentIntentValidator.new.call(self).status
|
||||
rescue Stripe::StripeError
|
||||
# Stripe::PaymentIntentValidator will raise an error if the response from the Stripe API
|
||||
# call indicates the last attempted action on the payment intent failed.
|
||||
"failed"
|
||||
end
|
||||
|
||||
# If the payment is a Stripe payment and has been captured in the associated Stripe account,
|
||||
# returns true, otherwise false.
|
||||
def stripe_captured?
|
||||
stripe_status == "succeeded"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :payment
|
||||
end
|
||||
55
spec/services/stripe_payment_status_spec.rb
Normal file
55
spec/services/stripe_payment_status_spec.rb
Normal file
@@ -0,0 +1,55 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
describe StripePaymentStatus do
|
||||
subject { StripePaymentStatus.new(payment) }
|
||||
let(:payment) { build(:payment) }
|
||||
|
||||
describe '#stripe_status' do
|
||||
context "when the payment is not a Stripe payment or does not have a payment intent" do
|
||||
it "returns nil" do
|
||||
expect(subject.stripe_status).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context "when the payment has a payment intent" do
|
||||
before { allow(payment).to receive(:response_code) { "pi_1234" } }
|
||||
|
||||
it "fetches the status with Stripe::PaymentIntentValidator" do
|
||||
expect(Stripe::PaymentIntentValidator).
|
||||
to receive_message_chain(:new, :call, :status) { true }
|
||||
|
||||
subject.stripe_status
|
||||
end
|
||||
|
||||
context "and the last action on the Stripe payment failed" do
|
||||
it "returns failed response" do
|
||||
allow(Stripe::PaymentIntentValidator).
|
||||
to receive_message_chain(:new, :call, :status).and_raise(Stripe::StripeError)
|
||||
|
||||
expect(subject.stripe_status).to eq "failed"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#stripe_captured?' do
|
||||
context "when the payment is not a Stripe payment or does not have a payment intent" do
|
||||
it "returns false" do
|
||||
expect(subject.stripe_captured?).to eq false
|
||||
end
|
||||
end
|
||||
|
||||
context "when the Stripe payment has been captured" do
|
||||
before { allow(payment).to receive(:response_code) { "pi_1234" } }
|
||||
|
||||
it "returns true" do
|
||||
allow(Stripe::PaymentIntentValidator).
|
||||
to receive_message_chain(:new, :call, :status) { "succeeded" }
|
||||
|
||||
expect(subject.stripe_captured?).to eq true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user