Skip to content

Instantly share code, notes, and snippets.

@nutterbrand
Created January 4, 2021 01:03
Show Gist options
  • Save nutterbrand/d49a34e2624318f54b627258af43c047 to your computer and use it in GitHub Desktop.
Save nutterbrand/d49a34e2624318f54b627258af43c047 to your computer and use it in GitHub Desktop.
server/getSubscriptionUrl.js
const getSubscriptionUrl = async (ctx, accessToken, shop) => {
const query = JSON.stringify({
query: `mutation {
appSubscriptionCreate(
name: "Super Duper Plan"
returnUrl: "${process.env.HOST}"
test: true
lineItems: [
{
plan: {
appRecurringPricingDetails: {
price: { amount: 10, currencyCode: USD }
}
}
}
]
) {
userErrors {
field
message
}
confirmationUrl
appSubscription {
id
}
}
}`,
});
const response = await fetch(
`https://${shop}/admin/api/2019-10/graphql.json`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Shopify-Access-Token": accessToken,
},
body: query,
}
);
const responseJson = await response.json();
const confirmationUrl =
responseJson.data.appSubscriptionCreate.confirmationUrl;
return ctx.redirect(confirmationUrl);
};
module.exports = getSubscriptionUrl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment