1use openssl::rsa::Rsa;
2use openssl::x509::X509;
3use pay::wechat::WeChatPay;
4use reqwest::Client;
5use serde_json::json;
6use std::collections::HashMap;
7use std::fs::File;
8use std::io::Read;
9
10#[tokio::main]
11async fn main() -> Result<(), Box<dyn std::error::Error>> {
12 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}