Skip to main content

afia_component/
stripe.rs

1//! Types related to the Stripe JS SDK.
2//!
3//! We plan to research ways to remove first-party support for Stripe while still making it easy
4//! for third-party developers to write some sort of plugin/add-on/component/etc that can be used
5//! to get the same functionality that we're currently providing.
6
7use crate::context::crate_private::CratePrivate;
8use crate::context::Context;
9use crate::ComponentImports;
10
11/// An initialized Stripe JS SDK instance.
12///
13/// ## Stripe Documentation
14/// https://docs.stripe.com/js/initializing
15#[repr(C)]
16pub struct StripeJsSdkInstanceHandle(i64);
17impl StripeJsSdkInstanceHandle {
18    /// TODO: Delete this once the `pm-checkout-page` component is fully converted to using
19    ///  `afia-component`.
20    ///  Basically:
21    ///  1. don't use this method for anything new
22    ///  2. once this method is unused we can delete it
23    pub fn temporary_way_to_get_i64(&self) -> i64 {
24        self.0
25    }
26}
27
28/// An initialized Stripe Elements instance.
29///
30/// ## Stripe Documentation
31/// https://docs.stripe.com/js/elements_object
32#[repr(C)]
33pub struct StripeElementsHandle(i64);
34impl StripeElementsHandle {
35    /// TODO: Delete this once the `pm-checkout-page` component is fully converted to using
36    ///  `afia-component`.
37    ///  Basically:
38    ///  1. don't use this method for anything new
39    ///  2. once this method is unused we can delete it
40    pub fn temporary_way_to_get_i64(&self) -> i64 {
41        self.0
42    }
43}
44
45impl ComponentImports {
46    /// Initialize the Stripe JS SDK.
47    pub fn initialize_stripe<C: Context>(&self, context: C) {
48        let context = context.to_isize(CratePrivate);
49        unsafe { afia_component_sys::initialize_stripe(self.component_imports_ptr, context) };
50    }
51
52    /// Create a Stripe Elements object.
53    pub fn create_stripe_elements(
54        &self,
55        handle: &StripeJsSdkInstanceHandle,
56        amount_cents: u32,
57    ) -> StripeElementsHandle {
58        let elements = unsafe {
59            afia_component_sys::create_stripe_elements(
60                self.component_imports_ptr,
61                handle.0,
62                amount_cents,
63            )
64        };
65
66        StripeElementsHandle(elements)
67    }
68
69    /// Submit a Stripe Elements object.
70    pub fn submit_stripe_elements<C: Context>(&self, handle: &StripeElementsHandle, context: C) {
71        let context = context.to_isize(CratePrivate);
72        unsafe {
73            afia_component_sys::submit_stripe_elements(
74                self.component_imports_ptr,
75                handle.0,
76                context,
77            )
78        };
79    }
80}