WeChatPay

Struct WeChatPay 

Source
pub struct WeChatPay {
    pub mchid: String,
    pub serial: String,
    pub private_key: Rsa<Private>,
    pub certs: HashMap<String, PKey<Public>>,
}

Fields§

§mchid: String§serial: String§private_key: Rsa<Private>§certs: HashMap<String, PKey<Public>>

Implementations§

Source§

impl WeChatPay

Source

pub fn new( mchid: String, serial: String, private_key: Rsa<Private>, certs: HashMap<String, PKey<Public>>, ) -> Self

Examples found in repository?
examples/wechat.rs (lines 46-51)
11async fn main() -> Result<(), Box<dyn std::error::Error>> {
12    // 「商户API证书」的「证书序列号」
13    // 从本地文件中加载「微信支付平台证书」(可使用证书下载工具得到),用来验证微信支付应答的签名
14    let merchant_id = "190000****".to_string();
15    let merchant_certificate_serial = "3775B6A45ACD588826D15E583A95F5DD********".to_string();
16
17    let mut merchant_private_key_file = File::open("/path/to/merchant/apiclient_key.pem").unwrap();
18    let mut merchant_private_key = vec![];
19    merchant_private_key_file
20        .read_to_end(&mut merchant_private_key)
21        .unwrap();
22    let merchant_private_key_instance = Rsa::private_key_from_pem(&merchant_private_key).unwrap();
23
24    let mut platform_certificate_file = File::open("/path/to/wechatpay/cert.pem").unwrap();
25    let mut platform_certificate = vec![];
26    platform_certificate_file
27        .read_to_end(&mut platform_certificate)
28        .unwrap();
29    let platform_public_key_instance = X509::from_pem(&platform_certificate)
30        .unwrap()
31        .public_key()
32        .unwrap();
33
34    let platform_certificate_serial = X509::from_pem(&platform_certificate)
35        .unwrap()
36        .serial_number()
37        .to_bn()
38        .expect("REASON");
39    let mut certs = HashMap::new();
40    certs.insert(
41        platform_certificate_serial.to_string(),
42        platform_public_key_instance,
43    );
44
45
46    let instance = WeChatPay::new(
47        merchant_id,
48        merchant_certificate_serial,
49        merchant_private_key_instance,
50        certs,
51    );
52
53    let client = Client::new();
54
55    let payment = json!({
56        "mchid": "1900006XXX",
57        "out_trade_no": "native12177525012014070332333",
58        "appid": "wxdace645e0bc2cXXX",
59        "description": "Image形象店-深圳腾大-QQ公仔",
60        "notify_url": "https://weixin.qq.com/",
61        "amount": {
62            "total": 1,
63            "currency": "CNY"
64        }
65    });
66
67    let resp = client
68        .post("https://api.mch.weixin.qq.com/v3/pay/transactions/native")
69        .json(&payment)
70        .send()
71        .await?;
72
73    let body = resp.text().await?;
74
75    println!("{}", body);
76
77    Ok(())
78}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.