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;
13
14#[derive(Clone)]
15pub struct AliPay {
16 pub appid: String,
18 pub sp_mchid: String,
20 pub app_auth_token: String,
22 pub app_private: String,
24 pub content_encryp: String,
26 pub alipay_public_key: String,
28 pub notify_url: String,
29}
30impl AliPay {
31 pub fn http(&mut self, method: &str, biz_content: JsonValue) -> Result<JsonValue, String> {
32 let mut http = br_http::Http::new();
33 let sign = "";
34
35 let now = Local::now();
36 let timestamp = now.format("%Y-%m-%d %H:%M:%S").to_string();
37
38
39 let mut data = object! {
40 "charset":"UTF-8",
41 "method":method,
42 "app_id":self.appid.clone(),
43 "version":"1.0",
44 "sign_type":"RSA2",
45 "timestamp":timestamp,
46 "app_auth_token":self.app_auth_token.clone(),
47 "sign":sign,
48 "biz_content": biz_content,
49 };
50
51 let mut map = HashMap::new();
52 for (key, value) in data.entries() {
53 if key == "sign" {
54 continue;
55 }
56 if value.is_empty() {
57 continue;
58 }
59 map.insert(key, value);
60 }
61
62 let mut keys: Vec<_> = map.keys().cloned().collect();
63 keys.sort();
64 let mut txt = vec![];
65 for key in keys {
66 txt.push(format!("{}={}", key, map.get(&key).unwrap()));
67 }
68 let txt = txt.join("&");
69 let t = self.app_private.as_bytes().chunks(64).map(|chunk| std::str::from_utf8(chunk).unwrap_or("")).collect::<Vec<&str>>().join("\n");
70 let cart = format!("-----BEGIN PRIVATE KEY-----\n{}\n-----END PRIVATE KEY-----\n", t);
71
72 let rsa = match Rsa::private_key_from_pem(cart.as_bytes()) {
74 Ok(e) => e,
75 Err(e) => return Err(e.to_string())
76 };
77
78 let pkey = match PKey::from_rsa(rsa) {
79 Ok(e) => e,
80 Err(e) => return Err(e.to_string())
81 };
82
83 let mut signer = match Signer::new(MessageDigest::sha256(), &pkey) {
85 Ok(e) => e,
86 Err(e) => return Err(e.to_string())
87 };
88 match signer.update(txt.as_bytes()) {
89 Ok(()) => {}
90 Err(e) => return Err(e.to_string())
91 };
92 let signature = match signer.sign_to_vec() {
94 Ok(e) => e,
95 Err(e) => return Err(e.to_string())
96 };
97 data["sign"] = general_purpose::STANDARD.encode(signature).into();
99
100 let mut new_data = object! {};
101 for (key, value) in data.entries() {
102 let t = encode(value.to_string().as_str()).to_string();
103 new_data[key] = t.into();
104 }
105 data = new_data;
106 let url = "https://openapi.alipay.com/gateway.do".to_string();
107 let res = match method {
108 "alipay.trade.wap.pay" => {
109 match http.post(url.as_str()).query(data).html() {
110 Ok(e) => {
111 return Ok(e.into());
112 }
113 Err(e) => {
114 println!(">>>>>>{}", e);
115 return Err(e);
116 }
117 }
118 }
119 _ => {
120 match http.post(url.as_str()).query(data).json() {
121 Ok(e) => e,
122 Err(e) => {
123 println!(">>>>>>{}", e);
124 return Err(e);
125 }
126 }
127 }
128 };
129 if res.has_key("error_response") {
130 return Err(res["error_response"]["sub_msg"].to_string());
131 }
132 let key = method.replace(".", "_");
133 let key = format!("{}_response", key);
134 let data = res[key].clone();
135 if data.has_key("code") {
136 if data["code"] != "10000" {
137 Err(data["sub_msg"].to_string())
138 } else {
139 Ok(data)
140 }
141 } else {
142 Err(data.to_string())
143 }
144 }
145}
146impl PayMode for AliPay {
147 fn config(&mut self) -> JsonValue {
148 todo!()
149 }
150
151 fn login(&mut self, code: &str) -> Result<JsonValue, String> {
152 let res = self.http("alipay.system.oauth.token", object! {
153 code:code
155 })?;
156 Ok(res)
157 }
158
159 fn auth(&mut self, code: &str) -> Result<JsonValue, String> {
160 let res = match self.http("alipay.open.auth.token.app", object! {
161 grant_type:"authorization_code",
162 code:code
163 }) {
164 Ok(e) => e,
165 Err(e) => {
166 error!("Err: {:#}", e);
167 return Err(e);
168 }
169 };
170 Ok(res)
176 }
177
178 fn pay(&mut self, types: Types, sub_mchid: &str, out_trade_no: &str, description: &str, total_fee: f64, sp_openid: &str) -> Result<JsonValue, String> {
179 let mut api = "";
180 let mut order = object! {
181 out_trade_no:out_trade_no,
182 total_amount:total_fee,
183 subject:description,
184 product_code:"",
185 op_app_id:sub_mchid,
186 buyer_open_id:sp_openid
187 };
188
189 match types {
190 Types::Jsapi => {
191 api = "alipay.trade.create";
192 order["product_code"] = "JSAPI_PAY".into();
193 }
194 Types::H5 => {
195 api = "alipay.trade.wap.pay";
196 order["product_code"] = "QUICK_WAP_WAY".into();
197 }
198 _ => {
199 order["product_code"] = "JSAPI_PAY".into();
200 }
201 };
202
203 match self.http(api, order) {
204 Ok(e) => {
205 Ok(e)
206 }
207 Err(e) => {
208 println!("Err: {:#}", e);
209 Err(e)
210 }
211 }
212 }
213
214 fn jsapi(
215 &mut self,
216 _sub_mchid: &str,
217 out_trade_no: &str,
218 description: &str,
219 total_fee: f64,
220 _notify_url: &str,
221 sp_openid: &str,
222 ) -> Result<JsonValue, String> {
223 self.app_auth_token = "202503BB5aba0791592f4cd9966ba090d36dfX11".to_string();
224 match self.http("alipay.trade.create", object! {
225 out_trade_no:out_trade_no,
226 total_amount:total_fee,
227 subject:description,
228 product_code:"JSAPI_PAY",
229 op_app_id:"2088541900270114",
230 buyer_open_id:sp_openid
231 }) {
232 Ok(e) => {
233 println!("{:#}", e);
234 }
235 Err(e) => {
236 println!("Err: {:#}", e);
237 return Err(e);
238 }
239 };
240
241
242 Ok(object! {})
243 }
244
245 fn close(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
246 todo!()
247 }
248
249 fn pay_query(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
250 todo!()
251 }
252
253 fn pay_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
254 todo!()
255 }
256
257 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> {
258 todo!()
259 }
260
261 fn refund_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
262 todo!()
263 }
264
265 fn refund_query(&mut self, _out_refund_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
266 todo!()
267 }
268}