br_pay/
lib.rs

1use crate::wechat::Wechat;
2use json::{JsonValue};
3
4pub mod alipay;
5pub mod wechat;
6
7#[derive(Clone)]
8pub enum Pay {
9    Wechat(Wechat),
10    Alipay,
11}
12
13impl Pay {
14    pub fn new(data: JsonValue) -> Self {
15        match data["mode"].as_str().unwrap_or("") {
16            "wechat" => Pay::Wechat(Wechat {
17                appid: data["appid"].to_string(),
18                mchid: data["mchid"].to_string(),
19                serial_no: data["serial_no"].to_string(),
20                cert_path: data["cert_path"].to_string(),
21                secret: data["secret"].to_string(),
22            }),
23            _ => Pay::Alipay,
24        }
25    }
26}
27impl PayMode for Pay {
28    fn login(&self, code: &str) -> Result<JsonValue, String> {
29        match self {
30            Self::Wechat(e) => e.login(code),
31            Self::Alipay => Err("Alipay alipay".to_string()),
32        }
33    }
34
35    /// jsapi 支付
36    fn jsapi(
37        &mut self,
38        sub_mchid: &str,
39        out_trade_no: &str,
40        description: &str,
41        total_fee: f64,
42        notify_url: &str,
43        sp_openid: &str,
44    ) -> Result<JsonValue, String> {
45        match self {
46            Self::Wechat(e) => e.jsapi(
47                sub_mchid,
48                out_trade_no,
49                description,
50                total_fee,
51                notify_url,
52                sp_openid,
53            ),
54            Self::Alipay => Err("Alipay alipay".to_string()),
55        }
56    }
57
58    fn order_trade(&mut self, out_trade_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
59        match self {
60            Self::Wechat(e) => e.order_trade(
61                out_trade_no,
62                sub_mchid,
63            ),
64            Self::Alipay => Err("Alipay alipay".to_string()),
65        }
66    }
67
68    fn refunds(&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> {
69        match self {
70            Self::Wechat(e) => e.refunds(
71                sub_mchid,
72                out_trade_no,
73                transaction_id,
74                out_refund_no,
75                amount,
76                total,
77                currency
78            ),
79            Self::Alipay => Err("Alipay alipay".to_string()),
80        }
81    }
82}
83
84pub trait PayMode {
85    fn login(&self, code: &str) -> Result<JsonValue, String>;
86    /// JSAPI 支付
87    /// * out_trade_no 商户订单号
88    /// * sub_mchid 子商户号
89    /// * description 商品名称
90    /// * total_fee 支付金额
91    /// * notify_url 通知地址
92    /// * sp_openid 支付客户ID
93    fn jsapi(
94        &mut self,
95        sub_mchid: &str,
96        out_trade_no: &str,
97        description: &str,
98        total_fee: f64,
99        notify_url: &str,
100        sp_openid: &str,
101    ) -> Result<JsonValue, String>;
102    /// 订单查询
103    /// * out_trade_no 商户订单号
104    /// * sub_mchid 子商户号
105    fn order_trade(
106        &mut self,
107        out_trade_no: &str,
108        sub_mchid: &str,
109    ) -> Result<JsonValue, String>;
110    /// 订单退款
111    #[allow(clippy::too_many_arguments)]
112    fn refunds(&mut self,
113               sub_mchid: &str,
114               out_trade_no: &str,
115               transaction_id: &str,
116               out_refund_no: &str,
117               amount: f64,
118               total: f64,
119               currency: &str,
120    ) -> Result<JsonValue, String>;
121}