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