@flopay/shared 1.1.0 → 1.1.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/README.md +5 -4
- package/dist/index.cjs +83 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +141 -71
- package/dist/index.d.ts +141 -71
- package/dist/index.mjs +81 -11
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -63,7 +63,7 @@ function getFloPayEnvironment() {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
// src/constants.ts
|
|
66
|
-
var SDK_VERSION = "1.1.
|
|
66
|
+
var SDK_VERSION = "1.1.4";
|
|
67
67
|
var BILLING_API_URL_STAGING = "https://api.stage.flopay.com";
|
|
68
68
|
var BILLING_API_URL_PRODUCTION = "https://api.flopay.com";
|
|
69
69
|
var DEFAULT_API_BASE_URL = BILLING_API_URL_STAGING;
|
|
@@ -722,7 +722,7 @@ function nonBlank(value) {
|
|
|
722
722
|
const trimmed = value.trim();
|
|
723
723
|
return trimmed.length > 0 ? trimmed : void 0;
|
|
724
724
|
}
|
|
725
|
-
function resolveSessionCurrency(sessionCurrency, items, subscriptions) {
|
|
725
|
+
function resolveSessionCurrency(sessionCurrency, items, subscriptions, products) {
|
|
726
726
|
const session = nonBlank(sessionCurrency);
|
|
727
727
|
if (session) return session;
|
|
728
728
|
for (const item of items ?? []) {
|
|
@@ -733,7 +733,67 @@ function resolveSessionCurrency(sessionCurrency, items, subscriptions) {
|
|
|
733
733
|
const c = nonBlank(sub.currency);
|
|
734
734
|
if (c) return c;
|
|
735
735
|
}
|
|
736
|
-
|
|
736
|
+
for (const product of products ?? []) {
|
|
737
|
+
const c = nonBlank(product.currency);
|
|
738
|
+
if (c) return c;
|
|
739
|
+
}
|
|
740
|
+
return null;
|
|
741
|
+
}
|
|
742
|
+
function foldIntoProducts(items, subscriptions) {
|
|
743
|
+
const products = [];
|
|
744
|
+
for (const sub of subscriptions ?? []) {
|
|
745
|
+
products.push({
|
|
746
|
+
type: "subscription",
|
|
747
|
+
code: nonBlank(sub.code) ?? nonBlank(sub.providerPlanId),
|
|
748
|
+
providerPlanId: sub.providerPlanId,
|
|
749
|
+
name: nonBlank(sub.subscriptionName) ?? nonBlank(sub.providerPlanName) ?? null,
|
|
750
|
+
subscriptionName: sub.subscriptionName ?? null,
|
|
751
|
+
providerPlanName: sub.providerPlanName ?? null,
|
|
752
|
+
quantity: sub.quantity,
|
|
753
|
+
totalAmount: sub.totalAmount,
|
|
754
|
+
overrideAmount: sub.overrideAmount,
|
|
755
|
+
currency: sub.currency,
|
|
756
|
+
metadata: sub.metadata
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
for (const item of items ?? []) {
|
|
760
|
+
products.push({
|
|
761
|
+
type: "item",
|
|
762
|
+
code: nonBlank(item.code) ?? nonBlank(item.providerItemId),
|
|
763
|
+
providerItemId: item.providerItemId,
|
|
764
|
+
name: nonBlank(item.itemName) ?? nonBlank(item.providerItemName) ?? null,
|
|
765
|
+
itemName: item.itemName ?? null,
|
|
766
|
+
providerItemName: item.providerItemName ?? null,
|
|
767
|
+
quantity: item.quantity,
|
|
768
|
+
totalAmount: item.totalAmount,
|
|
769
|
+
overrideAmount: item.overrideAmount,
|
|
770
|
+
currency: item.currency,
|
|
771
|
+
metadata: item.metadata
|
|
772
|
+
});
|
|
773
|
+
}
|
|
774
|
+
return products;
|
|
775
|
+
}
|
|
776
|
+
function buildProductPayload(product, sessionCurrency) {
|
|
777
|
+
const code = nonBlank(product.code) ?? nonBlank(product.providerItemId) ?? nonBlank(product.providerPlanId);
|
|
778
|
+
if (!code) {
|
|
779
|
+
throw new Error(
|
|
780
|
+
"CheckoutProduct requires `code` (or the deprecated `providerItemId` / `providerPlanId`)."
|
|
781
|
+
);
|
|
782
|
+
}
|
|
783
|
+
const name = nonBlank(product.name) ?? nonBlank(product.itemName) ?? nonBlank(product.providerItemName) ?? nonBlank(product.subscriptionName) ?? nonBlank(product.providerPlanName) ?? null;
|
|
784
|
+
const payload = {
|
|
785
|
+
type: product.type,
|
|
786
|
+
code,
|
|
787
|
+
name,
|
|
788
|
+
quantity: product.quantity ?? 1,
|
|
789
|
+
totalAmount: product.totalAmount,
|
|
790
|
+
overrideAmount: product.overrideAmount ?? null,
|
|
791
|
+
currency: nonBlank(product.currency) ?? sessionCurrency
|
|
792
|
+
};
|
|
793
|
+
if (product.metadata) {
|
|
794
|
+
payload["metadata"] = product.metadata;
|
|
795
|
+
}
|
|
796
|
+
return payload;
|
|
737
797
|
}
|
|
738
798
|
function buildItemPayload(item, sessionCurrency) {
|
|
739
799
|
const code = nonBlank(item.code) ?? nonBlank(item.providerItemId);
|
|
@@ -783,13 +843,15 @@ function buildSubscriptionPayload(subscription, sessionCurrency) {
|
|
|
783
843
|
// src/display.ts
|
|
784
844
|
function buildCheckoutDisplayData(session, options = {}) {
|
|
785
845
|
const itemsList = [];
|
|
786
|
-
const
|
|
787
|
-
const
|
|
788
|
-
const
|
|
846
|
+
const products = session.products ?? [];
|
|
847
|
+
const subscriptions = products.filter((p) => p.type === "subscription");
|
|
848
|
+
const items = products.filter((p) => p.type === "item");
|
|
849
|
+
const resolvedCurrency = resolveSessionCurrency(session.currency, void 0, void 0, products);
|
|
850
|
+
const currency = (resolvedCurrency ?? "USD").toUpperCase();
|
|
789
851
|
for (const sub of subscriptions) {
|
|
790
852
|
const originalPrice = sub.totalAmount ?? 0;
|
|
791
853
|
const discountedPrice = sub.overrideAmount ?? originalPrice;
|
|
792
|
-
let name = sub.
|
|
854
|
+
let name = sub.name || sub.code || "Subscription";
|
|
793
855
|
if (name === "4-WEEK PLAN" && discountedPrice <= 1) {
|
|
794
856
|
name = "7-DAY TRIAL: FULL ACCESS";
|
|
795
857
|
}
|
|
@@ -806,7 +868,7 @@ function buildCheckoutDisplayData(session, options = {}) {
|
|
|
806
868
|
const originalPrice = item.totalAmount ?? 0;
|
|
807
869
|
const discountedPrice = item.overrideAmount ?? originalPrice;
|
|
808
870
|
itemsList.push({
|
|
809
|
-
name: item.
|
|
871
|
+
name: item.name || item.code || "Item",
|
|
810
872
|
quantity: item.quantity,
|
|
811
873
|
price: discountedPrice,
|
|
812
874
|
originalPrice
|
|
@@ -821,8 +883,13 @@ function buildCheckoutDisplayData(session, options = {}) {
|
|
|
821
883
|
originalPrice: session.amount / 100
|
|
822
884
|
});
|
|
823
885
|
}
|
|
824
|
-
const
|
|
825
|
-
const
|
|
886
|
+
const lineOriginalTotal = itemsList.reduce((sum, i) => sum + i.originalPrice * i.quantity, 0);
|
|
887
|
+
const lineTotal = itemsList.reduce((sum, i) => sum + i.price * i.quantity, 0);
|
|
888
|
+
const hasBackendTotals = typeof session.totalAmount === "number" && Number.isFinite(session.totalAmount);
|
|
889
|
+
const hasBackendSubtotal = typeof session.subtotalAmount === "number" && Number.isFinite(session.subtotalAmount);
|
|
890
|
+
const hasBackendDiscount = typeof session.discountAmount === "number" && Number.isFinite(session.discountAmount);
|
|
891
|
+
const total = hasBackendTotals ? session.totalAmount : lineTotal;
|
|
892
|
+
const originalTotal = hasBackendSubtotal ? session.subtotalAmount : lineOriginalTotal;
|
|
826
893
|
const totalSave = Math.max(0, originalTotal - total);
|
|
827
894
|
const discountPercent = originalTotal > 0 ? Math.round(totalSave / originalTotal * 100) : 0;
|
|
828
895
|
return {
|
|
@@ -831,7 +898,8 @@ function buildCheckoutDisplayData(session, options = {}) {
|
|
|
831
898
|
total,
|
|
832
899
|
originalTotal,
|
|
833
900
|
totalSave,
|
|
834
|
-
discountPercent
|
|
901
|
+
discountPercent,
|
|
902
|
+
...hasBackendDiscount ? { couponDiscount: session.discountAmount } : {}
|
|
835
903
|
};
|
|
836
904
|
}
|
|
837
905
|
|
|
@@ -871,8 +939,10 @@ export {
|
|
|
871
939
|
authenticationError,
|
|
872
940
|
buildCheckoutDisplayData,
|
|
873
941
|
buildItemPayload,
|
|
942
|
+
buildProductPayload,
|
|
874
943
|
buildSubscriptionPayload,
|
|
875
944
|
configureFlopay,
|
|
945
|
+
foldIntoProducts,
|
|
876
946
|
getConfiguredBillingApiUrl,
|
|
877
947
|
getCountryByCode,
|
|
878
948
|
getCurrencyByCountry,
|