Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

causing a billing redirect loop #951

Closed
maneko00 opened this issue Jul 27, 2023 · 1 comment
Closed

causing a billing redirect loop #951

maneko00 opened this issue Jul 27, 2023 · 1 comment

Comments

@maneko00
Copy link

maneko00 commented Jul 27, 2023

Issue summary

When I try to redirect the billing url for the app, a loop occurs and status code 302 is returned.
I used redirectOutOfApp function

status code 302 image

It works in the development environment, but when the app is deployed to Heroku, it enters a redirect loop and does not proceed to the billing screen.
redirect loop image

  • @shopify/shopify-api version:7.3.1
  • Node version:18.15.0
  • Operating system:M1 Mac
// @ts-check
import { join } from "path";
import { readFileSync } from "fs";
import express from "express";
import serveStatic from "serve-static";

import shopify from "./shopify.js";
import GDPRWebhookHandlers from "./gdpr.js";
import { requestBilling } from "./billing.js";

const PORT = parseInt(
  process.env.BACKEND_PORT || process.env.PORT || "3000",
  10
);

const STATIC_PATH =
  process.env.NODE_ENV === "production"
    ? `${process.cwd()}/frontend/dist`
    : `${process.cwd()}/frontend/`;

const app = express();

// Set up Shopify authentication and webhook handling
app.get(shopify.config.auth.path, shopify.auth.begin());
app.get(
  shopify.config.auth.callbackPath,
  shopify.auth.callback(),
  requestBilling,
  shopify.redirectToShopifyOrAppRoot()
);
app.post(
  shopify.config.webhooks.path,
  shopify.processWebhooks({ webhookHandlers: GDPRWebhookHandlers })
);

app.use("/api/*", shopify.validateAuthenticatedSession());

app.use(express.json());

app.use(shopify.cspHeaders());
app.use(serveStatic(STATIC_PATH, { index: false }));

app.use("/*", shopify.ensureInstalledOnShop(),
  requestBilling,
  async (_req, res, _next) => {
  return res
    .status(200)
    .set("Content-Type", "text/html")
    .send(readFileSync(join(STATIC_PATH, "index.html")));
});

app.listen(PORT);

billing.js

import shopify, { billingConfig } from "./shopify.js";


export const requestBilling = async (req, res, next) => {
  console.debug('Start checking billing');

  const plans = Object.keys(billingConfig);
  let session = res.locals.shopify?.session;
  const isAuth = !session ? false : true;

  if(!isAuth) {
    session = await getRequestShopSession(
      shopify.api,
      shopify.config,
      req,
      res
    );
  }

  const isTest = process.env.NODE_ENV !== 'production';

  const hasPayment = await shopify.api.billing.check({session, plans, isTest});
${shopInfo.planDisplayName}`);

  if (hasPayment) {
    next();
  } else {
    const redirectUri = await shopify.api.billing.request({session, plan: plans[0], isTest});
    if(isAuth) {
      res.redirect(redirectUri);
    }
    else {
      shopify.redirectOutOfApp({
        req,
        res,
        redirectUri: redirectUri,
        shop: shopify.api.utils.sanitizeShop(req.query.shop),
      });
    }
  }
};

const getRequestShopSession = async(api, config, req, res) => {
  const shop = shopify.api.utils.sanitizeShop(req.query.shop);
  if (!shop) {
    return undefined;
  }
  config.logger.debug('Checking if shop has installed the app', {
    shop
  });
  const sessionId = api.session.getOfflineId(shop);
  const session = await config.sessionStorage.loadSession(sessionId);
  return session;
}

Expected behavior

Actual behavior

Steps to reproduce the problem

  1. Create a new shopify/cli node app template.
  2. Add the billing process and deploy the app
  3. Install the application and cancel the billing.
  4. When you open the application again, you will be redirected to the billing page.
@maneko00 maneko00 changed the title redirectOutOfApp does not work, causing a billing redirect loop causing a billing redirect loop Jul 29, 2023
@maneko00
Copy link
Author

maneko00 commented Aug 4, 2023

Changed to redirect from frontend.

@maneko00 maneko00 closed this as completed Aug 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant