@edx/frontend-app-subscription-checkout 1.24.0 → 1.24.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -16,6 +16,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
16
16
|
*/
|
|
17
17
|
import { GET_CUSTOMER, GET_BILLING_PLANS, GET_PRODUCT, GET_ACTIVE_SUBSCRIPTIONS_STATE, GET_IS_CURRENT_USER_SUBSCRIBER, GET_COURSE_DETAIL } from './queries';
|
|
18
18
|
import { getSiteConfig } from '@openedx/frontend-base';
|
|
19
|
+
function encodeCourseRunKey(key) {
|
|
20
|
+
return encodeURIComponent(key.trim());
|
|
21
|
+
}
|
|
19
22
|
/** Resolve the viax customer uid for the authenticated user by email. */
|
|
20
23
|
export function getCustomerUid(gql, userEmail) {
|
|
21
24
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -109,13 +112,14 @@ export function getCourseDetail(gql, courseKey) {
|
|
|
109
112
|
const key = courseKey.trim();
|
|
110
113
|
if (!key)
|
|
111
114
|
return null;
|
|
115
|
+
const encodedKey = encodeCourseRunKey(key);
|
|
112
116
|
const siteConfig = getSiteConfig();
|
|
113
117
|
const LMS_BASE_URL = siteConfig.lmsBaseUrl;
|
|
114
118
|
// 1. Fetch resolved key from Partner REST API
|
|
115
119
|
let resolvedCourseKey = key;
|
|
116
120
|
try {
|
|
117
121
|
const url = new URL(`${LMS_BASE_URL.replace(/\/$/, '')}/api/subscription/course-runs`);
|
|
118
|
-
url.
|
|
122
|
+
url.search = `key=${encodedKey}`;
|
|
119
123
|
const res = yield fetch(url.toString(), {
|
|
120
124
|
method: 'GET',
|
|
121
125
|
headers: { Accept: 'application/json' },
|
|
@@ -123,9 +127,9 @@ export function getCourseDetail(gql, courseKey) {
|
|
|
123
127
|
if (res.ok) {
|
|
124
128
|
const payload = yield res.json();
|
|
125
129
|
// Extract key from elements array as per Partner API response format
|
|
126
|
-
const
|
|
127
|
-
if (
|
|
128
|
-
resolvedCourseKey =
|
|
130
|
+
const partnerCourseKey = (_b = (_a = payload === null || payload === void 0 ? void 0 : payload.elements) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.course;
|
|
131
|
+
if (partnerCourseKey) {
|
|
132
|
+
resolvedCourseKey = partnerCourseKey;
|
|
129
133
|
}
|
|
130
134
|
}
|
|
131
135
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bootstrapApi.js","sourceRoot":"","sources":["../../src/utils/bootstrapApi.ts"],"names":[],"mappings":";;;;;;;;;AAAA;;;;;;GAMG;AACH,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,WAAW,EAAE,8BAA8B,EAAE,8BAA8B,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAE5J,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAEvD,yEAAyE;AACzE,MAAM,UAAgB,cAAc,CAAC,GAAc,EAAE,SAAiB;;;QACpE,MAAM,MAAM,GAAG,gCAAgC,SAAS,GAAG,CAAC;QAC5D,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,YAAY,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC;QACjD,MAAM,IAAI,GAA6B,MAAA,MAAA,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,kBAAkB,0CAAE,KAAK,0CAAG,CAAC,CAAC,0CAAE,IAAI,CAAC;QAClF,IAAI,CAAC,CAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,GAAG,CAAA;YAAE,MAAM,IAAI,KAAK,CAAC,8BAA8B,SAAS,kCAAkC,CAAC,CAAC;QAC3G,OAAO,IAAI,CAAC,GAAG,CAAC;IAClB,CAAC;CAAA;AAED;;;;GAIG;AACH,MAAM,UAAgB,aAAa,CAAC,GAAc,EAAE,IAAY;;;QAC9D,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,W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,IAAI,mCAAI,IAAI;YAC/C,UAAU,EAAE,MAAA,IAAI,CAAC,UAAU,mCAAI,IAAI;YACnC,WAAW,EAAE,MAAA,IAAI,CAAC,WAAW,mCAAI,IAAI;YACrC,WAAW,EAAE,MAAA,MAAA,IAAI,CAAC,WAAW,0CAAE,IAAI,mCAAI,IAAI;YAC3C,UAAU,EAAE,MAAA,IAAI,CAAC,UAAU,mCAAI,IAAI;YACnC,yBAAyB,EAAE,MAAA,IAAI,CAAC,yBAAyB,mCAAI,IAAI;YACjE,oBAAoB,EAAE,MAAA,IAAI,CAAC,oBAAoB,mCAAI,IAAI;YACvD,cAAc,EAAE,MAAA,IAAI,CAAC,cAAc,mCAAI,IAAI;SAC5C,CAAC;IACJ,CAAC;CAAA","sourcesContent":["/**\n * api/bootstrapApi.ts — fetch the runtime ids needed to create a cart.\n *\n * customerUid — looked up by the user's email from their viax party record.\n * salableUid — the product uid from getProduct(maId), used as biiSalable.\n * billingPlanUid — selected from filterCustomerBillingPlan by the caller.\n */\nimport { GET_CUSTOMER, GET_BILLING_PLANS, GET_PRODUCT, GET_ACTIVE_SUBSCRIPTIONS_STATE, GET_IS_CURRENT_USER_SUBSCRIBER, GET_COURSE_DETAIL } from './queries';\nimport type { GqlRunner, BillingPlansResult, BillingPlan, TwouCustomer, ProductInfo, CurrentUserSubscriberResult, CourseDetail } from '../types';\nimport { getSiteConfig } from '@openedx/frontend-base';\n\n/** Resolve the viax customer uid for the authenticated user by email. */\nexport async function getCustomerUid(gql: GqlRunner, userEmail: string): Promise<string> {\n const filter = `parParty.paContacts.email = '${userEmail}'`;\n const data = await gql(GET_CUSTOMER, { filter });\n const node: TwouCustomer | undefined = data?.filterTwouCustomer?.edges?.[0]?.node;\n if (!node?.uid) throw new Error(`No viax customer found for ${userEmail}. Ensure your account is linked.`);\n return node.uid;\n}\n\n/**\n * Fetch the TwouSubscriptionPlan uid to use as biiSalable in createTwouCart.\n * The maId is the product market-area identifier, e.g. \"TWOU-SUB-PLAN-001\".\n * The returned `uid` is passed as biiSalable when creating the cart.\n */\nexport async function getSalableUid(gql: GqlRunner, maId: string): Promise<ProductInfo> {\n const data = await gql(GET_PRODUCT, { maId });\n const product = data?.getProduct;\n if (!product?.uid) throw new Error(`Product \"${maId}\" not found.`);\n return {\n uid: product.uid,\n maName: product.maName ?? '',\n ownershipModels: (product.saOwnershipModels ?? []).map(\n (m: { uid: string, omName: string }) => ({ uid: m.uid, omName: m.omName }),\n ),\n };\n}\n\n/** Fetch all available customer billing plans. */\nexport async function getBillingPlans(gql: GqlRunner): Promise<BillingPlansResult> {\n const data = await gql(GET_BILLING_PLANS);\n const edges: {\n node: {\n uid: string,\n tStripePriceId: string,\n tStripeAmount?: { amount: string, units?: { code: string } },\n bipIsTemplate: boolean,\n bipOwnershipModel?: { uid: string, omName: string },\n },\n }[] = data?.filterCustomerBillingPlan?.edges ?? [];\n\n if (!edges.length) throw new Error('No billing plans found.');\n\n const plans: BillingPlan[] = edges.map(({ node }) => ({\n uid: node.uid,\n stripePriceId: node.tStripePriceId,\n amount: node.tStripeAmount?.amount ?? '0',\n currency: node.tStripeAmount?.units?.code ?? 'USD',\n isTemplate: node.bipIsTemplate,\n ownershipModel: {\n uid: node.bipOwnershipModel?.uid ?? '',\n omName: node.bipOwnershipModel?.omName ?? '',\n },\n }));\n\n return { plans };\n}\n\n/** Check whether the currently authenticated user already has an active subscription. */\nexport async function isCurrentUserSubscriber(gql: GqlRunner): Promise<CurrentUserSubscriberResult | null> {\n const data = await gql(GET_IS_CURRENT_USER_SUBSCRIBER);\n const result = data?.tIsCurrentUserSubscriber;\n if (!result) return null;\n return {\n isSubscriber: result.isSubscriber === true,\n subscriptionBiId: result.subscriptionBiId ?? null,\n status: result.status ?? null,\n subscriptionStartDate: result.subscriptionStartDate ?? null,\n currentPeriodEnd: result.currentPeriodEnd ?? null,\n };\n}\n\n/** Fetch the number of active subscriptions. */\nexport async function getActiveSubscriptions(gql: GqlRunner): Promise<{ active: number }> {\n const data = await gql(GET_ACTIVE_SUBSCRIPTIONS_STATE);\n const active = data?.tActiveSubscriptionsState?.active;\n if (typeof active !== 'number') {\n throw new Error('Unexpected response shape for tActiveSubscriptionsState');\n }\n return { active };\n}\n\n/** Fetch course details for the enrollment card by tCourseKey.\n * Sends the course key to Viax exactly as provided (e.g. HarvardX+CS50+X). */\nexport async function getCourseDetail(gql: GqlRunner, courseKey: string): Promise<CourseDetail | null> {\n const key = courseKey.trim();\n if (!key) return null;\n\n const siteConfig = getSiteConfig();\n const LMS_BASE_URL = siteConfig.lmsBaseUrl;\n\n // 1. Fetch resolved key from Partner REST API\n let resolvedCourseKey = key;\n try {\n const url = new URL(`${LMS_BASE_URL.replace(/\\/$/, '')}/api/subscription/course-runs`);\n url.searchParams.set('key', key);\n\n const res = await fetch(url.toString(), {\n method: 'GET',\n headers: { Accept: 'application/json' },\n });\n\n if (res.ok) {\n const payload = await res.json();\n // Extract key from elements array as per Partner API response format\n const partnerApiKey = payload?.elements?.[0]?.key;\n if (partnerApiKey) {\n resolvedCourseKey = partnerApiKey;\n }\n }\n } catch (error) {\n // If Partner API fails, fallback gracefully to the original key\n console.error('Failed to fetch course run key from Partner API:', error);\n }\n\n // Escape single quotes for the filter string only — do not alter `+` or other chars.\n const filter = `tCourseKey = '${resolvedCourseKey.replace(/'/g, \"''\")}'`;\n const data = await gql(GET_COURSE_DETAIL, { filter });\n const node = data?.filterTwouCourse?.edges?.[0]?.node;\n if (!node?.maId) return null;\n\n return {\n maId: node.maId,\n maName: node.maName ?? '',\n maDescription: node.maDescription ?? null,\n tShortDescription: node.tShortDescription ?? null,\n imageUrl: node.imageUrl ?? null,\n tCourseKey: node.tCourseKey ?? null,\n tBrandText: node.tBrandText ?? null,\n tPrimarySubjectArea: node.tPrimarySubjectArea ?? null,\n tLevelType: node.tLevelType ?? null,\n tLanguage: node.tLanguage ?? null,\n tPacingType: node.tPacingType?.code ?? null,\n tDurationLow: node.tDurationLow ?? null,\n tDurationHigh: node.tDurationHigh ?? null,\n tDurationUnit: node.tDurationUnit?.code ?? null,\n tEffortLow: node.tEffortLow ?? null,\n tEffortHigh: node.tEffortHigh ?? null,\n tEffortUnit: node.tEffortUnit?.code ?? null,\n tUrlCourse: node.tUrlCourse ?? null,\n tB2cSubscriptionInclusion: node.tB2cSubscriptionInclusion ?? null,\n tHasOfacRestrictions: node.tHasOfacRestrictions ?? null,\n tCatalogActive: node.tCatalogActive ?? null,\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"bootstrapApi.js","sourceRoot":"","sources":["../../src/utils/bootstrapApi.ts"],"names":[],"mappings":";;;;;;;;;AAAA;;;;;;GAMG;AACH,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,WAAW,EAAE,8BAA8B,EAAE,8BAA8B,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AAE5J,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAEvD,SAAS,kBAAkB,CAAC,GAAW;IACrC,OAAO,kBAAkB,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;AACxC,CAAC;AAED,yEAAyE;AACzE,MAAM,UAAgB,cAAc,CAAC,GAAc,EAAE,SAAiB;;;QACpE,MAAM,MAAM,GAAG,gCAAgC,SAAS,GAAG,CAAC;QAC5D,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,YAAY,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC;QACjD,MAAM,IAAI,GAA6B,MAAA,MAAA,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,kBAAkB,0CAAE,KAAK,0CAAG,CAAC,CAAC,0CAAE,IAAI,CAAC;QAClF,IAAI,CAAC,CAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,GAAG,CAAA;YAAE,MAAM,IAAI,KAAK,CAAC,8BAA8B,SAAS,kCAAkC,CAAC,CAAC;QAC3G,OAAO,IAAI,CAAC,GAAG,CAAC;IAClB,CAAC;CAAA;AAED;;;;GAIG;AACH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AI,CAAC,YAAY,mCAAI,IAAI;YACvC,aAAa,EAAE,MAAA,IAAI,CAAC,aAAa,mCAAI,IAAI;YACzC,aAAa,EAAE,MAAA,MAAA,IAAI,CAAC,aAAa,0CAAE,IAAI,mCAAI,IAAI;YAC/C,UAAU,EAAE,MAAA,IAAI,CAAC,UAAU,mCAAI,IAAI;YACnC,WAAW,EAAE,MAAA,IAAI,CAAC,WAAW,mCAAI,IAAI;YACrC,WAAW,EAAE,MAAA,MAAA,IAAI,CAAC,WAAW,0CAAE,IAAI,mCAAI,IAAI;YAC3C,UAAU,EAAE,MAAA,IAAI,CAAC,UAAU,mCAAI,IAAI;YACnC,yBAAyB,EAAE,MAAA,IAAI,CAAC,yBAAyB,mCAAI,IAAI;YACjE,oBAAoB,EAAE,MAAA,IAAI,CAAC,oBAAoB,mCAAI,IAAI;YACvD,cAAc,EAAE,MAAA,IAAI,CAAC,cAAc,mCAAI,IAAI;SAC5C,CAAC;IACJ,CAAC;CAAA","sourcesContent":["/**\n * api/bootstrapApi.ts — fetch the runtime ids needed to create a cart.\n *\n * customerUid — looked up by the user's email from their viax party record.\n * salableUid — the product uid from getProduct(maId), used as biiSalable.\n * billingPlanUid — selected from filterCustomerBillingPlan by the caller.\n */\nimport { GET_CUSTOMER, GET_BILLING_PLANS, GET_PRODUCT, GET_ACTIVE_SUBSCRIPTIONS_STATE, GET_IS_CURRENT_USER_SUBSCRIBER, GET_COURSE_DETAIL } from './queries';\nimport type { GqlRunner, BillingPlansResult, BillingPlan, TwouCustomer, ProductInfo, CurrentUserSubscriberResult, CourseDetail } from '../types';\nimport { getSiteConfig } from '@openedx/frontend-base';\n\nfunction encodeCourseRunKey(key: string): string {\n return encodeURIComponent(key.trim());\n}\n\n/** Resolve the viax customer uid for the authenticated user by email. */\nexport async function getCustomerUid(gql: GqlRunner, userEmail: string): Promise<string> {\n const filter = `parParty.paContacts.email = '${userEmail}'`;\n const data = await gql(GET_CUSTOMER, { filter });\n const node: TwouCustomer | undefined = data?.filterTwouCustomer?.edges?.[0]?.node;\n if (!node?.uid) throw new Error(`No viax customer found for ${userEmail}. Ensure your account is linked.`);\n return node.uid;\n}\n\n/**\n * Fetch the TwouSubscriptionPlan uid to use as biiSalable in createTwouCart.\n * The maId is the product market-area identifier, e.g. \"TWOU-SUB-PLAN-001\".\n * The returned `uid` is passed as biiSalable when creating the cart.\n */\nexport async function getSalableUid(gql: GqlRunner, maId: string): Promise<ProductInfo> {\n const data = await gql(GET_PRODUCT, { maId });\n const product = data?.getProduct;\n if (!product?.uid) throw new Error(`Product \"${maId}\" not found.`);\n return {\n uid: product.uid,\n maName: product.maName ?? '',\n ownershipModels: (product.saOwnershipModels ?? []).map(\n (m: { uid: string, omName: string }) => ({ uid: m.uid, omName: m.omName }),\n ),\n };\n}\n\n/** Fetch all available customer billing plans. */\nexport async function getBillingPlans(gql: GqlRunner): Promise<BillingPlansResult> {\n const data = await gql(GET_BILLING_PLANS);\n const edges: {\n node: {\n uid: string,\n tStripePriceId: string,\n tStripeAmount?: { amount: string, units?: { code: string } },\n bipIsTemplate: boolean,\n bipOwnershipModel?: { uid: string, omName: string },\n },\n }[] = data?.filterCustomerBillingPlan?.edges ?? [];\n\n if (!edges.length) throw new Error('No billing plans found.');\n\n const plans: BillingPlan[] = edges.map(({ node }) => ({\n uid: node.uid,\n stripePriceId: node.tStripePriceId,\n amount: node.tStripeAmount?.amount ?? '0',\n currency: node.tStripeAmount?.units?.code ?? 'USD',\n isTemplate: node.bipIsTemplate,\n ownershipModel: {\n uid: node.bipOwnershipModel?.uid ?? '',\n omName: node.bipOwnershipModel?.omName ?? '',\n },\n }));\n\n return { plans };\n}\n\n/** Check whether the currently authenticated user already has an active subscription. */\nexport async function isCurrentUserSubscriber(gql: GqlRunner): Promise<CurrentUserSubscriberResult | null> {\n const data = await gql(GET_IS_CURRENT_USER_SUBSCRIBER);\n const result = data?.tIsCurrentUserSubscriber;\n if (!result) return null;\n return {\n isSubscriber: result.isSubscriber === true,\n subscriptionBiId: result.subscriptionBiId ?? null,\n status: result.status ?? null,\n subscriptionStartDate: result.subscriptionStartDate ?? null,\n currentPeriodEnd: result.currentPeriodEnd ?? null,\n };\n}\n\n/** Fetch the number of active subscriptions. */\nexport async function getActiveSubscriptions(gql: GqlRunner): Promise<{ active: number }> {\n const data = await gql(GET_ACTIVE_SUBSCRIPTIONS_STATE);\n const active = data?.tActiveSubscriptionsState?.active;\n if (typeof active !== 'number') {\n throw new Error('Unexpected response shape for tActiveSubscriptionsState');\n }\n return { active };\n}\n\n/** Fetch course details for the enrollment card by tCourseKey.\n * Sends the course key to Viax exactly as provided (e.g. HarvardX+CS50+X). */\nexport async function getCourseDetail(gql: GqlRunner, courseKey: string): Promise<CourseDetail | null> {\n const key = courseKey.trim();\n if (!key) return null;\n\n const encodedKey = encodeCourseRunKey(key);\n const siteConfig = getSiteConfig();\n const LMS_BASE_URL = siteConfig.lmsBaseUrl;\n\n // 1. Fetch resolved key from Partner REST API\n let resolvedCourseKey = key;\n try {\n const url = new URL(`${LMS_BASE_URL.replace(/\\/$/, '')}/api/subscription/course-runs`);\n url.search = `key=${encodedKey}`;\n\n const res = await fetch(url.toString(), {\n method: 'GET',\n headers: { Accept: 'application/json' },\n });\n\n if (res.ok) {\n const payload = await res.json();\n // Extract key from elements array as per Partner API response format\n const partnerCourseKey = payload?.elements?.[0]?.course;\n if (partnerCourseKey) {\n resolvedCourseKey = partnerCourseKey;\n }\n }\n } catch (error) {\n // If Partner API fails, fallback gracefully to the original key\n console.error('Failed to fetch course run key from Partner API:', error);\n }\n\n // Escape single quotes for the filter string only — do not alter `+` or other chars.\n const filter = `tCourseKey = '${resolvedCourseKey.replace(/'/g, \"''\")}'`;\n const data = await gql(GET_COURSE_DETAIL, { filter });\n const node = data?.filterTwouCourse?.edges?.[0]?.node;\n if (!node?.maId) return null;\n\n return {\n maId: node.maId,\n maName: node.maName ?? '',\n maDescription: node.maDescription ?? null,\n tShortDescription: node.tShortDescription ?? null,\n imageUrl: node.imageUrl ?? null,\n tCourseKey: node.tCourseKey ?? null,\n tBrandText: node.tBrandText ?? null,\n tPrimarySubjectArea: node.tPrimarySubjectArea ?? null,\n tLevelType: node.tLevelType ?? null,\n tLanguage: node.tLanguage ?? null,\n tPacingType: node.tPacingType?.code ?? null,\n tDurationLow: node.tDurationLow ?? null,\n tDurationHigh: node.tDurationHigh ?? null,\n tDurationUnit: node.tDurationUnit?.code ?? null,\n tEffortLow: node.tEffortLow ?? null,\n tEffortHigh: node.tEffortHigh ?? null,\n tEffortUnit: node.tEffortUnit?.code ?? null,\n tUrlCourse: node.tUrlCourse ?? null,\n tB2cSubscriptionInclusion: node.tB2cSubscriptionInclusion ?? null,\n tHasOfacRestrictions: node.tHasOfacRestrictions ?? null,\n tCatalogActive: node.tCatalogActive ?? null,\n };\n}\n"]}
|