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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! 分账服务模块
//!
//! 提供微信支付分账功能。

use serde::{Deserialize, Serialize};
use std::sync::Arc;

use crate::auth::Signer;
use crate::config::WxPayConfig;
use crate::error::WxPayResult;
use crate::http::{HttpClient, HttpMethod};
use crate::services::transport::{ServiceTransport, TransportObserver};

/// 分账请求
#[derive(Debug, Clone, Serialize)]
pub struct ProfitSharingRequest {
    /// 微信支付订单号
    pub transaction_id: String,

    /// 商户分账单号
    pub out_order_no: String,

    /// 分账接收方列表
    pub receivers: Vec<Receiver>,

    /// 分账说明
    pub description: String,
}

/// 查询分账结果请求
#[derive(Debug, Clone, Serialize)]
pub struct QueryProfitSharingRequest {
    /// 微信支付订单号
    pub transaction_id: String,

    /// 商户分账单号
    pub out_order_no: String,
}

/// 分账接收方
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Receiver {
    /// 接收方类型
    #[serde(rename = "type")]
    pub receiver_type: String,

    /// 接收方账号
    pub account: String,

    /// 分账金额(分)
    pub amount: u64,

    /// 分账描述
    pub description: String,

    /// 接收方名称
    pub name: Option<String>,
}

/// 添加分账接收方请求
#[derive(Debug, Clone, Serialize)]
pub struct AddProfitSharingReceiverRequest {
    /// 子商户号(服务商模式可选)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sub_mchid: Option<String>,

    /// 应用 ID
    pub appid: String,

    /// 子商户应用 ID(服务商模式可选)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sub_appid: Option<String>,

    /// 接收方类型
    #[serde(rename = "type")]
    pub receiver_type: String,

    /// 接收方账号
    pub account: String,

    /// 接收方名称(个人收款方时必填)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// 与分账方的关系类型
    #[serde(rename = "relation_type")]
    pub relation_type: String,

    /// 自定义关系(可选)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub custom_relation: Option<String>,
}

/// 删除分账接收方请求
#[derive(Debug, Clone, Serialize)]
pub struct DeleteProfitSharingReceiverRequest {
    /// 应用 ID
    pub appid: String,

    /// 接收方类型
    #[serde(rename = "type")]
    pub receiver_type: String,

    /// 接收方账号
    pub account: String,

    /// 子商户号(服务商模式可选)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sub_mchid: Option<String>,

    /// 子商户应用 ID(服务商模式可选)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sub_appid: Option<String>,
}

/// 分账响应
#[derive(Debug, Clone, Deserialize)]
pub struct ProfitSharingResponse {
    /// 微信分账单号
    pub order_id: String,

    /// 商户分账单号
    pub out_order_no: String,

    /// 微信支付订单号
    pub transaction_id: String,

    /// 分账单状态
    pub status: String,
}

/// 分账接收方响应(添加/删除)
#[derive(Debug, Clone, Deserialize)]
pub struct ProfitSharingReceiverResponse {
    /// 子商户号(服务商模式返回)
    pub sub_mchid: Option<String>,

    /// 应用 ID
    pub appid: Option<String>,

    /// 子商户应用 ID(服务商模式返回)
    pub sub_appid: Option<String>,

    /// 接收方类型
    #[serde(rename = "type")]
    pub receiver_type: Option<String>,

    /// 接收方账号
    pub account: Option<String>,

    /// 接收方姓名
    pub name: Option<String>,

    /// 与分账方的关系类型
    #[serde(rename = "relation_type")]
    pub relation_type: Option<String>,

    /// 自定义关系
    pub custom_relation: Option<String>,
}

/// 分账完结请求
#[derive(Debug, Clone, Serialize)]
pub struct ProfitSharingFinishRequest {
    /// 微信支付订单号
    pub transaction_id: String,

    /// 商户分账单号
    pub out_order_no: String,

    /// 分账完结描述
    pub description: String,
}

/// 分账完结响应
#[derive(Debug, Clone, Deserialize)]
pub struct ProfitSharingFinishResponse {
    /// 微信分账单号
    pub order_id: String,

    /// 商户分账单号
    pub out_order_no: String,

    /// 微信支付订单号
    pub transaction_id: String,

    /// 分账单状态
    pub status: String,
}

/// 分账服务
///
/// 提供微信支付分账的创建、查询、完结等功能。
///
/// # 示例
///
/// ```rust,no_run
/// use std::sync::Arc;
///
/// use wxpay_rs::{
///     auth::{Signer, Sha256RsaSigner},
///     config::WxPayConfig,
///     http::ReqwestHttpClient,
///     services::ProfitSharingService,
/// };
///
/// #[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 http_client = Arc::new(ReqwestHttpClient::builder().build()?);
///     let signer: Arc<dyn Signer> = Arc::new(Sha256RsaSigner::new(
///         "1900000109",
///         b"PRIVATE KEY",
///         "CERT123456",
///     )?);
///     let service = ProfitSharingService::new(Arc::new(config), http_client, signer);
///
///     let request = wxpay_rs::services::ProfitSharingRequest {
///         transaction_id: "1217752501201407033233368018".to_string(),
///         out_order_no: "P20150806125346".to_string(),
///         receivers: vec![wxpay_rs::services::Receiver {
///             receiver_type: "MERCHANT_ID".to_string(),
///             account: "1900000109".to_string(),
///             amount: 100,
///             description: "分账".to_string(),
///             name: None,
///         }],
///         description: "分账".to_string(),
///     };
///     let response = service.create_profit_sharing(&request).await?;
///     let _ = response;
///     Ok(())
/// }
/// ```
#[allow(dead_code)]
pub struct ProfitSharingService {
    /// 配置
    config: Arc<WxPayConfig>,

