server/getSubscriptionUrl.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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