Class: Munificent::Payment

Inherits:
ApplicationRecord show all
Defined in:
app/models/munificent/payment.rb

Class Method Summary collapse

Methods inherited from ApplicationRecord

#to_s

Class Method Details

.create_and_assign(amount:, currency:, stripe_payment_intent_id: nil, paypal_order_id: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/munificent/payment.rb', line 6

def create_and_assign(amount:, currency:, stripe_payment_intent_id: nil, paypal_order_id: nil)
  if stripe_payment_intent_id.nil? && paypal_order_id.nil?
    raise ArgumentError,
  "Either stripe_payment_intent_id or paypal_order_id must be provided"
  end

  params = {
    amount_decimals: amount,
    amount_currency: currency,
  }

  if stripe_payment_intent_id
    unless (payment = Payment.find_by(stripe_payment_intent_id:))
      payment = Payment.create!(stripe_payment_intent_id:, **params)
    end

    PaymentAssignmentJob.perform_later(payment.id, provider: :stripe)
  else
    unless (payment = Payment.find_by(paypal_order_id:))
      payment = Payment.create!(paypal_order_id:, **params)
    end

    PaymentAssignmentJob.perform_later(payment.id, provider: :paypal)
  end
end