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 auth(&mut self, _code: &str) -> Result<JsonValue, String> {
91 todo!()
92 }
93
94 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> {
95 let service = match types {
96 Types::Jsapi => "MPCreateTrade",
97 Types::Native => "",
98 Types::H5 => "",
99 Types::MiniJsapi => "MPCreateTrade",
100 Types::App => "",
101 Types::Micropay => ""
102 };
103 let now = Local::now();
105 let formatted = now.format("%Y%m%d%H%M%S").to_string();
106
107 let total = format!("{:.0}", total_fee * 100.0);
108 let body = object! {
109 "service"=>service,
110 "apiVersion"=>"1.1.0",
111 "tranCode"=>"120201",
112 "merId"=>self.sp_mchid.clone(),
113 "temId"=>self.terminal_number.clone(),
114 "accessProviderCode" => self.service_provider.clone(),
115 "seqNo"=>"",
116 "txnTime"=> formatted,
117 "tradeNo"=>out_trade_no,
118 "tradeChannel"=>"01",
119 "businessType"=>"001",
120 "totalAmt"=> total,
121 "totalNum"=>1,
122 "orderDesc"=>description,
123 "onlineFlag"=>"1",
124 "eventFlag"=>"1",
125 "ccy"=>"156",
126 "subOpenId"=>sp_openid,
127 "subAppId"=>self.appid.clone(),
128 "notifyUrl"=>self.notify_url.clone(),
129 };
130 match self.http(service, body) {
131 Ok(e) => {
132 match types {
133 Types::Native => {
134 if e.has_key("code_url") {
135 Ok(e["code_url"].clone())
136 } else {
137 Err(e["message"].to_string())
138 }
139 }
140 Types::Jsapi | Types::MiniJsapi => {
141 if e.has_key("prepay_id") {
142 let signinfo = "".into();
144 Ok(signinfo)
145 } else {
146 Err(e["message"].to_string())
147 }
148 }
149 _ => {
150 Ok(e)
151 }
152 }
153 }
154 Err(e) => Err(e),
155 }
156 }
157
158 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> {
159 todo!()
160 }
161
162 fn close(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
163 todo!()
164 }
165
166 fn pay_query(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
167 todo!()
168 }
169
170 fn pay_micropay_query(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
171 todo!()
172 }
173
174 fn pay_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
175 todo!()
176 }
177
178 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> {
179 todo!()
180 }
181
182 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> {
183 todo!()
184 }
185
186 fn refund_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
187 todo!()
188 }
189
190 fn refund_query(&mut self, _trade_no: &str, _out_refund_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
191 todo!()
192 }
193 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> {
194 todo!()
195 }
196}