1use reqwest::Method;
2
3use crate::data::Payment;
4use crate::endpoint::Endpoint;
5
6#[derive(Clone, Debug)]
7pub enum GetPayment {
8 PaymentKey(String),
9 OrderId(String),
10}
11
12impl Endpoint for GetPayment {
13 type Query = ();
14 type Body = ();
15 type Response = Payment;
16
17 fn relative_path(&self) -> String {
18 match self {
19 Self::PaymentKey(payment_key) => format!("/v1/payments/{}", payment_key),
20 Self::OrderId(order_id) => format!("/v1/payments/orders/{}", order_id),
21 }
22 }
23
24 fn method(&self) -> Method {
25 Method::GET
26 }
27}
28
29#[cfg(test)]
30mod tests {
31 use httpmock::prelude::*;
32 use serde_json::json;
33
34 use crate::Client;
35
36 use super::*;
37
38 #[tokio::test]
39 async fn get_payment_by_payment_key() {
40 let dummy_payment = json!({
41 "mId": "tvivarepublica4",
42 "lastTransactionKey": "7F8E548429D327BFE5ACEAF508E8DC36",
43 "paymentKey": "Kl56WYb7w4vZnjEJeQVxn0ZyqmKWbD8PmOoBN0k12dzgRG9p",
44 "orderId": "14854200076307017",
45 "orderName": "www.hilti.co.kr",
46 "taxExemptionAmount": 0,
47 "status": "DONE",
48 "requestedAt": "2023-12-01T00:05:06+09:00",
49 "approvedAt": "2023-12-01T00:05:07+09:00",
50 "useEscrow": false,
51 "cultureExpense": false,
52 "card": {
53 "issuerCode": "4V",
54 "acquirerCode": "21",
55 "number": "40000000****109*",
56 "installmentPlanMonths": 0,
57 "isInterestFree": false,
58 "interestPayer": null,
59 "approveNo": "00000000",
60 "useCardPoint": false,
61 "cardType": "미확인",
62 "ownerType": "미확인",
63 "acquireStatus": "READY",
64 "amount": 163020
65 },
66 "virtualAccount": null,
67 "transfer": null,
68 "mobilePhone": null,
69 "giftCertificate": null,
70 "cashReceipt": null,
71 "cashReceipts": null,
72 "discount": null,
73 "cancels": null,
74 "secret": null,
75 "type": "NORMAL",
76 "easyPay": null,
77 "country": "KR",
78 "failure": null,
79 "isPartialCancelable": false,
80 "receipt": {
81 "url": "https://dashboard.tosspayments.com/receipt/redirection?transactionId=tviva20231201000506vM4Q2&ref=PX"
82 },
83 "checkout": {
84 "url": "https://api.tosspayments.com/v1/payments/Kl56WYb7w4vZnjEJeQVxn0ZyqmKWbD8PmOoBN0k12dzgRG9p/checkout"
85 },
86 "currency": "KRW",
87 "totalAmount": 163020,
88 "balanceAmount": 163020,
89 "suppliedAmount": 148200,
90 "vat": 14820,
91 "taxFreeAmount": 0,
92 "method": "카드",
93 "version": "2022-11-16"
94 });
95 let payment_key = "Kl56WYb7w4vZnjEJeQVxn0ZyqmKWbD8PmOoBN0k12dzgRG9p";
96 let server = MockServer::start();
97 let mock = server.mock(|when, then| {
98 when
99 .method(GET)
100 .path(format!("/v1/payments/{}", payment_key))
101 .header("authorization", "Basic bXlfc2VjcmV0X2tleTo");
102 then
103 .status(200)
104 .header("content-type", "application/json")
105 .json_body(dummy_payment);
106 });
107 let client = Client::from_url(server.base_url(), "my_secret_key");
108 let payment = client
109 .execute(&GetPayment::PaymentKey(payment_key.to_string()))
110 .await
111 .unwrap();
112 mock.assert();
113 assert_eq!(payment.payment_key, payment_key);
114 }
115
116 #[tokio::test]
117 async fn get_payment_by_order_id() {
118 let dummy_payment = json!({
119 "mId": "tvivarepublica4",
120 "lastTransactionKey": "7F8E548429D327BFE5ACEAF508E8DC36",
121 "paymentKey": "Kl56WYb7w4vZnjEJeQVxn0ZyqmKWbD8PmOoBN0k12dzgRG9p",
122 "orderId": "14854200076307017",
123 "orderName": "www.hilti.co.kr",
124 "taxExemptionAmount": 0,
125 "status": "DONE",
126 "requestedAt": "2023-12-01T00:05:06+09:00",
127 "approvedAt": "2023-12-01T00:05:07+09:00",
128 "useEscrow": false,
129 "cultureExpense": false,
130 "card": {
131 "issuerCode": "4V",
132 "acquirerCode": "21",
133 "number": "40000000****109*",
134 "installmentPlanMonths": 0,
135 "isInterestFree": false,
136 "interestPayer": null,
137 "approveNo": "00000000",
138 "useCardPoint": false,
139 "cardType": "미확인",
140 "ownerType": "미확인",
141 "acquireStatus": "READY",
142 "amount": 163020
143 },
144 "virtualAccount": null,
145 "transfer": null,
146 "mobilePhone": null,
147 "giftCertificate": null,
148 "cashReceipt": null,
149 "cashReceipts": null,
150 "discount": null,
151 "cancels": null,
152 "secret": null,
153 "type": "NORMAL",
154 "easyPay": null,
155 "country": "KR",
156 "failure": null,
157 "isPartialCancelable": false,
158 "receipt": {
159 "url": "https://dashboard.tosspayments.com/receipt/redirection?transactionId=tviva20231201000506vM4Q2&ref=PX"
160 },
161 "checkout": {
162 "url": "https://api.tosspayments.com/v1/payments/Kl56WYb7w4vZnjEJeQVxn0ZyqmKWbD8PmOoBN0k12dzgRG9p/checkout"
163 },
164 "currency": "KRW",
165 "totalAmount": 163020,
166 "balanceAmount": 163020,
167 "suppliedAmount": 148200,
168 "vat": 14820,
169 "taxFreeAmount": 0,
170 "method": "카드",
171 "version": "2022-11-16"
172 });
173 let order_id = "14854200076307017";
174 let server = MockServer::start();
175 let mock = server.mock(|when, then| {
176 when
177 .method(GET)
178 .path(format!("/v1/payments/orders/{}", order_id))
179 .header("authorization", "Basic bXlfc2VjcmV0X2tleTo");
180 then
181 .status(200)
182 .header("content-type", "application/json")
183 .json_body(dummy_payment);
184 });
185 let client = Client::from_url(server.base_url(), "my_secret_key");
186 let payment = client
187 .execute(&GetPayment::OrderId(order_id.to_string()))
188 .await
189 .unwrap();
190 mock.assert();
191 assert_eq!(payment.order_id, order_id);
192 }
193}