Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Add Storefront API Checkout Create Payment endpoint (#11436)

Damian Legawiecd77996c9a764
diff --git a/api/app/controllers/spree/api/v2/storefront/account/credit_cards_controller.rb b/api/app/controllers/spree/api/v2/storefront/account/credit_cards_controller.rb
index f985b4eabb..3aa3cc1735 100644
--- a/api/app/controllers/spree/api/v2/storefront/account/credit_cards_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/account/credit_cards_controller.rb
@@ -23,7 +23,10 @@ module Spree
end
def scope
- super.where(user: spree_current_user, payment_method: current_store.payment_methods.available_on_front_end)
+ super.not_expired.not_removed.where(
+ user: spree_current_user,
+ payment_method: current_store.payment_methods.available_on_front_end
+ )
end
def collection_serializer
diff --git a/api/app/controllers/spree/api/v2/storefront/checkout_controller.rb b/api/app/controllers/spree/api/v2/storefront/checkout_controller.rb
index bfcf47de50..a7d0a87dbb 100644
--- a/api/app/controllers/spree/api/v2/storefront/checkout_controller.rb
+++ b/api/app/controllers/spree/api/v2/storefront/checkout_controller.rb
@@ -44,6 +44,16 @@ module Spree
render_order(result)
end
+ def create_payment
+ result = create_payment_service.call(order: spree_current_order, params: params)
+
+ if result.success?
+ render_serialized_payload(201) { serialize_resource(spree_current_order.reload) }
+ else
+ render_error_payload(result.error)
+ end
+ end
+
def add_store_credit
spree_authorize! :update, spree_current_order, order_token
@@ -118,6 +128,10 @@ module Spree
Spree::Api::Dependencies.storefront_shipment_serializer.constantize
end
+ def create_payment_service
+ Spree::Api::Dependencies.storefront_payment_create_service.constantize
+ end
+
def serialize_payment_methods(payment_methods)
payment_methods_serializer.new(payment_methods, params: serializer_params).serializable_hash
end
diff --git a/api/config/routes.rb b/api/config/routes.rb
index 4efb731f11..a9197c6296 100644
--- a/api/config/routes.rb
+++ b/api/config/routes.rb
@@ -145,6 +145,7 @@ Spree::Core::Engine.add_routes do
patch :next
patch :advance
patch :complete
+ post :create_payment
post :add_store_credit
post :remove_store_credit
get :payment_methods
diff --git a/api/docs/v2/storefront/index.yaml b/api/docs/v2/storefront/index.yaml
index ebb1ab3c71..adb067f555 100644
--- a/api/docs/v2/storefront/index.yaml
+++ b/api/docs/v2/storefront/index.yaml
@@ -730,13 +730,13 @@ paths:
source_attributes:
type: object
properties:
- number:
+ gateway_payment_profile_id:
type: string
month:
type: string
year:
type: string
- verification_value:
+ cc_type:
type: string
name:
type: string
@@ -778,10 +778,11 @@ paths:
payments_attributes:
- payment_method_id: '1'
source_attributes:
- number: '4111111111111111'
+ gateway_payment_profile_id: 'ABC123'
+ cc_type: 'visa'
+ last_digits: '1111'
month: '10'
year: '2022'
- verification_value: '123'
name: John Doe
description: |-
##### Add Customer Details
@@ -867,6 +868,92 @@ paths:
summary: Complete Checkout
tags:
- Checkout / State
+ /api/v2/storefront/checkout/create_payment:
+ post:
+ description: |-
+ Creates new Payment for the current checkout.
+
+ You can either create new payment source (eg. a Credit Card) or use an existing (for signed in users only).
+
+ Newly created payment source will be associated to the current signed in user.
+
+ System will automatically invalidate previous (non-finalized) payments (excluding store credit / gift card payments).
+
+ [More details on payment system](https://dev-docs.spreecommerce.org/internals/payments)
+ operationId: create-payment
+ responses:
+ '200':
+ $ref: '#/components/responses/Cart'
+ '404':
+ $ref: '#/components/responses/404NotFound'
+ '422':
+ $ref: '#/components/responses/422UnprocessableEntity'
+ security:
+ - orderToken: []
+ - bearerAuth: []
+ requestBody:
+ content:
+ application/vnd.api+json:
+ schema:
+ type: object
+ properties:
+ payment_method_id:
+ type: string
+ required: true
+ description: ID of the selected Payment Method
+ source_id:
+ type: string
+ description: ID of the selected Payment Source (eg. Credit Card, only for signed in users)
+ amount:
+ type: number
+ description: Amount for the newly created payment, when left blank will use the entire Order Total (reduced by used Store Credits / Gift Cards)
+ source_attributes:
+ type: object
+ properties:
+ gateway_payment_profile_id:
+ type: string
+ description: Payment source authorization token, more details for [Stripe implementation](https://stripe.com/docs/payments/accept-a-payment-charges#web-create-token)
+ required: true
+ cc_type:
+ type: string
+ enum:
+ - visa
+ - mastercard
+ - amex
+ last_digits:
+ type: string
+ description: Last 4 digits of CC number
+ month:
+ type: string
+ description: Expiration date month
+ year:
+ type: string
+ description: Expiration date year
+ name:
+ type: string
+ description: Card holder name
+ required: true
+ examples:
+ Create new Credit Card:
+ value:
+ payment_method_id: '1'
+ source_attributes:
+ gateway_payment_profile_id: 'card_1JqvNB2eZvKYlo2C5OlqLV7S'
+ cc_type: 'visa'
+ last_digits: '1111'
+ month: '10'
+ year: '2026'
+ name: 'John Snow'
+ Use existing Credit Card:
+ value:
+ payment_metod: '1'
+ source_id: '1'
+ parameters:
+ - $ref: '#/components/parameters/CartIncludeParam'
+ - $ref: '#/components/parameters/SparseFieldsCart'
+ summary: Create new Payment
+ tags:
+ - Checkout / Payments
/api/v2/storefront/checkout/add_store_credit:
post:
description: |-
@@ -2230,15 +2317,15 @@ components:
example: visa
last_digits:
type: string
- example: '1232'
+ example: '1111'
description: Last 4 digits of CC number
month:
type: string
description: Expiration date month
- example: '10'
+ example: '12'
year:
type: string
- example: '2019'
+ example: '2026'
description: Expiration date year
name:
type: string
@@ -3022,7 +3109,7 @@ components:
shipped_at:
type: string
format: date-time
- example: '2019-01-02 13:42:12'
+ example: '2021-01-02 13:42:12'
description: Date when Shipment was being sent from the warehouse
nullable: true
relationships:
@@ -5865,9 +5952,9 @@ components:
type: credit_card
attributes:
cc_type: visa
- last_digits: '1232'
- month: '10'
- year: '2019'
+ last_digits: '4111'
+ month: '12'
+ year: '2026'
name: John Doe
default: true
relationships:
@@ -5914,9 +6001,9 @@ components:
type: credit_card
attributes:
cc_type: visa
- last_digits: '1232'
- month: '10'
- year: '2019'
+ last_digits: '1111'
+ month: '12'
+ year: '2026'
name: John Doe
default: true
relationships:
@@ -15993,6 +16080,7 @@ tags:
- name: Cart / Other
- name: Checkout
- name: Checkout / State
+ - name: Checkout / Payments
- name: Checkout / Store Credit
- name: CMS Pages
- name: Countries
diff --git a/api/lib/spree/api/api_dependencies.rb b/api/lib/spree/api/api_dependencies.rb
index 4252a6a723..a94288bbde 100644
--- a/api/lib/spree/api/api_dependencies.rb
+++ b/api/lib/spree/api/api_dependencies.rb
@@ -20,7 +20,8 @@ module Spree
:storefront_account_create_address_service, :storefront_account_update_address_service, :storefront_address_finder,
:storefront_account_create_service, :storefront_account_update_service, :storefront_collection_sorter, :error_handler,
:storefront_cart_empty_service, :storefront_cart_destroy_service, :storefront_credit_cards_destroy_service, :platform_products_sorter,
- :storefront_cart_change_currency_service,
+ :storefront_cart_change_currency_service, :storefront_payment_serializer,
+ :storefront_payment_create_service,
:platform_admin_user_serializer, :platform_coupon_handler, :platform_order_update_service,
:platform_order_use_store_credit_service, :platform_order_remove_store_credit_service,
@@ -74,9 +75,12 @@ module Spree
@storefront_account_create_address_service = Spree::Dependencies.account_create_address_service
@storefront_account_update_address_service = Spree::Dependencies.account_update_address_service
- # credit cards
+ # credit card services
@storefront_credit_cards_destroy_service = Spree::Dependencies.credit_cards_destroy_service
+ # payment services
+ @storefront_payment_create_service = Spree::Dependencies.payment_create_service
+
# serializers
@storefront_address_serializer = 'Spree::V2::Storefront::AddressSerializer'
@storefront_cart_serializer = 'Spree::V2::Storefront::CartSerializer'
@@ -88,6 +92,7 @@ module Spree
@storefront_shipment_serializer = 'Spree::V2::Storefront::ShipmentSerializer'
@storefront_taxon_serializer = 'Spree::V2::Storefront::TaxonSerializer'
@storefront_payment_method_serializer = 'Spree::V2::Storefront::PaymentMethodSerializer'
+ @storefront_payment_serializer = 'Spree::V2::Storefront::PaymentSerializer'
@storefront_product_serializer = 'Spree::V2::Storefront::ProductSerializer'
@storefront_estimated_shipment_serializer = 'Spree::V2::Storefront::EstimatedShippingRateSerializer'
@storefront_store_serializer = 'Spree::V2::Storefront::StoreSerializer'
diff --git a/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb b/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
index b8a852d496..7135638f63 100644
--- a/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
@@ -23,11 +23,12 @@ describe 'API V2 Storefront Checkout Spec', type: :request do
let(:payment_source_attributes) do
{
- number: '4111111111111111',
+ gateway_payment_profile_id: 'BGS-123',
+ gateway_customer_profile_id: 'BGS-123',
month: 1.month.from_now.month,
year: 1.month.from_now.year,
- verification_value: '123',
- name: 'Spree Commerce'
+ name: 'Spree Commerce',
+ last_digits: '1111'
}
end
let(:payment_params) do
@@ -581,6 +582,104 @@ describe 'API V2 Storefront Checkout Spec', type: :request do
end
end
+ describe 'checkout#create_payment' do
+ 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(:execute) { post '/api/v2/storefront/checkout/create_payment?include=payments', headers: headers, params: params }
+ let!(:payment_method) { create(:credit_card_payment_method, stores: [store]) }
+ let(:payment_source) { payment_method.payment_source_class.last }
+ let(:payment) { order.payments.last }
+
+ shared_examples 'creates a payment' do
+ before { execute }
+
+ it_behaves_like 'returns 201 HTTP status'
+
+ it 'returns new payment' do
+ expect(json_response['data']).to have_relationship(:payments).with_data([{ 'id' => payment.id.to_s, 'type' => 'payment' }])
+ expect(json_response['included'][0]).to have_id(payment.id.to_s)
+ expect(json_response['included'][0]).to have_type('payment')
+ expect(json_response['included'][0]).to have_attribute(:amount).with_value(order.total.to_s)
+ expect(json_response['included'][0]).to have_jsonapi_attributes(:amount, :response_code, :number, :cvv_response_code, :cvv_response_message, :payment_method_id, :payment_method_name, :state)
+ expect(json_response['included'][0]).to have_relationship(:payment_method).with_data({ 'id' => payment_method.id.to_s, 'type' => 'payment_method' })
+ end
+
+ it 'creates new payment record' do
+ expect { change(order.payments, :count).by(1) }
+ end
+ end
+
+ shared_examples 'creates a payment source' do
+ let(:execute) { post '/api/v2/storefront/checkout/create_payment?include=payments.source', headers: headers, params: params }
+
+ before { execute }
+
+ it 'returns new payment source' do
+ expect(json_response['included'][0]).to have_id(payment_source.id.to_s)
+ expect(json_response['included'][0]).to have_type('credit_card')
+ expect(json_response['included'][0]).to have_relationship(:payment_method).with_data({ 'id' => payment_method.id.to_s, 'type' => 'payment_method' })
+ expect(json_response['included'][0]).to have_attribute(:last_digits).with_value('1111')
+ expect(json_response['included'][0]).to have_attribute(:name).with_value('John')
+ expect(json_response['included'][0]).to have_attribute(:year).with_value(2021)
+ expect(json_response['included'][0]).to have_attribute(:month).with_value(12)
+ end
+
+ it 'creates new payment source record' do
+ expect { change(payment_method.payment_source_class, :count).by(1) }
+ end
+ end
+
+ context 'as a guest user' do
+ include_context 'creates guest order with guest token'
+
+ it_behaves_like 'creates a payment'
+ it_behaves_like 'creates a payment source'
+ end
+
+ context 'as a signed in user' do
+ include_context 'order with a physical line item'
+
+ context 'new payment source' do
+ it_behaves_like 'creates a payment'
+ it_behaves_like 'creates a payment source'
+
+ it 'assigns new payment source to the signed in user' do
+ expect { execute }.to change(user.credit_cards, :count).by(1)
+ expect(payment_source.user).to eq(user)
+ end
+ end
+
+ context 'existing payment source' do
+ let!(:payment_source) { create(:credit_card, user: user, payment_method: payment_method) }
+
+ let(:params) do
+ {
+ payment_method_id: payment_method.id,
+ source_id: payment_source.id
+ }
+ end
+
+ it_behaves_like 'creates a payment'
+
+ it 'does not create new payment source record' do
+ expect { execute }.not_to change(payment_method.payment_source_class, :count)
+ end
+ end
+ end
+ end
+
describe 'checkout#shipping_rates' do
let(:execute) { get '/api/v2/storefront/checkout/shipping_rates', headers: headers }
diff --git a/core/app/models/spree/gateway/bogus.rb b/core/app/models/spree/gateway/bogus.rb
index 70b7589e96..7a07f89b02 100644
--- a/core/app/models/spree/gateway/bogus.rb
+++ b/core/app/models/spree/gateway/bogus.rb
@@ -71,6 +71,10 @@ module Spree
true
end
+ def payment_source_class
+ CreditCard
+ end
+
def actions
%w(capture void credit)
end
diff --git a/core/app/services/spree/payments/create.rb b/core/app/services/spree/payments/create.rb
new file mode 100644
index 0000000000..ca022dd12f
--- /dev/null
+++ b/core/app/services/spree/payments/create.rb
@@ -0,0 +1,66 @@
+module Spree
+ module Payments
+ class Create
+ prepend Spree::ServiceModule::Base
+
+ def call(order:, params: {})
+ ApplicationRecord.transaction do
+ run :prepare_payment_attributes
+ run :find_or_create_payment_source
+ run :save_payment
+ end
+ end
+
+ protected
+
+ def prepare_payment_attributes(order:, params:)
+ payment_method = order.available_payment_methods.find { |pm| pm.id.to_s == params[:payment_method_id]&.to_s }
+
+ payment_attributes = {
+ amount: params[:amount] || order.order_total_after_store_credit,
+ payment_method: payment_method
+ }
+
+ return failure(nil, :payment_method_not_found) if payment_method.blank?
+
+ success(order: order, params: params, payment_attributes: payment_attributes)
+ end
+
+ def find_or_create_payment_source(order:, params:, payment_attributes:)
+ payment_method = payment_attributes[:payment_method]
+
+ if payment_method&.source_required?
+ if order.user.present? && params[:source_id].present?
+ source = payment_method.payment_source_class.find_by(id: params[:source_id], user: order.user)
+
+ return failure(nil, :source_not_found) if source.nil?
+ else
+ result = Wallet::CreatePaymentSource.call(
+ payment_method: payment_method,
+ params: params.delete(:source_attributes),
+ user: order.user
+ )
+
+ return failure(nil, result.error.value) if result.failure?
+
+ source = result.value
+ end
+
+ payment_attributes[:source] = source
+ end
+
+ success(order: order, payment_attributes: payment_attributes)
+ end
+
+ def save_payment(order:, payment_attributes:)
+ payment = order.payments.new(payment_attributes)
+
+ if payment.save
+ success(payment)
+ else
+ failure(payment)
+ end
+ end
+ end
+ end
+end
diff --git a/core/app/services/spree/wallet/create_payment_source.rb b/core/app/services/spree/wallet/create_payment_source.rb
new file mode 100644
index 0000000000..c54e5b36fa
--- /dev/null
+++ b/core/app/services/spree/wallet/create_payment_source.rb
@@ -0,0 +1,26 @@
+module Spree
+ module Wallet
+ class CreatePaymentSource
+ prepend Spree::ServiceModule::Base
+
+ def call(payment_method:, params: {}, user: nil)
+ return failure(nil, :missing_attributes) if params.nil?
+
+ source_attributes = {
+ payment_method_id: payment_method.id,
+ 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
diff --git a/core/lib/spree/core/app_dependencies.rb b/core/lib/spree/core/app_dependencies.rb
index 918477cdbd..a8fb514f91 100644
--- a/core/lib/spree/core/app_dependencies.rb
+++ b/core/lib/spree/core/app_dependencies.rb
@@ -15,7 +15,8 @@ module Spree
:address_finder, :collection_sorter, :error_handler, :current_store_finder, :cart_empty_service, :cart_destroy_service,
:classification_reposition_service, :credit_cards_destroy_service, :cart_associate_service, :cart_change_currency_service,
:line_item_create_service, :line_item_update_service, :line_item_destroy_service,
- :order_approve_service, :order_cancel_service, :shipment_change_state_service, :shipment_update_service
+ :order_approve_service, :order_cancel_service, :shipment_change_state_service, :shipment_update_service,
+ :payment_create_service
].freeze
attr_accessor *INJECTION_POINTS
@@ -94,6 +95,8 @@ module Spree
@line_item_update_service = 'Spree::LineItems::Update'
@line_item_destroy_service = 'Spree::LineItems::Destroy'
+ @payment_create_service = 'Spree::Payments::Create'
+
# errors
@error_handler = 'Spree::ErrorReporter'
end
diff --git a/core/spec/services/spree/payments/create_spec.rb b/core/spec/services/spree/payments/create_spec.rb
new file mode 100644
index 0000000000..4acd76c225
--- /dev/null
+++ b/core/spec/services/spree/payments/create_spec.rb
@@ -0,0 +1,155 @@
+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: '[email protected]') }
+
+ 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
+
+ it { expect(execute.success?).to eq(true) }
+ it { expect(execute.value).to be_kind_of(Spree::Payment) }
+ 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(:params) do
+ {
+ payment_method_id: payment_method.id,
+ amount: order.total,
+ source_id: payment_source.id
+ }
+ end
+
+ context 'valid source' do
+ let(:user) { create(:user) }
+ let(:order) { create(:order_with_totals, store: store, user: user) }
+ let!(:payment_source) { create(:credit_card, payment_method: payment_method, user: user) }
+
+ include_context 'creates a payment'
+
+ it { expect { execute }.not_to change(payment_method.payment_source_class, :count) }
+ end
+
+ context 'source assigned to a different user' do
+ let(:user) { create(:user) }
+ let(:order) { create(:order_with_totals, store: store, user: user) }
+ let!(:payment_source) { create(:credit_card, payment_method: payment_method, user: create(:user)) }
+
+ it { expect(execute.success?).to eq(false) }
+ it { expect(execute.error.to_s).to eq('source_not_found') }
+ end
+ end
+
+ context 'without source' do
+ let(:params) do
+ {
+ payment_method_id: payment_method.id,
+ amount: order.total,
+ source_id: 'wrong-id'
+ }
+ end
+
+ let(:user) { create(:user) }
+ let(:order) { create(:order_with_totals, store: store, user: user) }
+
+ it { expect(execute.success?).to eq(false) }
+ it { expect(execute.error.to_s).to eq('source_not_found') }
+ end
+ end
+
+ context 'missing payment method' do
+ let(:params) do
+ {
+ 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
+
+ it { expect(execute.success?).to eq(false) }
+ it { expect(execute.error.to_s).to eq('payment_method_not_found') }
+ end
+
+ context 'invalid attributes' do
+ let(:params) do
+ {
+ payment_method_id: payment_method.id,
+ amount: order.total,
+ source_attributes: {
+ gateway_payment_profile_id: '',
+ cc_type: '',
+ last_digits: '',
+ name: 'John',
+ month: '12',
+ year: '2021'
+ }
+ }
+ end
+
+ it { expect(execute.success?).to eq(false) }
+ it { expect(execute.error.value).to be_kind_of(ActiveModel::Errors) }
+ end
+ end
+end
diff --git a/core/spec/services/spree/wallet/create_payment_source_spec.rb b/core/spec/services/spree/wallet/create_payment_source_spec.rb
new file mode 100644
index 0000000000..b56bd0d81a
--- /dev/null
+++ b/core/spec/services/spree/wallet/create_payment_source_spec.rb
@@ -0,0 +1,84 @@
+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
+
+ it 'assigns payment method' do
+ expect(payment_source.payment_method).to eq(payment_method)
+ 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
+
+ context 'missing attributes' do
+ let(:params) { nil }
+
+ it { expect(execute.success?).to eq(false) }
+ it { expect(execute.error.to_s).to eq('missing_attributes') }
+ end
+
+ context 'invalid attributes' do
+ let(:params) do
+ {
+ gateway_payment_profile_id: '',
+ cc_type: 'visa',
+ last_digits: '',
+ name: '',
+ month: '',
+ year: ''
+ }
+ end
+
+ it { expect(execute.success?).to eq(false) }
+ it { expect(execute.error.value).to be_kind_of(ActiveModel::Errors) }
+ end
+ end
+end