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
use serde::{Deserialize, Serialize};
use crate::error::StoreKitError;
use crate::subscription_info::BillingPlanType;
use crate::transaction::{Transaction, TransactionPayload};
use crate::verification_result::{VerificationResult, VerificationResultPayload};
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "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`.
value: f64,
},
/// Represents the `CustomBool` `StoreKit` case.
CustomBool {
/// Value forwarded to `StoreKit` for `key`.
key: String,
/// Value returned by `StoreKit`.
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: *mut core::ffi::c_void,
) -> 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" => {
if !transaction_handle.is_null() {
unsafe { crate::ffi::sk_transaction_release(transaction_handle) };
}
Ok(PurchaseResult::UserCancelled)
}
"pending" => {
if !transaction_handle.is_null() {
unsafe { crate::ffi::sk_transaction_release(transaction_handle) };
}
Ok(PurchaseResult::Pending)
}
other => {
if !transaction_handle.is_null() {
unsafe { crate::ffi::sk_transaction_release(transaction_handle) };
}
Err(StoreKitError::Unknown(format!(
"StoreKit returned an unknown purchase result kind '{other}'"
)))
}
}
}
}