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    /// 第三方关联小程序
30    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        // 1. 加载私钥
41        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        // 2. 创建签名器,使用 SHA256
52        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        // 3. 生成签名
61        let signature = match signer.sign_to_vec() {
62            Ok(e) => e,
63            Err(e) => return Err(e.to_string())
64        };
65        // 4. Base64 编码输出
66        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        //http.debug();
155        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                order["op_buyer_open_id"] = sp_openid.into();
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                        return Ok(e);
316                    }
317                    Types::App => {}
318                    Types::Micropay => {}
319                }
320                Ok(e)
321            }
322            Err(e) => {
323                if e == "ACCESS_FORBIDDEN" {
324                    return Err("请商户授权JSAPI 绑定 服务商小程序APPID".to_string());
325                }
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: 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, _channel: &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 send_pay_date = e["send_pay_date"].as_str().unwrap_or("");
430                let success_time = if !send_pay_date.is_empty() {
431                    PayNotify::alipay_time(send_pay_date)
432                } else {
433                    0
434                };
435                let res = PayNotify {
436                    trade_type: TradeType::None,
437                    out_trade_no: e["out_trade_no"].to_string(),
438                    sp_mchid: "".to_string(),
439                    sub_mchid: sub_mchid.to_string(),
440                    sp_appid: "".to_string(),
441                    transaction_id: e["trade_no"].to_string(),
442                    success_time,
443                    sp_openid: buyer_open_id.clone(),
444                    sub_openid: buyer_open_id.clone(),
445                    total: (e["total_amount"].to_string().parse::<f64>().unwrap_or(0.0)),
446                    payer_total: (e["total_amount"].to_string().parse::<f64>().unwrap_or(0.0)),
447                    currency: "CNY".to_string(),
448                    payer_currency: "CNY".to_string(),
449                    trade_state: TradeState::from(e["trade_status"].as_str().unwrap()),
450                };
451                Ok(res.json())
452            }
453            Err(e) => Err(e)
454        }
455    }
456
457    fn pay_micropay_query(&mut self, _out_trade_no: &str, _sub_mchid: &str, _channel: &str) -> Result<JsonValue, String> {
458        todo!()
459    }
460
461    fn pay_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
462        todo!()
463    }
464
465    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> {
466        let body = object! {
467            "biz_content"=> object! {
468                "trade_no"=>transaction_id,
469                "out_trade_no"=>out_trade_no,
470                "out_request_no"=>out_refund_no,
471                "refund_amount"=>format!("{:.2}",amount),
472            }
473        };
474        match self.http("alipay.trade.refund", body.clone()) {
475            Ok(e) => {
476                if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
477                    return Err(e["msg"].to_string());
478                }
479                let res = RefundNotify {
480                    out_trade_no: e["out_trade_no"].to_string(),
481                    refund_no: out_refund_no.to_string(),
482                    sp_mchid: "".to_string(),
483                    sub_mchid: sub_mchid.to_string(),
484                    transaction_id: e["trade_no"].to_string(),
485                    refund_id: out_refund_no.to_string(),
486                    success_time: PayNotify::alipay_time(e["gmt_refund_pay"].as_str().unwrap()),
487                    total,
488                    refund: e["refund_fee"].to_string().parse::<f64>().unwrap(),
489                    payer_total: e["refund_fee"].to_string().parse::<f64>().unwrap(),
490                    payer_refund: e["send_back_fee"].to_string().parse::<f64>().unwrap(),
491                    status: RefundStatus::from(e["fund_change"].as_str().unwrap()),
492                };
493                Ok(res.json())
494            }
495            Err(e) => Err(e)
496        }
497    }
498
499    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> {
500        todo!()
501    }
502
503    fn refund_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
504        todo!()
505    }
506
507    fn refund_query(&mut self, trade_no: &str, out_refund_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
508        let body = object! {
509             "biz_content"=> object! {
510               "out_request_no"=>out_refund_no,
511            "trade_no"=>trade_no,
512             }
513        };
514        match self.http("alipay.trade.fastpay.refund.query", 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: e["out_request_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: e["out_request_no"].to_string(),
526                    success_time: Local::now().timestamp(),
527                    total: e["total_amount"].to_string().parse::<f64>().unwrap(),
528                    payer_total: e["total_amount"].to_string().parse::<f64>().unwrap(),
529                    refund: e["refund_amount"].to_string().parse::<f64>().unwrap(),
530                    payer_refund: e["refund_amount"].to_string().parse::<f64>().unwrap(),
531                    status: RefundStatus::from(e["refund_status"].as_str().unwrap()),
532                };
533                Ok(res.json())
534            }
535            Err(e) => Err(e)
536        }
537    }
538
539    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> {
540        todo!()
541    }
542}