Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support Customer Session API #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ def_id!(RecipientId: String); // FIXME: This doesn't seem to be documented yet
def_id!(RefundId, "re_");
def_id!(ReviewId, "prv_");
def_id!(ScheduledQueryRunId, "sqr_");
def_id!(SessionId, "bps_");
def_id!(SetupIntentId, "seti_");
def_id!(SkuId, "sku_");
def_id!(SourceId, "src_");
Expand Down
4 changes: 4 additions & 0 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ mod plan;
#[cfg(feature = "billing")]
mod price;
#[cfg(feature = "billing")]
mod session_ext;
#[cfg(feature = "billing")]
mod subscription;
#[cfg(feature = "billing")]
mod subscription_ext;
Expand Down Expand Up @@ -136,6 +138,8 @@ pub use self::plan::*;
#[cfg(feature = "billing")]
pub use self::price::*;
#[cfg(feature = "billing")]
pub use self::session_ext::*;
#[cfg(feature = "billing")]
pub use self::subscription::*;
#[cfg(feature = "billing")]
pub use self::subscription_ext::*;
Expand Down
60 changes: 60 additions & 0 deletions src/resources/session_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use crate::config::{Client, Response};
use crate::ids::{CustomerId, SessionId};
use crate::params::Timestamp;
use serde_derive::{Deserialize, Serialize};

/// Creates a session of the customer portal.
///
/// For more details see [https://stripe.com/docs/api/customer_portal/create](https://stripe.com/docs/api/customer_portal/create).
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CreateSession {
/// The ID of an existing customer.
pub customer: CustomerId,

/// The URL to which Stripe should send customers when they click on the link to return to your website. This field is required if a default return URL has not been configured for the portal.
pub return_url: Option<String>,
}

/// The Session object.
///
/// A session describes the instantiation of the customer portal for a particular customer. By visiting the session's URL, the customer can manage their subscriptions and billing details. For security reasons, sessions are short-lived and will expire if the customer does not visit the URL. Create sessions on-demand when customers intend to manage their subscriptions and billing details.
///
/// For more details see [https://stripe.com/docs/api/customer_portal/object](https://stripe.com/docs/api/customer_portal/object).
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Session {
/// Unique identifier for the object.
pub id: SessionId,

/// String representing the object’s type. Objects of the same type share the same value.
pub object: String,

/// Time at which the object was created. Measured in seconds since the Unix epoch.
pub created: Timestamp,

/// The ID of the customer for this session.
pub customer: CustomerId,

/// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
pub livemode: bool,

/// The URL to which Stripe should send customers when they click on the link to return to your website.
pub return_url: String,

/// The short-lived URL of the session giving customers access to the customer portal.
pub url: String,
}

impl CreateSession {
pub fn new(customer: CustomerId) -> Self {
CreateSession { customer, return_url: None }
}
}

impl Session {
/// Creates a session of the customer portal.
///
/// For more details see [https://stripe.com/docs/api/customer_portal/create](https://stripe.com/docs/api/customer_portal/create).
pub fn create(client: &Client, params: CreateSession) -> Response<Self> {
client.post_form(&format!("/billing_portal/sessions"), &params)
}
}