br_pay/
yrcc.rs

1use chrono::Local;
2use json::{object, JsonValue};
3use crate::{PayMode, Types};
4
5/// 农村商业银行
6#[derive(Clone, Debug)]
7pub struct Yrcc {
8    /// 服务商APPID
9    pub appid: String,
10    /// 密钥
11    pub secret: String,
12    /// 终端号
13    pub terminal_number: String,
14    /// 服务商商户号
15    pub sp_mchid: String,
16    /// 服务商号
17    pub service_provider: String,
18    /// 密钥名称
19    pub key_name: String,
20    /// SM2私钥
21    pub app_private: String,
22    /// 银行公钥
23    pub app_public: String,
24    /// 通知
25    pub notify_url: String,
26
27}
28impl Yrcc {
29    pub fn http(&mut self, service: &str, mut body: JsonValue) -> Result<JsonValue, String> {
30
31        // 获取当前本地时间
32        let now = Local::now();
33        let formatted = now.format("%Y%m%d%H%M%S").to_string();
34        body["seqNo"] = formatted.into();
35        //let _sign = "4A227003C3D07580397471B80E4B8384D036BBB7D5D8A7B1A84173EA555455EC688C04E9B3F2AF0E0FEE7C349E6D1252859FDA66A928123F70BCD68064AAAC83";
36        let sign = self.sign(body.clone())?;
37        // 交易时间,格式:YYYYMMDDhhmmss
38        //let headers = object! {
39        //    "service" => service,
40        //    "apiVersion"=> "1.1.0",
41        //    "seqNo"=>"",
42        //    "txnTime"=>formatted
43        //};
44
45
46        let mut http = br_reqwest::Client::new();
47        let url = format!("https://cloud.ynrcc.com/YNCashier/acqu/nosign/{service}");
48        //let url = format!("https://cloud.ynrcc.com/YNCashier/acqu/{service}");
49        let send = http.post(url.as_str()).raw_json(object! {
50            request:body
51        });
52        match send.header("ynrcc-cert", self.key_name.as_str()).header("ynrcc-sign", &sign).send()?.json() {
53            Ok(e) => {
54                if !e.has_key("response") {
55                    return Err("响应格式错误".to_string());
56                }
57                let response = e["response"].clone();
58                let code = response["code"].as_str().unwrap_or("");
59                if code != "000000" {
60                    return Err(response["msg"].to_string());
61                }
62                println!("{:#}", e["response"]);
63                Ok(e)
64            }
65            Err(e) => Err(e)
66        }
67    }
68
69    fn sign(&self, body: JsonValue) -> Result<String, String> {
70        let body_str = body.to_string();
71        let r = br_crypto::sha256::encrypt_byte(body_str.as_bytes());
72        let res = br_crypto::sm2::sign_rs(&self.app_private.clone(), &r);
73        println!("{}", res.len());
74        Ok(res.to_uppercase())
75    }
76}
77impl PayMode for Yrcc {
78    fn check(&mut self) -> Result<bool, String> {
79        todo!()
80    }
81
82    fn get_sub_mchid(&mut self, _sub_mchid: &str) -> Result<JsonValue, String> {
83        todo!()
84    }
85
86    fn notify(&mut self, _data: JsonValue) -> Result<JsonValue, String> {
87        todo!()
88    }
89
90    fn config(&mut self) -> JsonValue {
91        todo!()
92    }
93    
94
95    fn auth(&mut self, _code: &str) -> Result<JsonValue, String> {
96        todo!()
97    }
98
99    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> {
100        let service = match types {
101            Types::Jsapi => "MPCreateTrade",
102            Types::Native => "",
103            Types::H5 => "",
104            Types::MiniJsapi => "MPCreateTrade",
105            Types::App => "",
106            Types::Micropay => ""
107        };
108        // 获取当前本地时间
109        let now = Local::now();
110        let formatted = now.format("%Y%m%d%H%M%S").to_string();
111
112        let total = format!("{:.0}", total_fee * 100.0);
113        let body = object! {
114            "service"=>service,
115            "apiVersion"=>"1.1.0",
116            "tranCode"=>"120201",
117            "merId"=>self.sp_mchid.clone(),
118            "temId"=>self.terminal_number.clone(),
119            "accessProviderCode" => self.service_provider.clone(),
120            "seqNo"=>"",
121            "txnTime"=> formatted,
122            "tradeNo"=>out_trade_no,
123            "tradeChannel"=>"01",
124            "businessType"=>"001",
125            "totalAmt"=> total,
126            "totalNum"=>1,
127            "orderDesc"=>description,
128            "onlineFlag"=>"1",
129            "eventFlag"=>"1",
130            "ccy"=>"156",
131            "subOpenId"=>sp_openid,
132            "subAppId"=>self.appid.clone(),
133            "notifyUrl"=>self.notify_url.clone(),
134        };
135        match self.http(service, body) {
136            Ok(e) => {
137                match types {
138                    Types::Native => {
139                        if e.has_key("code_url") {
140                            Ok(e["code_url"].clone())
141                        } else {
142                            Err(e["message"].to_string())
143                        }
144                    }
145                    Types::Jsapi | Types::MiniJsapi => {
146                        if e.has_key("prepay_id") {
147                            //let signinfo = self.paysign(format!("prepay_id={}", e["prepay_id"]).as_str())?;
148                            let signinfo = "".into();
149                            Ok(signinfo)
150                        } else {
151                            Err(e["message"].to_string())
152                        }
153                    }
154                    _ => {
155                        Ok(e)
156                    }
157                }
158            }
159            Err(e) => Err(e),
160        }
161    }
162
163    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> {
164        todo!()
165    }
166
167    fn close(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
168        todo!()
169    }
170
171    fn pay_query(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
172        todo!()
173    }
174
175    fn pay_micropay_query(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
176        todo!()
177    }
178
179    fn pay_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
180        todo!()
181    }
182
183    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> {
184        todo!()
185    }
186
187    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> {
188        todo!()
189    }
190
191    fn refund_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
192        todo!()
193    }
194
195    fn refund_query(&mut self, _trade_no: &str, _out_refund_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
196        todo!()
197    }
198    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> {
199        todo!()
200    }
201}