Skip to content

Instantly share code, notes, and snippets.

@perrupa
Created June 10, 2021 17:32
Show Gist options
  • Save perrupa/eb9d546f82ee70426ca133d8df4abebf to your computer and use it in GitHub Desktop.
Save perrupa/eb9d546f82ee70426ca133d8df4abebf to your computer and use it in GitHub Desktop.
Pricing Plan
import "isomorphic-fetch";
import { gql } from "apollo-boost";
const PRICING_PLANS = {
begginer: {
returnUrl: `${process.env.HOST}/getting_started`,
planName: "Kenyarlow Beginner Plan",
price: 5,
},
pro: {
returnUrl: `${process.env.HOST}/getting_started`,
planName: "Kenyarlow Proffesionals",
price: 20,
},
premium: {
returnUrl: `${process.env.HOST}/premium_getting_started`,
planName: "Kenyarlow Gold Member",
price: 50,
},
};
export function RECURRING_CREATE({ returnUrl, price, planName }) {
return gql`
mutation {
appSubscriptionCreate(
name: "${planName}"
returnUrl: "${returnUrl}"
lineItems: [
{
plan: {
appRecurringPricingDetails: {
price: { amount: ${price}, currencyCode: USD }
}
}
}
# {
# plan: {
# appUsagePricingDetails: {
# cappedAmount: { amount: ${price}, currencyCode: USD }
# terms: "$1 for 1000 emails"
# }
# }
# }
]
) {
userErrors {
field
message
}
confirmationUrl
appSubscription {
id
}
}
}`;
}
export const getSubscriptionUrl = async (ctx) => {
const chosenPlan = getPlanFromRequest(ctx)
const { client } = ctx;
const confirmationUrl = await client
.mutate({
mutation: RECURRING_CREATE(PRICING_PLANS[chosenPlan]),
})
.then((response) => response.data.appSubscriptionCreate.confirmationUrl);
return ctx.redirect(confirmationUrl);
};
function getPlanFromRequest() {
return 'begginer';
}
@perrupa
Copy link
Author

perrupa commented Jun 11, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment