Skip to content

Commit ac3fb71

Browse files
committed
more complete documentation
1 parent 1acac1c commit ac3fb71

File tree

7 files changed

+101
-20
lines changed

7 files changed

+101
-20
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
## [0.2.1] - 2024-05-12
11+
12+
### Added
13+
* proper rustdoc and readme with example
14+
1015
## [0.2.0] - 2024-05-09
1116

1217
### Changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[sh_crates]: https://img.shields.io/crates/v/bevy_ios_iap.svg
88
[lk_crates]: https://crates.io/crates/bevy_ios_iap
99
[sh_docs]: https://img.shields.io/docsrs/bevy_ios_iap
10-
[lk_docs]: https://docs.rs/bevy_ios_gamecenter/latest/bevy_ios_iap/
10+
[lk_docs]: https://docs.rs/bevy_ios_iap/latest/bevy_ios_iap/
1111
[sh_discord]: https://img.shields.io/discord/1176858176897953872?label=discord&color=5561E6
1212
[lk_discord]: https://discord.gg/rQNeEnMhus
1313

@@ -65,7 +65,7 @@ or
6565

6666
```toml
6767
# always pin to the same exact version you also of the Swift package
68-
bevy_ios_iap = { version = "=0.2.0" }
68+
bevy_ios_iap = { version = "=0.2.1" }
6969
```
7070

7171
### 3. Setup Plugin
@@ -79,6 +79,12 @@ app.add_plugins(IosIapPlugin::new(true));
7979

