-
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.
* add subscription * subscription tests and fixtures
- Loading branch information
1 parent
89b506a
commit e93f5a0
Showing
9 changed files
with
442 additions
and
0 deletions.
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
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,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", ¶ms)) | ||
} | ||
|
||
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", ¶ms) | ||
} | ||
|
||
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), ¶ms) | ||
} | ||
} |
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,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" | ||
} | ||
} |
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,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" | ||
} | ||
} |
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,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" | ||
} | ||
} |
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,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 | ||
} | ||
} |
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 @@ | ||
{ | ||
"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 | ||
} | ||
} |
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,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" | ||
} | ||
} |
Oops, something went wrong.