Skip to content
Open
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: 12 additions & 0 deletions controller/subscription_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,11 @@ func stripeHandleInvoicePaid(

AddRefreshTransferBalance(clientSession.Ctx, *networkId)

id, ok := clientSession.Ctx.Value(stripeWebhookIDKey).(server.Id)
if ok {
model.MarkStripeWebhookProcessed(clientSession.Ctx, id, *networkId)
}

return &StripeWebhookResult{}, nil

}
Expand Down Expand Up @@ -1483,12 +1488,19 @@ func PlaySubscriptionRenewalPost(
return nil
}

const stripeWebhookIDKey = "stripeWebhookID"

func VerifyStripeBody(req *http.Request) (io.Reader, error) {
bodyBytes, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}

id, _ := model.StoreStripeWebhookBody(req.Context(), bodyBytes)
// Attach the ID to the context
ctx := context.WithValue(req.Context(), stripeWebhookIDKey, id)
req = req.WithContext(ctx)

_, err = stripewebhook.ConstructEventWithOptions(
bodyBytes,
req.Header.Get("Stripe-Signature"),
Expand Down
13 changes: 12 additions & 1 deletion db_migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2558,7 +2558,7 @@ var migrations = []any{
min_block_number bigint NOT NULL DEFAULT 0,
max_block_number bigint NOT NULL DEFAULT 0,
country_location_id uuid NOT NULL DEFAULT gen_random_uuid(),

PRIMARY KEY (network_id, country_location_id)
)
`),
Expand Down Expand Up @@ -2638,4 +2638,15 @@ var migrations = []any{
ALTER TABLE network_client_speed
ADD COLUMN sample_count bigint NOT NULL DEFAULT 1
`),

newSqlMigration(`
CREATE TABLE subscription_webhook_stripe (
id uuid NOT NULL,
webhook_body text NOT NULL,
network_id uuid,
created_at timestamp NOT NULL DEFAULT now(),

PRIMARY KEY (id)
)
`),
}
49 changes: 49 additions & 0 deletions model/subscription_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -3020,3 +3020,52 @@ func GetOpenTransferByteCount(
return openTransferByteCount

}

func StoreStripeWebhookBody(ctx context.Context, webhookBody []byte) (id server.Id, returnErr error) {

server.Tx(ctx, func(tx server.PgTx) {

id = server.NewId()

server.RaisePgResult(tx.Exec(
ctx,
`
INSERT INTO subscription_webhook_stripe (
id,
webhook_body
)
VALUES ($1, $2)
`,
id,
string(webhookBody),
))
})

return
}

func MarkStripeWebhookProcessed(
ctx context.Context,
id server.Id,
networkId server.Id,
) (returnErr error) {

server.Tx(ctx, func(tx server.PgTx) {

server.RaisePgResult(tx.Exec(
ctx,
`
UPDATE subscription_webhook_stripe
SET
network_id = $2
WHERE
id = $1
`,
id,
networkId,
))
})

return

}
22 changes: 22 additions & 0 deletions model/subscription_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,28 @@ func TestGetOpenTransferByteCount(t *testing.T) {
})
}

func TestStoreStripeWebhook(t *testing.T) {
server.DefaultTestEnv().Run(func() {

ctx := context.Background()

webhookBody := `{
"id": "evt_1JQY2Y2eZvKYlo2C0q7Z1g5H",
"object": "event",
}`

id, err := StoreStripeWebhookBody(ctx, []byte(webhookBody))
assert.Equal(t, err, nil)
assert.NotEqual(t, id, "")

networkId := server.NewId()

err = MarkStripeWebhookProcessed(ctx, id, networkId)
assert.Equal(t, err, nil)

})
}

// FIXME a subsidy test where N clients pay each other
// FIXME each client uses a different amount of data, but sends to peer clients following the same offset distribution as the others
// FIXME the end result is that everyone should be paid the same, even though they get different amounts of data