Last active
March 9, 2021 15:34
-
-
Save Maybach91/2b4ba01ebb648143bfa4a047c600ec17 to your computer and use it in GitHub Desktop.
[gatsby + shopify + recurring payments] "instead of taking the user to shopify's checkout, you need to create a recharge's checkout. This can be only done from a server (backend) so you will either have to set one up or use serverless functions (I am using functions from netlify)." – https://github.com/blancomaberino - https://spectrum.chat/gats…
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
/* eslint-disable */ | |
const fetch = require('node-fetch') | |
const rechargeToken = process.env.RECHARGE_TOKEN | |
const subscriptionIntervalFrequency = process.env.SUBSCRIPTION_INTERVAL_FREQUENCY || 30 | |
const subscriptionIntervalUnit = process.env.SUBSCRIPTION_INTERVAL_UNIT || 'day' | |
/** | |
* Builds request headers | |
* @return {Array} | |
*/ | |
const buildHeaders = () => ({ | |
'X-Recharge-Access-Token': rechargeToken, | |
'Accept': 'application/json', | |
'Content-Type': 'application/json' | |
}) | |
exports.handler = async function(event, context) { | |
try { | |
const params = JSON.parse(event.body) | |
if (!params || !params.line_items) { | |
return { statusCode: 400, body: JSON.stringify({ | |
message: 'Missing parameters', | |
data: ['line_items'] | |
}) } | |
} | |
const line_items = params.line_items.map((item) => { | |
item = { | |
...item | |
} | |
// Without information below, item will be treated as `one-time` product and not generate a subscription, so you may want to use it for non-subscription products and unify everything under recharge's checkout instead of using shopify's one for non-subscription and recharge's for subscriptions. | |
if (item.is_subscription === true) { | |
item.charge_interval_frequency = subscriptionIntervalFrequency | |
item.order_interval_frequency = subscriptionIntervalFrequency | |
item.order_interval_unit = subscriptionIntervalUnit | |
} | |
delete item.is_subscription | |
return item | |
}) | |
const checkoutApiUrl = 'https://api.rechargeapps.com/checkouts' | |
const response = await fetch(checkoutApiUrl, { | |
headers: buildHeaders(), | |
method: 'post', | |
body: JSON.stringify({ | |
line_items | |
}) | |
}) | |
if (!response.ok) { | |
throw response | |
} | |
const data = await response.json() | |
const checkoutUrl = `https://checkout.rechargeapps.com/r/checkout/${data.checkout.token}?myshopify_domain=${process.env.SHOPIFY_SHOP_NAME}.myshopify.com` | |
return { | |
statusCode: 200, | |
body: JSON.stringify({ | |
webUrl: checkoutUrl | |
}) | |
} | |
} catch (err) { | |
// console.log(err) // output to netlify function log | |
return { | |
statusCode: err.statusCode || 500, | |
body: JSON.stringify({ message: err.statusText || err.message || 'Internal Server Error' }) // Could be a custom message or object i.e. JSON.stringify(err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment