Class: VendorsService::VendorPaymentService

Inherits:
Object
  • Object
show all
Defined in:
app/services/vendors_service/vendor_payment_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reservation) ⇒ VendorPaymentService

Returns a new instance of VendorPaymentService.



5
6
7
# File 'app/services/vendors_service/vendor_payment_service.rb', line 5

def initialize(reservation)
  @reservation = reservation
end

Instance Attribute Details

#reservationObject (readonly)

Returns the value of attribute reservation.



3
4
5
# File 'app/services/vendors_service/vendor_payment_service.rb', line 3

def reservation
  @reservation
end

Instance Method Details

#save_vendor_paymentVendorPayment?

Creates or updates a VendorPayment for the given reservation. If a VendorPayment exists, it updates the record; otherwise, it creates a new one. Logs the operation and reports errors to APM if any exception occurs.

Creates or updates a VendorPayment for the given reservation using find_or_initialize_by. This method will either find the existing VendorPayment or initialize a new one, then assign attributes and save. Logs the operation and reports errors to APM if any exception occurs.

Returns:

  • (VendorPayment, nil)

    The created or updated VendorPayment, or nil if failed.

  • (VendorPayment, nil)

    The created or updated VendorPayment, or nil if failed.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/vendors_service/vendor_payment_service.rb', line 19

def save_vendor_payment
  reservation.reload
  vendor_payment = VendorPayment.find_or_initialize_by(reservation_id: reservation.id)
  vendor_payment.assign_attributes(
    vendor_id: reservation.vendor_reservation.oauth_application.id,
    transaction_id: "Created-by-#{reservation.created_by}",
    amount_cents: reservation.property.prepayment_cents,
    currency: reservation.restaurant.currency_code.upcase,
    status: 'success',
    paid_at: Time.now.in_time_zone(reservation.restaurant.time_zone),
  )
  vendor_payment.save!

  action = vendor_payment.persisted? && vendor_payment.previous_changes.any? ? 'updated' : 'created'
  BUSINESS_LOGGER.info(
    "#{self.class}##{__method__}: VendorPayment #{action} successfully",
    { reservation_id: reservation.id },
  )

  VendorsService::UpdateMemoService.new(reservation).execute
  vendor_payment
rescue StandardError => e
  BUSINESS_LOGGER.error(
    "#{self.class}##{__method__}: VendorPayment save failed",
    { reservation_id: reservation.id, exception: { class: e.class.name, message: e.message, backtrace: e.backtrace } },
  )
  APMErrorHandler.report('VendorPayment save failed', exception: e, context: { reservation_id: reservation.id })
  nil
end