br_pay/
alipay.rs

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    /// 应用appid
17    pub appid: String,
18    /// 服务商商家号
19    pub sp_mchid: String,
20    /// 授权token
21    pub app_auth_token: String,
22    /// 应用私钥证书路径
23    pub app_private: String,
24    /// 接口内容加密密钥
25    pub content_encryp: String,
26    /// 支付宝公钥
27    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        // 1. 加载私钥
39        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        // 2. 创建签名器,使用 SHA256
50        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        // 3. 生成签名
59        let signature = match signer.sign_to_vec() {
60            Ok(e) => e,
61            Err(e) => return Err(e.to_string())
62        };
63        // 4. Base64 编码输出
64        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        //http.debug();
69        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        //http.debug();
154        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
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.clone().into();
289                order["buyer_open_id"] = sp_openid.into();
290                self.app_auth_token = "".to_string();
291            }
292            Types::Jsapi => {
293                api = "alipay.trade.create";
294                order["product_code"] = "JSAPI_PAY".into();
295            }
296            Types::H5 => {
297                api = "alipay.trade.wap.pay";
298                order["product_code"] = "QUICK_WAP_WAY".into();
299            }
300            Types::Native => {
301                api = "alipay.trade.wap.pay";
302                order["product_code"] = "QUICK_WAP_WAY".into();
303            }
304            _ => {
305                order["product_code"] = "JSAPI_PAY".into();
306            }
307        };
308        match self.http(api, object! {"biz_content":order}) {
309            Ok(e) => {
310                match types {
311                    Types::Jsapi => {}
312                    Types::Native => {}
313                    Types::H5 => {
314                        return Ok(object! {url:e});
315                    }
316                    Types::MiniJsapi => {
317                        println!("alipay.trade.wap.pay:{e:#}");
318                        return Ok(e);
319                    }
320                    Types::App => {}
321                    Types::Micropay => {}
322                }
323                Ok(e)
324            }
325            Err(e) => {
326                println!("Err: {e:#}");
327                Err(e)
328            }
329        }
330    }
331
332    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> {
333        let order = object! {
334            out_trade_no:out_trade_no,
335            total_amount:total_fee,
336            subject:description,
337            seller_id:sub_mchid,
338            auth_code:auth_code,
339            scene:"bar_code",
340            operator_id:org_openid,
341        };
342        match self.http("alipay.trade.pay", object! {"biz_content":order}) {
343            Ok(e) => {
344                println!("alipay.trade.pay:{e:#}");
345                if e["code"].ne("10000") {
346                    return Err(e["msg"].to_string());
347                }
348                if e["msg"].eq("FAIL") {
349                    if e["err_code_des"].ne("需要用户输入支付密码") {
350                        return Err(e["err_code_des"].to_string());
351                    }
352                    let res = PayNotify {
353                        trade_type: TradeType::MICROPAY,
354                        out_trade_no: out_trade_no.to_string(),
355                        sp_mchid: self.sp_mchid.clone(),
356                        sub_mchid: sub_mchid.to_string(),
357                        sp_appid: self.appid.to_string(),
358                        transaction_id: "".to_string(),
359                        success_time: 0,
360                        sp_openid: "".to_string(),
361                        sub_openid: "".to_string(),
362                        total: total_fee,
363                        payer_total: total_fee,
364                        currency: "CNY".to_string(),
365                        payer_currency: "CNY".to_string(),
366                        trade_state: TradeState::NOTPAY,
367                    };
368                    return Ok(res.json());
369                }
370                let res = PayNotify {
371                    trade_type: TradeType::MICROPAY,
372                    out_trade_no: out_trade_no.to_string(),
373                    sp_mchid: self.sp_mchid.clone(),
374                    sub_mchid: sub_mchid.to_string(),
375                    sp_appid: self.appid.to_string(),
376                    transaction_id: e["trade_no"].to_string(),
377                    success_time: crate::PayNotify::datetime_to_timestamp(e["gmt_payment"].as_str().unwrap(), "%Y-%m-%d %H:%M:%S"),
378                    sp_openid: e["buyer_open_id"].to_string(),
379                    sub_openid: e["buyer_open_id"].to_string(),
380                    total: total_fee,
381                    payer_total: total_fee,
382                    currency: "CNY".to_string(),
383                    payer_currency: "CNY".to_string(),
384                    trade_state: TradeState::SUCCESS,
385                };
386                Ok(res.json())
387            }
388            Err(e) => Err(e)
389        }
390    }
391
392
393    fn close(&mut self, out_trade_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
394        let order = object! {
395              "biz_content"=> object! {
396                out_trade_no:out_trade_no,
397                operator_id:sub_mchid
398              }
399        };
400        match self.http("alipay.trade.close", order) {
401            Ok(_) => {
402                Ok(true.into())
403            }
404            Err(e) => {
405                if e.contains("交易不存在") {
406                    return Ok(true.into());
407                }
408                Err(e)
409            }
410        }
411    }
412
413    fn pay_query(&mut self, out_trade_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
414        let order = object! {
415              "biz_content"=> object! {
416               out_trade_no:out_trade_no
417              }
418        };
419        match self.http("alipay.trade.query", order) {
420            Ok(e) => {
421                if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
422                    return Err(e["msg"].to_string());
423                }
424                let buyer_open_id = if e.has_key("buyer_open_id") {
425                    e["buyer_open_id"].to_string()
426                } else {
427                    e["buyer_user_id"].to_string()
428                };
429                let res = PayNotify {
430                    trade_type: TradeType::None,
431                    out_trade_no: e["out_trade_no"].to_string(),
432                    sp_mchid: "".to_string(),
433                    sub_mchid: sub_mchid.to_string(),
434                    sp_appid: "".to_string(),
435                    transaction_id: e["trade_no"].to_string(),
436                    success_time: PayNotify::alipay_time(e["send_pay_date"].as_str().unwrap()),
437                    sp_openid: buyer_open_id.clone(),
438                    sub_openid: buyer_open_id.clone(),
439                    total: (e["total_amount"].to_string().parse::<f64>().unwrap_or(0.0)),
440                    payer_total: (e["total_amount"].to_string().parse::<f64>().unwrap_or(0.0)),
441                    currency: "CNY".to_string(),
442                    payer_currency: "CNY".to_string(),
443                    trade_state: TradeState::from(e["trade_status"].as_str().unwrap()),
444                };
445                Ok(res.json())
446            }
447            Err(e) => Err(e)
448        }
449    }
450
451    fn pay_micropay_query(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
452        todo!()
453    }
454
455    fn pay_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
456        todo!()
457    }
458
459    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> {
460        let body = object! {
461            "biz_content"=> object! {
462                "trade_no"=>transaction_id,
463                "out_trade_no"=>out_trade_no,
464                "out_request_no"=>out_refund_no,
465                "refund_amount"=>format!("{:.2}",amount),
466            }
467        };
468        match self.http("alipay.trade.refund", body.clone()) {
469            Ok(e) => {
470                if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
471                    return Err(e["msg"].to_string());
472                }
473                let res = RefundNotify {
474                    out_trade_no: e["out_trade_no"].to_string(),
475                    refund_no: out_refund_no.to_string(),
476                    sp_mchid: "".to_string(),
477                    sub_mchid: sub_mchid.to_string(),
478                    transaction_id: e["trade_no"].to_string(),
479                    refund_id: out_refund_no.to_string(),
480                    success_time: PayNotify::alipay_time(e["gmt_refund_pay"].as_str().unwrap()),
481                    total,
482                    refund: e["refund_fee"].to_string().parse::<f64>().unwrap(),
483                    payer_total: e["refund_fee"].to_string().parse::<f64>().unwrap(),
484                    payer_refund: e["send_back_fee"].to_string().parse::<f64>().unwrap(),
485                    status: RefundStatus::from(e["fund_change"].as_str().unwrap()),
486                };
487                Ok(res.json())
488            }
489            Err(e) => Err(e)
490        }
491    }
492
493    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> {
494        todo!()
495    }
496
497    fn refund_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
498        todo!()
499    }
500
501    fn refund_query(&mut self, trade_no: &str, out_refund_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
502        let body = object! {
503             "biz_content"=> object! {
504               "out_request_no"=>out_refund_no,
505            "trade_no"=>trade_no,
506             }
507        };
508        match self.http("alipay.trade.fastpay.refund.query", body.clone()) {
509            Ok(e) => {
510                if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
511                    return Err(e["msg"].to_string());
512                }
513                let res = RefundNotify {
514                    out_trade_no: e["out_trade_no"].to_string(),
515                    refund_no: e["out_request_no"].to_string(),
516                    sp_mchid: "".to_string(),
517                    sub_mchid: sub_mchid.to_string(),
518                    transaction_id: e["trade_no"].to_string(),
519                    refund_id: e["out_request_no"].to_string(),
520                    success_time: Local::now().timestamp(),
521                    total: e["total_amount"].to_string().parse::<f64>().unwrap(),
522                    payer_total: e["total_amount"].to_string().parse::<f64>().unwrap(),
523                    refund: e["refund_amount"].to_string().parse::<f64>().unwrap(),
524                    payer_refund: e["refund_amount"].to_string().parse::<f64>().unwrap(),
525                    status: RefundStatus::from(e["refund_status"].as_str().unwrap()),
526                };
527                Ok(res.json())
528            }
529            Err(e) => Err(e)
530        }
531    }
532
533    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> {
534        todo!()
535    }
536}