Link checkout payments to a pending subscription row and activate it on success, or cancel it when payment fails, is cancelled, or expires. Co-authored-by: Cursor <cursoragent@cursor.com>
113 lines
3.1 KiB
Go
113 lines
3.1 KiB
Go
package checkoutsubscription
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"Yimaru-Backend/internal/domain"
|
|
"Yimaru-Backend/internal/ports"
|
|
)
|
|
|
|
// CreatePending creates a PENDING subscription when checkout/payment is initiated.
|
|
func CreatePending(
|
|
ctx context.Context,
|
|
store ports.SubscriptionStore,
|
|
userID int64,
|
|
plan *domain.SubscriptionPlan,
|
|
paymentRef string,
|
|
paymentMethod string,
|
|
) (*domain.UserSubscription, error) {
|
|
now := time.Now()
|
|
expiresAt := domain.CalculateSubscriptionExpiresAt(now, plan.Category, plan.DurationValue, plan.DurationUnit)
|
|
status := string(domain.SubscriptionStatusPending)
|
|
autoRenew := false
|
|
ref := paymentRef
|
|
method := paymentMethod
|
|
|
|
sub, err := store.CreateUserSubscription(ctx, domain.CreateUserSubscriptionInput{
|
|
UserID: userID,
|
|
PlanID: plan.ID,
|
|
StartsAt: &now,
|
|
ExpiresAt: expiresAt,
|
|
Status: &status,
|
|
PaymentReference: &ref,
|
|
PaymentMethod: &method,
|
|
AutoRenew: &autoRenew,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create pending subscription: %w", err)
|
|
}
|
|
return sub, nil
|
|
}
|
|
|
|
// AbandonPending cancels a PENDING subscription when checkout fails or is cancelled.
|
|
func AbandonPending(ctx context.Context, store ports.SubscriptionStore, subscriptionID *int64) {
|
|
if subscriptionID == nil || *subscriptionID < 1 {
|
|
return
|
|
}
|
|
sub, err := store.GetUserSubscriptionByID(ctx, *subscriptionID)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if sub.Status != string(domain.SubscriptionStatusPending) {
|
|
return
|
|
}
|
|
_ = store.CancelUserSubscription(ctx, *subscriptionID)
|
|
}
|
|
|
|
// Fulfill activates the payment's PENDING subscription, or creates an ACTIVE one
|
|
// for legacy payments that were not linked at initiate time.
|
|
func Fulfill(
|
|
ctx context.Context,
|
|
store ports.SubscriptionStore,
|
|
payment *domain.Payment,
|
|
plan *domain.SubscriptionPlan,
|
|
paymentMethod string,
|
|
) (*domain.UserSubscription, error) {
|
|
startsAt := time.Now()
|
|
expiresAt := domain.CalculateSubscriptionExpiresAt(startsAt, plan.Category, plan.DurationValue, plan.DurationUnit)
|
|
paymentRef := payment.Nonce
|
|
activeStatus := string(domain.SubscriptionStatusActive)
|
|
autoRenew := false
|
|
method := paymentMethod
|
|
|
|
if payment.SubscriptionID != nil {
|
|
sub, err := store.GetUserSubscriptionByID(ctx, *payment.SubscriptionID)
|
|
if err == nil {
|
|
switch sub.Status {
|
|
case string(domain.SubscriptionStatusActive):
|
|
return sub, nil
|
|
case string(domain.SubscriptionStatusPending):
|
|
activated, actErr := store.ActivatePendingUserSubscription(
|
|
ctx,
|
|
*payment.SubscriptionID,
|
|
startsAt,
|
|
expiresAt,
|
|
paymentRef,
|
|
method,
|
|
)
|
|
if actErr != nil {
|
|
return nil, fmt.Errorf("failed to activate pending subscription: %w", actErr)
|
|
}
|
|
return activated, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
created, err := store.CreateUserSubscription(ctx, domain.CreateUserSubscriptionInput{
|
|
UserID: payment.UserID,
|
|
PlanID: plan.ID,
|
|
StartsAt: &startsAt,
|
|
ExpiresAt: expiresAt,
|
|
Status: &activeStatus,
|
|
PaymentReference: &paymentRef,
|
|
PaymentMethod: &method,
|
|
AutoRenew: &autoRenew,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create subscription: %w", err)
|
|
}
|
|
return created, nil
|
|
}
|