How can I create a subscription using appSubscriptionCreate?

Solved

How can I create a subscription using appSubscriptionCreate?

izandev
Shopify Partner
2 1 0

After searching for days on how to redirect the customer from the app to the checkout subscription page when browsing the app or pressing a button, I discovered the appSubscriptionCreate mutation to create a charge. But after many attempts I get a 500 error when I try to create a subscription and I don't know if I'm missing something. I tried to set test: true but I still get the same error. Any ideas? Thanks.

Also, does anyone know how to return the url of the application in returnUrl?

 

 

Captura de pantalla 2023-02-23 201721.png

Captura de pantalla 2023-02-23 205908.png

This is the code I have been using to create a charge, does anyone see any errors?

Shopify.js file configuration:

 

import { LATEST_API_VERSION } from "@shopify/shopify-api";
import { shopifyApp } from "@shopify/shopify-app-express";
import { SQLiteSessionStorage } from "@shopify/shopify-app-session-storage-sqlite";
import { restResources } from "@shopify/shopify-api/rest/admin/2023-01";

import { billingConfig } from "./billing.js";

const DB_PATH = `${process.cwd()}/database.sqlite`;

const shopify = shopifyApp({
  api: {
    apiVersion: LATEST_API_VERSION,
    restResources,
    billing: billingConfig,
  },
  auth: {
    path: "/api/auth",
    callbackPath: "/api/auth/callback",
  },
  webhooks: {
    path: "/api/webhooks",
  },
  sessionStorage: new SQLiteSessionStorage(DB_PATH),
});

export default shopify;

 

 

Billing.js file configuration:

 

import { BillingInterval, BillingReplacementBehavior, GraphqlQueryError, ApiVersion } from "@shopify/shopify-api";

import shopify from "./shopify.js";

export const billingConfig = {
    "My Shopify Charge": {
        amount: 5.0,
        currencyCode: "USD",
        trialDays: 15,
        interval: BillingInterval.Every30Days,
        replacementBehavior: BillingReplacementBehavior.ApplyImmediately
    },
};

const CREATE_SUBSCRIPTION_RECORD = `
    mutation appSubscriptionCreate($lineItems: [AppSubscriptionLineItemInput!]!, $name: String!, $returnUrl: URL!, $trialDays: Int, $test: Boolean) {
        appSubscriptionCreate(lineItems: $lineItems, name: $name, returnUrl: $returnUrl, trialDays: $trialDays, test: $test) {
            appSubscription {
                id
            }
            confirmationUrl
            userErrors {
                field
                message
            }
        }
    }
`;

export async function createSubscriptionRecord(session, req) {
    let response = {}

    try {
        const client = new shopify.api.clients.Graphql({ session });

        response = await client.query({
            data: {
                query: CREATE_SUBSCRIPTION_RECORD,
                variables: {
                    lineItems: [
                        {
                            plan: {
                                appRecurringPricingDetails: {
                                    price: {
                                        amount: 10,
                                        currencyCode: "USD"
                                    },
                                    interval: "EVERY_30_DAYS"
                                }
                            }
                        }
                    ],
                    name: Object.keys(billingConfig)[0],
                    returnUrl: shopify.utils.sanitizeShop(req.query.shop, true),
                    trialDays: 15,
                    test: true
                },
            },
        });
        console.log(response);

    } catch (error) {
        if (error instanceof GraphqlQueryError) {
            throw new Error(
                `${error.message}\n${JSON.stringify(error.response, null, 2)}`
            );
        } else {
            throw error;
        }
    }

    return response;
}

 

 

Accepted Solution (1)

izandev
Shopify Partner
2 1 0

This is an accepted solution.

After a lot of research I realized that the problem was the "returnUrl", now it was working correctly.

View solution in original post

Replies 2 (2)

izandev
Shopify Partner
2 1 0

This is an accepted solution.

After a lot of research I realized that the problem was the "returnUrl", now it was working correctly.

Hamza31
Shopify Partner
6 0 2

please guide returnUrl correct format i'm also facing issue in subscription with 7 days trail using

mutation appSubscriptionCreate