Class: SolidusMollie::Client

Inherits:
Object
  • Object
show all
Defined in:
app/models/solidus_mollie/client.rb

Overview

Thin wrapper around the mollie-api-ruby gem. Centralises API-key handling and the (fiddly) amount formatting Mollie requires: a string value with exactly the right number of decimals for the currency.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


10
11
12
13
14
# File 'app/models/solidus_mollie/client.rb', line 10

def initialize(api_key:)
  raise ArgumentError, 'Mollie API key is blank' if api_key.to_s.empty?

  @api_key = api_key
end

Class Method Details

.cents_to_major(cents, currency) ⇒ Object

Convert a cents/minor-unit integer (as Solidus passes to #credit) into a major-unit BigDecimal.



56
57
58
# File 'app/models/solidus_mollie/client.rb', line 56

def self.cents_to_major(cents, currency)
  BigDecimal(cents.to_s) / (10**currency_exponent(currency))
end

.currency_exponent(currency) ⇒ Object



60
61
62
63
64
# File 'app/models/solidus_mollie/client.rb', line 60

def self.currency_exponent(currency)
  ::Money::Currency.new(currency).exponent
rescue StandardError
  2
end

.format_amount(amount, currency) ⇒ Object

Format a major-unit amount into the string Mollie expects, honouring the currency’s decimal places (EUR -> “10.00”, JPY -> “10”).



50
51
52
# File 'app/models/solidus_mollie/client.rb', line 50

def self.format_amount(amount, currency)
  format("%.#{currency_exponent(currency)}f", BigDecimal(amount.to_s))
end

Instance Method Details

#cancel_payment(payment_id) ⇒ Object



34
35
36
# File 'app/models/solidus_mollie/client.rb', line 34

def cancel_payment(payment_id)
  Mollie::Payment.delete(payment_id, api_key: @api_key)
end

#create_payment(amount:, currency:, description:, redirect_url:, webhook_url:, method: nil, metadata: {}) ⇒ Object

amount: a BigDecimal/Numeric in MAJOR units (e.g. 10.00 for €10).



17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/solidus_mollie/client.rb', line 17

def create_payment(amount:, currency:, description:, redirect_url:, webhook_url:,
                   method: nil, metadata: {})
  Mollie::Payment.create(
    amount: { value: self.class.format_amount(amount, currency), currency: currency },
    description: description,
    redirect_url: redirect_url,
    webhook_url: webhook_url,
    method: method.presence,
    metadata: ,
    api_key: @api_key
  )
end

#create_refund(payment_id:, amount:, currency:) ⇒ Object



38
39
40
41
42
43
44
# File 'app/models/solidus_mollie/client.rb', line 38

def create_refund(payment_id:, amount:, currency:)
  Mollie::Payment::Refund.create(
    payment_id: payment_id,
    amount: { value: self.class.format_amount(amount, currency), currency: currency },
    api_key: @api_key
  )
end

#get_payment(payment_id) ⇒ Object



30
31
32
# File 'app/models/solidus_mollie/client.rb', line 30

def get_payment(payment_id)
  Mollie::Payment.get(payment_id, api_key: @api_key)
end