8080
```rust
8181
fn bevy_system() {
82+
// If you set the plugin to manual init, this will register the
83+
// TranscactionObserver to listen to updates to any Transactions and trigger
84+
// `IosIapEvents::Transaction` accordingly.
85+
// Note: this will require the user to be logged in into their apple-id and popup a login dialog if not
86+
bevy_ios_iap::init();
87+
8288
// request product details, product IDs have to be explicitly provided
8389
// this will lead to a response: `IosIapEvents::Products`
8490
bevy_ios_iap::get_products(vec!["com.rustunit.zoolitaire.levelunlock".into()]);

bevy_ios_iap/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bevy_ios_iap"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
edition = "2021"
55
build = "build.rs"
66
readme = "../README.md"

bevy_ios_iap/src/lib.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ pub use methods::{
99
pub use plugin::{IosIapEvents, IosIapPlugin};
1010
pub use transaction::IosIapTransaction;
1111

12+
/// Expected event data in response to [`finish_transaction`] method call.
13+
///
14+
/// See Event [`IosIapEvents`]
1215
#[derive(Debug, Clone)]
1316
pub enum IosIapTransactionFinished {
17+
/// Unknown Unfinished Transaction, maybe a concurrent process to finish it in the meantime?
1418
UnknownTransaction(u64),
19+
/// Transaction successfully finished
1520
Finished(IosIapTransaction),
21+
/// Some error occured
1622
Error(String),
1723
}
1824

@@ -29,13 +35,21 @@ impl IosIapTransactionFinished {
2935
}
3036
}
3137

38+
/// Expected event data in response to [`purchase`] method call.
39+
///
40+
/// See Event [`IosIapEvents`]
3241
#[derive(Debug, Clone)]
3342
pub enum IosIapPurchaseResult {
43+
/// Purchase successful
3444
Success(IosIapTransaction),
45+
/// User canceled the purchase
3546
Canceled(String),
47+
/// The purchase is pending, and requires action from the customer. If the transaction completes,
48+
/// it's available through the TransactionObserver registered via [`init`] and lead to [`IosIapEvents::Transaction`] calls.
3649
Pending(String),
37-
/// Unknown / invalid product ID
50+
/// Unknown / invalid product ID was used to trigger purchase
3851
Unknown(String),
52+
/// Error occured
3953
Error(String),
4054
}
4155

@@ -61,6 +75,12 @@ impl IosIapPurchaseResult {
6175
}
6276
}
6377

78+
/// A cause of a purchase transaction, indicating whether it’s a customer’s purchase or
79+
/// an auto-renewable subscription renewal that the system initiates.
80+
///
81+
/// Part of [`IosIapTransaction`].
82+
///
83+
/// See <https://developer.apple.com/documentation/storekit/transaction/reason>
6484
#[derive(Debug, Clone)]
6585
pub enum IosIapTransactionReason {
6686
Renewal,
@@ -76,6 +96,11 @@ impl IosIapTransactionReason {
7696
}
7797
}
7898

99+
/// The server environment that generates and signs the transaction.
100+
///
101+
/// Part of [`IosIapTransaction`].
102+
///
103+
/// See <https://developer.apple.com/documentation/storekit/transaction/3963920-environment>
79104
#[derive(Debug, Clone)]
80105
pub enum IosIapEnvironment {
81106
Production,
@@ -95,6 +120,11 @@ impl IosIapEnvironment {
95120
}
96121
}
97122

123+
/// The App Store storefront associated with the transaction.
124+
///
125+
/// Part of [`IosIapTransaction`].
126+
///
127+
/// See <https://developer.apple.com/documentation/storekit/transaction/4193541-storefront>
98128
#[derive(Debug, Clone, Default)]
99129
pub struct IosIapStorefront {
100130
pub id: String,
@@ -107,6 +137,11 @@ impl IosIapStorefront {
107137
}
108138
}
109139

140+
/// The type of the in-app purchase.
141+
///
142+
/// Part of [`IosIapTransaction`] and [`IosIapProduct`].
143+
///
144+
/// See <https://developer.apple.com/documentation/storekit/transaction/3749708-producttype>
110145
#[derive(Debug, Clone)]
111146
pub enum IosIapProductType {
112147
Consumable,
@@ -133,6 +168,11 @@ impl IosIapProductType {
133168
}
134169
}
135170

171+
/// Expected event data in response to [`get_products`] method call.
172+
///
173+
/// See Event [`IosIapEvents`]
174+
///
175+
/// See <https://developer.apple.com/documentation/storekit/product>
136176
#[derive(Debug, Clone)]
137177
pub struct IosIapProduct {
138178
pub id: String,

bevy_ios_iap/src/methods.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,65 @@
33
#[allow(unused_imports)]
44
use crate::native;
55

6+
/// Registers for any updates on Transactions.
7+
/// Any such update will trigger: [`IosIapEvents::Transaction`][crate::IosIapEvents::Transaction]
8+
///
9+
/// See <https://developer.apple.com/documentation/storekit/transaction/3851206-updates>
610
pub fn init() {
711
#[cfg(target_os = "ios")]
812
native::ios_iap_init();
913
}
1014

15+
/// Fetch Product Details. A list of Product IDs has to be provided.
16+
/// Expected to be confirmed with event: [`IosIapEvents::Products`][crate::IosIapEvents::Products]
17+
///
18+
/// See <https://developer.apple.com/documentation/storekit/product/3851116-products>
1119
pub fn get_products(products: Vec<String>) {
1220
#[cfg(target_os = "ios")]
1321
native::ios_iap_products(products);
1422
}
1523

24+
/// Trigger a purchase flow. The product ID has to be provided
25+
/// Expected to be confirmed with event: [`IosIapEvents::Purchase`][crate::IosIapEvents::Purchase]
26+
///
27+
/// See <https://developer.apple.com/documentation/storekit/product/3791971-purchase>
1628
pub fn purchase(id: String) {
1729
#[cfg(target_os = "ios")]
1830
native::ios_iap_purchase(id);
1931
}
2032

33+
/// Finishes a Transaction. Identify the Transaction with an ID.
34+
/// Apple expects us to call this only after the user got the Product granted so we can safely consider this purchase finished.
35+
/// Until the transaction is finished iOS will keep triggering it as soon as we register the
36+
/// TransactionObserver via the `init` call. See [`crate::init`].
37+
///
38+
/// Expected to be confirmed with event: [`IosIapEvents::TransactionFinished`][crate::IosIapEvents::TransactionFinished]
39+
///
40+
/// See <https://developer.apple.com/documentation/storekit/transaction/3749694-finish>
2141
pub fn finish_transaction(id: u64) {
2242
#[cfg(target_os = "ios")]
2343
native::ios_iap_transaction_finish(id);
2444
}
2545

26-
/// > The transaction history includes consumable in-app purchases
27-
/// > that the app hasn’t finished by calling finish(). It doesn’t
28-
/// > include finished consumable products or finished non-renewing
29-
/// > subscriptions, repurchased non-consumable products or subscriptions,
30-
/// > or restored purchases.
46+
/// Quoted from Apple's docs: "A sequence that emits all the transactions for the user for your app."
47+
///
48+
/// Unlike [`crate::current_entitlements`] this will also return unfinished Transactions. Otherwise the result is the same.
49+
///
50+
/// Expected to be confirmed with event: [`IosIapEvents::AllTransactions`][crate::IosIapEvents::AllTransactions]
3151
///
3252
/// See <https://developer.apple.com/documentation/storekit/transaction/3851203-all>
3353
pub fn all_transactions() {
3454
#[cfg(target_os = "ios")]
3555
native::ios_iap_transactions_all();
3656
}
3757

38-
/// "A sequence of the latest transactions that entitle a user to in-app purchases and subscriptions."
58+
/// Quoted from Apple docs: "A sequence of the latest transactions that entitle a user to in-app purchases and subscriptions."
59+
///
60+
/// Ususally used for "RestorePurchases" functionality.
61+
/// Most importantly this will only included active subscriptions and non-consumables.
62+
/// Finished Transactions of Consumables will never appear again.
3963
///
40-
/// > The current entitlements sequence emits the latest transaction for each product the user has an entitlement to, specifically:
41-
/// > A transaction for each non-consumable in-app purchase
42-
/// > The latest transaction for each auto-renewable subscription that has a Product.SubscriptionInfo.RenewalState state of subscribed or inGracePeriod
43-
/// > The latest transaction for each non-renewing subscription, including finished ones
64+
/// Expected to be confirmed with event: [`IosIapEvents::CurrentEntitlements`][crate::IosIapEvents::CurrentEntitlements]
4465
///
4566
/// See <https://developer.apple.com/documentation/storekit/transaction/3851204-currententitlements>
4667
pub fn current_entitlements() {

bevy_ios_iap/src/plugin.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,32 @@ use bevy_ecs::prelude::*;
44
use crate::transaction::IosIapTransaction;
55
use crate::{IosIapProduct, IosIapPurchaseResult, IosIapTransactionFinished};
66

7-
///
7+
/// All events for communication from native iOS (Swift) side to Rust/Bevy
88
#[derive(Event, Clone, Debug)]
99
pub enum IosIapEvents {
10+
/// Triggered by calls to [`get_products`][crate::get_products]
1011
Products(Vec<IosIapProduct>),
12+
/// Triggered by calls to [`purchase`][crate::purchase]
1113
Purchase(IosIapPurchaseResult),
14+
/// Triggered automatically by TransactionObserver registered by [`init`][crate::init]
15+
/// for every update on any Transaction while app is running.
1216
Transaction(IosIapTransaction),
17+
/// Triggered in response to calls to [`finish_transaction`][crate::finish_transaction]
1318
TransactionFinished(IosIapTransactionFinished),
14-
15-
/// async response to `all_transaction` request
19+
/// Triggered in response to calls to [`all_transactions`][crate::all_transactions]
1620
AllTransactions(Vec<IosIapTransaction>),
17-
/// async response to `current_entitlements` request
21+
/// Triggered in response to calls to [`current_entitlements`][crate::current_entitlements]
1822
CurrentEntitlements(Vec<IosIapTransaction>),
1923
}
2024

21-
///
25+
/// Bevy plugin to integrate access to iOS StoreKit2
2226
#[allow(dead_code)]
2327
pub struct IosIapPlugin {
2428
auto_init: bool,
2529
}
2630

2731
impl IosIapPlugin {
28-
///
32+
/// create plugin and define whether it will call [`init`] automatically right on startup.
2933
pub fn new(auto_init: bool) -> Self {
3034
Self { auto_init }
3135
}

bevy_ios_iap/src/transaction.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
use crate::{IosIapEnvironment, IosIapProductType, IosIapStorefront, IosIapTransactionReason};
22

3+
/// Representation of a Transaction.
4+
/// Mirrors the Transcation type in Apple's StoreKit2 closely.
5+
/// See official docs for more details on the individual fields.
6+
///
7+
/// See <https://developer.apple.com/documentation/storekit/transaction>
38
#[derive(Debug, Clone)]
49
pub struct IosIapTransaction {
510
pub id: u64,

0 commit comments

Comments
 (0)