    /// HTTP 客户端
    http_client: Arc<dyn HttpClient>,

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

    /// 统一请求执行器
    transport: ServiceTransport,
}

impl ProfitSharingService {
    /// 创建新的分账服务
    pub fn new(
        config: Arc<WxPayConfig>,
        http_client: Arc<dyn HttpClient>,
        signer: Arc<dyn Signer>,
    ) -> Self {
        Self::new_with_observer(config.clone(), http_client.clone(), signer.clone(), None)
    }

    pub fn new_with_observer(
        config: Arc<WxPayConfig>,
        http_client: Arc<dyn HttpClient>,
        signer: Arc<dyn Signer>,
        transport_observer: Option<Arc<dyn TransportObserver>>,
    ) -> Self {
        Self {
            config: config.clone(),
            http_client: http_client.clone(),
            signer: signer.clone(),
            transport: ServiceTransport::new_with_observer(
                config,
                http_client,
                signer,
                transport_observer,
            ),
        }
    }

    /// 创建分账
    pub async fn create_profit_sharing(
        &self,
        request: &ProfitSharingRequest,
    ) -> WxPayResult<ProfitSharingResponse> {
        let body = serde_json::to_string(request)?;

        self.transport
            .request(
                HttpMethod::Post,
                "/v3/profitsharing/orders",
                Some(&body),
                "profit_sharing.create_profit_sharing",
            )
            .await
    }

    /// 创建分账(文档风格)
    pub async fn create(
        &self,
        request: &ProfitSharingRequest,
    ) -> WxPayResult<ProfitSharingResponse> {
        self.create_profit_sharing(request).await
    }

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

    /// 添加分账接收方
    pub async fn add_receiver(
        &self,
        request: &AddProfitSharingReceiverRequest,
    ) -> WxPayResult<ProfitSharingReceiverResponse> {
        let body = serde_json::to_string(request)?;

        self.transport
            .request(
                HttpMethod::Post,
                "/v3/profitsharing/receivers/add",
                Some(&body),
                "profit_sharing.add_receiver",
            )
            .await
    }

    /// 删除分账接收方
    pub async fn delete_receiver(
        &self,
        request: &DeleteProfitSharingReceiverRequest,
    ) -> WxPayResult<ProfitSharingReceiverResponse> {
        let body = serde_json::to_string(request)?;

        self.transport
            .request(
                HttpMethod::Post,
                "/v3/profitsharing/receivers/delete",
                Some(&body),
                "profit_sharing.delete_receiver",
            )
            .await
    }

    /// 查询分账
    pub async fn query_profit_sharing(
        &self,
        transaction_id: &str,
        out_order_no: &str,
    ) -> WxPayResult<ProfitSharingResponse> {
        let path = format!(
            "/v3/profitsharing/orders/{}?transaction_id={}",
            out_order_no, transaction_id
        );

        self.transport
            .request(
                HttpMethod::Get,
                &path,
                None,
                "profit_sharing.query_profit_sharing",
            )
            .await
    }

    /// 查询分账(文档风格)
    pub async fn query(
        &self,
        request: &QueryProfitSharingRequest,
    ) -> WxPayResult<ProfitSharingResponse> {
        self.query_profit_sharing(&request.transaction_id, &request.out_order_no)
            .await
    }

    /// 查询分账单(兼容 `wechatpay-go` 风格)
    pub async fn query_order(
        &self,
        request: &QueryProfitSharingRequest,
    ) -> WxPayResult<ProfitSharingResponse> {
        self.query_profit_sharing(&request.transaction_id, &request.out_order_no)
            .await
    }

    /// 完成分账
    pub async fn finish_profit_sharing(
        &self,
        request: &ProfitSharingFinishRequest,
    ) -> WxPayResult<ProfitSharingFinishResponse> {
        let body = serde_json::to_string(request)?;

        self.transport
            .request(
                HttpMethod::Post,
                "/v3/profitsharing/finish",
                Some(&body),
                "profit_sharing.finish_profit_sharing",
            )
            .await
    }

    /// 完成分账(文档风格)
    pub async fn finish(
        &self,
        request: &ProfitSharingFinishRequest,
    ) -> WxPayResult<ProfitSharingFinishResponse> {
        self.finish_profit_sharing(request).await
    }

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

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

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_profit_sharing_request_serialization() {
        let request = ProfitSharingRequest {
            transaction_id: "1217752501201407033233368018".to_string(),
            out_order_no: "P20150806125346".to_string(),
            receivers: vec![Receiver {
                receiver_type: "MERCHANT_ID".to_string(),
                account: "1900000109".to_string(),
                amount: 100,
                description: "分账".to_string(),
                name: None,
            }],
            description: "分账".to_string(),
        };

        let json = serde_json::to_string(&request).unwrap();
        assert!(json.contains("1217752501201407033233368018"));
        assert!(json.contains("P20150806125346"));
    }

    #[test]
    fn test_profit_sharing_response_deserialization() {
        let json = r#"{
            "order_id": "6110000071100999991182020050700019480101",
            "out_order_no": "P20150806125346",
            "transaction_id": "1217752501201407033233368018",
            "status": "FINISHED"
        }"#;
        let response: ProfitSharingResponse = serde_json::from_str(json).unwrap();
        assert_eq!(response.status, "FINISHED");
        assert_eq!(
            response.order_id,
            "6110000071100999991182020050700019480101"
        );
    }

    #[test]
    fn test_go_style_profit_sharing_alias_signatures_exist() {
        let _ = ProfitSharingService::create_order;
        let _ = ProfitSharingService::query_order;
        let _ = ProfitSharingService::finish_order;
    }
}