Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions db/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package db
import (
"errors"
"fmt"
"time"

"github.com/Quaver/api2/enums"
"gorm.io/gorm"
"time"
)

type OrderStatus string
Expand All @@ -18,8 +19,8 @@ const (
type Order struct {
Id int `gorm:"column:id; PRIMARY_KEY" json:"id"`
UserId int `gorm:"column:user_id" json:"user_id"`
OrderId int `gorm:"column:order_id" json:"-"`
TransactionId string `gorm:"column:transaction_id" json:"-"`
OrderId int `gorm:"column:order_id" json:"order_id"`
TransactionId string `gorm:"column:transaction_id" json:"transaction_id"`
IPAddress string `gorm:"column:ip_address" json:"-"`
ItemId OrderItemId `gorm:"column:item_id" json:"item_id"`
Quantity int `gorm:"column:quantity" json:"quantity"`
Expand Down Expand Up @@ -254,14 +255,17 @@ func (order *Order) FinalizeUserAccentColor() error {
}

// GetUserOrders Gets a user's orders
func GetUserOrders(userId int) ([]*Order, error) {
func GetUserOrders(userId int, limit int, page int) ([]*Order, error) {
var orders = make([]*Order, 0)

result := SQL.
Preload("Receiver").
Preload("Item").
Preload("Subscription").
Where("orders.user_id = ? AND orders.status = ?", userId, "Completed").
Order("orders.timestamp DESC").
Limit(limit).
Offset(page * limit).
Find(&orders)

if result.Error != nil {
Expand Down
8 changes: 7 additions & 1 deletion handlers/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ func GetUserOrders(c *gin.Context) *APIError {
return nil
}

orders, err := db.GetUserOrders(user.Id)
page, err := strconv.Atoi(c.Query("page"))

if err != nil {
page = 0
}

orders, err := db.GetUserOrders(user.Id, 10, page)

if err != nil {
return APIErrorServerError("Error retrieving orders from db", err)
Expand Down
15 changes: 13 additions & 2 deletions handlers/orders_stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"gorm.io/gorm"
"io"
"net/http"
"strings"
"time"
)

Expand Down Expand Up @@ -62,7 +63,7 @@ func InitiateStripeDonatorCheckoutSession(c *gin.Context) *APIError {
},
},
Mode: stripe.String(string(getStripeCheckoutMode(body.Recurring))),
SuccessURL: stripe.String(config.Instance.Stripe.DonateRedirectUrl),
SuccessURL: stripe.String(getStripeSuccessURL(config.Instance.Stripe.DonateRedirectUrl)),
AutomaticTax: &stripe.CheckoutSessionAutomaticTaxParams{Enabled: stripe.Bool(true)},
}

Expand Down Expand Up @@ -298,7 +299,7 @@ func createStripeCheckoutSession(c *gin.Context, orders []*db.Order) *APIError {
params := &stripe.CheckoutSessionParams{
LineItems: lineItems,
Mode: stripe.String(string(stripe.CheckoutSessionModePayment)),
SuccessURL: stripe.String(config.Instance.Stripe.StorePaymentRedirectUrl),
SuccessURL: stripe.String(getStripeSuccessURL(config.Instance.Stripe.StorePaymentRedirectUrl)),
AutomaticTax: &stripe.CheckoutSessionAutomaticTaxParams{Enabled: stripe.Bool(true)},
}

Expand Down Expand Up @@ -328,6 +329,16 @@ func createStripeCheckoutSession(c *gin.Context, orders []*db.Order) *APIError {
return nil
}

func getStripeSuccessURL(redirectURL string) string {
separator := "?"

if strings.Contains(redirectURL, "?") {
separator = "&"
}

return fmt.Sprintf("%s%sorder_id=-1&transaction_id={CHECKOUT_SESSION_ID}", redirectURL, separator)
}

// Gets the donator price id for Stripe
func getStripeDonatorPriceId(months int, isRecurring bool) string {
if config.Instance.IsProduction {
Expand Down
Loading