Files
soroush.asadi 62ea110605
CI/CD / CI · Web (tsc) (push) Successful in 1m33s
CI/CD / Deploy · full stack (push) Failing after 20s
feat(payment): admin-editable ZarinPal settings + in-panel test payment
Lets the broker's ZarinPal merchant / sandbox / amount-unit be set from
Admin → درگاه پرداخت (persisted in payment.settings) instead of env +
redeploy, and adds a per-app "test payment" button that mints a real
ZarinPal StartPay link straight from the panel — no site wiring needed.

- migration 33_payment_settings.sql: singleton payment.settings + a
  transactions.is_test column. (33, not 32 — 32 is content_render_engine.)
- broker read-path precedence: per-client override > DB settings > env.
- POST /v1/admin/clients/:id/test-payment + GET/PUT /v1/admin/settings.
- admin UI: «تنظیمات زرین‌پال» tab + «پرداخت آزمایشی» button.

Adversarial-review fixes (2 confirmed HIGH):
- do NOT pre-seed the settings row — a seeded sandbox=TRUE default would
  override a production ZARINPAL_SANDBOX=false env and silently route real
  payments to sandbox.zarinpal.com until an admin untouched the toggle.
  No row → env governs until an admin saves.
- test transactions are tagged is_test and the webhook dispatcher skips
  them, so an admin smoke-test can never notify (or credit) a real client,
  regardless of metadata. Broker-authoritative, not consumer-dependent.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-26 00:47:10 +03:30

120 lines
5.2 KiB
Go

package models
import (
"encoding/json"
"time"
"github.com/google/uuid"
)
// APIError is the standard error envelope across FlatRender services.
type APIError struct {
Code string `json:"code"`
Message string `json:"message"`
}
// Settings is the singleton global broker config (admin-editable ZarinPal default).
type Settings struct {
ZarinPalMerchantID string `json:"zarinpal_merchant_id"`
ZarinPalSandbox bool `json:"zarinpal_sandbox"`
ZarinPalAmountUnit string `json:"zarinpal_amount_unit"` // "rial" | "toman"
UpdatedAt time.Time `json:"updated_at"`
}
// ── Client apps (tenants of the broker — each site that pays through it) ───────
type ClientApp struct {
ID uuid.UUID `json:"id"`
TenantID *uuid.UUID `json:"tenant_id,omitempty"`
Name string `json:"name"`
Slug string `json:"slug"`
APIKey string `json:"api_key"`
Secret string `json:"secret,omitempty"` // returned only on create / rotate
ZarinPalMerchantID *string `json:"zarinpal_merchant_id,omitempty"`
ZarinPalSandbox *bool `json:"zarinpal_sandbox,omitempty"`
AllowedReturnOrigins []string `json:"allowed_return_origins"`
WebhookURL *string `json:"webhook_url,omitempty"`
IsActive bool `json:"is_active"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// ── Transactions ──────────────────────────────────────────────────────────────
type Transaction struct {
ID uuid.UUID `json:"id"`
ClientAppID uuid.UUID `json:"client_app_id"`
ClientSlug string `json:"client_slug,omitempty"` // joined for admin views
Status string `json:"status"`
Gateway string `json:"gateway"`
IsTest bool `json:"is_test"`
AmountRial int64 `json:"amount_rial"`
Currency string `json:"currency"`
Description *string `json:"description,omitempty"`
ClientRef *string `json:"client_ref,omitempty"`
ReturnURL string `json:"return_url"`
Metadata json.RawMessage `json:"metadata,omitempty"`
PayerMobile *string `json:"payer_mobile,omitempty"`
PayerEmail *string `json:"payer_email,omitempty"`
Authority *string `json:"authority,omitempty"`
RefID *string `json:"ref_id,omitempty"`
CardPan *string `json:"card_pan,omitempty"`
FeeRial *int64 `json:"fee_rial,omitempty"`
GatewayResponse json.RawMessage `json:"gateway_response,omitempty"`
FailureReason *string `json:"failure_reason,omitempty"`
PaidAt *time.Time `json:"paid_at,omitempty"`
FailedAt *time.Time `json:"failed_at,omitempty"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// Transaction status values (kept as plain text — no PG enum, for flexibility).
const (
StatusCreated = "Created"
StatusPending = "Pending"
StatusPaid = "Paid"
StatusFailed = "Failed"
StatusCancelled = "Cancelled"
StatusExpired = "Expired"
)
// ── Client-facing request/response shapes ─────────────────────────────────────
// PayRequest is the body a client site POSTs to /v1/pay/request.
type PayRequest struct {
Amount int64 `json:"amount"` // in `currency` units
Currency string `json:"currency,omitempty"` // "IRR" (Rial, default) or "IRT" (Toman)
Description string `json:"description,omitempty"`
ClientRef string `json:"client_ref,omitempty"`
ReturnURL string `json:"return_url"`
Mobile string `json:"mobile,omitempty"`
Email string `json:"email,omitempty"`
Metadata json.RawMessage `json:"metadata,omitempty"`
}
// PayResponse is returned from /v1/pay/request.
type PayResponse struct {
ID uuid.UUID `json:"id"`
Status string `json:"status"`
PaymentURL string `json:"payment_url"`
Authority string `json:"authority"`
AmountRial int64 `json:"amount_rial"`
}
// WebhookPayload is the signed body POSTed to a client's webhook_url.
type WebhookPayload struct {
Event string `json:"event"` // payment.paid | payment.failed
ID uuid.UUID `json:"id"`
Status string `json:"status"`
AmountRial int64 `json:"amount_rial"`
Currency string `json:"currency"`
ClientRef string `json:"client_ref,omitempty"`
RefID string `json:"ref_id,omitempty"`
Authority string `json:"authority,omitempty"`
CardPan string `json:"card_pan,omitempty"`
Metadata json.RawMessage `json:"metadata,omitempty"`
PaidAt *time.Time `json:"paid_at,omitempty"`
CreatedAtTs int64 `json:"ts"`
}