1use br_reqwest::Client;
2use std::collections::{HashMap};
3use base64::{Engine};
4use base64::engine::general_purpose::STANDARD;
5use chrono::{Local};
6use crate::{PayMode, PayNotify, RefundNotify, RefundStatus, TradeState, TradeType, Types};
7use json::{array, object, JsonValue};
8use openssl::hash::MessageDigest;
9use openssl::pkey::{PKey};
10use openssl::rsa::Rsa;
11use openssl::sign::{Signer};
12use urlencoding::{encode};
13
14#[derive(Clone, Debug)]
15pub struct AliPay {
16 pub appid: String,
18 pub sp_mchid: String,
20 pub app_auth_token: String,
22 pub app_private: String,
24 pub content_encryp: String,
26 pub alipay_public_key: String,
28 pub notify_url: String,
29}
30impl AliPay {
31 pub fn sign(&mut self, txt: &str) -> Result<JsonValue, String> {
32 let t = self.app_private.as_bytes().chunks(64).map(|chunk| std::str::from_utf8(chunk).unwrap_or("")).collect::<Vec<&str>>().join("\n");
33 if t.is_empty() {
34 return Err("No pem".to_string());
35 }
36 let cart = format!("-----BEGIN PRIVATE KEY-----\n{t}\n-----END PRIVATE KEY-----\n");
37
38 let rsa = match Rsa::private_key_from_pem(cart.as_bytes()) {
40 Ok(e) => e,
41 Err(e) => return Err(e.to_string())
42 };
43
44 let pkey = match PKey::from_rsa(rsa) {
45 Ok(e) => e,
46 Err(e) => return Err(e.to_string())
47 };
48
49 let mut signer = match Signer::new(MessageDigest::sha256(), &pkey) {
51 Ok(e) => e,
52 Err(e) => return Err(e.to_string())
53 };
54 match signer.update(txt.as_bytes()) {
55 Ok(()) => {}
56 Err(e) => return Err(e.to_string())
57 };
58 let signature = match signer.sign_to_vec() {
60 Ok(e) => e,
61 Err(e) => return Err(e.to_string())
62 };
63 Ok(STANDARD.encode(signature).into())
65 }
66 pub fn http(&mut self, method: &str, biz_content: JsonValue) -> Result<JsonValue, String> {
67 let mut http = Client::new();
68 let sign = "";
69
70 let now = Local::now();
71 let timestamp = now.format("%Y-%m-%d %H:%M:%S").to_string();
72
73 let mut data = object! {
74 "charset":"UTF-8",
75 "method":method,
76 "app_id":self.appid.clone(),
77 "app_private_key":self.app_private.clone(),
78 "version":"1.0",
79 "sign_type":"RSA2",
80 "timestamp":timestamp,
81 "alipay_public_key":self.alipay_public_key.clone(),
82 "sign":sign
83 };
84 if !self.app_auth_token.is_empty() {
85 data["app_auth_token"] = self.app_auth_token.clone().into();
86 }
87 if method.contains("alipay.trade.") {
88 data["notify_url"] = self.notify_url.clone().into();
89 }
90 for (key, value) in biz_content.entries() {
91 data[key] = value.clone()
92 }
93 let mut map = HashMap::new();
94 for (key, value) in data.entries() {
95 if key == "sign" {
96 continue;
97 }
98 if value.is_empty() {
99 continue;
100 }
101 map.insert(key, value);
102 }
103
104 let mut keys: Vec<_> = map.keys().cloned().collect();
105 keys.sort();
106 let mut txt = vec![];
107 for key in keys {
108 txt.push(format!("{}={}", key, map.get(&key).unwrap()));
109 }
110 let txt = txt.join("&");
111 data["sign"] = self.sign(&txt)?;
112
113 let mut new_data = object! {};
114 for (key, value) in data.entries() {
115 let t = encode(value.to_string().as_str()).to_string();
116 new_data[key] = t.into();
117 }
118 data = new_data;
119 let url = "https://openapi.alipay.com/gateway.do".to_string();
120 let res = match method {
121 "alipay.trade.wap.pay" => {
122 let tt = http.get(url.as_str()).query(data);
123 return Ok(tt.url.clone().into());
124 }
125 _ => {
126 match http.get(&url).query(data).form_data(biz_content).send() {
127 Ok(e) => e,
128 Err(e) => return Err(e.to_string())
129 }
130 }
131 };
132
133 let res = res.json()?;
134 if res.has_key("error_response") {
135 return Err(res["error_response"]["sub_msg"].to_string());
136 }
137 let key = method.replace(".", "_");
138 let key = format!("{key}_response");
139 let data = res[key].clone();
140 if data.has_key("code") {
141 if data["code"] != "10000" {
142 Err(data["sub_msg"].to_string())
143 } else {
144 Ok(data)
145 }
146 } else {
147 Err(data.to_string())
148 }
149 }
150 pub fn https(&mut self, method: &str, biz_content: JsonValue) -> Result<JsonValue, String> {
151 let mut http = Client::new();
152 let sign = "";
154
155 let now = Local::now();
156 let timestamp = now.format("%Y-%m-%d %H:%M:%S").to_string();
157
158 let mut data = object! {
159 "charset":"UTF-8",
160 "method":method,
161 "app_id":self.appid.clone(),
162 "app_private_key":self.app_private.clone(),
163 "version":"1.0",
164 "sign_type":"RSA2",
165 "timestamp":timestamp,
166 "alipay_public_key":self.alipay_public_key.clone(),
167 "sign":sign
168 };
169 if !self.app_auth_token.is_empty() {
170 data["app_auth_token"] = self.app_auth_token.clone().into();
171 }
172 if method.contains("alipay.trade.") {
173 data["notify_url"] = self.notify_url.clone().into();
174 }
175 for (key, value) in biz_content.entries() {
176 data[key] = value.clone()
177 }
178 let mut map = HashMap::new();
179 for (key, value) in data.entries() {
180 if key == "sign" {
181 continue;
182 }
183 if value.is_empty() {
184 continue;
185 }
186 map.insert(key, value);
187 }
188
189 let mut keys: Vec<_> = map.keys().cloned().collect();
190 keys.sort();
191 let mut txt = vec![];
192 for key in keys {
193 txt.push(format!("{}={}", key, map.get(&key).unwrap()));
194 }
195 let txt = txt.join("&");
196 data["sign"] = self.sign(&txt)?;
197
198 let mut new_data = object! {};
199 for (key, value) in data.entries() {
200 let t = encode(value.to_string().as_str()).to_string();
201 new_data[key] = t.into();
202 }
203 data = new_data;
204 let url = "https://openapi.alipay.com/gateway.do".to_string();
205 let res = match method {
206 "alipay.trade.wap.pay" => {
207 let tt = http.get(url.as_str()).query(data);
208 return Ok(tt.url.clone().into());
209 }
210 _ => {
211 match http.get(&url).query(data).form_data(biz_content).send() {
212 Ok(e) => e,
213 Err(e) => return Err(e.to_string())
214 }
215 }
216 };
217
218 let res = res.json()?;
219 if res.has_key("error_response") {
220 return Err(res["error_response"]["sub_msg"].to_string());
221 }
222 let key = method.replace(".", "_");
223 let key = format!("{key}_response");
224 let data = res[key].clone();
225 Ok(data)
226 }
227}
228impl PayMode for AliPay {
229 fn check(&mut self) -> Result<bool, String> {
230 let biz_content = object! {
231 "biz_content":{
232 "grant_type":"authorization_code",
233 "code":"123456"
234 }
235 };
236 match self.http("alipay.open.auth.token.app", biz_content) {
237 Ok(e) => e,
238 Err(e) => {
239 if e.contains("auth_code不存在") {
240 return Ok(true);
241 }
242 return Err(e);
243 }
244 };
245 Ok(true)
246 }
247
248 fn get_sub_mchid(&mut self, sub_mchid: &str) -> Result<JsonValue, String> {
249 let res = self.https("alipay.open.agent.signstatus.query", object! {
250 "biz_content":{
251 "pid":sub_mchid,
252 "product_codes":array!["QUICK_WAP_WAY"]
253 }
254 })?;
255 if !res["code"].eq("10000") {
256 return Err(res["msg"].to_string());
257 }
258 for item in res["sign_status_list"].members() {
259 if item["status"].eq("none") {
260 return Err(format!("{} 未开通", res["product_name"]));
261 }
262 }
263 Ok(true.into())
264 }
265
266
267 fn config(&mut self) -> JsonValue {
268 todo!()
269 }
270
271
272 fn pay(&mut self, _channel: &str, types: Types, sub_mchid: &str, out_trade_no: &str, description: &str, total_fee: f64, sp_openid: &str) -> Result<JsonValue, String> {
273 let mut api = "";
274 let mut order = object! {
275 out_trade_no:out_trade_no,
276 total_amount:total_fee,
277 subject:description,
278 product_code:"JSAPI_PAY",
279 op_app_id:sub_mchid,
280 buyer_open_id:sp_openid
281 };
282 match types {
283 Types::MiniJsapi => {
284 api = "alipay.trade.create";
285 order["product_code"] = "JSAPI_PAY".into();
286 order["op_app_id"] = self.appid.clone().into();
287 order["buyer_open_id"] = sp_openid.into();
288 }
290 Types::Jsapi => {
291 api = "alipay.trade.wap.pay";
292 order["product_code"] = "QUICK_WAP_WAY".into();
293 }
294 Types::H5 => {
295 api = "alipay.trade.wap.pay";
296 order["product_code"] = "QUICK_WAP_WAY".into();
297 }
298 Types::Native => {
299 api = "alipay.trade.wap.pay";
300 order["product_code"] = "QUICK_WAP_WAY".into();
301 }
302 _ => {
303 order["product_code"] = "JSAPI_PAY".into();
304 }
305 };
306 match self.http(api, object! {"biz_content":order}) {
307 Ok(e) => {
308 match types {
309 Types::Native => {}
310 Types::Jsapi | Types::H5 => {
311 return Ok(object! {url:e});
312 }
313 Types::MiniJsapi => {
314 println!("alipay.trade.wap.pay:{e:#}");
315 return Ok(e);
316 }
317 Types::App => {}
318 Types::Micropay => {}
319 }
320 Ok(e)
321 }
322 Err(e) => {
323 println!("Err: {e:#}");
324 Err(e)
325 }
326 }
327 }
328
329 fn micropay(&mut self, _channel: &str, auth_code: &str, sub_mchid: &str, out_trade_no: &str, description: &str, total_fee: f64, org_openid: &str, _ip: &str) -> Result<JsonValue, String> {
330 let order = object! {
331 out_trade_no:out_trade_no,
332 total_amount:total_fee,
333 subject:description,
334 seller_id:sub_mchid,
335 auth_code:auth_code,
336 scene:"bar_code",
337 operator_id:org_openid,
338 };
339 match self.http("alipay.trade.pay", object! {"biz_content":order}) {
340 Ok(e) => {
341 println!("alipay.trade.pay:{e:#}");
342 if e["code"].ne("10000") {
343 return Err(e["msg"].to_string());
344 }
345 if e["msg"].eq("FAIL") {
346 if e["err_code_des"].ne("需要用户输入支付密码") {
347 return Err(e["err_code_des"].to_string());
348 }
349 let res = PayNotify {
350 trade_type: TradeType::MICROPAY,
351 out_trade_no: out_trade_no.to_string(),
352 sp_mchid: self.sp_mchid.clone(),
353 sub_mchid: sub_mchid.to_string(),
354 sp_appid: self.appid.to_string(),
355 transaction_id: "".to_string(),
356 success_time: 0,
357 sp_openid: "".to_string(),
358 sub_openid: "".to_string(),
359 total: total_fee,
360 payer_total: total_fee,
361 currency: "CNY".to_string(),
362 payer_currency: "CNY".to_string(),
363 trade_state: TradeState::NOTPAY,
364 };
365 return Ok(res.json());
366 }
367 let res = PayNotify {
368 trade_type: TradeType::MICROPAY,
369 out_trade_no: out_trade_no.to_string(),
370 sp_mchid: self.sp_mchid.clone(),
371 sub_mchid: sub_mchid.to_string(),
372 sp_appid: self.appid.to_string(),
373 transaction_id: e["trade_no"].to_string(),
374 success_time: PayNotify::datetime_to_timestamp(e["gmt_payment"].as_str().unwrap(), "%Y-%m-%d %H:%M:%S"),
375 sp_openid: e["buyer_open_id"].to_string(),
376 sub_openid: e["buyer_open_id"].to_string(),
377 total: total_fee,
378 payer_total: total_fee,
379 currency: "CNY".to_string(),
380 payer_currency: "CNY".to_string(),
381 trade_state: TradeState::SUCCESS,
382 };
383 Ok(res.json())
384 }
385 Err(e) => Err(e)
386 }
387 }
388
389
390 fn close(&mut self, out_trade_no: &str, sub_mchid: &str, _channel: &str) -> Result<JsonValue, String> {
391 let order = object! {
392 "biz_content"=> object! {
393 out_trade_no:out_trade_no,
394 operator_id:sub_mchid
395 }
396 };
397 match self.http("alipay.trade.close", order) {
398 Ok(_) => {
399 Ok(true.into())
400 }
401 Err(e) => {
402 if e.contains("交易不存在") {
403 return Ok(true.into());
404 }
405 Err(e)
406 }
407 }
408 }
409
410 fn pay_query(&mut self, out_trade_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
411 let order = object! {
412 "biz_content"=> object! {
413 out_trade_no:out_trade_no
414 }
415 };
416 match self.http("alipay.trade.query", order) {
417 Ok(e) => {
418 if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
419 return Err(e["msg"].to_string());
420 }
421 let buyer_open_id = if e.has_key("buyer_open_id") {
422 e["buyer_open_id"].to_string()
423 } else {
424 e["buyer_user_id"].to_string()
425 };
426 let res = PayNotify {
427 trade_type: TradeType::None,
428 out_trade_no: e["out_trade_no"].to_string(),
429 sp_mchid: "".to_string(),
430 sub_mchid: sub_mchid.to_string(),
431 sp_appid: "".to_string(),
432 transaction_id: e["trade_no"].to_string(),
433 success_time: PayNotify::alipay_time(e["send_pay_date"].as_str().unwrap()),
434 sp_openid: buyer_open_id.clone(),
435 sub_openid: buyer_open_id.clone(),
436 total: (e["total_amount"].to_string().parse::<f64>().unwrap_or(0.0)),
437 payer_total: (e["total_amount"].to_string().parse::<f64>().unwrap_or(0.0)),
438 currency: "CNY".to_string(),
439 payer_currency: "CNY".to_string(),
440 trade_state: TradeState::from(e["trade_status"].as_str().unwrap()),
441 };
442 Ok(res.json())
443 }
444 Err(e) => Err(e)
445 }
446 }
447
448 fn pay_micropay_query(&mut self, _out_trade_no: &str, _sub_mchid: &str, _channel: &str) -> Result<JsonValue, String> {
449 todo!()
450 }
451
452 fn pay_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
453 todo!()
454 }
455
456 fn refund(&mut self, sub_mchid: &str, out_trade_no: &str, transaction_id: &str, out_refund_no: &str, amount: f64, total: f64, _currency: &str) -> Result<JsonValue, String> {
457 let body = object! {
458 "biz_content"=> object! {
459 "trade_no"=>transaction_id,
460 "out_trade_no"=>out_trade_no,
461 "out_request_no"=>out_refund_no,
462 "refund_amount"=>format!("{:.2}",amount),
463 }
464 };
465 match self.http("alipay.trade.refund", body.clone()) {
466 Ok(e) => {
467 if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
468 return Err(e["msg"].to_string());
469 }
470 let res = RefundNotify {
471 out_trade_no: e["out_trade_no"].to_string(),
472 refund_no: out_refund_no.to_string(),
473 sp_mchid: "".to_string(),
474 sub_mchid: sub_mchid.to_string(),
475 transaction_id: e["trade_no"].to_string(),
476 refund_id: out_refund_no.to_string(),
477 success_time: PayNotify::alipay_time(e["gmt_refund_pay"].as_str().unwrap()),
478 total,
479 refund: e["refund_fee"].to_string().parse::<f64>().unwrap(),
480 payer_total: e["refund_fee"].to_string().parse::<f64>().unwrap(),
481 payer_refund: e["send_back_fee"].to_string().parse::<f64>().unwrap(),
482 status: RefundStatus::from(e["fund_change"].as_str().unwrap()),
483 };
484 Ok(res.json())
485 }
486 Err(e) => Err(e)
487 }
488 }
489
490 fn micropay_refund(&mut self, _sub_mchid: &str, _out_trade_no: &str, _transaction_id: &str, _out_refund_no: &str, _amount: f64, _total: f64, _currency: &str, _refund_text: &str) -> Result<JsonValue, String> {
491 todo!()
492 }
493
494 fn refund_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
495 todo!()
496 }
497
498 fn refund_query(&mut self, trade_no: &str, out_refund_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
499 let body = object! {
500 "biz_content"=> object! {
501 "out_request_no"=>out_refund_no,
502 "trade_no"=>trade_no,
503 }
504 };
505 match self.http("alipay.trade.fastpay.refund.query", body.clone()) {
506 Ok(e) => {
507 if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
508 return Err(e["msg"].to_string());
509 }
510 let res = RefundNotify {
511 out_trade_no: e["out_trade_no"].to_string(),
512 refund_no: e["out_request_no"].to_string(),
513 sp_mchid: "".to_string(),
514 sub_mchid: sub_mchid.to_string(),
515 transaction_id: e["trade_no"].to_string(),
516 refund_id: e["out_request_no"].to_string(),
517 success_time: Local::now().timestamp(),
518 total: e["total_amount"].to_string().parse::<f64>().unwrap(),
519 payer_total: e["total_amount"].to_string().parse::<f64>().unwrap(),
520 refund: e["refund_amount"].to_string().parse::<f64>().unwrap(),
521 payer_refund: e["refund_amount"].to_string().parse::<f64>().unwrap(),
522 status: RefundStatus::from(e["refund_status"].as_str().unwrap()),
523 };
524 Ok(res.json())
525 }
526 Err(e) => Err(e)
527 }
528 }
529
530 fn incoming(&mut self, _business_code: &str, _contact_info: JsonValue, _subject_info: JsonValue, _business_info: JsonValue, _settlement_info: JsonValue, _bank_account_info: JsonValue) -> Result<JsonValue, String> {
531 todo!()
532 }
533}