-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
112 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package data | ||
|
||
// Session of a user to pay | ||
type Session struct { | ||
SuccessURL string `json:"success_url"` | ||
CancelURL string `json:"cancel_url"` | ||
PriceID string `json:"price_id"` | ||
CustomerID string `json:"customer_id"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package handlers | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
|
||
"github.com/alexandr-io/backend/payment/data" | ||
"github.com/alexandr-io/backend/payment/database/customer" | ||
stripeCustomer "github.com/alexandr-io/backend/payment/stripe/customer" | ||
) | ||
|
||
// GetCustomerSubscription get the current subscription price of an user | ||
func GetCustomerSubscription(ctx *fiber.Ctx) error { | ||
userID, err := userIDFromHeader(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var localCustomer *data.Customer | ||
localCustomer, err = customer.GetByUserID(userID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
price := stripeCustomer.GetCustomerSubscription(localCustomer.StripeID) | ||
|
||
if err = ctx.Status(fiber.StatusOK).Send([]byte(strconv.Itoa(int(price)))); err != nil { | ||
return data.NewHTTPErrorInfo(fiber.StatusInternalServerError, err.Error()) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package customer | ||
|
||
import ( | ||
"github.com/stripe/stripe-go/v72" | ||
"github.com/stripe/stripe-go/v72/customer" | ||
) | ||
|
||
// GetCustomerSubscription get a customer subscription | ||
func GetCustomerSubscription(stripeID string) int64 { | ||
params := &stripe.CustomerParams{} | ||
params.AddExpand("subscriptions") | ||
c, _ := customer.Get(stripeID, params) | ||
if len(c.Subscriptions.Data) != 0 { | ||
return c.Subscriptions.Data[0].Items.Data[0].Price.UnitAmount | ||
} | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package session | ||
|
||
import ( | ||
"github.com/alexandr-io/backend/payment/data" | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/stripe/stripe-go/v72" | ||
"github.com/stripe/stripe-go/v72/checkout/session" | ||
) | ||
|
||
// Create a stripe customer session | ||
func Create(sessionData data.Session) (*stripe.CheckoutSession, error) { | ||
params := &stripe.CheckoutSessionParams{ | ||
SuccessURL: stripe.String(sessionData.SuccessURL), | ||
CancelURL: stripe.String(sessionData.CancelURL), | ||
PaymentMethodTypes: stripe.StringSlice([]string{ | ||
"card", | ||
}), | ||
LineItems: []*stripe.CheckoutSessionLineItemParams{ | ||
{ | ||
Price: stripe.String(sessionData.PriceID), | ||
Quantity: stripe.Int64(1), | ||
}, | ||
}, | ||
Mode: stripe.String(string(stripe.CheckoutSessionModeSubscription)), | ||
Customer: stripe.String(sessionData.CustomerID), | ||
} | ||
|
||
result, err := session.New(params) | ||
if err != nil { | ||
return nil, data.NewHTTPErrorInfo(fiber.StatusInternalServerError, err.Error()) | ||
} | ||
return result, nil | ||
} |