Skip to content

Commit

Permalink
add subscription (#12)
Browse files Browse the repository at this point in the history
* add subscription

* subscription tests and fixtures
  • Loading branch information
montdidier authored Dec 28, 2023
1 parent 89b506a commit e93f5a0
Show file tree
Hide file tree
Showing 9 changed files with 442 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod bank_account;
mod recipient;
mod transfer;
mod plan;
mod subscription;

pub use currency::*;
pub use charge::*;
Expand All @@ -19,3 +20,4 @@ pub use bank_account::*;
pub use recipient::*;
pub use transfer::*;
pub use plan::*;
pub use subscription::*;
92 changes: 92 additions & 0 deletions src/resources/subscription.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use time::{OffsetDateTime};
use serde::{Deserialize, Serialize};

use crate::client::{Client, Response, StatusOnlyResponse};
use crate::error::PinError;
use crate::ids::{PlanId, CustomerId, SubscriptionId, CardId};
use crate::params::{unpack_contained, Page, Paginator, paginate};
use crate::resources::{Currency};
use crate::build_map;

#[derive(Debug, Default, Serialize)]
pub struct CreateSubscription {
pub plan_token: PlanId,
pub customer_token: CustomerId,

#[serde(skip_serializing_if = "Option::is_none")]
pub include_setup_fee: Option<bool>
}


#[derive(Debug, Default, Deserialize)]
pub struct Subscription {
pub token: SubscriptionId,
pub plan_token: PlanId,
pub customer_token: CustomerId,
pub card_token: CardId,
pub state: String,

#[serde(with = "time::serde::iso8601::option")]
pub next_billing_date: Option<OffsetDateTime>,

#[serde(with = "time::serde::iso8601::option")]
pub active_interval_started_at: Option<OffsetDateTime>,

#[serde(with = "time::serde::iso8601::option")]
pub active_interval_finishes_at: Option<OffsetDateTime>,

#[serde(with = "time::serde::iso8601::option")]
pub cancelled_at: Option<OffsetDateTime>,

#[serde(with = "time::serde::iso8601::option")]
pub created_at: Option<OffsetDateTime>
}

#[derive(Debug, Default, Deserialize)]
pub struct LedgerEntry {
pub r#type: String,
pub amount: i64,
pub currency: Currency,
pub annotation: String,

#[serde(with = "time::serde::iso8601::option")]
pub created_at: Option<OffsetDateTime>
}

impl Subscription {
pub fn create(client: &Client, params: CreateSubscription) -> Response<Subscription> {
unpack_contained(client.post_form("/subscriptions", &params))
}

pub fn list(client: &Client, page: Option<u32>, per_page: Option<u32>) -> Response<Page<Subscription>> {
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("/subscriptions", &params)
}

pub fn retrieve(client: &Client, token: &SubscriptionId) -> Response<Subscription> {
unpack_contained(client.get(&format!("/subscriptions/{}", token)))
}

pub fn delete(client: &Client, token: &SubscriptionId) -> Response<Subscription> {
unpack_contained(client.delete(&format!("/subscriptions/{}", token)))
}

pub fn reactivate(client: &Client, token: &SubscriptionId) -> Response<Subscription> {
unpack_contained(client.put(&format!("/subscriptions/{}/reactivate", token)))
}

pub fn list_ledger_entries(client: &Client, token: &SubscriptionId, page: Option<u32>, per_page: Option<u32>) -> Response<Page<LedgerEntry>> {
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(&format!("/subscriptions/{}/ledger", token), &params)
}
}
14 changes: 14 additions & 0 deletions tests/fixtures/create-subscription.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"response": {
"state": "active",
"next_billing_date": "2023-12-28T22:05:08Z",
"active_interval_started_at": "2023-12-28T22:05:08Z",
"active_interval_finishes_at": "2023-12-28T22:05:08Z",
"cancelled_at": null,
"created_at": "2023-12-28T22:05:08Z",
"token": "sub_bZWXhTzHooKpk9FZjQfzqQ",
"plan_token": "plan_ZyDee4HNeUHFHC4SpM2idg",
"customer_token": "cus_XZg1ULpWaROQCOT5PdwLkQ",
"card_token": "card_nytGw7koRg23EEp9NTmz9w"
}
}
14 changes: 14 additions & 0 deletions tests/fixtures/delete-subscription.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"response": {
"state": "cancelled",
"next_billing_date": null,
"active_interval_started_at": "2023-12-28T22:05:08Z",
"active_interval_finishes_at": "2023-12-28T22:05:08Z",
"cancelled_at": "2023-12-28T22:05:08Z",
"created_at": "2023-12-28T22:05:08Z",
"token": "sub_bZWXhTzHooKpk9FZjQfzqQ",
"plan_token": "plan_ZyDee4HNeUHFHC4SpM2idg",
"customer_token": "cus_XZg1ULpWaROQCOT5PdwLkQ",
"card_token": "card_nytGw7koRg23EEp9NTmz9w"
}
}
14 changes: 14 additions & 0 deletions tests/fixtures/get-subscription.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"response": {
"state": "active",
"next_billing_date": "2023-12-28T22:05:08Z",
"active_interval_started_at": "2023-12-28T22:05:08Z",
"active_interval_finishes_at": "2023-12-28T22:05:08Z",
"cancelled_at": null,
"created_at": "2023-12-28T22:05:08Z",
"token": "sub_bZWXhTzHooKpk9FZjQfzqQ",
"plan_token": "plan_ZyDee4HNeUHFHC4SpM2idg",
"customer_token": "cus_XZg1ULpWaROQCOT5PdwLkQ",
"card_token": "card_nytGw7koRg23EEp9NTmz9w"
}
}
22 changes: 22 additions & 0 deletions tests/fixtures/get-subscriptions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"response": [
{
"state": "active",
"next_billing_date": "2023-12-28T22:05:08Z",
"active_interval_started_at": "2023-12-28T22:05:08Z",
"active_interval_finishes_at": "2023-12-28T22:05:08Z",
"cancelled_at": null,
"created_at": "2023-12-28T22:05:08Z",
"token": "sub_bZWXhTzHooKpk9FZjQfzqQ",
"plan_token": "plan_ZyDee4HNeUHFHC4SpM2idg",
"customer_token": "cus_XZg1ULpWaROQCOT5PdwLkQ",
"card_token": "card_nytGw7koRg23EEp9NTmz9w"
}
],
"count": 1,
"pagination": {
"count": 1,
"per_page": 25,
"current": 1
}
}
17 changes: 17 additions & 0 deletions tests/fixtures/list-subscription-ledger-entries.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"response": [
{
"created_at": "2023-12-28T22:05:08Z",
"type": "credit",
"amount": 1000,
"currency": "AUD",
"annotation": "charge_credit"
}
],
"count": 1,
"pagination": {
"count": 1,
"per_page": 25,
"current": 1
}
}
14 changes: 14 additions & 0 deletions tests/fixtures/reactivate-subscription.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"response": {
"state": "active",
"next_billing_date": "2023-12-28T22:05:08Z",
"active_interval_started_at": "2023-12-28T22:05:08Z",
"active_interval_finishes_at": "2023-12-28T22:05:08Z",
"cancelled_at": null,
"created_at": "2023-12-28T22:05:08Z",
"token": "sub_bZWXhTzHooKpk9FZjQfzqQ",
"plan_token": "plan_ZyDee4HNeUHFHC4SpM2idg",
"customer_token": "cus_XZg1ULpWaROQCOT5PdwLkQ",
"card_token": "card_nytGw7koRg23EEp9NTmz9w"
}
}
Loading

0 comments on commit e93f5a0

Please sign in to comment.