shopify-app-template-node icon indicating copy to clipboard operation
shopify-app-template-node copied to clipboard

Recurring Billing on App Installation

Open d-motzer opened this issue 2 years ago • 1 comments

I'm having an issue with showing the subscription agreement page after auth.

In my auth.js, this is my /auth/callback logic with the GQL query for appSubscriptionCreate:

`

app.get("/auth/callback", async (req, res) => {
    try {
        const session = await Shopify.Auth.validateAuthCallback(
            req,
            res,
            req.query
        );
        console.log("validating auth ");

        const host = req.query.host;
        app.set(
            "active-shopify-shops",
            Object.assign(app.get("active-shopify-shops"), {
                [session.shop]: session.scope,
            })
        );

        const response = await Shopify.Webhooks.Registry.register({
            shop: session.shop,
            accessToken: session.accessToken,
            topic: "APP_UNINSTALLED",
            path: "/webhooks",
        });

        if (!response["APP_UNINSTALLED"].success) {
            console.log(
                `Failed to register APP_UNINSTALLED webhook: ${response.result}`
            );
        }

       

        // Redirect to app with shop parameter upon auth

        const returnUrl = `/?shop=${session.shop}&host=${host}`
        const subscriptionQuery = `mutation {
  appSubscriptionCreate(
      name: "A test recurring plan"
      returnUrl: "${returnUrl}"
      test: true
      lineItems: [
      {
        plan: {
          appRecurringPricingDetails: {
              price: { amount: 69.99, currencyCode: USD }
          }
        }
      }
      ]
    ) {
        userErrors {
          field
          message
        }
        confirmationUrl
        appSubscription {
          id
        }
    }
}`
        const client = new Shopify.Clients.Graphql(
            session.shop,
            session.accessToken
        );
        await client.query({ data: subscriptionQuery });

        res.redirect(`/?shop=${session.shop}&host=${host}`);
    } catch (e) {
        switch (true) {
            case e instanceof Shopify.Errors.InvalidOAuthError:
                res.status(400);
                res.send(e.message);
                break;
            case e instanceof Shopify.Errors.CookieNotFound:
            case e instanceof Shopify.Errors.SessionNotFound:
                // This is likely because the OAuth session cookie expired before the merchant approved the request
                res.redirect(`/auth?shop=${req.query.shop}`);
                break;
            default:
                res.status(500);
                res.send(e.message);
                break;
        }
    }
});

`

However, all it's doing is installing the app without prompting for agreement to the recurring charge. Any one else having this as well?

d-motzer avatar Apr 18 '22 16:04 d-motzer

You got error on returnUrl, you have to use a absolute url and not relative url:

const returnUrl = `/?shop=${session.shop}&host=${host}` 
     -> 
const returnUrl = `${process.env.HOST}?shop=${session.shop}&host=${host}`

You have to redirect to confirmationUrl and not to default redirect also:

const respGL = await client.query({ data: subscriptionQuery });
const confirmUrl = respGL.body.data.appSubscriptionCreate.confirmationUrl;
res.redirect(confirmUrl); 

elachino avatar May 03 '22 08:05 elachino

This issue is stale because it has been open for 60 days with no activity. It will be closed if no further action occurs in 14 days.

github-actions[bot] avatar Oct 07 '22 02:10 github-actions[bot]

We are closing this issue because it has been inactive for a few months. This probably means that it is not reproducible or it has been fixed in a newer version. If it’s an enhancement and hasn’t been taken on since it was submitted, then it seems other issues have taken priority.

If you still encounter this issue with the latest stable version, please reopen using the issue template. You can also contribute directly by submitting a pull request– see the CONTRIBUTING.md file for guidelines

Thank you!

github-actions[bot] avatar Oct 22 '22 02:10 github-actions[bot]