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