payjp_core/subscription/
requests.rs

1use payjp_client_core::{PayjpClient, BlockingClient, PayjpRequest, RequestBuilder, PayjpMethod};
2
3#[derive(Clone,Debug,)]#[derive(serde::Serialize)]
4 struct ListSubscriptionBuilder {
5#[serde(skip_serializing_if = "Option::is_none")]
6 limit: Option<i64>,
7#[serde(skip_serializing_if = "Option::is_none")]
8 offset: Option<i64>,
9#[serde(skip_serializing_if = "Option::is_none")]
10 since: Option<i64>,
11#[serde(skip_serializing_if = "Option::is_none")]
12 until: Option<i64>,
13#[serde(skip_serializing_if = "Option::is_none")]
14 plan: Option<String>,
15#[serde(skip_serializing_if = "Option::is_none")]
16 customer: Option<String>,
17#[serde(skip_serializing_if = "Option::is_none")]
18 status: Option<ListSubscriptionStatus>,
19
20}
21impl ListSubscriptionBuilder {
22     fn new() -> Self {
23    Self {
24        limit: None,offset: None,since: None,until: None,plan: None,customer: None,status: None,
25    }
26}
27
28}
29    /// 定期課金ステータス。`active`, `trial`, `canceled` または `paused` のみ指定可能。.
30#[derive(Copy,Clone,Eq, PartialEq,)]pub enum ListSubscriptionStatus {
31Active,
32Trial,
33Canceled,
34Paused,
35
36}
37impl ListSubscriptionStatus {
38    pub fn as_str(self) -> &'static str {
39        use ListSubscriptionStatus::*;
40        match self {
41Active => "active",
42Trial => "trial",
43Canceled => "canceled",
44Paused => "paused",
45
46        }
47    }
48}
49
50impl std::str::FromStr for ListSubscriptionStatus {
51    type Err = payjp_types::ParseError;
52    fn from_str(s: &str) -> Result<Self, Self::Err> {
53        use ListSubscriptionStatus::*;
54        match s {
55    "active" => Ok(Active),
56"trial" => Ok(Trial),
57"canceled" => Ok(Canceled),
58"paused" => Ok(Paused),
59_ => Err(payjp_types::ParseError)
60
61        }
62    }
63}
64impl std::fmt::Display for ListSubscriptionStatus {
65    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
66        f.write_str(self.as_str())
67    }
68}
69
70impl std::fmt::Debug for ListSubscriptionStatus {
71    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
72        f.write_str(self.as_str())
73    }
74}
75impl serde::Serialize for ListSubscriptionStatus {
76    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
77        serializer.serialize_str(self.as_str())
78    }
79}
80#[cfg(feature = "deserialize")]
81impl<'de> serde::Deserialize<'de> for ListSubscriptionStatus {
82    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
83        use std::str::FromStr;
84        let s: std::borrow::Cow<'de, str> = serde::Deserialize::deserialize(deserializer)?;
85        Self::from_str(&s).map_err(|_| serde::de::Error::custom("Unknown value for ListSubscriptionStatus"))
86    }
87}
88    /// 生成した定期課金のリストを取得します。
89    ///
90    /// リストは、直近で生成された順番に取得されます。
91#[derive(Clone,Debug,)]#[derive(serde::Serialize)]
92pub struct ListSubscription {
93 inner: ListSubscriptionBuilder,
94
95}
96impl ListSubscription {
97    /// Construct a new `ListSubscription`.
98pub fn new() -> Self {
99    Self {
100        inner: ListSubscriptionBuilder::new()
101    }
102}
103    /// 取得するデータ数の最大値(1~100まで)。指定がない場合は 10 となる。
104pub fn limit(mut self, limit: impl Into<i64>) -> Self {
105    self.inner.limit = Some(limit.into());
106    self
107}
108    /// 基準点からのデータ取得を行う開始位置。指定がない場合は 0 となる。
109pub fn offset(mut self, offset: impl Into<i64>) -> Self {
110    self.inner.offset = Some(offset.into());
111    self
112}
113    /// ここに指定したタイムスタンプ以降に作成されたデータを取得
114pub fn since(mut self, since: impl Into<i64>) -> Self {
115    self.inner.since = Some(since.into());
116    self
117}
118    /// ここに指定したタイムスタンプ以前に作成されたデータを取得
119pub fn until(mut self, until: impl Into<i64>) -> Self {
120    self.inner.until = Some(until.into());
121    self
122}
123    /// 絞り込みたいプランID
124pub fn plan(mut self, plan: impl Into<String>) -> Self {
125    self.inner.plan = Some(plan.into());
126    self
127}
128    /// 絞り込みたい顧客ID
129pub fn customer(mut self, customer: impl Into<String>) -> Self {
130    self.inner.customer = Some(customer.into());
131    self
132}
133        /// 定期課金ステータス。`active`, `trial`, `canceled` または `paused` のみ指定可能。.
134pub fn status(mut self, status: impl Into<ListSubscriptionStatus>) -> Self {
135    self.inner.status = Some(status.into());
136    self
137}
138
139}
140    impl Default for ListSubscription {
141    fn default() -> Self {
142        Self::new()
143    }
144}impl ListSubscription {
145    /// Send the request and return the deserialized response.
146    pub async fn send<C: PayjpClient>(&self, client: &C) -> Result<<Self as PayjpRequest>::Output, C::Err> {
147        self.customize().send(client).await
148    }
149
150    /// Send the request and return the deserialized response, blocking until completion.
151    pub fn send_blocking<C: BlockingClient>(&self, client: &C) -> Result<<Self as PayjpRequest>::Output, C::Err> {
152        self.customize().send_blocking(client)
153    }
154
155    pub fn paginate(&self) -> payjp_client_core::ListPaginator<payjp_types::List<payjp_core::Subscription>> {
156    
157    payjp_client_core::ListPaginator::new_list("/subscriptions", &self.inner)
158}
159
160}
161
162impl PayjpRequest for ListSubscription {
163    type Output = payjp_types::List<payjp_core::Subscription>;
164
165    fn build(&self) -> RequestBuilder {
166    RequestBuilder::new(PayjpMethod::Get, "/subscriptions").query(&self.inner)
167}
168
169}
170#[derive(Clone,Debug,)]#[derive(serde::Serialize)]
171 struct CreateSubscriptionBuilder {
172 customer: String,
173#[serde(skip_serializing_if = "Option::is_none")]
174 metadata: Option<payjp_shared::Metadata>,
175 plan: String,
176#[serde(skip_serializing_if = "Option::is_none")]
177 prorate: Option<bool>,
178#[serde(skip_serializing_if = "Option::is_none")]
179 trial_end: Option<CreateSubscriptionTrialEnd>,
180
181}
182impl CreateSubscriptionBuilder {
183     fn new(customer: impl Into<String>,plan: impl Into<String>,) -> Self {
184    Self {
185        customer: customer.into(),metadata: None,plan: plan.into(),prorate: None,trial_end: None,
186    }
187}
188
189}
190    /// リクエスト時より未来のタイムスタンプ(5年後まで) or 文字列 `now` が指定可能です。.
191#[derive(Clone,Debug,)]#[derive(serde::Serialize)]
192#[serde(rename_all = "snake_case")]
193pub enum CreateSubscriptionTrialEnd {
194#[serde(untagged)]
195I64(i64),
196#[serde(untagged)]
197String(String),
198
199}
200    /// 顧客IDとプランIDを指定して、定期課金を開始します。
201    ///
202        /// 前払い式のため、定期課金作成時に最初の課金が実行されます。但し以下の場合には作成時の課金はされません。.
203    ///
204    /// - トライアル状態(`status=trial`)で作成された場合
205        /// - プランの課金日(`billing_day`)が指定され、定期課金の日割り設定(`prorate`)が設定されていない場合.
206#[derive(Clone,Debug,)]#[derive(serde::Serialize)]
207pub struct CreateSubscription {
208 inner: CreateSubscriptionBuilder,
209
210}
211impl CreateSubscription {
212    /// Construct a new `CreateSubscription`.
213pub fn new(customer:impl Into<String>,plan:impl Into<String>) -> Self {
214    Self {
215        inner: CreateSubscriptionBuilder::new(customer.into(),plan.into(),)
216    }
217}
218pub fn metadata(mut self, metadata: impl Into<payjp_shared::Metadata>) -> Self {
219    self.inner.metadata = Some(metadata.into());
220    self
221}
222    /// `true`の場合、日割り課金を設定します
223pub fn prorate(mut self, prorate: impl Into<bool>) -> Self {
224    self.inner.prorate = Some(prorate.into());
225    self
226}
227        /// リクエスト時より未来のタイムスタンプ(5年後まで) or 文字列 `now` が指定可能です。.
228pub fn trial_end(mut self, trial_end: impl Into<CreateSubscriptionTrialEnd>) -> Self {
229    self.inner.trial_end = Some(trial_end.into());
230    self
231}
232
233}
234    impl CreateSubscription {
235    /// Send the request and return the deserialized response.
236    pub async fn send<C: PayjpClient>(&self, client: &C) -> Result<<Self as PayjpRequest>::Output, C::Err> {
237        self.customize().send(client).await
238    }
239
240    /// Send the request and return the deserialized response, blocking until completion.
241    pub fn send_blocking<C: BlockingClient>(&self, client: &C) -> Result<<Self as PayjpRequest>::Output, C::Err> {
242        self.customize().send_blocking(client)
243    }
244
245    
246}
247
248impl PayjpRequest for CreateSubscription {
249    type Output = payjp_core::Subscription;
250
251    fn build(&self) -> RequestBuilder {
252    RequestBuilder::new(PayjpMethod::Post, "/subscriptions").form(&self.inner)
253}
254
255}
256    /// 指定されたIDの定期課金情報を取得します。
257#[derive(Clone,Debug,)]#[derive(serde::Serialize)]
258pub struct RetrieveSubscription {
259 subscription: String,
260
261}
262impl RetrieveSubscription {
263    /// Construct a new `RetrieveSubscription`.
264pub fn new(subscription:impl Into<String>) -> Self {
265    Self {
266        subscription: subscription.into(),
267    }
268}
269
270}
271    impl RetrieveSubscription {
272    /// Send the request and return the deserialized response.
273    pub async fn send<C: PayjpClient>(&self, client: &C) -> Result<<Self as PayjpRequest>::Output, C::Err> {
274        self.customize().send(client).await
275    }
276
277    /// Send the request and return the deserialized response, blocking until completion.
278    pub fn send_blocking<C: BlockingClient>(&self, client: &C) -> Result<<Self as PayjpRequest>::Output, C::Err> {
279        self.customize().send_blocking(client)
280    }
281
282    
283}
284
285impl PayjpRequest for RetrieveSubscription {
286    type Output = payjp_core::Subscription;
287
288    fn build(&self) -> RequestBuilder {
289    let subscription = &self.subscription;
290RequestBuilder::new(PayjpMethod::Get, format!("/subscriptions/{subscription}"))
291}
292
293}
294#[derive(Clone,Debug,)]#[derive(serde::Serialize)]
295 struct UpdateSubscriptionBuilder {
296#[serde(skip_serializing_if = "Option::is_none")]
297 metadata: Option<payjp_shared::Metadata>,
298#[serde(skip_serializing_if = "Option::is_none")]
299 next_cycle_plan: Option<String>,
300#[serde(skip_serializing_if = "Option::is_none")]
301 plan: Option<String>,
302#[serde(skip_serializing_if = "Option::is_none")]
303 prorate: Option<bool>,
304#[serde(skip_serializing_if = "Option::is_none")]
305 trial_end: Option<UpdateSubscriptionTrialEnd>,
306
307}
308impl UpdateSubscriptionBuilder {
309     fn new() -> Self {
310    Self {
311        metadata: None,next_cycle_plan: None,plan: None,prorate: None,trial_end: None,
312    }
313}
314
315}
316    /// リクエスト時より未来のタイムスタンプ(5年後まで) or 文字列 `now` が指定可能です。.
317#[derive(Clone,Debug,)]#[derive(serde::Serialize)]
318#[serde(rename_all = "snake_case")]
319pub enum UpdateSubscriptionTrialEnd {
320#[serde(untagged)]
321I64(i64),
322#[serde(untagged)]
323String(String),
324
325}
326        /// トライアル期間を新たに設定したり、プランの変更を行うことができます。.
327#[derive(Clone,Debug,)]#[derive(serde::Serialize)]
328pub struct UpdateSubscription {
329 inner: UpdateSubscriptionBuilder,
330 subscription: String,
331
332}
333impl UpdateSubscription {
334    /// Construct a new `UpdateSubscription`.
335pub fn new(subscription:impl Into<String>) -> Self {
336    Self {
337        subscription: subscription.into(),inner: UpdateSubscriptionBuilder::new()
338    }
339}
340pub fn metadata(mut self, metadata: impl Into<payjp_shared::Metadata>) -> Self {
341    self.inner.metadata = Some(metadata.into());
342    self
343}
344        /// 次回サイクル更新時に指定のプランへと自動的に切り替えを行い課金を試みます。.
345pub fn next_cycle_plan(mut self, next_cycle_plan: impl Into<String>) -> Self {
346    self.inner.next_cycle_plan = Some(next_cycle_plan.into());
347    self
348}
349    /// 新しいプランのID
350pub fn plan(mut self, plan: impl Into<String>) -> Self {
351    self.inner.plan = Some(plan.into());
352    self
353}
354    /// `true`の場合、日割り課金を設定します
355pub fn prorate(mut self, prorate: impl Into<bool>) -> Self {
356    self.inner.prorate = Some(prorate.into());
357    self
358}
359        /// リクエスト時より未来のタイムスタンプ(5年後まで) or 文字列 `now` が指定可能です。.
360pub fn trial_end(mut self, trial_end: impl Into<UpdateSubscriptionTrialEnd>) -> Self {
361    self.inner.trial_end = Some(trial_end.into());
362    self
363}
364
365}
366    impl UpdateSubscription {
367    /// Send the request and return the deserialized response.
368    pub async fn send<C: PayjpClient>(&self, client: &C) -> Result<<Self as PayjpRequest>::Output, C::Err> {
369        self.customize().send(client).await
370    }
371
372    /// Send the request and return the deserialized response, blocking until completion.
373    pub fn send_blocking<C: BlockingClient>(&self, client: &C) -> Result<<Self as PayjpRequest>::Output, C::Err> {
374        self.customize().send_blocking(client)
375    }
376
377    
378}
379
380impl PayjpRequest for UpdateSubscription {
381    type Output = payjp_core::Subscription;
382
383    fn build(&self) -> RequestBuilder {
384    let subscription = &self.subscription;
385RequestBuilder::new(PayjpMethod::Post, format!("/subscriptions/{subscription}")).form(&self.inner)
386}
387
388}
389#[derive(Copy,Clone,Debug,)]#[derive(serde::Serialize)]
390 struct DeleteSubscriptionBuilder {
391#[serde(skip_serializing_if = "Option::is_none")]
392 prorate: Option<bool>,
393
394}
395impl DeleteSubscriptionBuilder {
396     fn new() -> Self {
397    Self {
398        prorate: None,
399    }
400}
401
402}
403        /// 定期課金をすぐに削除します。
404        /// 次回以降の課金は行われず、一度削除した定期課金は再び戻すことができません。.
405#[derive(Clone,Debug,)]#[derive(serde::Serialize)]
406pub struct DeleteSubscription {
407 inner: DeleteSubscriptionBuilder,
408 subscription: String,
409
410}
411impl DeleteSubscription {
412    /// Construct a new `DeleteSubscription`.
413pub fn new(subscription:impl Into<String>) -> Self {
414    Self {
415        subscription: subscription.into(),inner: DeleteSubscriptionBuilder::new()
416    }
417}
418        /// `true`の場合、削除時から現在の周期の終了日までの日割り分を算出し、返金処理を行います。.
419pub fn prorate(mut self, prorate: impl Into<bool>) -> Self {
420    self.inner.prorate = Some(prorate.into());
421    self
422}
423
424}
425    impl DeleteSubscription {
426    /// Send the request and return the deserialized response.
427    pub async fn send<C: PayjpClient>(&self, client: &C) -> Result<<Self as PayjpRequest>::Output, C::Err> {
428        self.customize().send(client).await
429    }
430
431    /// Send the request and return the deserialized response, blocking until completion.
432    pub fn send_blocking<C: BlockingClient>(&self, client: &C) -> Result<<Self as PayjpRequest>::Output, C::Err> {
433        self.customize().send_blocking(client)
434    }
435
436    
437}
438
439impl PayjpRequest for DeleteSubscription {
440    type Output = payjp_shared::DeleteResponse;
441
442    fn build(&self) -> RequestBuilder {
443    let subscription = &self.subscription;
444RequestBuilder::new(PayjpMethod::Delete, format!("/subscriptions/{subscription}")).form(&self.inner)
445}
446
447}
448        /// 引き落としの失敗やカードが不正である、また定期課金を停止したい場合はこのリクエストで定期購入を停止させます。.
449    ///
450        /// 定期課金を停止させると、再開されるまで引き落とし処理は一切行われません。.
451#[derive(Clone,Debug,)]#[derive(serde::Serialize)]
452pub struct PauseSubscription {
453 subscription: String,
454
455}
456impl PauseSubscription {
457    /// Construct a new `PauseSubscription`.
458pub fn new(subscription:impl Into<String>) -> Self {
459    Self {
460        subscription: subscription.into(),
461    }
462}
463
464}
465    impl PauseSubscription {
466    /// Send the request and return the deserialized response.
467    pub async fn send<C: PayjpClient>(&self, client: &C) -> Result<<Self as PayjpRequest>::Output, C::Err> {
468        self.customize().send(client).await
469    }
470
471    /// Send the request and return the deserialized response, blocking until completion.
472    pub fn send_blocking<C: BlockingClient>(&self, client: &C) -> Result<<Self as PayjpRequest>::Output, C::Err> {
473        self.customize().send_blocking(client)
474    }
475
476    
477}
478
479impl PayjpRequest for PauseSubscription {
480    type Output = payjp_core::Subscription;
481
482    fn build(&self) -> RequestBuilder {
483    let subscription = &self.subscription;
484RequestBuilder::new(PayjpMethod::Post, format!("/subscriptions/{subscription}/pause"))
485}
486
487}
488#[derive(Clone,Debug,)]#[derive(serde::Serialize)]
489 struct ResumeSubscriptionBuilder {
490#[serde(skip_serializing_if = "Option::is_none")]
491 prorate: Option<bool>,
492#[serde(skip_serializing_if = "Option::is_none")]
493 trial_end: Option<ResumeSubscriptionTrialEnd>,
494
495}
496impl ResumeSubscriptionBuilder {
497     fn new() -> Self {
498    Self {
499        prorate: None,trial_end: None,
500    }
501}
502
503}
504    /// リクエスト時より未来のタイムスタンプ(5年後まで) or 文字列 `now` が指定可能です。.
505#[derive(Clone,Debug,)]#[derive(serde::Serialize)]
506#[serde(rename_all = "snake_case")]
507pub enum ResumeSubscriptionTrialEnd {
508#[serde(untagged)]
509I64(i64),
510#[serde(untagged)]
511String(String),
512
513}
514        /// 停止もしくはキャンセル状態(`status=canceled or paused`)の定期課金を再開させます。.
515    ///
516    /// トライアル期間中であればトライアル状態(`status=trial`)で再開します。
517    ///
518        /// 再開時の `current_period_end` が過去の日時の場合、トライアル期間内でなければ支払いが行われ、その時点が周期の開始として設定されます。.
519        /// 支払いの失敗により停止していた場合などは、 `current_period_end` は支払い失敗時の値になるため、必ず過去の日時がセットされます。.
520    ///
521    /// 再開時の支払いに失敗すると、定期課金は再開されません。
522        /// この場合は、有効なカードを顧客のデフォルトカードにセットしてから、再度定期課金の再開を行ってください。.
523#[derive(Clone,Debug,)]#[derive(serde::Serialize)]
524pub struct ResumeSubscription {
525 inner: ResumeSubscriptionBuilder,
526 subscription: String,
527
528}
529impl ResumeSubscription {
530    /// Construct a new `ResumeSubscription`.
531pub fn new(subscription:impl Into<String>) -> Self {
532    Self {
533        subscription: subscription.into(),inner: ResumeSubscriptionBuilder::new()
534    }
535}
536    /// `true`の場合、日割り課金を設定します
537pub fn prorate(mut self, prorate: impl Into<bool>) -> Self {
538    self.inner.prorate = Some(prorate.into());
539    self
540}
541        /// リクエスト時より未来のタイムスタンプ(5年後まで) or 文字列 `now` が指定可能です。.
542pub fn trial_end(mut self, trial_end: impl Into<ResumeSubscriptionTrialEnd>) -> Self {
543    self.inner.trial_end = Some(trial_end.into());
544    self
545}
546
547}
548    impl ResumeSubscription {
549    /// Send the request and return the deserialized response.
550    pub async fn send<C: PayjpClient>(&self, client: &C) -> Result<<Self as PayjpRequest>::Output, C::Err> {
551        self.customize().send(client).await
552    }
553
554    /// Send the request and return the deserialized response, blocking until completion.
555    pub fn send_blocking<C: BlockingClient>(&self, client: &C) -> Result<<Self as PayjpRequest>::Output, C::Err> {
556        self.customize().send_blocking(client)
557    }
558
559    
560}
561
562impl PayjpRequest for ResumeSubscription {
563    type Output = payjp_core::Subscription;
564
565    fn build(&self) -> RequestBuilder {
566    let subscription = &self.subscription;
567RequestBuilder::new(PayjpMethod::Post, format!("/subscriptions/{subscription}/resume")).form(&self.inner)
568}
569
570}
571        /// 定期課金をキャンセルし、現在の周期の終了日をもって定期課金を終了させます。.
572    ///
573        /// 終了日以前であれば、定期課金の再開リクエスト(/resume)を行うことで、キャンセルを取り消すことができます。.
574    /// 終了日をむかえた定期課金は自動的に削除されますのでご注意ください。
575#[derive(Clone,Debug,)]#[derive(serde::Serialize)]
576pub struct CancelSubscription {
577 subscription: String,
578
579}
580impl CancelSubscription {
581    /// Construct a new `CancelSubscription`.
582pub fn new(subscription:impl Into<String>) -> Self {
583    Self {
584        subscription: subscription.into(),
585    }
586}
587
588}
589    impl CancelSubscription {
590    /// Send the request and return the deserialized response.
591    pub async fn send<C: PayjpClient>(&self, client: &C) -> Result<<Self as PayjpRequest>::Output, C::Err> {
592        self.customize().send(client).await
593    }
594
595    /// Send the request and return the deserialized response, blocking until completion.
596    pub fn send_blocking<C: BlockingClient>(&self, client: &C) -> Result<<Self as PayjpRequest>::Output, C::Err> {
597        self.customize().send_blocking(client)
598    }
599
600    
601}
602
603impl PayjpRequest for CancelSubscription {
604    type Output = payjp_core::Subscription;
605
606    fn build(&self) -> RequestBuilder {
607    let subscription = &self.subscription;
608RequestBuilder::new(PayjpMethod::Post, format!("/subscriptions/{subscription}/cancel"))
609}
610
611}
612