Skip to content

Commit 08bef4e

Browse files
committed
fix: add transactionId and improve type safety in conversion
1 parent b7b9d11 commit 08bef4e

File tree

2 files changed

+134
-139
lines changed

2 files changed

+134
-139
lines changed

example/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ PODS:
88
- hermes-engine (0.81.1):
99
- hermes-engine/Pre-built (= 0.81.1)
1010
- hermes-engine/Pre-built (0.81.1)
11-
- NitroIap (14.4.28):
11+
- NitroIap (14.4.31):
1212
- boost
1313
- DoubleConversion
1414
- fast_float
@@ -2747,7 +2747,7 @@ SPEC CHECKSUMS:
27472747
fmt: a40bb5bd0294ea969aaaba240a927bd33d878cdd
27482748
glog: 5683914934d5b6e4240e497e0f4a3b42d1854183
27492749
hermes-engine: 4f8246b1f6d79f625e0d99472d1f3a71da4d28ca
2750-
NitroIap: c47254900c48b2350b06585aeb2da3b0b18c212b
2750+
NitroIap: 0749b6c017852a8d302e34493151b5f907a4363f
27512751
NitroModules: 0ba3a58906a86566ea83abc016f8692374c19761
27522752
openiap: f2d584b18d051b063bd26ec2feb77b8c8619e9ef
27532753
RCT-Folly: 846fda9475e61ec7bcbf8a3fe81edfcaeb090669

src/utils/type-bridge.ts

