-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a984e7
commit cde713b
Showing
5 changed files
with
197 additions
and
25 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Spree | ||
module Wallet | ||
class CreatePaymentSource | ||
prepend Spree::ServiceModule::Base | ||
|
||
def call(payment_method:, params: {}, user: nil) | ||
return failure(:missing_attributes) if params.nil? | ||
|
||
source_attributes = { | ||
user_id: user&.id, | ||
gateway_payment_profile_id: params[:gateway_payment_profile_id], | ||
gateway_customer_profile_id: params[:gateway_customer_profile_id], | ||
last_digits: params[:last_digits], | ||
month: params[:month], | ||
year: params[:year], | ||
name: params[:name] | ||
} | ||
|
||
source = payment_method.payment_source_class.new(source_attributes) | ||
|
||
source.save ? success(source) : failure(source) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
require 'spec_helper' | ||
|
||
module Spree | ||
describe Payments::Create do | ||
subject { described_class } | ||
|
||
let(:store) { create(:store) } | ||
let(:order) { create(:order_with_totals, store: store, user: nil, email: 'john@snow.org') } | ||
|
||
let(:execute) { subject.call(order: order, params: params) } | ||
let(:value) { execute.value } | ||
|
||
let(:params) do | ||
{ | ||
payment_method_id: payment_method.id, | ||
amount: order.total, | ||
source_attributes: { | ||
gateway_payment_profile_id: '12345', | ||
cc_type: 'visa', | ||
last_digits: '1111', | ||
name: 'John', | ||
month: '12', | ||
year: '2021' | ||
} | ||
} | ||
end | ||
|
||
let!(:payment_method) { create(:credit_card_payment_method, stores: [store]) } | ||
let(:payment_source) { payment_method.payment_source_class.last } | ||
let(:payment) { order.payments.last } | ||
|
||
context 'valid attributes' do | ||
shared_context 'creates a payment' do | ||
it 'creates new payment record' do | ||
expect { execute }.to change(order.payments, :count).by(1) | ||
expect(payment.payment_method).to eq(payment_method) | ||
end | ||
end | ||
|
||
shared_context 'creates a payment source' do | ||
it 'creates new payment source record' do | ||
expect { execute }.to change(payment_method.payment_source_class, :count).by(1) | ||
expect(payment.payment_source).to eq(payment_source) | ||
end | ||
end | ||
|
||
context 'with new source attributes' do | ||
include_context 'creates a payment' | ||
include_context 'creates a payment source' | ||
|
||
context 'with user' do | ||
let(:user) { create(:user) } | ||
let(:order) { create(:order_with_totals, store: store, user: user) } | ||
|
||
include_context 'creates a payment' | ||
include_context 'creates a payment source' | ||
|
||
context 'assigns user' do | ||
before { execute } | ||
|
||
it { expect(payment_source.user).to eq(user) } | ||
end | ||
end | ||
end | ||
|
||
context 'with existing source' do | ||
let(:payment_source) { create(:credit_card, payment_method: payment_method) } | ||
let(:params) do | ||
{ | ||
payment_method_id: payment_method.id, | ||
amount: order.total, | ||
source_id: payment_source.id | ||
} | ||
end | ||
|
||
include_context 'creates a payment' | ||
|
||
it { expect { execute }.not_to change(payment_method.payment_source_class, :count) } | ||
end | ||
|
||
context 'without source' do | ||
|
||
end | ||
end | ||
|
||
context 'missing payment method' do | ||
|
||
end | ||
|
||
context 'invalid attributes' do | ||
|
||
end | ||
end | ||
end |
57 changes: 57 additions & 0 deletions
57
core/spec/services/spree/wallet/create_payment_source_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,57 @@ | ||
require 'spec_helper' | ||
|
||
module Spree | ||
describe Wallet::CreatePaymentSource do | ||
subject { described_class } | ||
|
||
let(:store) { create(:store) } | ||
|
||
let(:execute) { subject.call(payment_method: payment_method, params: params) } | ||
let(:value) { execute.value } | ||
|
||
let(:params) do | ||
{ | ||
gateway_payment_profile_id: '12345', | ||
cc_type: 'visa', | ||
last_digits: '1111', | ||
name: 'John', | ||
month: '12', | ||
year: '2021' | ||
} | ||
end | ||
|
||
let!(:payment_method) { create(:credit_card_payment_method, stores: [store]) } | ||
let(:payment_source) { payment_method.payment_source_class.last } | ||
|
||
context 'valid attributes' do | ||
shared_context 'creates a payment source' do | ||
before { execute } | ||
|
||
it 'creates new payment source record' do | ||
expect { change(payment_method.payment_source_class, :count).by(1) } | ||
end | ||
|
||
it 'returns newly created record in .value' do | ||
expect(execute.value).to be_kind_of(payment_method.payment_source_class) | ||
end | ||
end | ||
|
||
context 'with source attributes' do | ||
include_context 'creates a payment source' | ||
|
||
context 'with user' do | ||
let(:user) { create(:user) } | ||
let(:execute) { subject.call(payment_method: payment_method, params: params, user: user) } | ||
|
||
include_context 'creates a payment source' | ||
|
||
context 'assigns user' do | ||
before { execute } | ||
|
||
it { expect(payment_source.user).to eq(user) } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |