Billing API, app approve charge again after cookies is cleared

Highlighted
Tourist
6 0 1

Hello,

I am playing around a bit with the Shopify CLI. I have created a node app through the CLI and used the command shopify create billing->recurring-billing to generate billing.

When I install the app I get prompted to accept the billing. If I clear the cookies in the browser and click in to the already installed app in my test store this prompt shows up once again.
Is this how its supposed to work or is there some additional implementation needed to get the billing process to work properly? If so how do I implement billing so this issue does not occur.

Thanks in advance!

 

0 Likes
Highlighted
Tourist
6 0 1

Below is the code in server.js CLI creates after cli created project and I have run shopify create billing.

 

import "@babel/polyfill";
import dotenv from "dotenv";
import "isomorphic-fetch";
import createShopifyAuth, { verifyRequest } from "@shopify/koa-shopify-auth";
import graphQLProxy, { ApiVersion } from "@shopify/koa-shopify-graphql-proxy";
import Koa from "koa";
import next from "next";
import Router from "koa-router";
import session from "koa-session";
import * as handlers from "./handlers/index";
dotenv.config();
const port = parseInt(process.env.PORT, 10) || 8081;
const dev = process.env.NODE_ENV !== "production";
const app = next({
  dev
});
const handle = app.getRequestHandler();
const { SHOPIFY_API_SECRET, SHOPIFY_API_KEY, SCOPES } = process.env;
app.prepare().then(() => {
  const server = new Koa();
  const router = new Router();
  server.use(
    session(
      {
        sameSite: "none",
        secure: true
      },
      server
    )
  );
  server.keys = [SHOPIFY_API_SECRET];
  server.use(
    createShopifyAuth({
      apiKey: SHOPIFY_API_KEY,
      secret: SHOPIFY_API_SECRET,
      scopes: [SCOPES],

      async afterAuth(ctx) {
        //Auth token and shop available in session
        //Redirect to shop upon auth
        const { shop, accessToken } = ctx.session;
        ctx.cookies.set("shopOrigin", shop, {
          httpOnly: false,
          secure: true,
          sameSite: "none"
        });
        server.context.client = await handlers.createClient(shop, accessToken);

        await handlers.getSubscriptionUrl(ctx);
      }
    })
  );
  server.use(
    graphQLProxy({
      version: ApiVersion.October19
    })
  );
  router.get("(.*)", verifyRequest(), async ctx => {
    await handle(ctx.req, ctx.res);
    ctx.respond = false;
    ctx.res.statusCode = 200;
  });
  server.use(router.allowedMethods());
  server.use(router.routes());
  server.listen(port, () => {
    console.log(`> Ready on http://localhost:${port}`);
  });
});

 

 

 

If Im not misunderstanding how this is working I still do need to implement logic to fetch a list of current subscriptions for the store and only if no active subscription exist execute getSubscriptionUrl(). Or else every time proper cookies is not there user will be presented to approve a new charge. This charge would inactivate the old charge and it would just be a mess.

Please help to confirm my understanding is correct?

Thanks in advance.

0 Likes