224 lines
8.3 KiB
Go
224 lines
8.3 KiB
Go
package domain
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"math"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
var ErrAffiliateInput = errors.New("invalid affiliate request")
|
|
|
|
type AffiliateApplicationInput struct {
|
|
Application string `json:"application"`
|
|
TermsVersion string `json:"terms_version"`
|
|
AcceptTerms bool `json:"accept_terms"`
|
|
}
|
|
type AffiliateReviewInput struct {
|
|
Status string `json:"status" enums:"APPROVED,REJECTED,SUSPENDED"`
|
|
Note string `json:"note"`
|
|
}
|
|
type AffiliateSettingsInput struct {
|
|
CommissionPercent *float64 `json:"commission_percent" minimum:"0" maximum:"100"`
|
|
CommissionMode string `json:"commission_mode" enums:"FIRST_PURCHASE,EVERY_COURSE"`
|
|
AttributionDays int32 `json:"attribution_days" minimum:"1" maximum:"365"`
|
|
HoldingDays int32 `json:"holding_days" minimum:"0" maximum:"365"`
|
|
ShareBaseURL string `json:"share_base_url"`
|
|
}
|
|
type AffiliatePayoutInput struct {
|
|
Currency string `json:"currency"`
|
|
Method string `json:"method" enums:"BANK,MOBILE_MONEY"`
|
|
AccountName string `json:"account_name"`
|
|
AccountReference string `json:"account_reference"`
|
|
}
|
|
type AffiliatePayoutReviewInput struct {
|
|
Status string `json:"status" enums:"PAID,REJECTED"`
|
|
PaymentReference string `json:"payment_reference"`
|
|
Note string `json:"note"`
|
|
}
|
|
type AffiliateReversalInput struct {
|
|
Reason string `json:"reason"`
|
|
}
|
|
|
|
func AffiliateTextValid(value string, min, max int) bool {
|
|
value = strings.TrimSpace(value)
|
|
return utf8.RuneCountInString(value) >= min && utf8.RuneCountInString(value) <= max && !strings.ContainsAny(value, "\x00\r")
|
|
}
|
|
func ValidateAffiliateSettings(input AffiliateSettingsInput) error {
|
|
if input.CommissionPercent == nil {
|
|
return ErrAffiliateInput
|
|
}
|
|
p := *input.CommissionPercent
|
|
if math.IsNaN(p) || math.IsInf(p, 0) || p < 0 || p > 100 || math.Abs(p*100-math.Round(p*100)) > 0.000001 {
|
|
return ErrAffiliateInput
|
|
}
|
|
if input.CommissionMode != "FIRST_PURCHASE" && input.CommissionMode != "EVERY_COURSE" {
|
|
return ErrAffiliateInput
|
|
}
|
|
if input.AttributionDays < 1 || input.AttributionDays > 365 || input.HoldingDays < 0 || input.HoldingDays > 365 {
|
|
return ErrAffiliateInput
|
|
}
|
|
if input.ShareBaseURL != "" {
|
|
u, err := url.Parse(input.ShareBaseURL)
|
|
if err != nil || u.Host == "" || u.User != nil || (u.Scheme != "https" && u.Scheme != "http") || len(input.ShareBaseURL) > 2000 {
|
|
return ErrAffiliateInput
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func ValidateAffiliatePayout(input AffiliatePayoutInput) error {
|
|
if len(input.Currency) != 3 {
|
|
return ErrAffiliateInput
|
|
}
|
|
for _, ch := range input.Currency {
|
|
if ch < 'A' || ch > 'Z' {
|
|
return ErrAffiliateInput
|
|
}
|
|
}
|
|
if input.Method != "BANK" && input.Method != "MOBILE_MONEY" {
|
|
return ErrAffiliateInput
|
|
}
|
|
if !AffiliateTextValid(input.AccountName, 2, 120) || !AffiliateTextValid(input.AccountReference, 3, 120) {
|
|
return ErrAffiliateInput
|
|
}
|
|
return nil
|
|
}
|
|
func AffiliateShareURL(base, code string) string {
|
|
if base == "" || code == "" {
|
|
return ""
|
|
}
|
|
u, err := url.Parse(base)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
q := u.Query()
|
|
q.Set("ref", code)
|
|
u.RawQuery = q.Encode()
|
|
return u.String()
|
|
}
|
|
|
|
type AffiliateProfile struct {
|
|
TermsVersion string `json:"terms_version"`
|
|
UserID int64 `json:"user_id"`
|
|
Status string `json:"status"`
|
|
Application string `json:"application"`
|
|
TermsAcceptedAt time.Time `json:"terms_accepted_at"`
|
|
ReviewNote string `json:"review_note"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
Email string `json:"email"`
|
|
FirstName string `json:"first_name"`
|
|
LastName string `json:"last_name"`
|
|
Code string `json:"code"`
|
|
}
|
|
type AffiliateSettings struct {
|
|
CommissionPercent string `json:"commission_percent"`
|
|
CommissionMode string `json:"commission_mode"`
|
|
AttributionDays int32 `json:"attribution_days"`
|
|
HoldingDays int32 `json:"holding_days"`
|
|
ShareBaseURL string `json:"share_base_url"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
type AffiliateBalance struct {
|
|
Currency string `json:"currency"`
|
|
Pending string `json:"pending"`
|
|
Available string `json:"available"`
|
|
Reversed string `json:"reversed"`
|
|
RecoveryDue string `json:"recovery_due"`
|
|
Reserved string `json:"reserved"`
|
|
Paid string `json:"paid"`
|
|
}
|
|
type AffiliateTerms struct {
|
|
Version string `json:"version"`
|
|
Rules []string `json:"rules"`
|
|
}
|
|
|
|
func CurrentAffiliateTerms() AffiliateTerms {
|
|
return AffiliateTerms{Version: "1", Rules: []string{
|
|
"Administrator approval is required before promoting courses or requesting payouts.",
|
|
"Use your code or referral link before the learner's first paid purchase. Self-referrals and replacing an existing referrer are not allowed.",
|
|
"Only verified positive course payments qualify, within the configured attribution window. The current rate and commission policy apply at payment confirmation.",
|
|
"Earnings are held for the configured period. Reversed sales cancel unpaid earnings; reversals after payout reduce future payouts.",
|
|
"Payout requests reserve available earnings in a single currency. Administrators record externally completed transfers; a request is not a completed payment.",
|
|
"Do not use misleading promotion, spam, or fraudulent purchases. Suspension stops new commissions and payout completion.",
|
|
}}
|
|
}
|
|
|
|
type AffiliateDashboard struct {
|
|
Terms AffiliateTerms `json:"terms"`
|
|
Profile *AffiliateProfile `json:"profile"`
|
|
Settings AffiliateSettings `json:"settings"`
|
|
ShareURL string `json:"share_url"`
|
|
TotalReferrals int64 `json:"total_referrals"`
|
|
ConvertedReferrals int64 `json:"converted_referrals"`
|
|
Balances []AffiliateBalance `json:"balances"`
|
|
}
|
|
type AffiliateCommission struct {
|
|
ID int64 `json:"id"`
|
|
ReferrerID int64 `json:"referrer_id"`
|
|
PaymentID int64 `json:"payment_id"`
|
|
CourseID *int64 `json:"course_id"`
|
|
CommissionPercent string `json:"commission_percent"`
|
|
Amount string `json:"amount"`
|
|
Currency string `json:"currency"`
|
|
Status string `json:"status"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
AvailableAt time.Time `json:"available_at"`
|
|
ReversedAt *time.Time `json:"reversed_at"`
|
|
ReversalReason *string `json:"reversal_reason"`
|
|
PayoutID *int64 `json:"payout_id"`
|
|
}
|
|
type AffiliatePayout struct {
|
|
ID int64 `json:"id"`
|
|
UserID int64 `json:"user_id"`
|
|
Currency string `json:"currency"`
|
|
Amount string `json:"amount"`
|
|
Status string `json:"status"`
|
|
Method string `json:"method"`
|
|
AccountName string `json:"account_name"`
|
|
AccountReference string `json:"account_reference"`
|
|
PaymentReference *string `json:"payment_reference"`
|
|
ReviewNote string `json:"review_note"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
ReviewedAt *time.Time `json:"reviewed_at"`
|
|
}
|
|
type AffiliateListData struct {
|
|
Items []AffiliateProfile `json:"items"`
|
|
TotalCount int64 `json:"total_count"`
|
|
Limit int32 `json:"limit"`
|
|
Offset int32 `json:"offset"`
|
|
}
|
|
type AffiliateCommissionData struct {
|
|
Items []AffiliateCommission `json:"items"`
|
|
TotalCount int64 `json:"total_count"`
|
|
Limit int32 `json:"limit"`
|
|
Offset int32 `json:"offset"`
|
|
}
|
|
type AffiliatePayoutData struct {
|
|
Items []AffiliatePayout `json:"items"`
|
|
TotalCount int64 `json:"total_count"`
|
|
Limit int32 `json:"limit"`
|
|
Offset int32 `json:"offset"`
|
|
}
|
|
type AffiliateMutationData struct {
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
type AffiliateEvent struct {
|
|
ID int64 `json:"id"`
|
|
AffiliateUserID *int64 `json:"affiliate_user_id"`
|
|
ActorID *int64 `json:"actor_id"`
|
|
Event string `json:"event"`
|
|
// Event-specific audit fields, such as from/to status, note, payout_id, commission_id, and rate snapshots.
|
|
Details json.RawMessage `json:"details" swaggertype:"object"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
type AffiliateEventData struct {
|
|
Items []AffiliateEvent `json:"items"`
|
|
Limit int32 `json:"limit"`
|
|
Offset int32 `json:"offset"`
|
|
}
|