88 lines
3.3 KiB
Go
88 lines
3.3 KiB
Go
package chapa
|
|
|
|
import (
|
|
"Rezina-Backend/internal/domain"
|
|
"Rezina-Backend/internal/ports"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/google/uuid"
|
|
"math"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var ErrCourseNotPurchasable = errors.New("course is unavailable for purchase or has no configured price")
|
|
var ErrCourseAlreadyOwned = errors.New("course already purchased")
|
|
|
|
type courseCheckoutStore interface {
|
|
ports.CoursePurchaseStore
|
|
ports.ExamPrepCatalogCourseStore
|
|
}
|
|
|
|
func (s *Service) InitiateCoursePayment(ctx context.Context, userID, courseID int64, req domain.CourseCheckoutRequest) (*domain.CourseCheckoutResponse, error) {
|
|
if err := s.configured(); err != nil {
|
|
return nil, err
|
|
}
|
|
store, ok := s.paymentStore.(courseCheckoutStore)
|
|
if !ok {
|
|
return nil, errors.New("course checkout store unavailable")
|
|
}
|
|
course, err := store.GetExamPrepCatalogCourseByID(ctx, courseID)
|
|
if err != nil {
|
|
return nil, ErrCourseNotPurchasable
|
|
}
|
|
if !course.VisibleToLearners() || course.Price <= 0 || math.IsNaN(course.Price) || math.IsInf(course.Price, 0) {
|
|
return nil, ErrCourseNotPurchasable
|
|
}
|
|
owned, err := store.HasCoursePurchase(ctx, userID, courseID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if owned {
|
|
return nil, ErrCourseAlreadyOwned
|
|
}
|
|
user, err := s.userStore.GetUserByID(ctx, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ref := uuid.NewString()
|
|
method := "chapa"
|
|
expires := time.Now().Add(3 * time.Hour)
|
|
// The server snapshots price/currency; clients cannot choose the amount charged.
|
|
payment, err := s.paymentStore.CreatePayment(ctx, domain.CreatePaymentInput{UserID: userID, CourseID: &courseID, Amount: course.Price, Currency: course.Currency, PaymentMethod: &method, Nonce: ref, ExpiresAt: &expires})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
init := domain.ChapaInitializeRequest{Amount: formatAmount(course.Price), Currency: course.Currency, Email: user.Email, FirstName: user.FirstName, LastName: user.LastName, PhoneNumber: formatChapaPhone(req.Phone), TxRef: ref, CallbackURL: s.cfg.CHAPA_CALLBACK_URL, ReturnURL: s.cfg.CHAPA_RETURN_URL}
|
|
if strings.TrimSpace(init.FirstName) == "" {
|
|
init.FirstName = "Customer"
|
|
}
|
|
if strings.TrimSpace(init.LastName) == "" {
|
|
init.LastName = "User"
|
|
}
|
|
if init.PhoneNumber == "" {
|
|
init.PhoneNumber = formatChapaPhone(user.PhoneNumber)
|
|
}
|
|
init.Customization.Title = sanitizeChapaCustomization("Rezina Academy")
|
|
init.Customization.Description = sanitizeChapaCustomization(course.Name)
|
|
url, err := s.initializeTransaction(ctx, init)
|
|
if err != nil {
|
|
_ = s.paymentStore.UpdatePaymentStatus(ctx, payment.ID, string(domain.PaymentStatusFailed))
|
|
return nil, err
|
|
}
|
|
if err = s.paymentStore.UpdatePaymentSessionID(ctx, payment.ID, ref, url); err != nil {
|
|
return nil, err
|
|
}
|
|
return &domain.CourseCheckoutResponse{PaymentID: payment.ID, SessionID: ref, PaymentURL: url, CourseID: courseID, Amount: course.Price, Currency: course.Currency}, nil
|
|
}
|
|
|
|
func validateCoursePaymentAmount(payment *domain.Payment, data domain.ChapaTransactionData) error {
|
|
amount, err := strconv.ParseFloat(data.Amount.String(), 64)
|
|
if err != nil || math.IsNaN(amount) || math.IsInf(amount, 0) || math.Abs(amount-payment.Amount) > 0.001 || !strings.EqualFold(data.Currency, payment.Currency) {
|
|
return fmt.Errorf("verified course payment amount or currency does not match checkout")
|
|
}
|
|
return nil
|
|
}
|