Class: Spree::PayoutProvider::Base

Inherits:
Object
  • Object
show all
Extended by:
ApiTyped
Defined in:
app/models/spree/payout_provider/base.rb

Overview

Contract for whatever actually moves money to a seller.

Core records both levels of the ledger for every marketplace — what each seller earned as their orders shipped, and what has been settled to them. A provider decides whether that money moves on its own: the built-in one records and leaves the operator to pay by bank, while a connected provider performs the transfer and reports back.

Three verbs, one per thing that can happen to money:

transfer! — a seller earned on a fulfilled order (level one)
pay!      — their accrued earnings are settled (level two)
reverse!  — a refund takes some of an earning back

Every verb must be idempotent, keyed on the record's prefixed id: a crash between the remote call and the local commit is retried, and a retry must find the movement it already made rather than make a second.

Providers are stateless and constructed without arguments, as tax and delivery-rate providers are; anything request-specific arrives as an argument.

Direct Known Subclasses

System

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApiTyped

api_type, api_type_for, api_typed_family, class_name_for, find, registered_class

Class Method Details

.available_for_store?(_store) ⇒ Boolean

Whether this provider can be selected for a store — an external one overrides it to require connected credentials.

Parameters:

Returns:

  • (Boolean)


80
81
82
# File 'app/models/spree/payout_provider/base.rb', line 80

def self.available_for_store?(_store)
  true
end

.display_name ⇒ String

The name a merchant sees when choosing how sellers are paid.

Returns:

  • (String)


30
31
32
# File 'app/models/spree/payout_provider/base.rb', line 30

def self.display_name
  name.demodulize.titleize
end

.provider_key ⇒ String

What a ledger row records as having moved its money.

The full class name rather than its last segment: every provider gem calls its class PayoutProvider, so the leaf alone identifies nothing and would make two gems' rows indistinguishable — including to the unique index on (provider, reference) that guards retries.

Returns:

  • (String)


42
43
44
# File 'app/models/spree/payout_provider/base.rb', line 42

def self.provider_key
  name
end

.reference_system ⇒ String

Returns:

  • (String)


71
72
73
# File 'app/models/spree/payout_provider/base.rb', line 71

def self.reference_system
  name.demodulize.underscore
end

.requires_payout_account? ⇒ Boolean

Whether this provider needs sellers to hold an account with it before they can be paid. A marketplace settling by bank transfer does not; a connected provider does, and core will not credit a seller it cannot pay through one.

Returns:

  • (Boolean)


90
91
92
# File 'app/models/spree/payout_provider/base.rb', line 90

def self.requires_payout_account?
  false
end

.to_api_hash(store) ⇒ Hash

The key a seller's account with this provider is recorded under, as a ExternalReference system.

Underscored rather than the class name, because a system key is a lowercase identifier by contract — and where a provider gem also ships a Integration, the convention is that the two agree, so a connector, its references and its settings page name the same thing.

How a provider describes itself to the settings picker, so an operator chooses from what is actually installed and can see which of them this store could use today.

Parameters:

Returns:

  • (Hash)


60
61
62
63
64
65
66
67
68
# File 'app/models/spree/payout_provider/base.rb', line 60

def self.to_api_hash(store)
  {
    id: api_type,
    name: display_name,
    available: available_for_store?(store),
    requires_payout_account: requires_payout_account?,
    default: name == Spree.default_payout_provider.to_s
  }
end

Instance Method Details

#available_payout(_seller, _currency) ⇒ BigDecimal?

How much of what a seller is owed this provider can move right now.

What the ledger says a seller has earned and what their account can actually send are different figures: money credited on fulfilment is only payable once the customer's payment settles. A provider that knows the difference answers it here, and the sweep settles what fits.

Nil means there is no limit — the built-in provider moves no money, so everything owed is payable. A provider that cannot find out must raise rather than answer nil: the sweep settles the whole balance on nil, and asking for more than an account holds is what this exists to prevent.

Parameters:

Returns:

  • (BigDecimal, nil)


194
195
196
# File 'app/models/spree/payout_provider/base.rb', line 194

def available_payout(_seller, _currency)
  nil
