Skip to content

Commit

Permalink
Added support for showManageSubscriptions on Storekit 2
Browse files Browse the repository at this point in the history
  • Loading branch information
timbrandin committed Oct 19, 2022
1 parent 8a7ff05 commit db01ad3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
4 changes: 4 additions & 0 deletions ios/RNIapIosSk2.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ @interface RCT_EXTERN_MODULE (RNIapIosSk2, NSObject)
(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(showManageSubscriptions:
(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(clearTransaction:
(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
Expand Down
47 changes: 45 additions & 2 deletions ios/RNIapIosSk2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ protocol Sk2Delegate {
reject: @escaping RCTPromiseRejectBlock
)

func showManageSubscriptions(
_ resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock
)

func clearTransaction(
_ resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock
Expand Down Expand Up @@ -214,6 +219,13 @@ class DummySk2: Sk2Delegate {
reject(errorCode, errorMessage, nil)
}

func showManageSubscriptions(
_ resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock
) {
reject(errorCode, errorMessage, nil)
}

func clearTransaction(_ resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
reject(errorCode, errorMessage, nil)
}
Expand Down Expand Up @@ -356,7 +368,7 @@ class RNIapIosSk2: RCTEventEmitter, Sk2Delegate {
delegate.latestTransaction(sku, resolve: resolve, reject: reject)
}

@objc public func finishTransaction(
@objc public func finishTransaction(
_ transactionIdentifier: String,
resolve: @escaping RCTPromiseResolveBlock = { _ in },
reject: @escaping RCTPromiseRejectBlock = { _, _, _ in }
Expand All @@ -378,13 +390,20 @@ class RNIapIosSk2: RCTEventEmitter, Sk2Delegate {
delegate.sync(resolve, reject: reject)
}

@objc public func presentCodeRedemptionSheet(
@objc public func presentCodeRedemptionSheet(
_ resolve: @escaping RCTPromiseResolveBlock = { _ in },
reject: @escaping RCTPromiseRejectBlock = { _, _, _ in }
) {
delegate.presentCodeRedemptionSheet(resolve, reject: reject)
}

@objc public func showManageSubscriptions(
_ resolve: @escaping RCTPromiseResolveBlock = { _ in },
reject: @escaping RCTPromiseRejectBlock = { _, _, _ in }
) {
delegate.showManageSubscriptions(resolve, reject: reject)
}

@objc func clearTransaction(
_ resolve: @escaping RCTPromiseResolveBlock = { _ in },
reject: @escaping RCTPromiseRejectBlock = { _, _, _ in }) {
Expand Down Expand Up @@ -847,6 +866,30 @@ class RNIapIosSk2iOS15: Sk2Delegate {
reject(IapErrors.E_USER_CANCELLED.rawValue, "This method is not available on tvOS", nil)
#endif
}

@objc public func showManageSubscriptions(
_ resolve: @escaping RCTPromiseResolveBlock = { _ in },
reject: @escaping RCTPromiseRejectBlock = { _, _, _ in }
) {
#if !os(tvOS)
guard let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
!ProcessInfo.processInfo.isiOSAppOnMac else {
return
}

Task {
do {
try await AppStore.showManageSubscriptions(in: scene)
} catch {
print("Error:(error)")
}
}

resolve(nil)
#else
reject(IapErrors.E_USER_CANCELLED.rawValue, "This method is not available on tvOS", nil)
#endif
}

func clearTransaction(_ resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
Task {
Expand Down
2 changes: 2 additions & 0 deletions src/modules/iosSk2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type finishTransaction = (transactionIdentifier: string) => Promise<boolean>;

type getPendingTransactions = () => Promise<ProductPurchase[]>;
type presentCodeRedemptionSheet = () => Promise<null>;
type showManageSubscriptions = () => Promise<null>;

export interface IosModulePropsSk2 extends NativeModuleProps {
isAvailable(): number;
Expand All @@ -53,6 +54,7 @@ export interface IosModulePropsSk2 extends NativeModuleProps {
finishTransaction: finishTransaction;
getPendingTransactions: getPendingTransactions;
presentCodeRedemptionSheet: presentCodeRedemptionSheet;
showManageSubscriptions: showManageSubscriptions;
disable: () => Promise<null>;
beginRefundRequest: (sku: string) => Promise<RefundRequestStatus>;
}
Expand Down

0 comments on commit db01ad3

Please sign in to comment.