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::{object, JsonValue};
8use openssl::hash::MessageDigest;
9use openssl::pkey::{PKey};
10use openssl::rsa::Rsa;
11use openssl::sign::{Signer, Verifier};
12use urlencoding::{decode, encode};
13use log::error;
14
15#[derive(Clone)]
16pub struct AliPay {
17    /// 应用appid
18    pub appid: String,
19    /// 服务商商家号
20    pub sp_mchid: String,
21    /// 授权token
22    pub app_auth_token: String,
23    /// 应用私钥证书路径
24    pub app_private: String,
25    /// 接口内容加密密钥
26    pub content_encryp: String,
27    /// 支付宝公钥
28    pub alipay_public_key: String,
29    pub notify_url: String,
30}
31impl AliPay {
32    pub fn sign(&mut self, txt: &str) -> Result<JsonValue, String> {
33        let t = self.app_private.as_bytes().chunks(64).map(|chunk| std::str::from_utf8(chunk).unwrap_or("")).collect::<Vec<&str>>().join("\n");
34        let cart = format!("-----BEGIN PRIVATE KEY-----\n{}\n-----END PRIVATE KEY-----\n", t);
35
36        // 1. 加载私钥
37        let rsa = match Rsa::private_key_from_pem(cart.as_bytes()) {
38            Ok(e) => e,
39            Err(e) => return Err(e.to_string())
40        };
41
42        let pkey = match PKey::from_rsa(rsa) {
43            Ok(e) => e,
44            Err(e) => return Err(e.to_string())
45        };
46
47        // 2. 创建签名器,使用 SHA256
48        let mut signer = match Signer::new(MessageDigest::sha256(), &pkey) {
49            Ok(e) => e,
50            Err(e) => return Err(e.to_string())
51        };
52        match signer.update(txt.as_bytes()) {
53            Ok(()) => {}
54            Err(e) => return Err(e.to_string())
55        };
56        // 3. 生成签名
57        let signature = match signer.sign_to_vec() {
58            Ok(e) => e,
59            Err(e) => return Err(e.to_string())
60        };
61        // 4. Base64 编码输出
62        Ok(STANDARD.encode(signature).into())
63    }
64    pub fn http(&mut self, method: &str, biz_content: JsonValue) -> Result<JsonValue, String> {
65        let mut http = Client::new();
66        //http.debug();
67        let sign = "";
68
69        let now = Local::now();
70        let timestamp = now.format("%Y-%m-%d %H:%M:%S").to_string();
71
72        let mut data = object! {
73                    "charset":"UTF-8",
74                    "method":method,
75                    "app_id":self.appid.clone(),
76                    "app_private_key":self.app_private.clone(),
77                    "version":"1.0",
78                    "sign_type":"RSA2",
79                    "timestamp":timestamp,
80                    "alipay_public_key":self.alipay_public_key.clone(),
81                    "sign":sign
82        };
83        if !self.app_auth_token.is_empty() {
84            data["app_auth_token"] = self.app_auth_token.clone().into();
85        }
86        if method.contains("alipay.trade.") {
87            data["notify_url"] = self.notify_url.clone().into();
88        }
89        for (key, value) in biz_content.entries() {
90            data[key] = value.clone()
91        }
92        let mut map = HashMap::new();
93        for (key, value) in data.entries() {
94            if key == "sign" {
95                continue;
96            }
97            if value.is_empty() {
98                continue;
99            }
100            map.insert(key, value);
101        }
102
103        let mut keys: Vec<_> = map.keys().cloned().collect();
104        keys.sort();
105        let mut txt = vec![];
106        for key in keys {
107            txt.push(format!("{}={}", key, map.get(&key).unwrap()));
108        }
109        let txt = txt.join("&");
110        data["sign"] = self.sign(&txt)?;
111
112        let mut new_data = object! {};
113        for (key, value) in data.entries() {
114            let t = encode(value.to_string().as_str()).to_string();
115            new_data[key] = t.into();
116        }
117        data = new_data;
118        let url = "https://openapi.alipay.com/gateway.do".to_string();
119        let res = match method {
120            "alipay.trade.wap.pay" => {
121                let tt = http.get(url.as_str()).query(data);
122                return Ok(tt.url.clone().into());
123            }
124            _ => {
125                match http.get(&url).query(data).form_data(biz_content).send() {
126                    Ok(e) => e,
127                    Err(e) => {
128                        return Err(e.to_string())
129                    }
130                }
131            }
132        };
133
134        let res = res.json()?;
135
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!("{}_response", key);
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}
153impl PayMode for AliPay {
154    fn notify(&mut self, data: JsonValue) -> Result<JsonValue, String> {
155        let sign = match STANDARD.decode(data["sign"].to_string()) {
156            Ok(e) => e,
157            Err(e) => return Err(format!("decode sign: {}", e))
158        };
159        let mut map = HashMap::new();
160        for (key, value) in data.entries() {
161            if key == "sign" {
162                continue;
163            }
164            if value.is_empty() {
165                continue;
166            }
167            map.insert(key, value);
168        }
169
170        let mut keys: Vec<_> = map.keys().cloned().collect();
171        keys.sort();
172
173        let mut txt = vec![];
174        for key in keys {
175            let value = decode(map.get(&key).unwrap().to_string().as_str()).unwrap().to_string();
176            txt.push(format!("{}={}", key, value));
177        }
178        let txt = txt.join("&");
179
180        let public_key_pem = self.alipay_public_key.clone();
181        let public_key_pem = format!("-----BEGIN PUBLIC KEY-----\n{}\n-----END PUBLIC KEY-----\n", public_key_pem);
182        let public_key = match PKey::public_key_from_pem(public_key_pem.as_bytes()) {
183            Ok(e) => e,
184            Err(e) => return Err(format!("Invalid public key: {}", e))
185        };
186
187        // 4. 验签
188        let mut verifier = match Verifier::new(MessageDigest::sha256(), &public_key) {
189            Ok(e) => e,
190            Err(e) => return Err(format!("Invalid verifier: {}", e))
191        };
192        match verifier.update(txt.as_bytes()) {
193            Ok(_) => {}
194            Err(_) => return Err("Invalid transaction signature".to_string())
195        };
196
197        let result = match verifier.verify(&sign) {
198            Ok(e) => e,
199            Err(_) => return Err("Invalid transaction signature".to_string())
200        };
201        if !result {
202            return Err("sign error".to_string());
203        }
204        if data.has_key("service") {
205            let service = data["service"].to_string();
206            if service.as_str() == "alipay.service.check" {
207                let sign_txt = "<success>true</success>";
208                let sign = self.sign(sign_txt)?;
209                let text = format!(r#"<?xml version="1.0" encoding="GBK"?><alipay><response><success>true</success></response><sign>{}</sign><sign_type>RSA2</sign_type></alipay>"#, sign);
210                return Ok(JsonValue::String(text));
211            }
212        }
213        Ok(result.into())
214    }
215
216    fn config(&mut self) -> JsonValue {
217        todo!()
218    }
219
220    fn login(&mut self, code: &str) -> Result<JsonValue, String> {
221        let res = self.http("alipay.system.oauth.token", object! {
222            "grant_type":"authorization_code",
223            "code":code
224        })?;
225        println!(">>>>{:#}", res);
226        Ok(res)
227    }
228
229    fn auth(&mut self, code: &str) -> Result<JsonValue, String> {
230        let biz_content = object! {
231            "biz_content":{
232                "grant_type":"authorization_code",
233                "code":code
234            }
235        };
236        let res = match self.http("alipay.open.auth.token.app", biz_content) {
237            Ok(e) => e,
238            Err(e) => {
239                error!("Err: {:#}", e);
240                return Err(e);
241            }
242        };
243        let data = object! {
244            user_id : res["user_id"].clone(),
245            auth_app_id : res["auth_app_id"].clone(),
246            re_expires_in :res["re_expires_in"].clone(),
247            app_auth_token:res["app_auth_token"].clone(),
248            app_refresh_token:res["app_refresh_token"].clone()
249        };
250        Ok(data)
251    }
252
253    fn pay(&mut self, types: Types, sub_mchid: &str, out_trade_no: &str, description: &str, total_fee: f64, sp_openid: &str) -> Result<JsonValue, String> {
254        let mut api = "";
255        let mut order = object! {
256            out_trade_no:out_trade_no,
257            total_amount:total_fee,
258            subject:description,
259            product_code:"",
260            op_app_id:sub_mchid,
261            buyer_open_id:sp_openid
262        };
263
264        match types {
265            Types::Jsapi => {
266                api = "alipay.trade.create";
267                order["product_code"] = "JSAPI_PAY".into();
268            }
269            Types::H5 => {
270                api = "alipay.trade.wap.pay";
271                order["product_code"] = "QUICK_WAP_WAY".into();
272            }
273            Types::Native => {
274                api = "alipay.trade.wap.pay";
275                order["product_code"] = "QUICK_WAP_WAY".into();
276            }
277            _ => {
278                order["product_code"] = "JSAPI_PAY".into();
279            }
280        };
281
282        match self.http(api, object! {"biz_content":order}) {
283            Ok(e) => {
284                match types {
285                    Types::Jsapi => {}
286                    Types::Native => {}
287                    Types::H5 => {
288                        return Ok(object! {url:e});
289                    }
290                    Types::MiniJsapi => {}
291                    Types::App => {}
292                    Types::Micropay => {}
293                }
294                Ok(e)
295            }
296            Err(e) => {
297                println!("Err: {:#}", e);
298                Err(e)
299            }
300        }
301    }
302
303
304    fn close(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
305        todo!()
306    }
307
308    fn pay_query(&mut self, out_trade_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
309        let order = object! {
310              "biz_content"=> object! {
311               out_trade_no:out_trade_no
312              }
313        };
314        match self.http("alipay.trade.query", order) {
315            Ok(e) => {
316                if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
317                    return Err(e["msg"].to_string());
318                }
319                let res = PayNotify {
320                    trade_type: TradeType::None,
321                    out_trade_no: e["out_trade_no"].to_string(),
322                    sp_mchid: "".to_string(),
323                    sub_mchid: sub_mchid.to_string(),
324                    sp_appid: "".to_string(),
325                    transaction_id: e["trade_no"].to_string(),
326                    success_time: PayNotify::alipay_time(e["send_pay_date"].as_str().unwrap()),
327                    sp_openid: e["buyer_open_id"].to_string(),
328                    sub_openid: e["buyer_open_id"].to_string(),
329                    total: (e["total_amount"].to_string().parse::<f64>().unwrap_or(0.0) * 100.0) as usize,
330                    payer_total: (e["total_amount"].to_string().parse::<f64>().unwrap_or(0.0) * 100.0) as usize,
331                    currency: "CNY".to_string(),
332                    payer_currency: "CNY".to_string(),
333                    trade_state: TradeState::from(e["trade_status"].as_str().unwrap()),
334                };
335                Ok(res.json())
336            }
337            Err(e) => {
338                println!("Err: {:#}", e);
339                Err(e)
340            }
341        }
342    }
343
344    fn pay_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
345        todo!()
346    }
347
348    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> {
349        let body = object! {
350            "biz_content"=> object! {
351                "trade_no"=>transaction_id,
352                "out_trade_no"=>out_trade_no,
353                "out_request_no"=>out_refund_no,
354                "refund_amount"=>format!("{:.2}",amount),
355            }
356        };
357        match self.http("alipay.trade.refund", body.clone()) {
358            Ok(e) => {
359                if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
360                    return Err(e["msg"].to_string());
361                }
362                let res = RefundNotify {
363                    out_trade_no: e["out_trade_no"].to_string(),
364                    refund_no: out_refund_no.to_string(),
365                    sp_mchid: "".to_string(),
366                    sub_mchid: sub_mchid.to_string(),
367                    transaction_id: e["trade_no"].to_string(),
368                    refund_id: out_refund_no.to_string(),
369                    success_time: PayNotify::alipay_time(e["gmt_refund_pay"].as_str().unwrap()),
370                    total,
371                    refund: e["refund_fee"].to_string().parse::<f64>().unwrap(),
372                    payer_total: e["refund_fee"].to_string().parse::<f64>().unwrap(),
373                    payer_refund: e["send_back_fee"].to_string().parse::<f64>().unwrap(),
374                    status: RefundStatus::from(e["fund_change"].as_str().unwrap()),
375                };
376                Ok(res.json())
377            }
378            Err(e) => Err(e)
379        }
380    }
381
382    fn refund_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
383        todo!()
384    }
385
386    fn refund_query(&mut self, trade_no: &str, out_refund_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
387        let body = object! {
388             "biz_content"=> object! {
389               "out_request_no"=>out_refund_no,
390            "trade_no"=>trade_no,
391             }
392        };
393        match self.http("alipay.trade.fastpay.refund.query", body.clone()) {
394            Ok(e) => {
395                if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
396                    return Err(e["msg"].to_string());
397                }
398                let res = RefundNotify {
399                    out_trade_no: e["out_trade_no"].to_string(),
400                    refund_no: e["out_request_no"].to_string(),
401                    sp_mchid: "".to_string(),
402                    sub_mchid: sub_mchid.to_string(),
403                    transaction_id: e["trade_no"].to_string(),
404                    refund_id: e["out_request_no"].to_string(),
405                    success_time: Local::now().timestamp(),
406                    total: e["total_amount"].to_string().parse::<f64>().unwrap(),
407                    payer_total: e["total_amount"].to_string().parse::<f64>().unwrap(),
408                    refund: e["refund_amount"].to_string().parse::<f64>().unwrap(),
409                    payer_refund: e["refund_amount"].to_string().parse::<f64>().unwrap(),
410                    status: RefundStatus::from(e["refund_status"].as_str().unwrap()),
411                };
412                Ok(res.json())
413            }
414            Err(e) => Err(e)
415        }
416    }
417}