-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
580dddb
commit 89b506a
Showing
8 changed files
with
386 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use pinpayments::{Client, Plan}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let secret_key = std::env::var("PINPAYMENTS_SECRET_KEY").expect("Missing PINPAYMENTS_SECRET_KEY in env"); | ||
let client = Client::from_url(pinpayments::DEFAULT_TEST_API_BASE_URL, secret_key); | ||
|
||
let plans = match Plan::list(&client, None, None).await { | ||
Ok(c) => Some(c), | ||
Err(e) => { | ||
println!("{e:?}"); | ||
None | ||
}, | ||
}; | ||
|
||
println!("Results are {plans:?}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use time::{OffsetDateTime}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::client::{Client, Response, StatusOnlyResponse}; | ||
use crate::error::PinError; | ||
use crate::ids::{PlanId}; | ||
use crate::params::{unpack_contained, Page, Paginator, paginate}; | ||
use crate::resources::{Currency}; | ||
use crate::build_map; | ||
|
||
#[derive(PartialEq, Debug, Serialize, Default, Deserialize)] | ||
#[serde(rename_all = "snake_case")] | ||
pub enum IntervalUnit { | ||
#[default] | ||
Day, | ||
Week, | ||
Month, | ||
Year | ||
} | ||
|
||
#[derive(PartialEq, Debug, Serialize, Deserialize)] | ||
#[serde(rename_all = "snake_case")] | ||
pub enum CustomerPermission { | ||
Cancel | ||
} | ||
|
||
#[derive(Debug, Default, Serialize)] | ||
pub struct CreatePlan<'a> { | ||
pub name: &'a str, | ||
pub amount: i64, | ||
pub currency: Currency, | ||
pub interval: u32, | ||
pub interval_unit: IntervalUnit, | ||
|
||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub intervals: Option<u32>, | ||
|
||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub setup_amount: Option<u32>, | ||
|
||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub trial_amount: Option<u32>, | ||
|
||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub trial_interval: Option<u32>, | ||
|
||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub trial_interval_unit: Option<IntervalUnit>, | ||
|
||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub customer_permissions: Option<Vec<CustomerPermission>> | ||
} | ||
|
||
#[derive(Debug, Default, Deserialize)] | ||
pub struct SubscriptionCounts { | ||
pub trial: u32, | ||
pub active: u32, | ||
pub cancelling: u32, | ||
pub cancelled: u32 | ||
} | ||
|
||
#[derive(Debug, Default, Deserialize)] | ||
pub struct Plan { | ||
pub token: PlanId, | ||
pub name: String, | ||
pub amount: i64, | ||
pub currency: Currency, | ||
pub interval: u32, | ||
pub interval_unit: IntervalUnit, | ||
pub intervals: u32, | ||
pub setup_amount: u32, | ||
pub trial_amount: u32, | ||
pub trial_interval: u32, | ||
pub trial_interval_unit: IntervalUnit, | ||
pub customer_permissions: Vec<CustomerPermission>, | ||
pub subscription_counts: SubscriptionCounts, | ||
|
||
#[serde(with = "time::serde::iso8601::option")] | ||
pub created_at: Option<OffsetDateTime> | ||
} | ||
|
||
impl Plan { | ||
pub fn create(client: &Client, params: CreatePlan<'_>) -> Response<Plan> { | ||
unpack_contained(client.post_form("/plans", ¶ms)) | ||
} | ||
|
||
pub fn list(client: &Client, page: Option<u32>, per_page: Option<u32>) -> Response<Page<Plan>> { | ||
let page = page.map(|s| s.to_string()); | ||
let per_page = per_page.map(|s| s.to_string()); | ||
let params = build_map([ | ||
("page", page.as_deref()), | ||
("per_page", per_page.as_deref()) | ||
]); | ||
client.get_query("/plans", ¶ms) | ||
} | ||
|
||
pub fn list_with_paginator(client: &Client, per_page: Option<u32>) -> Paginator<Result<Plan, PinError>> { | ||
paginate( | ||
move |page, per_page| { | ||
Plan::list(client, Some(page), Some(per_page)) | ||
}, | ||
per_page.unwrap_or(25) | ||
) | ||
} | ||
|
||
pub fn retrieve(client: &Client, token: &PlanId) -> Response<Plan> { | ||
unpack_contained(client.get(&format!("/plans/{}", token))) | ||
} | ||
|
||
pub fn delete(client: &Client, token: &PlanId) -> StatusOnlyResponse { | ||
client.delete_status_only(&format!("/plans/{}", token)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"response": { | ||
"name": "Coffee Plan", | ||
"amount": 1000, | ||
"currency": "USD", | ||
"setup_amount": 0, | ||
"trial_amount": 0, | ||
"interval": 30, | ||
"interval_unit": "day", | ||
"intervals": 6, | ||
"trial_interval": 7, | ||
"trial_interval_unit": "day", | ||
"created_at": "2023-12-28T05:13:07Z", | ||
"token": "plan_ZyDee4HNeUHFHC4SpM2idg", | ||
"customer_permissions": [ | ||
"cancel" | ||
], | ||
"subscription_counts": { | ||
"trial": 0, | ||
"active": 0, | ||
"cancelling": 0, | ||
"cancelled": 0 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"response": { | ||
"name": "Coffee Plan", | ||
"amount": 1000, | ||
"currency": "USD", | ||
"setup_amount": 0, | ||
"trial_amount": 0, | ||
"interval": 30, | ||
"interval_unit": "day", | ||
"intervals": 0, | ||
"trial_interval": 7, | ||
"trial_interval_unit": "day", | ||
"created_at": "2023-12-28T05:44:36Z", | ||
"token": "plan_ZyDee4HNeUHFHC4SpM2idg", | ||
"customer_permissions": [ | ||
"cancel" | ||
], | ||
"subscription_counts": { | ||
"trial": 0, | ||
"active": 0, | ||
"cancelling": 0, | ||
"cancelled": 0 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"response": [ | ||
{ | ||
"name": "Coffee Plan", | ||
"amount": 1000, | ||
"currency": "USD", | ||
"setup_amount": 0, | ||
"trial_amount": 0, | ||
"interval": 30, | ||
"interval_unit": "day", | ||
"intervals": 0, | ||
"trial_interval": 7, | ||
"trial_interval_unit": "day", | ||
"created_at": "2023-12-28T05:31:34Z", | ||
"token": "plan_ZyDee4HNeUHFHC4SpM2idg", | ||
"customer_permissions": [ | ||
"cancel" | ||
], | ||
"subscription_counts": { | ||
"trial": 0, | ||
"active": 0, | ||
"cancelling": 0, | ||
"cancelled": 0 | ||
} | ||
} | ||
], | ||
"pagination": { | ||
"count": 1, | ||
"per_page": 25, | ||
"current": 1 | ||
} | ||
} |
Oops, something went wrong.