Lines changed: 132 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import type {
2222
Product,
2323
ProductSubscription,
2424
Purchase,
25+
PurchaseAndroid,
26+
PurchaseIOS,
2527
SubscriptionStatusIOS,
2628
} from '../types';
2729
import {RnIapConsole} from './debug';
@@ -317,153 +319,146 @@ export function convertNitroPurchaseToPurchase(
317319
nitroPurchase: NitroPurchase,
318320
): Purchase {
319321
const platform = normalizePlatform(nitroPurchase.platform);
320-
const purchase: any = {
321-
id: nitroPurchase.id,
322-
productId: nitroPurchase.productId,
323-
transactionDate: nitroPurchase.transactionDate ?? Date.now(),
324-
purchaseToken:
325-
nitroPurchase.purchaseToken ?? nitroPurchase.purchaseTokenAndroid ?? null,
326-
platform,
327-
quantity: nitroPurchase.quantity ?? 1,
328-
purchaseState: normalizePurchaseState(
329-
nitroPurchase.purchaseState ?? nitroPurchase.purchaseStateAndroid,
330-
),
331-
isAutoRenewing: Boolean(nitroPurchase.isAutoRenewing),
332-
};
333322

323+
let purchaseState = normalizePurchaseState(
324+
nitroPurchase.purchaseState ?? nitroPurchase.purchaseStateAndroid,
325+
);
326+
327+
// Fallback for unknown purchase state
334328
if (
335-
purchase.purchaseState === PURCHASE_STATE_UNKNOWN &&
329+
purchaseState === PURCHASE_STATE_UNKNOWN &&
336330
nitroPurchase.purchaseStateAndroid != null
337331
) {
338-
purchase.purchaseState = normalizePurchaseState(
339-
nitroPurchase.purchaseStateAndroid,
340-
);
332+
purchaseState = normalizePurchaseState(nitroPurchase.purchaseStateAndroid);
341333
}
342334

343335
if (platform === PLATFORM_IOS) {
344-
const iosPurchase: any = purchase;
345-
iosPurchase.quantityIOS = toNullableNumber(nitroPurchase.quantityIOS);
346-
iosPurchase.originalTransactionDateIOS = toNullableNumber(
347-
nitroPurchase.originalTransactionDateIOS,
348-
);
349-
iosPurchase.originalTransactionIdentifierIOS = toNullableString(
350-
nitroPurchase.originalTransactionIdentifierIOS,
351-
);
352-
iosPurchase.appAccountToken = toNullableString(
353-
nitroPurchase.appAccountToken,
354-
);
355-
iosPurchase.appBundleIdIOS = toNullableString(nitroPurchase.appBundleIdIOS);
356-
iosPurchase.countryCodeIOS = toNullableString(nitroPurchase.countryCodeIOS);
357-
iosPurchase.currencyCodeIOS = toNullableString(
358-
nitroPurchase.currencyCodeIOS,
359-
);
360-
iosPurchase.currencySymbolIOS = toNullableString(
361-
nitroPurchase.currencySymbolIOS,
362-
);
363-
iosPurchase.environmentIOS = toNullableString(nitroPurchase.environmentIOS);
364-
iosPurchase.expirationDateIOS = toNullableNumber(
365-
nitroPurchase.expirationDateIOS,
366-
);
367-
iosPurchase.isUpgradedIOS = toNullableBoolean(nitroPurchase.isUpgradedIOS);
368-
// Parse offerIOS from JSON string if present
369-
if (nitroPurchase.offerIOS) {
370-
try {
371-
iosPurchase.offerIOS = JSON.parse(nitroPurchase.offerIOS);
372-
} catch {
373-
iosPurchase.offerIOS = null;
374-
}
375-
} else {
376-
iosPurchase.offerIOS = null;
377-
}
378-
iosPurchase.ownershipTypeIOS = toNullableString(
379-
nitroPurchase.ownershipTypeIOS,
380-
);
381-
iosPurchase.reasonIOS = toNullableString(nitroPurchase.reasonIOS);
382-
iosPurchase.reasonStringRepresentationIOS = toNullableString(
383-
nitroPurchase.reasonStringRepresentationIOS,
384-
);
385-
iosPurchase.revocationDateIOS = toNullableNumber(
386-
nitroPurchase.revocationDateIOS,
387-
);
388-
iosPurchase.revocationReasonIOS = toNullableString(
389-
nitroPurchase.revocationReasonIOS,
390-
);
391-
iosPurchase.storefrontCountryCodeIOS = toNullableString(
392-
nitroPurchase.storefrontCountryCodeIOS,
393-
);
394-
iosPurchase.subscriptionGroupIdIOS = toNullableString(
395-
nitroPurchase.subscriptionGroupIdIOS,
396-
);
397-
iosPurchase.transactionReasonIOS = toNullableString(
398-
nitroPurchase.transactionReasonIOS,
399-
);
400-
iosPurchase.webOrderLineItemIdIOS = toNullableString(
401-
nitroPurchase.webOrderLineItemIdIOS,
402-
);
403-
// Convert renewalInfoIOS from Nitro type to JS type
404-
if (nitroPurchase.renewalInfoIOS) {
405-
iosPurchase.renewalInfoIOS = {
406-
autoRenewPreference: toNullableString(
407-
nitroPurchase.renewalInfoIOS.autoRenewPreference,
408-
),
409-
expirationReason: toNullableString(
410-
nitroPurchase.renewalInfoIOS.expirationReason,
411-
),
412-
gracePeriodExpirationDate: toNullableNumber(
413-
nitroPurchase.renewalInfoIOS.gracePeriodExpirationDate,
414-
),
415-
isInBillingRetry: toNullableBoolean(
416-
nitroPurchase.renewalInfoIOS.isInBillingRetry,
417-
),
418-
jsonRepresentation: toNullableString(
419-
nitroPurchase.renewalInfoIOS.jsonRepresentation,
420-
),
421-
pendingUpgradeProductId: toNullableString(
422-
nitroPurchase.renewalInfoIOS.pendingUpgradeProductId,
423-
),
424-
priceIncreaseStatus: toNullableString(
425-
nitroPurchase.renewalInfoIOS.priceIncreaseStatus,
426-
),
427-
renewalDate: toNullableNumber(nitroPurchase.renewalInfoIOS.renewalDate),
428-
renewalOfferId: toNullableString(
429-
nitroPurchase.renewalInfoIOS.renewalOfferId,
430-
),
431-
renewalOfferType: toNullableString(
432-
nitroPurchase.renewalInfoIOS.renewalOfferType,
433-
),
434-
willAutoRenew: nitroPurchase.renewalInfoIOS.willAutoRenew ?? false,
435-
};
436-
} else {
437-
iosPurchase.renewalInfoIOS = null;
438-
}
439-
return iosPurchase as Purchase;
336+
const iosPurchase: PurchaseIOS = {
337+
id: nitroPurchase.id,
338+
productId: nitroPurchase.productId,
339+
transactionDate: nitroPurchase.transactionDate ?? Date.now(),
340+
purchaseToken: nitroPurchase.purchaseToken ?? null,
341+
platform,
342+
quantity: nitroPurchase.quantity ?? 1,
343+
purchaseState,
344+
isAutoRenewing: Boolean(nitroPurchase.isAutoRenewing),
345+
// PurchaseIOS requires both id and transactionId (they are the same value)
346+
transactionId: nitroPurchase.id,
347+
quantityIOS: toNullableNumber(nitroPurchase.quantityIOS),
348+
originalTransactionDateIOS: toNullableNumber(
349+
nitroPurchase.originalTransactionDateIOS,
350+
),
351+
originalTransactionIdentifierIOS: toNullableString(
352+
nitroPurchase.originalTransactionIdentifierIOS,
353+
),
354+
appAccountToken: toNullableString(nitroPurchase.appAccountToken),
355+
appBundleIdIOS: toNullableString(nitroPurchase.appBundleIdIOS),
356+
countryCodeIOS: toNullableString(nitroPurchase.countryCodeIOS),
357+
currencyCodeIOS: toNullableString(nitroPurchase.currencyCodeIOS),
358+
currencySymbolIOS: toNullableString(nitroPurchase.currencySymbolIOS),
359+
environmentIOS: toNullableString(nitroPurchase.environmentIOS),
360+
expirationDateIOS: toNullableNumber(nitroPurchase.expirationDateIOS),
361+
isUpgradedIOS: toNullableBoolean(nitroPurchase.isUpgradedIOS),
362+
offerIOS: nitroPurchase.offerIOS
363+
? (() => {
364+
try {
365+
return JSON.parse(nitroPurchase.offerIOS!);
366+
} catch {
367+
return null;
368+
}
369+
})()
370+
: null,
371+
ownershipTypeIOS: toNullableString(nitroPurchase.ownershipTypeIOS),
372+
reasonIOS: toNullableString(nitroPurchase.reasonIOS),
373+
reasonStringRepresentationIOS: toNullableString(
374+
nitroPurchase.reasonStringRepresentationIOS,
375+
),
376+
revocationDateIOS: toNullableNumber(nitroPurchase.revocationDateIOS),
377+
revocationReasonIOS: toNullableString(nitroPurchase.revocationReasonIOS),
378+
storefrontCountryCodeIOS: toNullableString(
379+
nitroPurchase.storefrontCountryCodeIOS,
380+
),
381+
subscriptionGroupIdIOS: toNullableString(
382+
nitroPurchase.subscriptionGroupIdIOS,
383+
),
384+
transactionReasonIOS: toNullableString(
385+
nitroPurchase.transactionReasonIOS,
386+
),
387+
webOrderLineItemIdIOS: toNullableString(
388+
nitroPurchase.webOrderLineItemIdIOS,
389+
),
390+
renewalInfoIOS: nitroPurchase.renewalInfoIOS
391+
? {
392+
autoRenewPreference: toNullableString(
393+
nitroPurchase.renewalInfoIOS.autoRenewPreference,
394+
),
395+
expirationReason: toNullableString(
396+
nitroPurchase.renewalInfoIOS.expirationReason,
397+
),
398+
gracePeriodExpirationDate: toNullableNumber(
399+
nitroPurchase.renewalInfoIOS.gracePeriodExpirationDate,
400+
),
401+
isInBillingRetry: toNullableBoolean(
402+
nitroPurchase.renewalInfoIOS.isInBillingRetry,
403+
),
404+
jsonRepresentation: toNullableString(
405+
nitroPurchase.renewalInfoIOS.jsonRepresentation,
406+
),
407+
pendingUpgradeProductId: toNullableString(
408+
nitroPurchase.renewalInfoIOS.pendingUpgradeProductId,
409+
),
410+
priceIncreaseStatus: toNullableString(
411+
nitroPurchase.renewalInfoIOS.priceIncreaseStatus,
412+
),
413+
renewalDate: toNullableNumber(
414+
nitroPurchase.renewalInfoIOS.renewalDate,
415+
),
416+
renewalOfferId: toNullableString(
417+
nitroPurchase.renewalInfoIOS.renewalOfferId,
418+
),
419+
renewalOfferType: toNullableString(
420+
nitroPurchase.renewalInfoIOS.renewalOfferType,
421+
),
422+
willAutoRenew: nitroPurchase.renewalInfoIOS.willAutoRenew ?? false,
423+
}
424+
: null,
425+
};
426+
return iosPurchase;
440427
}
441428

442-
const androidPurchase: any = purchase;
443-
androidPurchase.autoRenewingAndroid = toNullableBoolean(
444-
nitroPurchase.autoRenewingAndroid ?? nitroPurchase.isAutoRenewing,
445-
);
446-
androidPurchase.dataAndroid = toNullableString(nitroPurchase.dataAndroid);
447-
androidPurchase.signatureAndroid = toNullableString(
448-
nitroPurchase.signatureAndroid,
449-
);
450-
androidPurchase.isAcknowledgedAndroid = toNullableBoolean(
451-
nitroPurchase.isAcknowledgedAndroid,
452-
);
453-
androidPurchase.packageNameAndroid = toNullableString(
454-
nitroPurchase.packageNameAndroid,
455-
);
456-
androidPurchase.obfuscatedAccountIdAndroid = toNullableString(
457-
nitroPurchase.obfuscatedAccountIdAndroid,
458-
);
459-
androidPurchase.obfuscatedProfileIdAndroid = toNullableString(
460-
nitroPurchase.obfuscatedProfileIdAndroid,
461-
);
462-
androidPurchase.developerPayloadAndroid = toNullableString(
463-
nitroPurchase.developerPayloadAndroid,
464-
);
429+
const androidPurchase: PurchaseAndroid = {
430+
id: nitroPurchase.id,
431+
productId: nitroPurchase.productId,
432+
transactionDate: nitroPurchase.transactionDate ?? Date.now(),
433+
purchaseToken:
434+
nitroPurchase.purchaseToken ?? nitroPurchase.purchaseTokenAndroid ?? null,
435+
platform,
436+
quantity: nitroPurchase.quantity ?? 1,
437+
purchaseState,
438+
isAutoRenewing: Boolean(nitroPurchase.isAutoRenewing),
439+
// PurchaseAndroid has optional transactionId (may differ from id/orderId)
440+
transactionId: toNullableString(nitroPurchase.id),
441+
autoRenewingAndroid: toNullableBoolean(
442+
nitroPurchase.autoRenewingAndroid ?? nitroPurchase.isAutoRenewing,
443+
),
444+
dataAndroid: toNullableString(nitroPurchase.dataAndroid),
445+
signatureAndroid: toNullableString(nitroPurchase.signatureAndroid),
446+
isAcknowledgedAndroid: toNullableBoolean(
447+
nitroPurchase.isAcknowledgedAndroid,
448+
),
449+
packageNameAndroid: toNullableString(nitroPurchase.packageNameAndroid),
450+
obfuscatedAccountIdAndroid: toNullableString(
451+
nitroPurchase.obfuscatedAccountIdAndroid,
452+
),
453+
obfuscatedProfileIdAndroid: toNullableString(
454+
nitroPurchase.obfuscatedProfileIdAndroid,
455+
),
456+
developerPayloadAndroid: toNullableString(
457+
nitroPurchase.developerPayloadAndroid,
458+
),
459+
};
465460

466-
return androidPurchase as Purchase;
461+
return androidPurchase;
467462
}
468463

469464
/**

0 commit comments

Comments
 (0)