Skip to content

Commit

Permalink
Update README, add purchase restoring functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Tikhonenko committed Oct 10, 2021
1 parent b3e8695 commit 8513ad8
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 8 deletions.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,48 @@

[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat)](http://mit-license.org)
[![Platform](http://img.shields.io/badge/platform-iOS%20%7C%20macOS%20%7C%20tvOS%20%7C%20watchOS-lightgrey.svg?style=flat)](https://developer.apple.com/resources/)
[![Language](https://img.shields.io/badge/swift-5.0-orange.svg)](https://developer.apple.com/swift)
[![Language](https://img.shields.io/badge/swift-5.5-orange.svg)](https://developer.apple.com/swift)

Mercato is a lightweight In-App Purchases (StoreKit 2) library for iOS, tvOS, watchOS, macOS, and Mac Catalyst.

Installation
------------

### Swift Package Manager

To integrate using Apple's Swift package manager, add the following as a dependency to your `Package.swift`:

```swift
.package(url: "https://github.com/tikhop/Mercato.git", .upToNextMajor(from: "0.0.1"))
```

Then, specify `"Mercato"` as a dependency of the Target in which you wish to use Mercato.

Lastly, run the following command:
```swift
swift package update
```

### CocoaPods

In progress...

Then, run the following command:

```bash
$ pod install
```

In any swift file you'd like to use Mercato, import the framework with `import Mercato`.

### Requirements

- iOS 15.0 / OSX 12.0 / watchOS 8.0
- Swift 5.5

Usage
-------------

## Essential Reading
* [Apple - Meet StoreKit 2](https://developer.apple.com/videos/play/wwdc2021/10114/)
* [Apple - In-App Purchase](https://developer.apple.com/documentation/storekit/in-app_purchase)
Expand Down
30 changes: 30 additions & 0 deletions Sources/Mercato/Mercato+StoreKit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// File.swift
//
//
// Created by Pavel Tikhonenko on 10.10.2021.
//

import Foundation
import StoreKit

typealias RenewalState = Product.SubscriptionInfo.RenewalState

public extension Product
{
var isEligibleForIntroOffer: Bool
{
get async
{
await subscription?.isEligibleForIntroOffer ?? false
}
}

var hasActiveSubscription: Bool
{
get async
{
await (try? subscription?.status.first?.state == RenewalState.subscribed) ?? false
}
}
}
5 changes: 5 additions & 0 deletions Sources/Mercato/Mercato.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ extension Mercato
{
await shared.beginRefundProcess(for: productID, in: scene)
}

public static func restorePurchases() async throws
{
try await AppStore.sync()
}
}

func checkVerified<T>(_ result: VerificationResult<T>) throws -> T
Expand Down
10 changes: 3 additions & 7 deletions Sources/Mercato/ProductService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class ProductService
{
do
{
return try await Product.products(for: productIds)
let products = try await Product.products(for: productIds)
cachedProducts = products
return products
}catch{
throw MercatoError.storeKit(error: error as! StoreKitError)
}
Expand All @@ -36,12 +38,6 @@ class ProductService
}

let transaction = try checkVerified(result)

//Ignore revoked transactions, they're no longer purchased.

//For subscriptions, a user can upgrade in the middle of their subscription period. The lower service
//tier will then have the `isUpgraded` flag set and there will be a new transaction for the higher service
//tier. Ignore the lower service tier transactions which have been upgraded.
return transaction.revocationDate == nil && !transaction.isUpgraded
}
}
Binary file added www/logo-social.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8513ad8

Please sign in to comment.