https://github.com/tikhop/mercato
Lightweight StoreKit 2 Wrapper
https://github.com/tikhop/mercato
in-app-purchase in-app-receipt ios macos storekit storekit2
Last synced: about 1 month ago
JSON representation
Lightweight StoreKit 2 Wrapper
- Host: GitHub
- URL: https://github.com/tikhop/mercato
- Owner: tikhop
- License: mit
- Created: 2021-09-25T01:32:03.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-10-16T21:21:52.000Z (7 months ago)
- Last Synced: 2025-04-02T02:39:33.461Z (about 2 months ago)
- Topics: in-app-purchase, in-app-receipt, ios, macos, storekit, storekit2
- Language: Swift
- Homepage:
- Size: 94.7 KB
- Stars: 73
- Watchers: 4
- Forks: 13
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
![]()
# Mercato
[](http://mit-license.org)
[](https://developer.apple.com/resources/)
[](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.5Usage
-------------#### Listen for transaction updates
Start transaction update listener as soon as your app launches so you don't miss a single transaction. This is important, for example, to handle transactions that may have occured after `purchase` returns, like an adult approving a child's purchase request or a purchase made on another device.
> If your app has unfinished transactions, you receive them immediately after the app launches
```swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
{
Mercato.listenForTransactions(finishAutomatically: false) { transaction in
//Deliver content to the user.
//Finish transaction
await transaction.finish()
}
return true
}
```#### Fetching products
```swift
do
{
let productIds: Set = ["com.test.product.1", "com.test.product.2", "com.test.product.3"]
let products = try await Mercato.retrieveProducts(productIds: productIds)
//Show products to the user
}catch{
//Handle errors
}
```#### Purchase a product
```swift
try await Mercato.purchase(product: product, quantity: 1, finishAutomatically: false, appAccountToken: nil, simulatesAskToBuyInSandbox: false)
```#### Offering in-app refunds
```swift
try await Mercato.beginRefundProcess(for: product, in: windowScene)
```#### Restore completed transactions
In general users won't need to restore completed transactions when your app is reinstalled or downloaded on a new device. Everything should automatically be fetched by StoreKit and stay up to date. In the rare case that a user thinks they should have a transaction but you don't see it, you have to provide UI in your app that allows users to initiate the sync. It should be very rare that a user needs to initiate a sync manually. Automatic synchronization should cover the majority of cases.
```swift
try await Mercato.restorePurchases()
```## 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)
* [WWDC by Sundell - Working With In-App Purchases in StoreKit 2](https://wwdcbysundell.com/2021/working-with-in-app-purchases-in-storekit2/)## License
Mercato is released under an MIT license. See [LICENSE](https://github.com/tikhop/Mercato/blob/master/LICENSE) for more information.