1use json::{object, JsonValue};
2use crate::{PayMode, Types};
3
4#[derive(Clone, Debug)]
6pub struct Ccbc {
7 pub appid: String,
9 pub secret: String,
11 pub mchid: String,
13 pub sp_mchid: String,
15 pub notify_url: String,
17 pub posid: String,
19 pub branchid: String,
21 pub smername: String,
23 pub smertypeid: String,
25 pub smertype: String,
27 pub public_key: String,
29 pub client_ip: String,
30
31}
32
33impl Ccbc {
34 pub fn http(&mut self, url: &str, mut body: JsonValue) -> Result<JsonValue, String> {
35 let mut mac = vec![];
36 let mut path = vec![];
37 let fields = ["MAC"];
38 for (key, value) in body.entries() {
39 if value.is_empty() && fields.contains(&key) {
40 continue;
41 }
42 if key != "PUB" {
43 path.push(format!("{key}={value}"));
44 }
45 mac.push(format!("{key}={value}"));
46 }
47
48
49 let mac_text = mac.join("&");
50 let path = path.join("&");
51 body["MAC"] = br_crypto::md5::encrypt_hex(mac_text.as_bytes()).into();
52 body.remove("PUB");
53 let mac = format!("{}&MAC={}", path, body["MAC"]);
54
55
56 let mut http = br_reqwest::Client::new();
57 let url = format!("{url}&{mac}");
58 println!("url: {url}");
59 println!("body: {body:#}");
60 let send = http.post(url.as_str()).raw_json(body);
61 let res = send.header("Content-Type", "application/json").send()?;
62 match res.content_type().as_str() {
63 "text/html" => {
64 match res.json() {
65 Ok(e) => Ok(e),
66 Err(_) => Err(res.body().to_string())
67 }
68 }
69 _ => {
70 match res.json() {
71 Ok(e) => Ok(e),
72 Err(e) => Err(e)
73 }
74 }
75 }
76 }
77 fn escape_unicode(&mut self, s: &str) -> String {
78 s.chars().map(|c| {
79 if c.is_ascii() {
80 c.to_string()
81 } else {
82 format!("%u{:04X}", c as u32)
83 }
84 }).collect::<String>()
85 }
86 fn _unescape_unicode(&mut self, s: &str) -> String {
87 let mut output = String::new();
88 let mut chars = s.chars().peekable();
89 while let Some(c) = chars.next() {
90 if c == '%' && chars.peek() == Some(&'u') {
91 chars.next(); let codepoint: String = chars.by_ref().take(4).collect();
93 if let Ok(value) = u32::from_str_radix(&codepoint, 16) {
94 if let Some(ch) = std::char::from_u32(value) {
95 output.push(ch);
96 }
97 }
98 } else {
99 output.push(c);
100 }
101 }
102 output
103 }
104}
105impl PayMode for Ccbc {
106 fn check(&mut self) -> Result<bool, String> {
107 todo!()
108 }
109
110 fn get_sub_mchid(&mut self, _sub_mchid: &str) -> Result<JsonValue, String> {
111 todo!()
112 }
113
114 fn notify(&mut self, _data: JsonValue) -> Result<JsonValue, String> {
115 todo!()
116 }
117
118 fn config(&mut self) -> JsonValue {
119 todo!()
120 }
121
122
123 fn auth(&mut self, _code: &str) -> Result<JsonValue, String> {
124 todo!()
125 }
126
127 fn pay(&mut self, channel: &str, types: Types, _sub_mchid: &str, out_trade_no: &str, description: &str, total_fee: f64, sp_openid: &str) -> Result<JsonValue, String> {
128 let pubtext = self.public_key[0..30].to_string();
129 let mut body = object! {
130 MERCHANTID:self.mchid.clone(),
131 POSID:self.posid.clone(),
132 BRANCHID:self.branchid.clone(),
133 ORDERID:out_trade_no,
134 PAYMENT:total_fee,
135 CURCODE:"01",
136 TXCODE:"530590",
137 REMARK1:"",
138 REMARK2:"",
139 TYPE:"1",
140 PUB:pubtext,
141 GATEWAY:"0",
142 CLIENTIP:self.client_ip.clone(),
143 REGINFO:self.escape_unicode(&self.smername.clone()),
144 PROINFO: self.escape_unicode(description),
145 REFERER:"",
146 TRADE_TYPE:"",
147 MAC:"",
148 };
149
150 let url = match channel {
151 "wechat" => "https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain?CCB_IBSVersion=V6",
152 "alipay" => "https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain?CCB_IBSVersion=V6",
153 _ => return Err(format!("Invalid channel: {channel}")),
154 };
155
156 match channel {
157 "wechat" => {
158 body["TRADE_TYPE"] = match types {
159 Types::Jsapi => "JSAPI",
160 Types::MiniJsapi => "MINIPRO",
161 _ => return Err(format!("Invalid channel: {types:?}")),
162 }.into();
163 body["SUB_APPID"] = self.appid.clone().into();
164 body["SUB_OPENID"] = sp_openid.into();
165 body["WX_CHANNELID"] = self.sp_mchid.clone().into();
166
167 }
181 "alipay" => {
182 body["TXCODE"] = "530591".into();
183 body["TRADE_TYPE"] = match types {
184 Types::Jsapi => "JSAPI",
185 Types::MiniJsapi => "JSAPI",
186 _ => return Err(format!("Invalid channel: {types:?}")),
187 }.into();
188 body["USERID"] = sp_openid.into();
189 }
190 _ => return Err(format!("Invalid channel: {channel}")),
191 }
192 let res = self.http(url, body)?;
193 match types {
194 Types::Jsapi | Types::MiniJsapi => {
195 if res.has_key("PAYURL") {
196 let url = res["PAYURL"].to_string();
197 let mut http = br_reqwest::Client::new();
198 let re = http.post(url.as_str()).send()?;
199 let res = re.json()?;
200 if res.has_key("ERRCODE") && res["ERRCODE"] != "000000" {
201 return Err(format!("获取支付参数: 错误码: [{}] {} 失败", res["ERRCODE"], res["ERRMSG"]));
202 }
203 Ok(res)
204 } else {
205 Err(res.to_string())
206 }
207 }
208 _ => {
209 Ok(res)
210 }
211 }
212 }
213
214 fn micropay(&mut self, channel: &str, auth_code: &str, _sub_mchid: &str, out_trade_no: &str, description: &str, total_fee: f64, _org_openid: &str, _ip: &str) -> Result<JsonValue, String> {
215 let mut body = object! {
216 MERCHANTID:self.sp_mchid.clone(),
217 POSID:self.posid.clone(),
218 BRANCHID:self.branchid.clone(),
219 ccbParam:"",
220 TXCODE:"PAY100",
221 MERFLAG:"1",
222 ORDERID:out_trade_no,
223 QRCODE:auth_code,
224 AMOUNT:total_fee,
225 PROINFO:"商品名称",
226 REMARK1:description
227 };
228
229 let url = match channel {
230 "wechat" => "https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain?CCB_IBSVersion=V6",
231 "alipay" => "https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain?CCB_IBSVersion=V6",
232 _ => return Err(format!("Invalid channel: {channel}")),
233 };
234
235 match channel {
236 "wechat" => {
237 body["SUB_APPID"] = self.appid.clone().into();
238 }
239 "alipay" => {}
240 _ => return Err(format!("Invalid channel: {channel}")),
241 }
242
243 let res = self.http(url, body)?;
244 Ok(res)
245 }
246
247 fn close(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
248 todo!()
249 }
250
251 fn pay_query(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
252 todo!()
253 }
254
255 fn pay_micropay_query(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
256 todo!()
257 }
258
259 fn pay_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
260 todo!()
261 }
262
263 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> {
264 todo!()
265 }
266
267 fn micropay_refund(&mut self, _sub_mchid: &str, _out_trade_no: &str, _transaction_id: &str, _out_refund_no: &str, _amount: f64, _total: f64, _currency: &str, _refund_text: &str) -> Result<JsonValue, String> {
268 todo!()
269 }
270
271 fn refund_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
272 todo!()
273 }
274
275 fn refund_query(&mut self, _trade_no: &str, _out_refund_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
276 todo!()
277 }
278
279 fn incoming(&mut self, _business_code: &str, _contact_info: JsonValue, _subject_info: JsonValue, _business_info: JsonValue, _settlement_info: JsonValue, _bank_account_info: JsonValue) -> Result<JsonValue, String> {
280 todo!()
281 }
282}