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