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