br_pay/
alipay.rs

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