Skip to content

Commit 59c0693

Browse files
Introduce AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_METADATA_MARKER_VALUE
1 parent 2c4e73d commit 59c0693

File tree

4 files changed

+38
-21
lines changed

4 files changed

+38
-21
lines changed

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_CANCEL_URL=https://www.authgear.com/paymen
1111
AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_PRICE_ID=price_foobar
1212
AUTHGEAR_ONCE_STRIPE_WEBHOOK_SIGNING_SECRET=whsec_foobar
1313

14+
# The value of a metadata attached to the created checkout session.
15+
# It is used to distinguish between other checkout sessions that ARE NOT created by this server.
16+
AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_METADATA_MARKER_VALUE=authgear-once-license-server-local
17+
1418
# SMTP related cnfigurations.
1519
AUTHGEAR_ONCE_SMTP_HOST=smtp.example.com
1620
AUTHGEAR_ONCE_SMTP_PORT=587

cmd/cli/main.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ type Dependencies struct {
129129
StripeCheckoutSessionCancelURL string
130130
StripeCheckoutSessionPriceID string
131131
StripeWebhookSigningSecret string
132+
StripeCheckoutSessionMetadataMarkerValue string
132133
AUTHGEAR_ONCE_PUBLIC_URL_SCHEME string
133134
AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE string
134135
AUTHGEAR_ONCE_ONCE_COMMAND_IMAGE_OVERRIDE string
@@ -291,9 +292,10 @@ func Handler_v1_stripe_checkout(w http.ResponseWriter, r *http.Request) {
291292
stripeClient := deps.StripeClient
292293

293294
checkoutSession, err := pkgstripe.NewCheckoutSession(ctx, stripeClient, &pkgstripe.CheckoutSessionParams{
294-
SuccessURL: deps.StripeCheckoutSessionSuccessURL,
295-
CancelURL: deps.StripeCheckoutSessionCancelURL,
296-
PriceID: deps.StripeCheckoutSessionPriceID,
295+
MarkerValue: deps.StripeCheckoutSessionMetadataMarkerValue,
296+
SuccessURL: deps.StripeCheckoutSessionSuccessURL,
297+
CancelURL: deps.StripeCheckoutSessionCancelURL,
298+
PriceID: deps.StripeCheckoutSessionPriceID,
297299
})
298300
if err != nil {
299301
slogging.Error(ctx, logger, "failed to create checkout session",
@@ -315,7 +317,7 @@ func Handler_v1_stripe_webhook(w http.ResponseWriter, r *http.Request) {
315317

316318
e, err := pkgstripe.ConstructEvent(ctx, deps.StripeClient, r, pkgstripe.ConstructEventOptions{
317319
SigningSecret: deps.StripeWebhookSigningSecret,
318-
PriceID: deps.StripeCheckoutSessionPriceID,
320+
MarkerValue: deps.StripeCheckoutSessionMetadataMarkerValue,
319321
})
320322
if err != nil {
321323
if errors.Is(err, pkgstripe.ErrUnknownEvent) {
@@ -421,15 +423,16 @@ func main() {
421423
})
422424

423425
dependencies := Dependencies{
424-
HTTPClient: &http.Client{},
425-
StripeClient: stripeClient,
426-
SMTPDialer: smtpDialer,
427-
SMTPSender: os.Getenv("AUTHGEAR_ONCE_SMTP_SENDER"),
428-
StripeCheckoutSessionSuccessURL: os.Getenv("AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_SUCCESS_URL"),
429-
StripeCheckoutSessionCancelURL: os.Getenv("AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_CANCEL_URL"),
430-
StripeCheckoutSessionPriceID: os.Getenv("AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_PRICE_ID"),
431-
StripeWebhookSigningSecret: os.Getenv("AUTHGEAR_ONCE_STRIPE_WEBHOOK_SIGNING_SECRET"),
432-
AUTHGEAR_ONCE_PUBLIC_URL_SCHEME: os.Getenv("AUTHGEAR_ONCE_PUBLIC_URL_SCHEME"),
426+
HTTPClient: &http.Client{},
427+
StripeClient: stripeClient,
428+
SMTPDialer: smtpDialer,
429+
SMTPSender: os.Getenv("AUTHGEAR_ONCE_SMTP_SENDER"),
430+
StripeCheckoutSessionSuccessURL: os.Getenv("AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_SUCCESS_URL"),
431+
StripeCheckoutSessionCancelURL: os.Getenv("AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_CANCEL_URL"),
432+
StripeCheckoutSessionPriceID: os.Getenv("AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_PRICE_ID"),
433+
StripeWebhookSigningSecret: os.Getenv("AUTHGEAR_ONCE_STRIPE_WEBHOOK_SIGNING_SECRET"),
434+
StripeCheckoutSessionMetadataMarkerValue: os.Getenv("AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_METADATA_MARKER_VALUE"),
435+
AUTHGEAR_ONCE_PUBLIC_URL_SCHEME: os.Getenv("AUTHGEAR_ONCE_PUBLIC_URL_SCHEME"),
433436
AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE: os.Getenv("AUTHGEAR_ONCE_ONCE_COMMAND_DOWNLOAD_URL_GO_TEMPLATE"),
434437
AUTHGEAR_ONCE_ONCE_COMMAND_IMAGE_OVERRIDE: os.Getenv("AUTHGEAR_ONCE_ONCE_COMMAND_IMAGE_OVERRIDE"),
435438
KeygenConfig: keygen.KeygenConfig{
@@ -451,6 +454,11 @@ func main() {
451454

452455
ctx = slogging.WithLogger(ctx, logger)
453456

457+
if dependencies.StripeCheckoutSessionMetadataMarkerValue == "" {
458+
slogging.Error(ctx, logger, "AUTHGEAR_ONCE_STRIPE_CHECKOUT_SESSION_METADATA_MARKER_VALUE must be set")
459+
os.Exit(1)
460+
}
461+
454462
if err := rootCmd.ExecuteContext(ctx); err != nil {
455463
slogging.Error(ctx, logger, "root command completed with error",
456464
"error", err)

pkg/stripe/checkout.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import (
77
"github.com/stripe/stripe-go/v82/client"
88
)
99

10+
const MetadataKeyMarker = "authgear_once_license_server"
11+
1012
type CheckoutSessionParams struct {
11-
SuccessURL string
12-
CancelURL string
13-
PriceID string
13+
MarkerValue string
14+
SuccessURL string
15+
CancelURL string
16+
PriceID string
1417
}
1518

1619
func NewCheckoutSession(ctx context.Context, client *client.API, params *CheckoutSessionParams) (*stripe.CheckoutSession, error) {
@@ -29,6 +32,9 @@ func NewCheckoutSession(ctx context.Context, client *client.API, params *Checkou
2932
Quantity: stripe.Int64(1),
3033
},
3134
},
35+
Metadata: map[string]string{
36+
MetadataKeyMarker: params.MarkerValue,
37+
},
3238
}
3339

3440
sess, err := client.CheckoutSessions.New(sessParams)

pkg/stripe/webhook.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var ErrUnknownEvent = errors.New("pkgstripe: unknown event")
1616

1717
type ConstructEventOptions struct {
1818
SigningSecret string
19-
PriceID string
19+
MarkerValue string
2020
}
2121

2222
func ConstructEvent(ctx context.Context, client *client.API, r *http.Request, opts ConstructEventOptions) (*stripe.Event, error) {
@@ -45,10 +45,9 @@ func ConstructEvent(ctx context.Context, client *client.API, r *http.Request, op
4545
return nil, err
4646
}
4747

48-
for _, lineItem := range checkoutSession.LineItems.Data {
49-
if lineItem.Price.ID == opts.PriceID {
50-
return &e, nil
51-
}
48+
marker := checkoutSession.Metadata[MetadataKeyMarker]
49+
if marker == opts.MarkerValue {
50+
return &e, nil
5251
}
5352

5453
return &e, ErrUnknownEvent

0 commit comments

Comments
 (0)