Class: Providers::VendorPayment

Inherits:
Object
  • Object
show all
Includes:
DefaultErrorContainer
Defined in:
app/my_lib/payment/providers/vendor_payment.rb

Overview

The Providers::VendorPayment class is responsible for handling vendor payment processing.

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Constructor Details

#initialize(ticket_transaction, options = {}) ⇒ VendorPayment

Initializes a new VendorPayment instance.

Parameters:

  • ticket_transaction (Object)

    The ticket transaction associated with the payment.

  • options (Hash) (defaults to: {})

    Additional options for the payment, such as transaction details. Example:

    transaction_id: "12345678",
    status: "success",
    paid_at: "2025-05-24 17:54:00",
    amount_currency: "THB",
    vendor_id: 1,
    



20
21
22
23
# File 'app/my_lib/payment/providers/vendor_payment.rb', line 20

def initialize(ticket_transaction, options = {})
  @ticket_transaction = ticket_transaction
  @options = options
end

Instance Method Details

#charge(amount) ⇒ Boolean

Processes a charge for the given amount.

Parameters:

  • amount (Numeric)

    The amount to be charged.

Returns:

  • (Boolean)

    Returns true if the charge is successfully created, false otherwise.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/my_lib/payment/providers/vendor_payment.rb', line 29

def charge(amount)
  omise_source = create_omise_source

  if omise_source.blank?
    errors.add(:base, 'Failed to create new vendor payment omise source')
    return false
  end

  vendor_payment_attributes = create_vendor_payment_params(omise_source.id, amount).permit!
  VendorTicketTransactionPayment.create_payment!(vendor_payment_attributes.to_h)

  charge_record = ticket_transaction.charges.build(charge_attributes(omise_source.id, amount))
  charge_record.present?
rescue StandardError => e
  error.add :base, e.message
  false
end

#valid?Boolean

Checks if the payment instance is valid.

Returns:

  • (Boolean)

    Returns true if all required fields are present, false otherwise.



50
51
52
53
54
55
56
57
58
59
60
# File 'app/my_lib/payment/providers/vendor_payment.rb', line 50

def valid?
  required_fields = [:transaction_id, :status, :paid_at, :vendor_id]
  missing_fields = required_fields.select { |field| options[field].blank? }

  if missing_fields.any?
    errors.add(:base, "Missing required fields: #{missing_fields.join(', ')}")
    return false
  end

  true
end