1use 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#[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 pub provider: String,
34 pub r#type: String,
35
36 #[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 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 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 pub order: String,
63 pub order_obj: Option<Order>,
64 pub out_order_id: String,
66 pub pay_url: String,
67 pub success_url: String,
69 pub state: String,
70 pub message: String,
71}
72
73impl Client {
74 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 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 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 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 pub async fn add_payment(&self, payment: &Payment) -> Result<bool> {
119 Ok(self.modify_payment("add-payment", payment, &[]).await?.1)
120 }
121
122 pub async fn update_payment(&self, payment: &Payment) -> Result<bool> {
124 Ok(self.modify_payment("update-payment", payment, &[]).await?.1)
125 }
126
127 pub async fn delete_payment(&self, payment: &Payment) -> Result<bool> {
129 Ok(self.modify_payment("delete-payment", payment, &[]).await?.1)
130 }
131
132 pub async fn notify_payment(&self, payment: &Payment) -> Result<bool> {
135 Ok(self.modify_payment("notify-payment", payment, &[]).await?.1)
136 }
137
138 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}