defmodule DaProductApp.Partners.EmailService do @moduledoc """ Service for sending API credential emails to partners. Uses Swoosh for email delivery with professional templates. """ import Swoosh.Email alias DaProductApp.Mailer alias DaProductApp.Partners.Partner @from_email "noreply@mercurypay.com" @support_email "support@mercurypay.com" @doc """ Sends API credentials to partner contact email. """ def send_api_credentials(%Partner{} = partner, credentials, admin_user) do email = new() |> to(partner.contact_email) |> from(@from_email) |> subject("Your Mercury UPI PSP API Credentials") |> html_body(credentials_html_template(partner, credentials, admin_user)) |> text_body(credentials_text_template(partner, credentials, admin_user)) case Mailer.deliver(email) do {:ok, _email} -> {:ok, "API credentials sent successfully to #{partner.contact_email}"} {:error, reason} -> {:error, "Failed to send email: #{inspect(reason)}"} end end @doc """ Sends API key revocation notification. """ def send_revocation_notice(%Partner{} = partner, admin_user) do email = new() |> to(partner.contact_email) |> from(@from_email) |> subject("Mercury UPI PSP API Access Revoked") |> html_body(revocation_html_template(partner, admin_user)) |> text_body(revocation_text_template(partner, admin_user)) case Mailer.deliver(email) do {:ok, _email} -> {:ok, "Revocation notice sent successfully to #{partner.contact_email}"} {:error, reason} -> {:error, "Failed to send email: #{inspect(reason)}"} end end @doc """ Sends API key expiration warning. """ def send_expiration_warning(%Partner{} = partner, days_remaining) do email = new() |> to(partner.contact_email) |> from(@from_email) |> subject("Mercury UPI PSP API Key Expiring Soon") |> html_body(expiration_warning_html_template(partner, days_remaining)) |> text_body(expiration_warning_text_template(partner, days_remaining)) case Mailer.deliver(email) do {:ok, _email} -> {:ok, "Expiration warning sent successfully to #{partner.contact_email}"} {:error, reason} -> {:error, "Failed to send email: #{inspect(reason)}"} end end # HTML Templates defp credentials_html_template(partner, credentials, admin_user) do """ API Credentials - Mercury UPI PSP

🔐 API Credentials Generated

Your Mercury UPI PSP API access credentials

Hello #{partner.partner_name},

Your API credentials have been #{if String.contains?(to_string(admin_user), "regenerated"), do: "regenerated", else: "generated"} successfully. Please store these credentials securely and do not share them.

🔑 API Credentials

#{credentials.api_key}
#{credentials.api_secret}
#{credentials.rate_limit} requests/minute
#{format_datetime(credentials.expires_at)}
⚠️ Security Notice:
  • Store these credentials securely and never share them publicly
  • Use HTTPS for all API communications
  • Monitor your API usage regularly
  • Contact support immediately if you suspect a compromise

📚 Getting Started

Include the following headers in your API requests:

X-API-Key: #{credentials.api_key}
X-API-Secret: #{credentials.api_secret}
View API Documentation
""" end defp revocation_html_template(partner, admin_user) do """ API Access Revoked - Mercury UPI PSP

🚫 API Access Revoked

Your Mercury UPI PSP API credentials have been revoked

Hello #{partner.partner_name},

Notice: Your API credentials for Mercury UPI PSP have been revoked and are no longer valid for accessing our services.

What this means:

Next Steps:

If you believe this was done in error, please contact our support team immediately.

""" end defp expiration_warning_html_template(partner, days_remaining) do """ API Key Expiring Soon - Mercury UPI PSP

⚠️ API Key Expiring Soon

Your Mercury UPI PSP API credentials expire in #{days_remaining} days

Hello #{partner.partner_name},

Important: Your API credentials will expire in #{days_remaining} days. Please take action to avoid service interruption.

What happens when credentials expire:

Action Required:

Request Renewal
""" end # Text Templates (for email clients that don't support HTML) defp credentials_text_template(partner, credentials, admin_user) do """ Mercury UPI PSP - API Credentials Generated Hello #{partner.partner_name}, Your API credentials have been generated successfully. Please store these credentials securely and do not share them. API CREDENTIALS: ================ API Key: #{credentials.api_key} API Secret: #{credentials.api_secret} Rate Limit: #{credentials.rate_limit} requests/minute Expires At: #{format_datetime(credentials.expires_at)} SECURITY NOTICE: ================ - Store these credentials securely and never share them publicly - Use HTTPS for all API communications - Monitor your API usage regularly - Contact support immediately if you suspect a compromise GETTING STARTED: ================ Include the following headers in your API requests: X-API-Key: #{credentials.api_key} X-API-Secret: #{credentials.api_secret} Documentation: https://docs.mercurypay.com/api Generated by: #{admin_user.email} on #{format_datetime(DateTime.utc_now())} Support: #{@support_email} © #{Date.utc_today().year} Mercury Pay - UPI PSP Services """ end defp revocation_text_template(partner, admin_user) do """ Mercury UPI PSP - API Access Revoked Hello #{partner.partner_name}, NOTICE: Your API credentials for Mercury UPI PSP have been revoked and are no longer valid for accessing our services. WHAT THIS MEANS: ================ - All API requests using your previous credentials will be rejected - Any integration using these credentials must be updated - Your merchant services may be affected if dependent on API access NEXT STEPS: =========== - Contact our support team if you need new credentials - Review and update any systems using the revoked credentials - Ensure compliance with any updated terms of service If you believe this was done in error, please contact our support team immediately. Revoked by: #{admin_user.email} on #{format_datetime(DateTime.utc_now())} Support: #{@support_email} © #{Date.utc_today().year} Mercury Pay - UPI PSP Services """ end defp expiration_warning_text_template(partner, days_remaining) do """ Mercury UPI PSP - API Key Expiring Soon Hello #{partner.partner_name}, IMPORTANT: Your API credentials will expire in #{days_remaining} days. Please take action to avoid service interruption. WHAT HAPPENS WHEN CREDENTIALS EXPIRE: ===================================== - All API requests will be rejected with authentication errors - Your merchant services may be disrupted - Payment processing through our platform will stop ACTION REQUIRED: ================ - Contact our support team to renew your credentials - Update your systems with new credentials when provided - Test your integration after updating credentials Contact support at: #{@support_email} © #{Date.utc_today().year} Mercury Pay - UPI PSP Services """ end # Helper functions defp format_datetime(%DateTime{} = datetime) do datetime |> DateTime.truncate(:second) |> DateTime.to_string() end defp format_datetime(nil), do: "N/A" end