Skip to main content

storekit/
refund.rs

1use core::ptr;
2
3use crate::error::StoreKitError;
4use crate::ffi;
5use crate::private::{cstring_from_str, error_from_status, take_string};
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum RefundRequestStatus {
9    Success,
10    UserCancelled,
11    Unknown(String),
12}
13
14impl RefundRequestStatus {
15    pub fn as_str(&self) -> &str {
16        match self {
17            Self::Success => "success",
18            Self::UserCancelled => "userCancelled",
19            Self::Unknown(value) => value.as_str(),
20        }
21    }
22
23    fn from_raw(raw: String) -> Self {
24        match raw.as_str() {
25            "success" => Self::Success,
26            "userCancelled" => Self::UserCancelled,
27            _ => Self::Unknown(raw),
28        }
29    }
30}
31
32#[derive(Debug, Clone, Copy, Default)]
33pub struct Refund;
34
35impl Refund {
36    pub fn begin_for_transaction_id(
37        transaction_id: u64,
38    ) -> Result<RefundRequestStatus, StoreKitError> {
39        let transaction_id = cstring_from_str(&transaction_id.to_string(), "transaction id")?;
40        let mut status_ptr = ptr::null_mut();
41        let mut error_message = ptr::null_mut();
42        let status = unsafe {
43            ffi::sk_refund_begin_request_for_transaction_id(
44                transaction_id.as_ptr(),
45                &mut status_ptr,
46                &mut error_message,
47            )
48        };
49        if status != ffi::status::OK {
50            return Err(unsafe { error_from_status(status, error_message) });
51        }
52
53        let raw_status = unsafe { take_string(status_ptr) }
54            .ok_or_else(|| StoreKitError::Unknown("missing refund status payload".to_owned()))?;
55        Ok(RefundRequestStatus::from_raw(raw_status))
56    }
57}