-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow stripe connect options when create intent
- Loading branch information
1 parent
694d7e5
commit 99a75af
Showing
5 changed files
with
119 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
app/models/solidus_stripe/prepare_options_for_intent_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusStripe | ||
class PrepareOptionsForIntentService | ||
attr_reader :order, :payment_method | ||
|
||
def self.call(...) | ||
new(...).call | ||
end | ||
|
||
def initialize(order, payment_method) | ||
@order = order | ||
@payment_method = payment_method | ||
end | ||
|
||
def call | ||
options = { | ||
description: "Solidus Order ID: #{order.number} (pending)", | ||
currency: order.currency, | ||
confirmation_method: 'automatic', | ||
capture_method: 'manual', | ||
confirm: true, | ||
setup_future_usage: 'off_session', | ||
metadata: { order_id: order.id }, | ||
} | ||
options.merge!(connect_options) if payment_method.preferred_stripe_connect | ||
options | ||
end | ||
|
||
private | ||
|
||
def connect_options | ||
return unless payment_method.preferred_stripe_connect | ||
|
||
opts = { | ||
application_fee_amount: SolidusStripe.configuration.application_fee | ||
} | ||
|
||
case payment_method.preferred_connected_mode | ||
when 'direct_charge' | ||
opts.merge!(stripe_account: connected_account) | ||
when 'destination_charge' | ||
opts.merge!(transfer_data: { destination: connected_account }) | ||
end | ||
opts | ||
end | ||
|
||
def connected_account | ||
payment_method.preferred_connected_account | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
spec/models/solidus_stripe/prepare_options_for_intent_service_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe SolidusStripe::PrepareOptionsForIntentService do | ||
let(:order) { create(:order_with_line_items) } | ||
let(:payment_method) { | ||
Spree::PaymentMethod::StripeCreditCard.create( | ||
name: 'Stripe', | ||
preferred_secret_key: 'secret', | ||
preferred_publishable_key: 'published', | ||
preferred_stripe_connect: stripe_connect, | ||
preferred_connected_account: 'connect_account', | ||
preferred_connected_mode: connected_mode | ||
) | ||
} | ||
let(:connected_mode) { 'direct_charge' } | ||
let(:service) { described_class.new(order, payment_method) } | ||
let(:intent_options) { service.call } | ||
|
||
context 'without stripe connect' do | ||
let(:stripe_connect) { false } | ||
|
||
it 'dont has any connect attributes' do | ||
expect(intent_options[:application_fee]).to be_nil | ||
end | ||
end | ||
|
||
context 'with stripe connect' do | ||
let(:stripe_connect) { true } | ||
|
||
before do | ||
SolidusStripe.configure do |app| | ||
app.application_fee = 5 | ||
end | ||
end | ||
|
||
it 'has application_fee option' do | ||
expect(intent_options[:application_fee_amount]).to eq(5) | ||
end | ||
|
||
it 'has stripe_account option' do | ||
expect(intent_options[:stripe_account]).to eq('connect_account') | ||
end | ||
|
||
context 'with destination_charge mode' do | ||
let(:connected_mode) { 'destination_charge' } | ||
|
||
it 'has transfer_destionation option' do | ||
expect(intent_options[:transfer_data][:destination]).to eq('connect_account') | ||
end | ||
|
||
it 'doesnt have stripe_account option' do | ||
expect(intent_options[:stripe_account]).to be_nil | ||
end | ||
end | ||
end | ||
end |