1use chrono::Local;
2use json::{object, JsonValue};
3use crate::{PayMode, Types};
4
5#[derive(Clone, Debug)]
7pub struct Yrcc {
8 pub appid: String,
10 pub secret: String,
12 pub terminal_number: String,
14 pub sp_mchid: String,
16 pub service_provider: String,
18 pub key_name: String,
20 pub app_private: String,
22 pub app_public: String,
24 pub notify_url: String,
26
27}
28impl Yrcc {
29 pub fn http(&mut self, service: &str, mut body: JsonValue) -> Result<JsonValue, String> {
30
31 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 = self.sign(body.clone())?;
37 let mut http = br_reqwest::Client::new();
47 let url = format!("https://cloud.ynrcc.com/YNCashier/acqu/nosign/{service}");
48 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 fn config(&mut self) -> JsonValue {
86 todo!()
87 }
88
89
90 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> {
91 let service = match types {
92 Types::Jsapi => "MPCreateTrade",
93 Types::Native => "",
94 Types::H5 => "",
95 Types::MiniJsapi => "MPCreateTrade",
96 Types::App => "",
97 Types::Micropay => ""
98 };
99 let now = Local::now();
101 let formatted = now.format("%Y%m%d%H%M%S").to_string();
102
103 let total = format!("{:.0}", total_fee * 100.0);
104 let body = object! {
105 "service"=>service,
106 "apiVersion"=>"1.1.0",
107 "tranCode"=>"120201",
108 "merId"=>self.sp_mchid.clone(),
109 "temId"=>self.terminal_number.clone(),
110 "accessProviderCode" => self.service_provider.clone(),
111 "seqNo"=>"",
112 "txnTime"=> formatted,
113 "tradeNo"=>out_trade_no,
114 "tradeChannel"=>"01",
115 "businessType"=>"001",
116 "totalAmt"=> total,
117 "totalNum"=>1,
118 "orderDesc"=>description,
119 "onlineFlag"=>"1",
120 "eventFlag"=>"1",
121 "ccy"=>"156",
122 "subOpenId"=>sp_openid,
123 "subAppId"=>self.appid.clone(),
124 "notifyUrl"=>self.notify_url.clone(),
125 };
126 match self.http(service, body) {
127 Ok(e) => {
128 match types {
129 Types::Native => {
130 if e.has_key("code_url") {
131 Ok(e["code_url"].clone())
132 } else {
133 Err(e["message"].to_string())
134 }
135 }
136 Types::Jsapi | Types::MiniJsapi => {
137 if e.has_key("prepay_id") {
138 let signinfo = "".into();
140 Ok(signinfo)
141 } else {
142 Err(e["message"].to_string())
143 }
144 }
145 _ => {
146 Ok(e)
147 }
148 }
149 }
150 Err(e) => Err(e),
151 }
152 }
153
154 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> {
155 todo!()
156 }
157
158 fn close(&mut self, _out_trade_no: &str, _sub_mchid: &str, _channel: &str) -> Result<JsonValue, String> {
159 todo!()
160 }
161
162 fn pay_query(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
163 todo!()
164 }
165
166 fn pay_micropay_query(&mut self, _out_trade_no: &str, _sub_mchid: &str, _channel: &str) -> Result<JsonValue, String> {
167 todo!()
168 }
169
170 fn pay_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
171 todo!()
172 }
173
174 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> {
175 todo!()
176 }
177
178 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> {
179 todo!()
180 }
181
182 fn refund_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
183 todo!()
184 }
185
186 fn refund_query(&mut self, _trade_no: &str, _out_refund_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
187 todo!()
188 }
189 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> {
190 todo!()
191 }
192}