wxpay-rs 2.0.1

WeChat Pay API v3 Rust SDK
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! 客户端模块
//!
//! 提供微信支付客户端的统一入口。

pub mod builder;

pub use builder::WxPayClientBuilder;

use std::sync::Arc;

use crate::auth::{Credentials, Sha256RsaSigner, Sha256RsaVerifier, Signer, Verifier};
use crate::cert::CertManager;
use crate::config::WxPayConfig;
use crate::error::WxPayResult;
use crate::http::{HttpClient, ReqwestHttpClient};
use crate::notify::NotifyHandler;
use crate::services::certificate::CertificateService;
use crate::services::payments::{AppService, H5Service, JsapiService, NativeService};
use crate::services::profit_sharing::{
    ProfitSharingFinishRequest, ProfitSharingFinishResponse, ProfitSharingRequest,
    ProfitSharingResponse, ProfitSharingService, QueryProfitSharingRequest,
};
use crate::services::query::{QueryService, Transaction};
use crate::services::refund::{RefundResponse, RefundService};
use crate::services::transfer::{TransferRequest, TransferResponse, TransferService};
use crate::services::transport::TransportObserver;

/// 微信支付客户端
///
/// 这是 SDK 的主入口,提供了访问所有微信支付 API 的方法。
///
/// # 示例
///
/// ```rust,no_run
/// use wxpay_rs::{WxPayClient, WxPayConfig};
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let config = WxPayConfig::builder()
///         .app_id("wx88888888")
///         .merchant_id("1900000109")
///         .api_v3_key("abcdefghijklmnopqrstuvwxyz123456")
///         .private_key_from_file("path/to/private_key.pem")
///         .cert_serial_number("CERT123456")
///         .build()?;
///
///     let client = WxPayClient::new(config).await?;
///
///     // 使用 JSAPI 服务
///     let jsapi = client.jsapi();
///     let _ = jsapi;
///
///     Ok(())
/// }
/// ```
pub struct WxPayClient {
    /// 配置信息
    config: Arc<WxPayConfig>,

    /// HTTP 客户端
    #[allow(dead_code)]
    http_client: Arc<dyn HttpClient>,

    /// 凭证管理器
    credentials: Arc<Credentials>,

    /// 签名器
    signer: Arc<dyn Signer>,

    /// 验签器
    verifier: Arc<dyn Verifier>,

    /// 证书管理器
    cert_manager: Arc<CertManager>,

    /// 服务注册表
    services: ServiceRegistry,
}

/// 服务注册表
struct ServiceRegistry {
    jsapi: Arc<JsapiService>,
    native: Arc<NativeService>,
    h5: Arc<H5Service>,
    app: Arc<AppService>,
    refund: Arc<RefundService>,
    transfer: Arc<TransferService>,
    profitsharing: Arc<ProfitSharingService>,
    certificates: Arc<CertificateService>,
    query: Arc<QueryService>,
}

impl WxPayClient {
    fn create_services(
        config: &Arc<WxPayConfig>,
        http_client: &Arc<dyn HttpClient>,
        signer: &Arc<dyn Signer>,
        transport_observer: Option<Arc<dyn TransportObserver>>,
    ) -> ServiceRegistry {
        ServiceRegistry {
            jsapi: Arc::new(JsapiService::new_with_observer(
                config.clone(),
                http_client.clone(),
                signer.clone(),
                transport_observer.clone(),
            )),
            native: Arc::new(NativeService::new_with_observer(
                config.clone(),
                http_client.clone(),
                signer.clone(),
                transport_observer.clone(),
            )),
            h5: Arc::new(H5Service::new_with_observer(
                config.clone(),
                http_client.clone(),
                signer.clone(),
                transport_observer.clone(),
            )),
            app: Arc::new(AppService::new_with_observer(
                config.clone(),
                http_client.clone(),
                signer.clone(),
                transport_observer.clone(),
            )),
            refund: Arc::new(RefundService::new_with_observer(
                config.clone(),
                http_client.clone(),
                signer.clone(),
                transport_observer.clone(),
            )),
            transfer: Arc::new(TransferService::new_with_observer(
                config.clone(),
                http_client.clone(),
                signer.clone(),
                transport_observer.clone(),
            )),
            profitsharing: Arc::new(ProfitSharingService::new_with_observer(
                config.clone(),
                http_client.clone(),
                signer.clone(),
                transport_observer.clone(),
            )),
            certificates: Arc::new(CertificateService::new_with_observer(
                config.clone(),
                http_client.clone(),
                signer.clone(),
                transport_observer.clone(),
            )),
            query: Arc::new(QueryService::new_with_observer(
                config.clone(),
                http_client.clone(),
                signer.clone(),
                transport_observer,
            )),
        }
    }

