MobilePayKit
Client library for making in-app purchases on iOS and macOS Automattic apps
Introduction
MobilePayKit is a client library for making in-app purchases. This project facilitates in-app purchases by coordinating requests to:
- Apple's StoreKit APIs
- Our WP.com in-app purchase API
Realistically this library is only useful for Automattic-based projects, but the idea is to share what we've made.
Features
- iOS and macOS compatible
- Consumable in-app purchases support
- Non-Consumable in-app purchases support
- Auto-Renewable Subscriptions in-app purchases support
- Non-Renewable Subscriptions in-app purchases support
- Restoring purchases
Requirements
- iOS 13+
- macOS 10.15+
- Swift 5.3
Installation
Cocoapods
To integrate MobilePayKit into your Xcode project via CocoaPods, specify it in your Podfile
:
pod 'MobilePayKit', :git => '[email protected]:Automattic/MobilePay-Apple.git', :branch => 'develop'
Swift Package Manager
To integrate MobilePayKit into your Xcode project via Swift Package Manager, add MobilePayKit as a dependency in Package.swift
:
dependencies: [
.package(url: "https://github.com/Automattic/MobilePay-Apple", .upToNextMajor(from: "0.0.1"))
]
Usage
Check out the iOS and macOS examples under the Example directory for more information on how to make in-app purchases via MobilePayKit.
Import MobilePayKit
Import the MobilePayKit module in your project.
import MobilePayKit
Configuring MobilePayKit
Before you call any MobilePayKit methods, configure a MobilePayKit shared instance by setting the WordPress oAuth token as well as the app's bundle ID.
MobilePayKit.configure( oAuthToken: "" , bundleId: "" )
Fetching products
Fetch products that are available for in-app purchases.
MobilePayKit.shared.fetchProducts { [weak self] result in
switch result {
case .success(let products):
print("Fetched products: \(products)")
case .failure(let error):
print("Error fetching products: \(error.localizedDescription)")
}
}
Purchasing a product
Purchase a product using the product identifier.
MobilePayKit.shared.purchaseProduct(with: identifier) { [weak self] result in
switch result {
case .success(let transaction):
print("Purchased product: \(transaction.payment.productIdentifier)")
case .failure(let error):
print("Error purchasing product: \(error.localizedDescription)")
}
}
Testing in-app purchases
Testing locally via Xcode
To test in-app purchases locally, each developer will have to modify their Xcode scheme to let Xcode know to use the local Configuration.storekit
file. Check out the Apple documentation on testing locally via Xcode for more information.
- Click on your app's scheme at the top of Xcode and select Edit Scheme
- Select Run
- Select Options
- Change the StoreKit Configuration to point to
Configuration.storekit
(the local StoreKit file)
Testing via the App Store Connect Sandbox
TBD
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
License
MobilePayKit is available under the GPL license. See the LICENSE file for more info.