2021-04-16 12:25:48 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-04-20 17:22:53 +02:00
|
|
|
require 'payment/item_builder'
|
|
|
|
|
2021-04-16 12:25:48 +02:00
|
|
|
# A link between an object in the local database and another object in the remote payment gateway database
|
|
|
|
class PaymentGatewayObject < ApplicationRecord
|
|
|
|
belongs_to :item, polymorphic: true
|
2022-11-10 16:14:49 +01:00
|
|
|
belongs_to :invoice, foreign_type: 'Invoice', foreign_key: 'item_id', inverse_of: :payment_gateway_object
|
|
|
|
belongs_to :invoice_item, foreign_type: 'InvoiceItem', foreign_key: 'item_id', inverse_of: :payment_gateway_object
|
|
|
|
belongs_to :subscription, foreign_type: 'Subscription', foreign_key: 'item_id', inverse_of: :payment_gateway_object
|
|
|
|
belongs_to :payment_schedule, foreign_type: 'PaymentSchedule', foreign_key: 'item_id', inverse_of: :payment_gateway_object
|
|
|
|
belongs_to :payment_schedule_item, foreign_type: 'PaymentScheduleItem', foreign_key: 'item_id', inverse_of: :payment_gateway_object
|
|
|
|
belongs_to :user, foreign_type: 'User', foreign_key: 'item_id', inverse_of: :payment_gateway_object
|
|
|
|
belongs_to :plan, foreign_type: 'Plan', foreign_key: 'item_id', inverse_of: :payment_gateway_object
|
|
|
|
belongs_to :machine, foreign_type: 'Machine', foreign_key: 'item_id', inverse_of: :payment_gateway_object
|
|
|
|
belongs_to :space, foreign_type: 'Space', foreign_key: 'item_id', inverse_of: :payment_gateway_object
|
|
|
|
belongs_to :training, foreign_type: 'Training', foreign_key: 'item_id', inverse_of: :payment_gateway_object
|
|
|
|
belongs_to :order, foreign_type: 'Order', foreign_key: 'item_id', inverse_of: :payment_gateway_object
|
2021-04-20 17:22:53 +02:00
|
|
|
|
2021-06-03 09:48:21 +02:00
|
|
|
belongs_to :payment_gateway_object # some objects may require a reference to another object for remote recovery
|
2021-06-02 20:07:53 +02:00
|
|
|
|
2021-04-20 17:22:53 +02:00
|
|
|
def gateway_object
|
2021-06-03 09:48:21 +02:00
|
|
|
related_item = payment_gateway_object&.gateway_object_id
|
2021-06-02 20:07:53 +02:00
|
|
|
Payment::ItemBuilder.build(gateway_object_type, gateway_object_id, related_item)
|
2021-04-20 17:22:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def gateway_object=(object)
|
|
|
|
self.gateway_object_id = object.id
|
|
|
|
self.gateway_object_type = object.class
|
|
|
|
end
|
2021-04-16 12:25:48 +02:00
|
|
|
end
|