@appfunnel-dev/sdk 2.0.0-canary.3 → 2.0.0-canary.4
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.
- package/dist/index.cjs +37 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +16 -1
- package/dist/index.d.ts +16 -1
- package/dist/index.js +37 -11
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1125,12 +1125,25 @@ var FALLBACK_DRIVER = createMockDriver();
|
|
|
1125
1125
|
function useCheckout(opts = {}) {
|
|
1126
1126
|
const driver = useDriver();
|
|
1127
1127
|
const tracker = useTrackerRef();
|
|
1128
|
+
const catalog = useCatalog();
|
|
1128
1129
|
const nav = useNavigation();
|
|
1129
1130
|
const [status, setStatus] = React.useState("idle");
|
|
1130
1131
|
const [error, setError] = React.useState(null);
|
|
1131
1132
|
const intent = opts.intent ?? "purchase";
|
|
1132
1133
|
const kind = opts.kind;
|
|
1133
1134
|
const advance = opts.advanceOnSuccess ?? true;
|
|
1135
|
+
const productFacts = React.useCallback(
|
|
1136
|
+
(product) => {
|
|
1137
|
+
const resolved = product ? catalog.get(product) : void 0;
|
|
1138
|
+
return {
|
|
1139
|
+
productId: resolved?.catalogKey ?? product,
|
|
1140
|
+
amount: resolved ? resolved.priceMinor / 100 : void 0,
|
|
1141
|
+
currency: resolved?.currency,
|
|
1142
|
+
isSubscription: resolved ? resolved.period !== "one-time" : kind === "subscription" || kind === "upgrade"
|
|
1143
|
+
};
|
|
1144
|
+
},
|
|
1145
|
+
[catalog, kind]
|
|
1146
|
+
);
|
|
1134
1147
|
const recovering = React.useRef(false);
|
|
1135
1148
|
const failRef = React.useRef(() => {
|
|
1136
1149
|
});
|
|
@@ -1138,18 +1151,24 @@ function useCheckout(opts = {}) {
|
|
|
1138
1151
|
async () => ({ ok: false })
|
|
1139
1152
|
);
|
|
1140
1153
|
const succeed = React.useCallback(
|
|
1141
|
-
(result) => {
|
|
1154
|
+
(result, product) => {
|
|
1142
1155
|
setStatus("success");
|
|
1156
|
+
const { productId, isSubscription } = productFacts(product);
|
|
1157
|
+
const amount = result.amountMinor != null ? result.amountMinor / 100 : void 0;
|
|
1143
1158
|
tracker.track("purchase.complete", {
|
|
1144
|
-
amount
|
|
1159
|
+
amount,
|
|
1145
1160
|
currency: result.currency,
|
|
1161
|
+
productId,
|
|
1146
1162
|
eventId: result.eventId
|
|
1147
1163
|
// server-minted (browser↔CAPI dedup); undefined if none
|
|
1148
1164
|
});
|
|
1165
|
+
if (isSubscription) {
|
|
1166
|
+
tracker.track("subscription.created", { amount, currency: result.currency, productId, eventId: result.eventId });
|
|
1167
|
+
}
|
|
1149
1168
|
opts.onSuccess?.(result);
|
|
1150
1169
|
if (advance) nav.next();
|
|
1151
1170
|
},
|
|
1152
|
-
[tracker, nav, advance, opts]
|
|
1171
|
+
[tracker, nav, advance, opts, productFacts]
|
|
1153
1172
|
);
|
|
1154
1173
|
const succeedRef = React.useRef(succeed);
|
|
1155
1174
|
succeedRef.current = succeed;
|
|
@@ -1157,11 +1176,18 @@ function useCheckout(opts = {}) {
|
|
|
1157
1176
|
(product, surface) => {
|
|
1158
1177
|
setStatus("loading");
|
|
1159
1178
|
setError(null);
|
|
1160
|
-
|
|
1179
|
+
const { productId, amount, currency } = productFacts(product);
|
|
1180
|
+
tracker.track("checkout.start", { productId, amount, currency, surface, eventId: newEventId() });
|
|
1161
1181
|
},
|
|
1162
|
-
[tracker]
|
|
1182
|
+
[tracker, productFacts]
|
|
1183
|
+
);
|
|
1184
|
+
const paymentAdded = React.useCallback(
|
|
1185
|
+
(product) => {
|
|
1186
|
+
const { productId, amount, currency } = productFacts(product);
|
|
1187
|
+
tracker.track("checkout.payment_added", { productId, amount, currency, eventId: newEventId() });
|
|
1188
|
+
},
|
|
1189
|
+
[tracker, productFacts]
|
|
1163
1190
|
);
|
|
1164
|
-
const paymentAdded = React.useCallback(() => tracker.track("checkout.payment_added", {}), [tracker]);
|
|
1165
1191
|
const hostInSheet = (surface) => surface === "sheet" || chunkLJYLGLFS_cjs.isInlineSurface(surface) && !!chunkLJYLGLFS_cjs.PROVIDER_PROFILES[driver.provider]?.surfaces.sheet;
|
|
1166
1192
|
const startRecovery = (product, surface) => {
|
|
1167
1193
|
recovering.current = true;
|
|
@@ -1172,9 +1198,9 @@ function useCheckout(opts = {}) {
|
|
|
1172
1198
|
surface: "sheet",
|
|
1173
1199
|
intent,
|
|
1174
1200
|
kind,
|
|
1175
|
-
onSuccess: (r) => succeedRef.current(r),
|
|
1201
|
+
onSuccess: (r) => succeedRef.current(r, product),
|
|
1176
1202
|
onError: (e) => failRef.current(e, product),
|
|
1177
|
-
onPaymentAdded: () => paymentAdded()
|
|
1203
|
+
onPaymentAdded: () => paymentAdded(product)
|
|
1178
1204
|
});
|
|
1179
1205
|
} else {
|
|
1180
1206
|
void attemptRef.current(product, surface);
|
|
@@ -1228,7 +1254,7 @@ function useCheckout(opts = {}) {
|
|
|
1228
1254
|
begin(product, surface);
|
|
1229
1255
|
try {
|
|
1230
1256
|
const result = await driver.start({ product, intent, surface, kind });
|
|
1231
|
-
if (result.ok) succeedRef.current(result);
|
|
1257
|
+
if (result.ok) succeedRef.current(result, product);
|
|
1232
1258
|
else failRef.current(result.error ?? chunkLJYLGLFS_cjs.checkoutError("unknown", "Checkout failed"), product);
|
|
1233
1259
|
return result;
|
|
1234
1260
|
} catch (e) {
|
|
@@ -1247,9 +1273,9 @@ function useCheckout(opts = {}) {
|
|
|
1247
1273
|
recovering.current = false;
|
|
1248
1274
|
begin(product, surface);
|
|
1249
1275
|
},
|
|
1250
|
-
onSuccess: (result) => succeedRef.current(result),
|
|
1276
|
+
onSuccess: (result) => succeedRef.current(result, product),
|
|
1251
1277
|
onError: (err) => failRef.current(err, product),
|
|
1252
|
-
onPaymentAdded: () => paymentAdded()
|
|
1278
|
+
onPaymentAdded: () => paymentAdded(product)
|
|
1253
1279
|
}),
|
|
1254
1280
|
[begin, paymentAdded]
|
|
1255
1281
|
);
|