1use std::collections::{HashMap};
2use base64::Engine;
3use base64::engine::general_purpose;
4use chrono::Local;
5use crate::{PayMode, Types};
6use json::{object, JsonValue};
7use openssl::hash::MessageDigest;
8use openssl::pkey::{PKey};
9use openssl::rsa::Rsa;
10use openssl::sign::Signer;
11use urlencoding::encode;
12use log::error;
13use select::document::Document;
14use select::predicate::Name;
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 http(&mut self, method: &str, biz_content: JsonValue) -> Result<JsonValue, String> {
34 let mut http = br_http::Http::new();
35 let sign = "";
36
37 let now = Local::now();
38 let timestamp = now.format("%Y-%m-%d %H:%M:%S").to_string();
39
40
41 let mut data = object! {
42 "charset":"UTF-8",
43 "method":method,
44 "app_id":self.appid.clone(),
45 "version":"1.0",
46 "sign_type":"RSA2",
47 "timestamp":timestamp,
48 "app_auth_token":self.app_auth_token.clone(),
49 "sign":sign,
50 "biz_content": biz_content,
51 };
52
53 let mut map = HashMap::new();
54 for (key, value) in data.entries() {
55 if key == "sign" {
56 continue;
57 }
58 if value.is_empty() {
59 continue;
60 }
61 map.insert(key, value);
62 }
63
64 let mut keys: Vec<_> = map.keys().cloned().collect();
65 keys.sort();
66 let mut txt = vec![];
67 for key in keys {
68 txt.push(format!("{}={}", key, map.get(&key).unwrap()));
69 }
70 let txt = txt.join("&");
71 let t = self.app_private.as_bytes().chunks(64).map(|chunk| std::str::from_utf8(chunk).unwrap_or("")).collect::<Vec<&str>>().join("\n");
72 let cart = format!("-----BEGIN PRIVATE KEY-----\n{}\n-----END PRIVATE KEY-----\n", t);
73
74 let rsa = match Rsa::private_key_from_pem(cart.as_bytes()) {
76 Ok(e) => e,
77 Err(e) => return Err(e.to_string())
78 };
79
80 let pkey = match PKey::from_rsa(rsa) {
81 Ok(e) => e,
82 Err(e) => return Err(e.to_string())
83 };
84
85 let mut signer = match Signer::new(MessageDigest::sha256(), &pkey) {
87 Ok(e) => e,
88 Err(e) => return Err(e.to_string())
89 };
90 match signer.update(txt.as_bytes()) {
91 Ok(()) => {}
92 Err(e) => return Err(e.to_string())
93 };
94 let signature = match signer.sign_to_vec() {
96 Ok(e) => e,
97 Err(e) => return Err(e.to_string())
98 };
99 data["sign"] = general_purpose::STANDARD.encode(signature).into();
101
102 let mut new_data = object! {};
103 for (key, value) in data.entries() {
104 let t = encode(value.to_string().as_str()).to_string();
105 new_data[key] = t.into();
106 }
107 data = new_data;
108 let url = "https://openapi.alipay.com/gateway.do".to_string();
109 let res = match method {
110 "alipay.trade.wap.pay" => {
111 let tt = http.get(url.as_str()).query(data);
112 return Ok(tt.url.clone().into());
113 }
123 _ => {
124 match http.post(url.as_str()).query(data).json() {
125 Ok(e) => e,
126 Err(e) => {
127 println!(">>>>>>{}", e);
128 return Err(e);
129 }
130 }
131 }
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 config(&mut self) -> JsonValue {
152 todo!()
153 }
154
155 fn login(&mut self, code: &str) -> Result<JsonValue, String> {
156 let res = self.http("alipay.system.oauth.token", object! {
157 code:code
159 })?;
160 Ok(res)
161 }
162
163 fn auth(&mut self, code: &str) -> Result<JsonValue, String> {
164 let res = match self.http("alipay.open.auth.token.app", object! {
165 grant_type:"authorization_code",
166 code:code
167 }) {
168 Ok(e) => e,
169 Err(e) => {
170 error!("Err: {:#}", e);
171 return Err(e);
172 }
173 };
174 Ok(res)
180 }
181
182 fn pay(&mut self, types: Types, sub_mchid: &str, out_trade_no: &str, description: &str, total_fee: f64, sp_openid: &str) -> Result<JsonValue, String> {
183 let mut api = "";
184 let mut order = object! {
185 out_trade_no:out_trade_no,
186 total_amount:total_fee,
187 subject:description,
188 product_code:"",
189 op_app_id:sub_mchid,
190 buyer_open_id:sp_openid
191 };
192
193 match types {
194 Types::Jsapi => {
195 api = "alipay.trade.create";
196 order["product_code"] = "JSAPI_PAY".into();
197 }
198 Types::H5 => {
199 api = "alipay.trade.wap.pay";
200 order["product_code"] = "QUICK_WAP_WAY".into();
201 }
202 Types::Native => {
203 api = "alipay.trade.wap.pay";
204 order["product_code"] = "QUICK_WAP_WAY".into();
205 }
206 _ => {
207 order["product_code"] = "JSAPI_PAY".into();
208 }
209 };
210
211 match self.http(api, order) {
212 Ok(e) => {
213 match types {
214 Types::Jsapi => {}
215 Types::Native => {}
216 Types::H5 => {
217 return Ok(object! {url:e});
218 }
230 Types::MiniJsapi => {}
231 Types::App => {}
232 Types::Micropay => {}
233 }
234 Ok(e)
235 }
236 Err(e) => {
237 println!("Err: {:#}", e);
238 Err(e)
239 }
240 }
241 }
242
243 fn jsapi(
244 &mut self,
245 _sub_mchid: &str,
246 out_trade_no: &str,
247 description: &str,
248 total_fee: f64,
249 _notify_url: &str,
250 sp_openid: &str,
251 ) -> Result<JsonValue, String> {
252 self.app_auth_token = "202503BB5aba0791592f4cd9966ba090d36dfX11".to_string();
253 match self.http("alipay.trade.create", object! {
254 out_trade_no:out_trade_no,
255 total_amount:total_fee,
256 subject:description,
257 product_code:"JSAPI_PAY",
258 op_app_id:"2088541900270114",
259 buyer_open_id:sp_openid
260 }) {
261 Ok(e) => {
262 println!("{:#}", e);
263 }
264 Err(e) => {
265 println!("Err: {:#}", e);
266 return Err(e);
267 }
268 };
269
270
271 Ok(object! {})
272 }
273
274 fn close(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
275 todo!()
276 }
277
278 fn pay_query(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
279 todo!()
280 }
281
282 fn pay_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
283 todo!()
284 }
285
286 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> {
287 todo!()
288 }
289
290 fn refund_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
291 todo!()
292 }
293
294 fn refund_query(&mut self, _out_refund_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
295 todo!()
296 }
297}