I made a capacitor plugin to get AppTransaction
data to check for originalAppVersion
originalPurchaseDate
to verify previous paid users of my App. The App has been converted from paid to a subscription model and I want to identify previously paid users so they do not need to subscribe. The plugin works well if the AppTransaction
is present but the promise is not returned if it is not present (a user never bought the paid version or is a new user)
This is my first plugin and swift is foreign to me so I am hoping someone can review my code and see if I am missing anything that would cause this.
This is the swift code:
import Foundation
import Capacitor
import StoreKit
@objc(VersionPlugin)
public class Version: CAPPlugin {
@objc func getVersion(_ call: CAPPluginCall) {
if #available(iOS 16, *) {
Task {
do {
let verificationResult = try await AppTransaction.shared
switch verificationResult {
case .verified(let appTransaction):
let AppMajorVersion = "3"
let versionComponents = appTransaction.originalAppVersion.split(separator: ".")
let originalMajorVersion = versionComponents[0]
let originalPurchase = appTransaction.originalPurchaseDate
// let jsonData = appTransaction.jsonRepresentation
var hasPurchased: Bool
if originalMajorVersion < AppMajorVersion {
hasPurchased = true
} else {
hasPurchased = false
}
// Return the result to JavaScript
call.resolve([
"originalVersion": originalMajorVersion,
"Purchased": hasPurchased,
"originalPurchase": originalPurchase,
// "JSON": jsonData
])
case .unverified(_, let verificationError):
call.reject("Unverified app transaction: \(verificationError.localizedDescription)")
}
} catch {
// call.reject("An error occurred: \(error.localizedDescription)")
call.reject("Unable to get AppTransaction.shared")
}
}
} else {
call.reject("iOS 16 or above is required")
}
}
}
and I call the plugin like this
async function checkOriginalAppVersion() {
console.log("checkOriginalAppVersion called");
try {
await network();
// Fetch customer info Capacitor plugin
const customerInfo = await Capacitor.Plugins.Version.getVersion();
// Extract the 'originalMajorVersion' value from the plugin's response
const originalV = customerInfo?.originalVersion;
const originalDate = customerInfo?.originalPurchase;
// Compare versions
isOriginalAppVersion = originalV
? compareVersions(originalV, subscribeVersion) < 0
: false;
console.log("isOriginalAppVersion ", isOriginalAppVersion);
console.log("originalDate ", originalDate);
} catch (e) {
// initialization error
console.log("checkOriginalAppVersion Error " + e);
}
}
Any help or comments are appreciated!