end

#idempotency_key(record) ⇒ String

The key a provider passes to its own API so a retried call cannot move money twice.

Parameters:

Returns:

  • (String)


215
216
217
# File 'app/models/spree/payout_provider/base.rb', line 215

def idempotency_key(record)
  "spree-#{record.prefixed_id}"
end

#onboarded?(seller) ⇒ Boolean

Whether this provider will now accept money for a seller. Asked of the provider rather than read off the seller, because only the provider knows — and its answer can change without anything happening in Spree.

Parameters:

Returns:

  • (Boolean)


120
121
122
# File 'app/models/spree/payout_provider/base.rb', line 120

def onboarded?(seller)
  !self.class.requires_payout_account? || seller.payouts_enabled?
end

#onboarding_message(_seller) ⇒ String?

A provider's own words about what is outstanding, when it has any.

Best-effort and unlocalized — providers write these in English and say so. Shown as supporting detail beside copy the panel has translated, never as the only explanation a seller gets.

Parameters:

Returns:

  • (String, nil)


159
160
161
# File 'app/models/spree/payout_provider/base.rb', line 159

def onboarding_message(_seller)
  nil
end

#onboarding_state(_seller) ⇒ Symbol?

Why a seller is not payable yet, in terms any provider can answer.

Deliberately a state rather than a list of outstanding fields. What each provider is missing is its own vocabulary — field paths, document codes, review case ids — and none of it survives translation into a shared one. The reference provider goes further and asks not to be enumerated at all: its guidance is to send the seller back to the hosted flow, which already knows what it wants, rather than to reimplement its form badly.

What a seller actually needs from this is which of three things is true, because each deserves different words and a different button:

nil       — nothing to say; they are payable, or no provider asks
:action   — the provider wants something from them
:pending  — the provider is checking, and nobody can hurry it
:rejected — the provider has refused them

A provider may add a sentence of its own through #onboarding_message, which is best-effort and unlocalized.

Parameters:

Returns:

  • (Symbol, nil)


147
148
149
# File 'app/models/spree/payout_provider/base.rb', line 147

def onboarding_state(_seller)
  nil
end

#onboarding_url(_seller, refresh_url:, return_url:) ⇒ String?

Where a seller goes to give this provider what it needs before it will pay them — bank details, identity documents, whatever it asks.

Nil when there is nowhere to send them, which is the built-in provider's answer: an operator paying by bank transfer collects those details themselves, so the requirement is met by the operator marking it so rather than by the seller going anywhere.

Implementations mint the link per call — hosted onboarding links are short-lived and single-use by design, so one is never stored.

Parameters:

  • seller (Spree::Seller)
  • refresh_url (String) —

    where the provider sends a seller whose link expired before they finished

  • return_url (String) —

    where it sends them when they are done

Returns:

  • (String, nil)


110
111
112
# File 'app/models/spree/payout_provider/base.rb', line 110

def onboarding_url(_seller, refresh_url:, return_url:)
  nil
end

#pay!(_seller_payout) ⇒ Spree::SellerPayout

Settles a seller's accrued earnings.

Parameters:

Returns:

Raises:

  • (NotImplementedError)


175
176
177
# File 'app/models/spree/payout_provider/base.rb', line 175

def pay!(_seller_payout)
  raise NotImplementedError
end

#reverse!(_seller_transfer) ⇒ Spree::SellerTransfer

Takes back part of an earning after a refund.

The ledger row is written by core either way — the books stay correct in every tier — so a provider that cannot pull money back implements this as a no-op rather than refusing.

Parameters:

Returns:

Raises:

  • (NotImplementedError)


206
207
208
# File 'app/models/spree/payout_provider/base.rb', line 206

def reverse!(_seller_transfer)
  raise NotImplementedError
end

#transfer!(_seller_transfer) ⇒ Spree::SellerTransfer

Credits a seller for one fulfilled order.

Parameters:

Returns:

Raises:

  • (NotImplementedError)


167
168
169
# File 'app/models/spree/payout_provider/base.rb', line 167

def transfer!(_seller_transfer)
  raise NotImplementedError
end