1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use serde::{Deserialize, Serialize};
use crate::error::StoreKitError;
use crate::subscription_info::BillingPlanType;
use crate::transaction::{Transaction, TransactionHandle, TransactionPayload};
use crate::verification_result::{VerificationResult, VerificationResultPayload};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
tag = "kind",
rename_all = "camelCase",
rename_all_fields = "camelCase"
)]
/// Represents options passed to `StoreKit.Product.purchase(options:)`.
pub enum PurchaseOption {
/// Represents the `AppAccountToken` `StoreKit` case.
AppAccountToken {
/// App account token reported by `StoreKit`.
app_account_token: String,
},
/// Represents the `BillingPlanType` `StoreKit` case.
BillingPlanType {
/// Value forwarded to `StoreKit` for `billingPlanType`.
#[serde(rename = "billingPlanType")]
billing_plan_type: BillingPlanType,
},
/// Represents the `Quantity` `StoreKit` case.
Quantity {
/// Value forwarded to `StoreKit` for `quantity`.
quantity: i64,
},
/// Represents the `SimulatesAskToBuyInSandbox` `StoreKit` case.
SimulatesAskToBuyInSandbox {
/// Value forwarded to `StoreKit` for `simulate_ask_to_buy_in_sandbox`.
simulate_ask_to_buy_in_sandbox: bool,
},
/// Represents the `CustomString` `StoreKit` case.
CustomString {
/// Value forwarded to `StoreKit` for `key`.
key: String,
/// Value returned by `StoreKit`.
value: String,
},
/// Represents the `CustomNumber` `StoreKit` case.
CustomNumber {
/// Value forwarded to `StoreKit` for `key`.
key: String,
/// Value returned by `StoreKit`.
#[serde(rename = "doubleValue")]
value: f64,
},
/// Represents the `CustomBool` `StoreKit` case.
CustomBool {
/// Value forwarded to `StoreKit` for `key`.
key: String,
/// Value returned by `StoreKit`.
#[serde(rename = "boolValue")]
value: bool,
},
/// Represents the `CustomData` `StoreKit` case.
CustomData {
/// Value forwarded to `StoreKit` for `key`.
key: String,
/// Value forwarded to `StoreKit` for `value_base64`.
value_base64: String,
},
/// Represents the `PromotionalOfferSignature` `StoreKit` case.
PromotionalOfferSignature {
/// Value forwarded to `StoreKit` for `offer_id`.
offer_id: String,
/// Value forwarded to `StoreKit` for `key_id`.
key_id: String,
/// Value forwarded to `StoreKit` for `nonce`.
nonce: String,
/// Value forwarded to `StoreKit` for `signature_base64`.
signature_base64: String,
/// Value forwarded to `StoreKit` for `timestamp`.
timestamp: i64,
},
/// Represents the `PromotionalOfferCompactJws` `StoreKit` case.
PromotionalOfferCompactJws {
/// Value forwarded to `StoreKit` for `offer_id`.
offer_id: String,
/// Value forwarded to `StoreKit` for `compact_jws`.
compact_jws: String,
},
/// Represents the `IntroductoryOfferEligibility` `StoreKit` case.
IntroductoryOfferEligibility {
/// Value forwarded to `StoreKit` for `compact_jws`.
compact_jws: String,
},
/// Represents the `WinBackOffer` `StoreKit` case.
WinBackOffer {
/// Value forwarded to `StoreKit` for `offer_id`.
offer_id: String,
},
/// Represents the `OnStorefrontChange` `StoreKit` case.
OnStorefrontChange {
/// Value forwarded to `StoreKit` for `should_continue_purchase`.
should_continue_purchase: bool,
},
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
/// Represents the result returned by `StoreKit.Product.purchase(options:)`.
pub enum PurchaseResult {
/// The `StoreKit` operation succeeded.
Success(VerificationResult<Transaction>),
/// The person cancelled the `StoreKit` flow.
UserCancelled,
/// The `StoreKit` flow is pending further action.
Pending,
}
#[allow(clippy::unsafe_derive_deserialize)]
#[derive(Debug, Deserialize)]
pub(crate) struct PurchaseResultPayload {
kind: String,
#[serde(rename = "verificationResult")]
verification_result: Option<VerificationResultPayload<TransactionPayload>>,
}
impl PurchaseResultPayload {
pub(crate) fn into_purchase_result(
self,
transaction_handle: Option<TransactionHandle>,
) -> Result<PurchaseResult, StoreKitError> {
match self.kind.as_str() {
"success" => {
let verification_result = self.verification_result.ok_or_else(|| {
StoreKitError::Unknown(
"StoreKit reported a successful purchase without a verification result"
.to_owned(),
)
})?;
let transaction = verification_result.into_result(|payload| {
Transaction::from_raw_parts(transaction_handle, payload)
})?;
Ok(PurchaseResult::Success(transaction))
}
"userCancelled" => Ok(PurchaseResult::UserCancelled),
"pending" => Ok(PurchaseResult::Pending),
other => Err(StoreKitError::Unknown(format!(
"StoreKit returned an unknown purchase result kind '{other}'"
))),
}
}
}
#[cfg(test)]
mod tests {
use super::{PurchaseResult, PurchaseResultPayload};
use crate::error::StoreKitError;
fn payload(json: &str) -> PurchaseResultPayload {
serde_json::from_str(json).expect("valid purchase result payload")
}
#[test]
fn cancelled_and_pending_results_carry_no_transaction() {
assert!(matches!(
payload(r#"{"kind":"userCancelled","verificationResult":null}"#)
.into_purchase_result(None),
Ok(PurchaseResult::UserCancelled)
));
assert!(matches!(
payload(r#"{"kind":"pending","verificationResult":null}"#).into_purchase_result(None),
Ok(PurchaseResult::Pending)
));
}
#[test]
fn success_without_a_verification_result_is_an_error() {
let result =
payload(r#"{"kind":"success","verificationResult":null}"#).into_purchase_result(None);
assert!(matches!(result, Err(StoreKitError::Unknown(_))));
}
#[test]
fn unknown_result_kinds_are_errors() {
let result =
payload(r#"{"kind":"deferred","verificationResult":null}"#).into_purchase_result(None);
match result {
Err(StoreKitError::Unknown(message)) => assert!(message.contains("deferred")),
other => panic!("expected an unknown-kind error, got {other:?}"),
}
}
}