br_pay/
ccbc.rs

1use json::{object, JsonValue};
2use crate::{PayMode, Types};
3
4/// 建设银行
5#[derive(Clone, Debug)]
6pub struct Ccbc {
7    /// 服务商APPID
8    pub appid: String,
9    /// 密钥
10    pub secret: String,
11    /// 银行商户号
12    pub mchid: String,
13    /// 微信商户号
14    pub sp_mchid: String,
15    /// 通知地址
16    pub notify_url: String,
17    /// 商户柜台代码
18    pub posid: String,
19    /// 分行代码
20    pub branchid: String,
21    /// 二级商户名称
22    pub smername: String,
23    /// 二级商户类别代码
24    pub smertypeid: String,
25    /// 二级商户类别名称
26    pub smertype: String,
27    /// 二级商户公钥
28    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(); // consume 'u'
92                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                //body["SMERID"] = sub_mchid.into();
168                //body["SMERNAME"] = self.escape_unicode(self.smername.clone().as_str()).into();
169                //body["SMERTYPEID"] = self.smertypeid.clone().into();
170                //body["SMERTYPE"] = self.escape_unicode(self.smertype.clone().as_str()).into();
171
172                //body["SMERNAME"] = self.escape_unicode(self.smername.clone().as_str()).into();
173                //body["SMERTYPEID"] = 1.into();
174                //body["SMERTYPE"] = self.escape_unicode("宾馆餐娱类").into();
175
176                //body["TRADECODE"] = "交易类型代码".into();
177                //body["TRADENAME"] = self.escape_unicode("消费").into();
178                //body["SMEPROTYPE"] = "商品类别代码".into();
179                //body["PRONAME"] = self.escape_unicode("商品").into();
180            }
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}