    pub(crate) async fn new_with_components(
        config: WxPayConfig,
        http_client: Arc<dyn HttpClient>,
        signer: Arc<dyn Signer>,
        verifier: Arc<dyn Verifier>,
        cert_manager: Arc<CertManager>,
        transport_observer: Option<Arc<dyn TransportObserver>>,
    ) -> WxPayResult<Self> {
        let config = Arc::new(config);
        let credentials = Arc::new(Credentials::from_config(&config));
        let services = Self::create_services(&config, &http_client, &signer, transport_observer);

        Ok(Self {
            config,
            http_client,
            credentials,
            signer,
            verifier,
            cert_manager,
            services,
        })
    }

    /// 创建新的微信支付客户端
    ///
    /// # 参数
    ///
    /// * `config` - 配置信息
    ///
    /// # 返回
    ///
    /// 返回客户端实例
    pub async fn new(config: WxPayConfig) -> WxPayResult<Self> {
        let config = Arc::new(config);
        let http_client: Arc<dyn HttpClient> = Arc::new(
            ReqwestHttpClient::builder()
                .timeout(config.timeout)
                .max_retries(config.max_retries)
                .build()?,
        );
        let signer: Arc<dyn Signer> = Arc::new(Sha256RsaSigner::new(
            &config.merchant_id,
            &config.private_key,
            &config.cert_serial_number,
        )?);
        let verifier: Arc<dyn Verifier> = Arc::new(Sha256RsaVerifier::new(
            config.platform_certificates.clone(),
        )?);
        let cert_manager = Arc::new(CertManager::new());

        Self::new_with_components(
            (*config).clone(),
            http_client,
            signer,
            verifier,
            cert_manager,
            None,
        )
        .await
    }

    /// 创建客户端构建器
    pub fn builder() -> WxPayClientBuilder {
        WxPayClientBuilder::new()
    }

    /// 获取配置信息
    pub fn config(&self) -> &WxPayConfig {
        &self.config
    }

    /// 获取凭证管理器
    pub fn credentials(&self) -> &Credentials {
        &self.credentials
    }

    /// 获取签名器
    pub fn signer(&self) -> &dyn Signer {
        self.signer.as_ref()
    }

    /// 获取验签器
    pub fn verifier(&self) -> &dyn Verifier {
        self.verifier.as_ref()
    }

    /// 获取证书管理器
    pub fn cert_manager(&self) -> &CertManager {
        &self.cert_manager
    }

    /// 获取 JSAPI 服务
    pub fn jsapi(&self) -> &JsapiService {
        &self.services.jsapi
    }

    /// 获取 Native 服务
    pub fn native(&self) -> &NativeService {
        &self.services.native
    }

    /// 获取 H5 服务
    pub fn h5(&self) -> &H5Service {
        &self.services.h5
    }

    /// 获取 APP 服务
    pub fn app(&self) -> &AppService {
        &self.services.app
    }

    /// 获取退款服务
    pub fn refund(&self) -> &RefundService {
        &self.services.refund
    }

    /// 获取退款服务(兼容 `wechatpay-go` 的 `refunddomestic` 命名)
    pub fn refunddomestic(&self) -> &RefundService {
        self.refund()
    }

    /// 获取转账服务
    pub fn transfer(&self) -> &TransferService {
        &self.services.transfer
    }

    /// 获取转账服务(兼容 `wechatpay-go` 的 `transferbatch` 命名)
    pub fn transferbatch(&self) -> &TransferService {
        self.transfer()
    }

    /// 获取分账服务
    pub fn profit_sharing(&self) -> &ProfitSharingService {
        &self.services.profitsharing
    }

    /// 获取分账服务(兼容 `wechatpay-go` 的 `profitsharing` 命名)
    pub fn profitsharing(&self) -> &ProfitSharingService {
        self.profit_sharing()
    }

