Skip to content

Commit

Permalink
Merge pull request #611 from bizz84/develop
Browse files Browse the repository at this point in the history
Fixes for macOS
  • Loading branch information
Sam-Spencer authored Jan 7, 2021
2 parents 8fa93ef + cf82d21 commit 3b54c8e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
34 changes: 15 additions & 19 deletions Sources/SwiftyStoreKit/PaymentQueueController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protocol TransactionController {
/// - parameter paymentQueue: payment queue for finishing transactions
/// - returns: array of unhandled transactions
func processTransactions(_ transactions: [SKPaymentTransaction], on paymentQueue: PaymentQueue) -> [SKPaymentTransaction]

}

public enum TransactionResult {
Expand All @@ -58,11 +59,13 @@ public protocol PaymentQueue: class {
}

extension SKPaymentQueue: PaymentQueue {

#if os(watchOS) && swift(<5.3)
public func resume(_ downloads: [SKDownload]) {
resumeDownloads(downloads)
}
#endif

}

extension SKPaymentTransaction {
Expand All @@ -71,12 +74,12 @@ extension SKPaymentTransaction {
let transactionId = transactionIdentifier ?? "null"
return "productId: \(payment.productIdentifier), transactionId: \(transactionId), state: \(transactionState), date: \(String(describing: transactionDate))"
}

}

extension SKPaymentTransactionState: CustomDebugStringConvertible {

public var debugDescription: String {

switch self {
case .purchasing: return "purchasing"
case .purchased: return "purchased"
Expand All @@ -90,7 +93,7 @@ extension SKPaymentTransactionState: CustomDebugStringConvertible {

struct EntitlementRevocation {
let callback: ([String]) -> Void

init(callback: @escaping ([String]) -> Void) {
self.callback = callback
}
Expand All @@ -99,15 +102,11 @@ struct EntitlementRevocation {
class PaymentQueueController: NSObject, SKPaymentTransactionObserver {

private let paymentsController: PaymentsController

private let restorePurchasesController: RestorePurchasesController

private let completeTransactionsController: CompleteTransactionsController

unowned let paymentQueue: PaymentQueue

private var entitlementRevocation: EntitlementRevocation?

deinit {
paymentQueue.remove(self)
}
Expand Down Expand Up @@ -160,10 +159,10 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
print("SwiftyStoreKit.onEntitlementRevocation() should only be called once when the app launches. Ignoring this call")
return
}

self.entitlementRevocation = revocation
}

func restorePurchases(_ restorePurchases: RestorePurchases) {
assertCompleteTransactionsWasCalled()

Expand All @@ -177,7 +176,6 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
}

func completeTransactions(_ completeTransactions: CompleteTransactions) {

guard completeTransactionsController.completeTransactions == nil else {
print("SwiftyStoreKit.completeTransactions() should only be called once when the app launches. Ignoring this call")
return
Expand All @@ -197,23 +195,26 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
func start(_ downloads: [SKDownload]) {
paymentQueue.start(downloads)
}

func pause(_ downloads: [SKDownload]) {
paymentQueue.pause(downloads)
}

func resume(_ downloads: [SKDownload]) {
paymentQueue.resume(downloads)
}

func cancel(_ downloads: [SKDownload]) {
paymentQueue.cancel(downloads)
}

var shouldAddStorePaymentHandler: ShouldAddStorePaymentHandler?
var updatedDownloadsHandler: UpdatedDownloadsHandler?

// MARK: SKPaymentTransactionObserver

// MARK: - SKPaymentTransactionObserver

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

/*
* Some notes about how requests are processed by SKPaymentQueue:
*
Expand Down Expand Up @@ -253,7 +254,6 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
}

func paymentQueue(_ queue: SKPaymentQueue, didRevokeEntitlementsForProductIdentifiers productIdentifiers: [String]) {

self.entitlementRevocation?.callback(productIdentifiers)
}

Expand All @@ -262,24 +262,20 @@ class PaymentQueueController: NSObject, SKPaymentTransactionObserver {
}

func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {

restorePurchasesController.restoreCompletedTransactionsFailed(withError: error)
}

func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {

restorePurchasesController.restoreCompletedTransactionsFinished()
}

func paymentQueue(_ queue: SKPaymentQueue, updatedDownloads downloads: [SKDownload]) {

updatedDownloadsHandler?(downloads)
}

#if os(iOS) && !targetEnvironment(macCatalyst)
// #if os(iOS) && !targetEnvironment(macCatalyst)
func paymentQueue(_ queue: SKPaymentQueue, shouldAddStorePayment payment: SKPayment, for product: SKProduct) -> Bool {

return shouldAddStorePaymentHandler?(payment, product) ?? false
}
#endif
// #endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
// Copyright © 2017 musevisions. All rights reserved.
//

#if os(iOS)

import UIKit

class PaymentTransactionObserverFake: NSObject {

}

#endif
11 changes: 4 additions & 7 deletions Tests/SwiftyStoreKitTests/ProductsInfoControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ import Foundation

class TestInAppProductRequest: InAppProductRequest {

var hasCompleted: Bool
var cachedResults: RetrieveResults?

private let productIds: Set<String>
private let callback: InAppProductRequestCallback

init(productIds: Set<String>, callback: @escaping InAppProductRequestCallback) {
self.productIds = productIds
self.callback = callback
self.hasCompleted = false
}

func start() {
Expand All @@ -51,15 +55,8 @@ class TestInAppProductRequest: InAppProductRequest {
class TestInAppProductRequestBuilder: InAppProductRequestBuilder {

var requests: [ TestInAppProductRequest ] = []
var os_unfair_lock_s = os_unfair_lock()

func request(productIds: Set<String>, callback: @escaping InAppProductRequestCallback) -> InAppProductRequest {
// add locks to make sure the test does not fail in preparation
os_unfair_lock_lock(&self.os_unfair_lock_s)
defer {
os_unfair_lock_unlock(&self.os_unfair_lock_s)
}

let request = TestInAppProductRequest(productIds: productIds, callback: callback)
requests.append(request)
return request
Expand Down

0 comments on commit 3b54c8e

Please sign in to comment.