Skip to content

Commit

Permalink
fix(ios): serialize discount offer
Browse files Browse the repository at this point in the history
  • Loading branch information
hyochan committed Jul 25, 2024
1 parent e5e62f7 commit 3cc4f9f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
16 changes: 12 additions & 4 deletions ios/ExpoIapModule.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import ExpoModulesCore
import StoreKit

func serializeDebug (_ s: String) -> String? {
#if DEBUG
return s
#else
return nil
#endif
}

@available(iOS 15.0, *)
func serializeProduct(_ p: Product) -> [String: Any?] {
return [
"debugDescription": p.debugDescription,
"debugDescription": serializeDebug(p.debugDescription),
"description": p.description,
"displayName": p.displayName,
"displayPrice": p.displayPrice,
"id": p.id,
"isFamilyShareable": p.isFamilyShareable,
"jsonRepresentation": p.jsonRepresentation,
"jsonRepresentation": serializeDebug(String(data: p.jsonRepresentation, encoding: .utf8) ?? ""),
"price": p.price,
"subscription": p.subscription,
"type": p.type,
Expand Down Expand Up @@ -205,7 +213,7 @@ public class ExpoIapModule: Module, EXEventEmitter {
return purchasedItems.map { serializeTransaction($0) }
}

AsyncFunction("buyProduct") { (sku: String, autoFinish: Bool, appAccountToken: String?, quantity: Int, offer: [String: String]) -> [String: Any?]? in
AsyncFunction("buyProduct") { (sku: String, autoFinish: Bool, appAccountToken: String?, quantity: Int, discountOffer: [String: String]?) -> [String: Any?]? in
guard let productStore = self.productStore else {
throw NSError(domain: "ExpoIapModule", code: 1, userInfo: [NSLocalizedDescriptionKey: "Connection not initialized"])
}
Expand All @@ -217,7 +225,7 @@ public class ExpoIapModule: Module, EXEventEmitter {
if quantity > -1 {
options.insert(.quantity(quantity))
}
if let offerID = offer["offerID"], let keyID = offer["keyID"], let nonce = offer["nonce"], let signature = offer["signature"], let timestamp = offer["timestamp"], let uuidNonce = UUID(uuidString: nonce), let signatureData = signature.data(using: .utf8), let timestampInt = Int(timestamp) {
if let offerID = discountOffer?["identifier"], let keyID = discountOffer?["keyIdentifier"], let nonce = discountOffer?["nonce"], let signature = discountOffer?["signature"], let timestamp = discountOffer?["timestamp"], let uuidNonce = UUID(uuidString: nonce), let signatureData = signature.data(using: .utf8), let timestampInt = Int(timestamp) {
options.insert(.promotionalOffer(offerID: offerID, keyID: keyID, nonce: uuidNonce, signature: signatureData, timestamp: timestampInt))
}
if let appAccountToken = appAccountToken, let appAccountUUID = UUID(uuidString: appAccountToken) {
Expand Down
22 changes: 10 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Import the native module. On web, it will be resolved to ExpoIap.web.ts
// and on native platforms to ExpoIap.ts
import {
NativeModulesProxy,
EventEmitter,
} from 'expo-modules-core';
import {NativeModulesProxy, EventEmitter} from 'expo-modules-core';
import {Platform} from 'react-native';

import {
Expand Down Expand Up @@ -245,16 +242,17 @@ export const requestPurchase = (
);
}

const purchase = iosTransactionToPurchaseMap(
await ExpoIapModule.buyProduct(
sku,
andDangerouslyFinishTransactionAutomaticallyIOS,
appAccountToken,
quantity ?? -1,
offerToRecordIos(withOffer),
),
const offer = offerToRecordIos(withOffer);

const result = await ExpoIapModule.buyProduct(
sku,
andDangerouslyFinishTransactionAutomaticallyIOS,
appAccountToken,
quantity ?? -1,
offer,
);

const purchase = iosTransactionToPurchaseMap(result);
return Promise.resolve(purchase);
},
android: async () => {
Expand Down

0 comments on commit 3cc4f9f

Please sign in to comment.