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 pub appid: String,
20 pub sp_mchid: String,
22 pub app_auth_token: String,
24 pub app_private: String,
26 pub content_encryp: String,
28 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 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 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 let signature = match signer.sign_to_vec() {
59 Ok(e) => e,
60 Err(e) => return Err(e.to_string())
61 };
62 Ok(general_purpose::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 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 "app_auth_token":self.app_auth_token.clone(),
83 "sign":sign
84 };
85 for (key, value) in biz_content.entries() {
86 data[key] = value.clone()
87 }
88
89 let mut map = HashMap::new();
90 for (key, value) in data.entries() {
91 if key == "sign" {
92 continue;
93 }
94 if value.is_empty() {
95 continue;
96 }
97 map.insert(key, value);
98 }
99
100 let mut keys: Vec<_> = map.keys().cloned().collect();
101 keys.sort();
102 let mut txt = vec![];
103 for key in keys {
104 txt.push(format!("{}={}", key, map.get(&key).unwrap()));
105 }
106 let txt = txt.join("&");
107 data["sign"] = self.sign(&txt)?;
108
109 let mut new_data = object! {};
110 for (key, value) in data.entries() {
111 let t = encode(value.to_string().as_str()).to_string();
112 new_data[key] = t.into();
113 }
114 data = new_data;
115 let url = "https://openapi.alipay.com/gateway.do".to_string();
116 let res = match method {
117 "alipay.trade.wap.pay" => {
118 let tt = http.get(url.as_str()).query(data);
119 return Ok(tt.url.clone().into());
120 }
121 _ => {
122 match http.get(&url).query(data).form_data(biz_content).send() {
123 Ok(e) => e,
124 Err(e) => {
125 return Err(e.to_string())
126 }
127 }
128 }
129 };
130
131 let res = res.json()?;
132
133 if res.has_key("error_response") {
134 return Err(res["error_response"]["sub_msg"].to_string());
135 }
136 let key = method.replace(".", "_");
137 let key = format!("{}_response", key);
138 let data = res[key].clone();
139 if data.has_key("code") {
140 if data["code"] != "10000" {
141 Err(data["sub_msg"].to_string())
142 } else {
143 Ok(data)
144 }
145 } else {
146 Err(data.to_string())
147 }
148 }
149}
150impl PayMode for AliPay {
151 fn notify(&mut self, data: JsonValue) -> Result<JsonValue, String> {
152 let sign = match STANDARD.decode(data["sign"].to_string()) {
153 Ok(e) => e,
154 Err(e) => return Err(format!("decode sign: {}", e))
155 };
156 let mut map = HashMap::new();
157 for (key, value) in data.entries() {
158 if key == "sign" {
159 continue;
160 }
161 if value.is_empty() {
162 continue;
163 }
164 map.insert(key, value);
165 }
166
167 let mut keys: Vec<_> = map.keys().cloned().collect();
168 keys.sort();
169
170 let mut txt = vec![];
171 for key in keys {
172 let value = decode(map.get(&key).unwrap().to_string().as_str()).unwrap().to_string();
173 txt.push(format!("{}={}", key, value));
174 }
175 let txt = txt.join("&");
176
177 let public_key_pem = self.alipay_public_key.clone();
178 let public_key_pem = format!("-----BEGIN PUBLIC KEY-----\n{}\n-----END PUBLIC KEY-----\n", public_key_pem);
179 let public_key = match PKey::public_key_from_pem(public_key_pem.as_bytes()) {
180 Ok(e) => e,
181 Err(e) => return Err(format!("Invalid public key: {}", e))
182 };
183
184 let mut verifier = match Verifier::new(MessageDigest::sha256(), &public_key) {
186 Ok(e) => e,
187 Err(e) => return Err(format!("Invalid verifier: {}", e))
188 };
189 match verifier.update(txt.as_bytes()) {
190 Ok(_) => {}
191 Err(_) => return Err("Invalid transaction signature".to_string())
192 };
193
194 let result = match verifier.verify(&sign) {
195 Ok(e) => e,
196 Err(_) => return Err("Invalid transaction signature".to_string())
197 };
198 if !result {
199 return Err("sign error".to_string());
200 }
201 if data.has_key("service") {
202 let service = data["service"].to_string();
203 if service.as_str() == "alipay.service.check" {
204 let sign_txt = "<success>true</success>";
205 let sign = self.sign(sign_txt)?;
206 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);
207 return Ok(JsonValue::String(text));
208 }
209 }
210 Ok(result.into())
211 }
212
213 fn config(&mut self) -> JsonValue {
214 todo!()
215 }
216
217 fn login(&mut self, code: &str) -> Result<JsonValue, String> {
218 let res = self.http("alipay.system.oauth.token", object! {
219 "grant_type":"authorization_code",
220 "code":code
221 })?;
222 println!(">>>>{:#}", res);
223 Ok(res)
224 }
225
226 fn auth(&mut self, code: &str) -> Result<JsonValue, String> {
227 let res = match self.http("alipay.open.auth.token.app", object! {
228 grant_type:"authorization_code",
229 code:code
230 }) {
231 Ok(e) => e,
232 Err(e) => {
233 error!("Err: {:#}", e);
234 return Err(e);
235 }
236 };
237 println!(">>>>{:#}", res);
238 Ok(res)
244 }
245
246 fn pay(&mut self, types: Types, sub_mchid: &str, out_trade_no: &str, description: &str, total_fee: f64, sp_openid: &str) -> Result<JsonValue, String> {
247 let mut api = "";
248 let mut order = object! {
249 out_trade_no:out_trade_no,
250 total_amount:total_fee,
251 subject:description,
252 product_code:"",
253 op_app_id:sub_mchid,
254 buyer_open_id:sp_openid
255 };
256
257 match types {
258 Types::Jsapi => {
259 api = "alipay.trade.create";
260 order["product_code"] = "JSAPI_PAY".into();
261 }
262 Types::H5 => {
263 api = "alipay.trade.wap.pay";
264 order["product_code"] = "QUICK_WAP_WAY".into();
265 }
266 Types::Native => {
267 api = "alipay.trade.wap.pay";
268 order["product_code"] = "QUICK_WAP_WAY".into();
269 }
270 _ => {
271 order["product_code"] = "JSAPI_PAY".into();
272 }
273 };
274
275 match self.http(api, object! {"biz_content":order}) {
276 Ok(e) => {
277 match types {
278 Types::Jsapi => {}
279 Types::Native => {}
280 Types::H5 => {
281 return Ok(object! {url:e});
282 }
283 Types::MiniJsapi => {}
284 Types::App => {}
285 Types::Micropay => {}
286 }
287 Ok(e)
288 }
289 Err(e) => {
290 println!("Err: {:#}", e);
291 Err(e)
292 }
293 }
294 }
295
296
297 fn close(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
298 todo!()
299 }
300
301 fn pay_query(&mut self, out_trade_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
302 let order = object! {
303 "biz_content"=> object! {
304 out_trade_no:out_trade_no
305 }
306 };
307 match self.http("alipay.trade.query", order) {
308 Ok(e) => {
309 if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
310 return Err(e["msg"].to_string());
311 }
312 let res = PayNotify {
313 trade_type: TradeType::None,
314 out_trade_no: e["out_trade_no"].to_string(),
315 sp_mchid: "".to_string(),
316 sub_mchid: sub_mchid.to_string(),
317 sp_appid: "".to_string(),
318 transaction_id: e["trade_no"].to_string(),
319 success_time: PayNotify::alipay_time(e["send_pay_date"].as_str().unwrap()),
320 sp_openid: e["buyer_open_id"].to_string(),
321 sub_openid: e["buyer_open_id"].to_string(),
322 total: (e["total_amount"].to_string().parse::<f64>().unwrap_or(0.0) * 100.0) as usize,
323 payer_total: (e["total_amount"].to_string().parse::<f64>().unwrap_or(0.0) * 100.0) as usize,
324 currency: "CNY".to_string(),
325 payer_currency: "CNY".to_string(),
326 trade_state: TradeState::from(e["trade_status"].as_str().unwrap()),
327 };
328 Ok(res.json())
329 }
330 Err(e) => {
331 println!("Err: {:#}", e);
332 Err(e)
333 }
334 }
335 }
336
337 fn pay_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
338 todo!()
339 }
340
341 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> {
342 let body = object! {
343 "biz_content"=> object! {
344 "trade_no"=>transaction_id,
345 "out_trade_no"=>out_trade_no,
346 "out_request_no"=>out_refund_no,
347 "refund_amount"=>format!("{:.2}",amount),
348 }
349 };
350 match self.http("alipay.trade.refund", body.clone()) {
351 Ok(e) => {
352 if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
353 return Err(e["msg"].to_string());
354 }
355 let res = RefundNotify {
356 out_trade_no: e["out_trade_no"].to_string(),
357 refund_no: out_refund_no.to_string(),
358 sp_mchid: "".to_string(),
359 sub_mchid: sub_mchid.to_string(),
360 transaction_id: e["trade_no"].to_string(),
361 refund_id: out_refund_no.to_string(),
362 success_time: PayNotify::alipay_time(e["gmt_refund_pay"].as_str().unwrap()),
363 total,
364 refund: e["refund_fee"].to_string().parse::<f64>().unwrap(),
365 payer_total: e["refund_fee"].to_string().parse::<f64>().unwrap(),
366 payer_refund: e["send_back_fee"].to_string().parse::<f64>().unwrap(),
367 status: RefundStatus::from(e["fund_change"].as_str().unwrap()),
368 };
369 Ok(res.json())
370 }
371 Err(e) => Err(e)
372 }
373 }
374
375 fn refund_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
376 todo!()
377 }
378
379 fn refund_query(&mut self, trade_no: &str, out_refund_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
380 let body = object! {
381 "biz_content"=> object! {
382 "out_request_no"=>out_refund_no,
383 "trade_no"=>trade_no,
384 }
385 };
386 match self.http("alipay.trade.fastpay.refund.query", body.clone()) {
387 Ok(e) => {
388 if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
389 return Err(e["msg"].to_string());
390 }
391 let res = RefundNotify {
392 out_trade_no: e["out_trade_no"].to_string(),
393 refund_no: e["out_request_no"].to_string(),
394 sp_mchid: "".to_string(),
395 sub_mchid: sub_mchid.to_string(),
396 transaction_id: e["trade_no"].to_string(),
397 refund_id: e["out_request_no"].to_string(),
398 success_time: Local::now().timestamp(),
399 total: e["total_amount"].to_string().parse::<f64>().unwrap(),
400 payer_total: e["total_amount"].to_string().parse::<f64>().unwrap(),
401 refund: e["refund_amount"].to_string().parse::<f64>().unwrap(),
402 payer_refund: e["refund_amount"].to_string().parse::<f64>().unwrap(),
403 status: RefundStatus::from(e["refund_status"].as_str().unwrap()),
404 };
405 Ok(res.json())
406 }
407 Err(e) => Err(e)
408 }
409 }
410}