Skip to content

Instantly share code, notes, and snippets.

@procarrera
Created July 11, 2021 15:49
Show Gist options
  • Save procarrera/c8786f0113077d477ba6f6428f06f156 to your computer and use it in GitHub Desktop.
Save procarrera/c8786f0113077d477ba6f6428f06f156 to your computer and use it in GitHub Desktop.
ReCharge Theme Engine
// this code will update subscriptions from customer dashboard
async function skipCharge(charge_id) {
console.log(charge_id);
const charges = await manageSubscriptions("charges");
const charge = {};
charges.charges.forEach((item) => {
if (item.id === charge_id) {
charge.obj = item;
}
});
const results = {};
for (const line_item of charge.obj.line_items) {
results[line_item.subscription_id] = await dispatchSkip(
line_item.subscription_id
);
}
console.log("results", results);
}
async function dispatchSkip(subscription_id) {
const urlSearchParams = new URLSearchParams(window.location.search);
const params = Object.fromEntries(urlSearchParams.entries());
const hostname = window.location.origin;
const userHash = window.location.href
.split(`${hostname}/tools/recurring/portal/`)[1]
.split("/")[0];
const url = `${hostname}/tools/recurring/portal/${userHash}/subscriptions/${subscription_id}/skip?token=${params.token}`;
console.log("SKIP URL: ", url);
const options = {
method: "POST",
credentials: "same-origin",
headers: {
"Content-Type": "application/json",
},
};
const response = await window.fetch(url, options);
const data = await response.json();
return data;
}
async function manageSubscriptions(schema) {
// defined schemas are charges or customer_data
const urlSearchParams = new URLSearchParams(window.location.search);
const params = Object.fromEntries(urlSearchParams.entries());
const hostname = window.location.origin;
const schemas = {
charges: '{ "charges": [] }',
customer_data:
'{ "addresses": { "discount": { "id": "parent.discount_id" } }, "subscriptions": { "product": {} }, "onetimes": { "product": {} }, "customer": {}, "settings": {}, "store": {} }',
};
const userHash = window.location.href
.split(`${hostname}/tools/recurring/portal/`)[1]
.split("/")[0];
const url = `${hostname}/tools/recurring/portal/${userHash}/request_objects?token=${params.token}&schema=${schemas[schema]}`;
console.log(url);
const response = await window.fetch(url);
const data = await response.json();
console.log(data);
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment