Skip to main content

casdoor_rust_sdk/
payment.rs

1// Copyright 2026 The Casdoor Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use serde::{Deserialize, Serialize};
16
17use crate::client::{get_owner, Client, Response};
18use crate::error::Result;
19use crate::order::Order;
20use crate::serde_util::null_to_default;
21
22/// Payment has the same definition as
23/// <https://github.com/casdoor/casdoor/blob/master/object/payment.go>.
24#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
25#[serde(rename_all = "camelCase", default)]
26pub struct Payment {
27    pub owner: String,
28    pub name: String,
29    pub created_time: String,
30    pub display_name: String,
31
32    // Payment provider info.
33    pub provider: String,
34    pub r#type: String,
35
36    // Product info.
37    #[serde(deserialize_with = "null_to_default")]
38    pub products: Vec<String>,
39    pub products_display_name: String,
40    pub product_name: String,
41    pub product_display_name: String,
42    pub detail: String,
43    pub currency: String,
44    pub price: f64,
45
46    // Payer info.
47    pub user: String,
48    pub person_name: String,
49    pub person_id_card: String,
50    pub person_email: String,
51    pub person_phone: String,
52
53    // Invoice info.
54    pub invoice_type: String,
55    pub invoice_title: String,
56    pub invoice_tax_id: String,
57    pub invoice_remark: String,
58    pub invoice_url: String,
59
60    // Order info.
61    /// The internal order name.
62    pub order: String,
63    pub order_obj: Option<Order>,
64    /// The order ID of the external payment provider.
65    pub out_order_id: String,
66    pub pay_url: String,
67    /// `success_url` is redirected from `pay_url` after a successful payment.
68    pub success_url: String,
69    pub state: String,
70    pub message: String,
71}
72
73impl Client {
74    /// Get the payments of the client's organization.
75    pub async fn get_payments(&self) -> Result<Vec<Payment>> {
76        self.get_list("get-payments", &[("owner", &self.config.organization_name)])
77            .await
78    }
79
80    /// Get one page of the payments of the client's organization, together with the total
81    /// number of the payments.
82    pub async fn get_pagination_payments(
83        &self,
84        p: i32,
85        page_size: i32,
86        query: &[(&str, &str)],
87    ) -> Result<(Vec<Payment>, i32)> {
88        self.get_pagination(
89            "get-payments",
90            &self.config.organization_name,
91            p,
92            page_size,
93            query,
94        )
95        .await
96    }
97
98    /// Get one payment by name.
99    pub async fn get_payment(&self, name: &str) -> Result<Option<Payment>> {
100        self.get_object("get-payment", &[("id", &self.get_id(name))])
101            .await
102    }
103
104    /// Get the payments of one user.
105    pub async fn get_user_payments(&self, user_name: &str) -> Result<Vec<Payment>> {
106        self.get_list(
107            "get-user-payments",
108            &[
109                ("owner", &self.config.organization_name),
110                ("organization", &self.config.organization_name),
111                ("user", user_name),
112            ],
113        )
114        .await
115    }
116
117    /// Add a payment.
118    pub async fn add_payment(&self, payment: &Payment) -> Result<bool> {
119        Ok(self.modify_payment("add-payment", payment, &[]).await?.1)
120    }
121
122    /// Update a payment.
123    pub async fn update_payment(&self, payment: &Payment) -> Result<bool> {
124        Ok(self.modify_payment("update-payment", payment, &[]).await?.1)
125    }
126
127    /// Delete a payment.
128    pub async fn delete_payment(&self, payment: &Payment) -> Result<bool> {
129        Ok(self.modify_payment("delete-payment", payment, &[]).await?.1)
130    }
131
132    /// Notify Casdoor that a payment was made, so that it queries the payment provider and
133    /// updates the state of the payment.
134    pub async fn notify_payment(&self, payment: &Payment) -> Result<bool> {
135        Ok(self.modify_payment("notify-payment", payment, &[]).await?.1)
136    }
137
138    /// Ask the payment provider to issue the invoice of a payment.
139    pub async fn invoice_payment(&self, payment: &Payment) -> Result<bool> {
140        Ok(self
141            .modify_payment("invoice-payment", payment, &[])
142            .await?
143            .1)
144    }
145
146    async fn modify_payment(
147        &self,
148        action: &str,
149        payment: &Payment,
150        columns: &[&str],
151    ) -> Result<(Response, bool)> {
152        let mut payment = payment.clone();
153        payment.owner = get_owner(&payment.owner, &self.config.organization_name);
154
155        let id = format!("{}/{}", payment.owner, payment.name);
156        self.modify_object(action, &id, &payment, columns).await
157    }
158}