SkynetLabs / skynet-accounts

Other
8 stars 5 forks source link

Stripe promote on checkout #228

Closed ro-tex closed 2 years ago

ro-tex commented 2 years ago

PULL REQUEST

Overview

Our typical payment flow starts with us creating a checkout session and redirecting the user to a Stripe-controlled page where they make the actual payment. After the payment is done, we wait for a push notification on our webhook endpoint. We process that notification and promote/demote the user accordingly. This works well and is reliable because Stripe will keep sending the webhook event until they get a successful response. The problem with it is that it might take a while and the user will be left without any notification what is going on.

This PR solves that problem by introducing a new endpoint which we can proactively call in order to check the status of a given checkout session and promote the user, if required. Note that we won't demote the user by calling this endpoint - that will be handled by the webhook endpoint. This new endpoint will only be used in order to speed up the user promotion process, so we get happy users.

Example for Visual Changes

Response JSON example:

{
    "created": 1658417273,
    "currentPeriodStart": 1658417273,
    "discount": {
        "amountOff": 0,
        "currency": "",
        "duration": "repeating",
        "durationInMonths": 3,
        "name": "test50%",
        "percentOff": 50
    },
    "id": "sub_1LO1I5IzjULiPWN6zatWjjxl",
    "plan": {
        "amount": 2000,
        "currency": "usd",
        "interval": "month",
        "intervalCount": 1,
        "product": {
            "description": "Pin up to 4TB",
            "name": "Skynet Pro"
        }
    },
    "startDate": 1658417273,
    "status": "active"
}

Checklist

Issues Closed

Closes SKY-1056

linear[bot] commented 2 years ago
SKY-1056 Implement an API endpoint to query the status of a payment

This endpoint should synchronously check the payment status with Stripe and update the user record if it was successful (so that we don't need to wait for the webhook call to arrive).