defmodule DaProductApp.Partners.Merchant do @moduledoc """ Merchant schema representing stores/businesses enrolled under partners. Merchants are the actual stores/businesses where customers make payments. Each merchant belongs to a partner (acquirer/aggregator). """ use Ecto.Schema import Ecto.Changeset @primary_key {:id, :binary_id, autogenerate: true} @foreign_key_type :binary_id schema "merchants" do field :merchant_code, :string # Unique merchant identifier field :brand_name, :string # Business name field :merchant_vpa, :string # UPI VPA (e.g., merchant@partner) field :business_type, :string # "RETAIL", "ECOMMERCE", "SERVICES" field :business_category, :string # "GROCERY", "RESTAURANT", "PHARMACY" field :static_qr_code, :string # Static QR for the merchant field :qr_enabled, :boolean, default: true field :dynamic_qr_enabled, :boolean, default: false field :max_transaction_limit, :decimal field :daily_transaction_limit, :decimal field :settlement_frequency, :string, default: "T+1" # "T+0", "T+1", "WEEKLY" field :settlement_account_intl, :string # Merchant's bank account field :contact_person, :string field :contact_email, :string field :contact_phone, :string field :address, :string field :city, :string field :state, :string field :pincode, :string field :gstin, :string # GST identification number field :pan, :string # PAN card number field :intl_status, :string, default: "ACTIVE" # "ACTIVE", "SUSPENDED", "INACTIVE" field :onboarded_at, :utc_datetime field :last_transaction_at, :utc_datetime belongs_to :partner, DaProductApp.Partners.Partner has_many :transactions, DaProductApp.Transactions.Transaction timestamps(type: :utc_datetime) end def changeset(merchant, attrs) do merchant |> cast(attrs, [ :merchant_code, :merchant_name, :merchant_vpa, :business_type, :business_category, :static_qr_code, :qr_enabled, :dynamic_qr_enabled, :max_transaction_limit, :daily_transaction_limit, :settlement_frequency, :settlement_account, :contact_person, :contact_email, :contact_phone, :address, :city, :state, :pincode, :gstin, :pan, :status, :onboarded_at, :last_transaction_at, :partner_id ]) |> validate_required([:merchant_code, :merchant_name, :merchant_vpa, :partner_id]) |> validate_inclusion(:business_type, ["RETAIL", "ECOMMERCE", "SERVICES"]) |> validate_inclusion(:settlement_frequency, ["T+0", "T+1", "WEEKLY"]) |> validate_inclusion(:status, ["ACTIVE", "SUSPENDED", "INACTIVE"]) |> validate_format(:contact_email, ~r/@/) |> validate_format(:merchant_vpa, ~r/^[\w\.-]+@[\w\.-]+$/) |> unique_constraint(:merchant_code) |> unique_constraint(:merchant_vpa) end end