Загрузка данных


await (async () => {
  const session = await (await fetch("/api/auth/session", { credentials: "include" })).json();
  const authToken = session?.accessToken;
  const accountId = session?.account?.id;

  if (!authToken || !accountId) {
    const error = { error: "AUTH_OR_ACCOUNT_ID_MISSING", message: "Open ChatGPT in the active account and try again." };
    await navigator.clipboard?.writeText?.(JSON.stringify(error, null, 2)).catch(() => {});
    return JSON.stringify(error);
  }

  const a = {
    checkout_ui_mode: "redirect",
    entry_point: "credit_purchase_modal",
    credit_purchase_data: {
      quantity: 500,
      unit: "credit",
      account_id: accountId
    },
    billing_details: {
      country: "IN",
      currency: "INR"
    },
    cancel_url: window.location.href
  };

  const res = await fetch("https://chatgpt.com/backend-api/payments/checkout", {
    body: JSON.stringify(a),
    method: "POST",
    credentials: "include",
    headers: {
      "Content-Type": "application/json",
      authorization: `Bearer ${authToken}`
    }
  });
  const data = await res.json().catch(async () => ({ raw: await res.text().catch(() => "") }));
  if (!res.ok) {
    const error = {
      status: res.status,
      entry_point: a.entry_point,
      quantity: a.credit_purchase_data.quantity,
      billing_details: a.billing_details,
      data
    };
    await navigator.clipboard?.writeText?.(JSON.stringify(error, null, 2)).catch(() => {});
    return JSON.stringify(error);
  }

  const url = data.url || (
    data.checkout_session_id && data.processor_entity
      ? `https://chatgpt.com/checkout/${data.processor_entity}/${data.checkout_session_id}`
      : JSON.stringify(data)
  );

  await navigator.clipboard?.writeText?.(url).catch(() => {});
  return url;
})();