Skip to content

Commit

Permalink
add plan (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
montdidier committed Dec 28, 2023
1 parent 580dddb commit 89b506a
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 1 deletion.
17 changes: 17 additions & 0 deletions examples/plan.rs
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:?}");
}
2 changes: 1 addition & 1 deletion src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub struct PaginationDetails {
pub previous: Option<u64>,
pub next: Option<u64>,
pub per_page: u32,
pub pages: u32,
pub pages: Option<u32>,
pub count: u64
}

Expand Down
2 changes: 2 additions & 0 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod balance;
mod bank_account;
mod recipient;
mod transfer;
mod plan;

pub use currency::*;
pub use charge::*;
Expand All @@ -17,3 +18,4 @@ pub use balance::*;
pub use bank_account::*;
pub use recipient::*;
pub use transfer::*;
pub use plan::*;
113 changes: 113 additions & 0 deletions src/resources/plan.rs
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", &params))
}

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", &params)
}

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))
}
}
25 changes: 25 additions & 0 deletions tests/fixtures/create-plan.json
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
}
}
}
25 changes: 25 additions & 0 deletions tests/fixtures/get-plan.json
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
}
}
}
32 changes: 32 additions & 0 deletions tests/fixtures/get-plans.json
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
}
}
Loading

0 comments on commit 89b506a

Please sign in to comment.