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 sp_mchid: String,
13 pub notify_url: String,
15 pub posid: String,
17 pub branchid: String,
19 pub smername: String,
21 pub smertypeid: String,
23 pub smertype: String,
25}
26
27impl Ccbc {
28 pub fn http(&mut self, url: &str, mut body: JsonValue) -> Result<JsonValue, String> {
29 let mut mac = vec![];
30 for (key, value) in body.entries() {
31 if value.is_empty() {
32 continue;
33 }
34 mac.push(format!("{key}={value}"));
35 }
36 let mac = mac.join("&");
37 body["MAC"] = br_crypto::md5::encrypt_hex(mac.as_bytes()).into();
38 let mac = format!("{}&MAC={}", mac, body["MAC"]);
39
40 let mut http = br_reqwest::Client::new();
41 let url = format!("{url}&{mac}");
42 let send = http.post(url.as_str()).raw_json(body);
43 let res = send.header("Content-Type", "application/json").send()?;
44
45 match res.content_type().as_str() {
46 "text/html" => {
47 Err(res.body().to_string())
48 }
49 _ => {
50 match res.json() {
51 Ok(e) => Ok(e),
52 Err(e) => Err(e)
53 }
54 }
55 }
56 }
57 fn escape_unicode(&mut self, s: &str) -> String {
58 s.chars().map(|c| {
59 if c.is_ascii() {
60 c.to_string()
61 } else {
62 format!("%u{:04X}", c as u32)
63 }
64 }).collect::<String>()
65 }
66 fn _unescape_unicode(&mut self, s: &str) -> String {
67 let mut output = String::new();
68 let mut chars = s.chars().peekable();
69 while let Some(c) = chars.next() {
70 if c == '%' && chars.peek() == Some(&'u') {
71 chars.next(); let codepoint: String = chars.by_ref().take(4).collect();
73 if let Ok(value) = u32::from_str_radix(&codepoint, 16) {
74 if let Some(ch) = std::char::from_u32(value) {
75 output.push(ch);
76 }
77 }
78 } else {
79 output.push(c);
80 }
81 }
82 output
83 }
84}
85impl PayMode for Ccbc {
86 fn get_sub_mchid(&mut self, _sub_mchid: &str) -> Result<JsonValue, String> {
87 todo!()
88 }
89
90 fn notify(&mut self, _data: JsonValue) -> Result<JsonValue, String> {
91 todo!()
92 }
93
94 fn config(&mut self) -> JsonValue {
95 todo!()
96 }
97
98 fn login(&mut self, _code: &str) -> Result<JsonValue, String> {
99 todo!()
100 }
101
102 fn auth(&mut self, _code: &str) -> Result<JsonValue, String> {
103 todo!()
104 }
105
106 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> {
107 let mut body = object! {
108 MERCHANTID:self.sp_mchid.clone(),
109 POSID:self.posid.clone(),
110 BRANCHID:self.branchid.clone(),
111 ORDERID:out_trade_no,
112 PAYMENT:total_fee,
113 CURCODE:"01",
114 TXCODE:"",
115 MAC:"",
116 PUB:"21313123213"
117 };
118
119 let url = match channel {
120 "wechat" => "https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain?CCB_IBSVersion=V6",
121 "alipay" => "https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain?CCB_IBSVersion=V6",
122 _ => return Err(format!("Invalid channel: {channel}")),
123 };
124
125 match channel {
126 "wechat" => {
127 body["TXCODE"] = "530590".into();
128 body["TYPE"] = "1".into();
129 body["GATEWAY"] = "0".into();
130
131 body["TRADE_TYPE"] = match types {
132 Types::Jsapi => "JSAPI",
133 Types::MiniJsapi => "MINIPRO",
134 _ => return Err(format!("Invalid channel: {types:?}")),
135 }.into();
136
137 body["SUB_APPID"] = self.appid.clone().into();
138 body["SUB_OPENID"] = sp_openid.into();
139
140
141 body["SMERID"] = sub_mchid.into();
142 body["SMERNAME"] = self.escape_unicode(self.smername.clone().as_str()).into();
143 body["SMERTYPEID"] = self.smertypeid.clone().into();
144 body["SMERTYPE"] = self.escape_unicode(self.smertype.clone().as_str()).into();
145 body["TRADECODE"] = "交易类型代码".into();
146 body["TRADENAME"] = self.escape_unicode("消费").into();
147
148 body["SMEPROTYPE"] = "商品类别代码".into();
149 body["PRONAME"] = self.escape_unicode("商品").into();
150 }
151 "alipay" => {
152 body["TXCODE"] = "530591".into();
153 body["TRADE_TYPE"] = match types {
154 Types::Jsapi => "JSAPI",
155 Types::MiniJsapi => "JSAPI",
156 _ => return Err(format!("Invalid channel: {types:?}")),
157 }.into();
158 body["USERID"] = sp_openid.into();
159 }
160 _ => return Err(format!("Invalid channel: {channel}")),
161 }
162
163 let res = self.http(url, body)?;
164 Ok(res)
165 }
166
167 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> {
168 let mut body = object! {
169 MERCHANTID:self.sp_mchid.clone(),
170 POSID:self.posid.clone(),
171 BRANCHID:self.branchid.clone(),
172 ccbParam:"",
173 TXCODE:"PAY100",
174 MERFLAG:"1",
175 ORDERID:out_trade_no,
176 QRCODE:auth_code,
177 AMOUNT:total_fee,
178 PROINFO:"商品名称",
179 REMARK1:description
180 };
181
182 let url = match channel {
183 "wechat" => "https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain?CCB_IBSVersion=V6",
184 "alipay" => "https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain?CCB_IBSVersion=V6",
185 _ => return Err(format!("Invalid channel: {channel}")),
186 };
187
188 match channel {
189 "wechat" => {
190 body["SUB_APPID"] = self.appid.clone().into();
191 }
192 "alipay" => {
193
194 }
195 _ => return Err(format!("Invalid channel: {channel}")),
196 }
197
198 let res = self.http(url, body)?;
199 Ok(res)
200 }
201
202 fn close(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
203 todo!()
204 }
205
206 fn pay_query(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
207 todo!()
208 }
209
210 fn pay_micropay_query(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
211 todo!()
212 }
213
214 fn pay_notify(&mut self, _nonce: &str,_ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
215 todo!()
216 }
217
218 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> {
219 todo!()
220 }
221
222 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> {
223 todo!()
224 }
225
226 fn refund_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
227 todo!()
228 }
229
230 fn refund_query(&mut self, _trade_no: &str, _out_refund_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
231 todo!()
232 }
233
234 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> {
235 todo!()
236 }
237}