    /// 获取证书服务
    pub fn certificates(&self) -> &CertificateService {
        &self.services.certificates
    }

    /// 获取查询服务
    pub fn query(&self) -> &QueryService {
        &self.services.query
    }

    /// 通过商户订单号查询订单(兼容 `wechatpay-go` 风格快捷入口)
    pub async fn query_order_by_out_trade_no(
        &self,
        out_trade_no: &str,
    ) -> WxPayResult<Transaction> {
        self.query().query_order_by_out_trade_no(out_trade_no).await
    }

    /// 通过微信支付订单号查询订单(兼容 `wechatpay-go` 风格快捷入口)
    pub async fn query_order_by_id(&self, transaction_id: &str) -> WxPayResult<Transaction> {
        self.query().query_order_by_id(transaction_id).await
    }

    /// 按商户退款单号查询退款(兼容 `wechatpay-go` 风格快捷入口)
    pub async fn query_by_out_refund_no(&self, out_refund_no: &str) -> WxPayResult<RefundResponse> {
        self.refunddomestic()
            .query_by_out_refund_no(out_refund_no)
            .await
    }

    /// 发起批量转账(兼容 `wechatpay-go` 风格快捷入口)
    pub async fn initiate_batch_transfer(
        &self,
        request: &TransferRequest,
    ) -> WxPayResult<TransferResponse> {
        self.transferbatch().initiate_batch_transfer(request).await
    }

    /// 按商户批次单号查询转账批次(兼容 `wechatpay-go` 风格快捷入口)
    pub async fn get_transfer_batch_by_out_batch_no(
        &self,
        out_batch_no: &str,
    ) -> WxPayResult<TransferResponse> {
        self.transferbatch()
            .get_transfer_batch_by_out_batch_no(out_batch_no)
            .await
    }

    /// 创建分账单(兼容 `wechatpay-go` 风格快捷入口)
    pub async fn create_profit_sharing_order(
        &self,
        request: &ProfitSharingRequest,
    ) -> WxPayResult<ProfitSharingResponse> {
        self.profitsharing().create_order(request).await
    }

    /// 查询分账单(兼容 `wechatpay-go` 风格快捷入口)
    pub async fn query_profit_sharing_order(
        &self,
        request: &QueryProfitSharingRequest,
    ) -> WxPayResult<ProfitSharingResponse> {
        self.profitsharing().query_order(request).await
    }

    /// 完成分账单(兼容 `wechatpay-go` 风格快捷入口)
    pub async fn finish_profit_sharing_order(
        &self,
        request: &ProfitSharingFinishRequest,
    ) -> WxPayResult<ProfitSharingFinishResponse> {
        self.profitsharing().finish_order(request).await
    }

    /// 创建通知处理器
    pub fn notify_handler(&self) -> WxPayResult<NotifyHandler> {
        let config = crate::config::NotifyConfig::builder()
            .api_v3_key(&self.config.api_v3_key)
            .cert_serial_number(&self.config.cert_serial_number)
            .platform_certificate(
                self.config
                    .platform_certificates
                    .first()
                    .cloned()
                    .unwrap_or_default(),
            )
            .build()?;

        NotifyHandler::new(config, self.verifier.clone())
    }
}

impl std::fmt::Debug for WxPayClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WxPayClient")
            .field("config", &self.config)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::services::profit_sharing::ProfitSharingService;
    use crate::services::refund::RefundService;
    use crate::services::transfer::TransferService;

    fn create_test_config() -> WxPayConfig {
        WxPayConfig::builder()
            .app_id("wx88888888")
            .merchant_id("1900000109")
            .api_v3_key("abcdefghijklmnopqrstuvwxyz123456")
            .private_key(vec![1, 2, 3, 4])
            .cert_serial_number("CERT123456")
            .build()
            .unwrap()
    }

    #[test]
    fn test_config() {
        let config = create_test_config();
        assert_eq!(config.app_id, "wx88888888");
        assert_eq!(config.merchant_id, "1900000109");
    }

    #[test]
    fn test_go_style_accessor_signatures_exist() {
        let _: fn(&WxPayClient) -> &RefundService = WxPayClient::refunddomestic;
        let _: fn(&WxPayClient) -> &TransferService = WxPayClient::transferbatch;
        let _: fn(&WxPayClient) -> &ProfitSharingService = WxPayClient::profitsharing;
    }
}