MCPcopy Create free account
hub / github.com/Acode-Foundation/Acode / buy

Function buy

src/pages/plugin/plugin.js:265–342  ·  view source on GitHub ↗
(e)

Source from the content-addressed store, hash-verified

263 }
264
265 async function buy(e) {
266 const $button = e.target;
267 const oldText = $button.textContent;
268
269 try {
270 if (helpers.shouldAllowExternalPurchase()) {
271 await customTab(`${config.BASE_URL}/plugin/${id}?callback=app`);
272 if (!purchaseHandlerSet) {
273 purchaseHandlerSet = true;
274 const handler = async ({ module, action, value }) => {
275 if (module === "plugin" && action === "purchased" && value === id) {
276 loader.show();
277
278 try {
279 const pluginData = await fsOperation(
280 config.API_BASE,
281 `plugin/${id}`,
282 ).readFile("json");
283
284 purchased = pluginData.owned;
285 render();
286 } catch (error) {
287 window.log(error);
288 helpers.error(error);
289 }
290
291 loader.hide();
292 purchaseHandlerSet = false;
293 removeIntentHandler(handler);
294 }
295 };
296 addIntentHandler(handler);
297 cleanups.push(() => removeIntentHandler(handler));
298 }
299 return;
300 }
301 } catch (error) {}
302
303 try {
304 if (!product) throw new Error("Product not found");
305 const apiStatus = await helpers.checkAPIStatus();
306
307 if (!apiStatus) {
308 alert(strings.error, strings.api_error);
309 return;
310 }
311
312 iap.setPurchaseUpdatedListener(...purchaseListener(onpurchase, onerror));
313 $button.textContent = strings["loading..."];
314 await helpers.promisify(iap.purchase, product.productId);
315
316 async function onpurchase(e) {
317 const purchase = await getPurchase(product.productId);
318 await fetch(Url.join(config.API_BASE, "plugin/order"), {
319 method: "POST",
320 body: JSON.stringify({
321 id: plugin.id,
322 token: purchase?.purchaseToken,

Callers

nothing calls this directly

Calls 8

addIntentHandlerFunction · 0.90
removeIntentHandlerFunction · 0.90
customTabFunction · 0.85
alertFunction · 0.85
purchaseListenerFunction · 0.85
logMethod · 0.80
errorMethod · 0.45

Tested by

no test coverage detected