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
//! 对alipay api的简单封装
//!
//! # Usage:
//! ```toml
//! [dependencies]
//! alipay-rs = {git = "https://github.com/chenhuaiyuan/alipay-rs"}
//! 已在原先的alipay-rs库中删除AlipayParam宏,需要添加struct-map库来实现AlipayParam宏,如果未使用到AlipayParam宏可以不添加
//! struct-map = {git = "https://github.com/chenhuaiyuan/struct-map"}
//!
//! or
//!
//! alipay-rs = "0.2"
//! struct-map = "0.1"
//! ```
//!
//! # Example:
//! ```rust
//! // 默认的公共参数只包含了最基础的,如果需要增加公共参数,可用通过set_public_params函数实现
//! // 默认的公共参数包含:app_id,charset,sign_type,format,version,method,timestamp,sign
//! // 通过set_public_params设置公共参数,如果参数值为None会自动过滤,重复的参数后面的值会覆盖前面的值
//! // 下面是单笔转账的几种示例
//! use serde::Serialize;
//! use chrono::{Local};
//! use alipay_rs::AlipayParam;
//!
//! // 单笔转账接口需要的参数
//! #[derive(Serialize, Debug)]
//! struct Transfer {
//! out_biz_no: String,
//! trans_amount: String,
//! product_code: String,
//! biz_scene: String,
//! payee_info: PayeeInfo,
//! }
//! #[derive(Serialize, Debug)]
//! struct PayeeInfo {
//! identity: String,
//! identity_type: String,
//! name: String,
//! }
//!
//! // 通过post方法访问单笔转账接口
//! async fn naive_fund_transfer() {
//! let transfer = Transfer {
//! out_biz_no: format!("{}", Local::now().timestamp()),
//! trans_amount: String::from("0.1"),
//! product_code: String::from("TRANS_ACCOUNT_NO_PWD"),
//! biz_scene: String::from("DIRECT_TRANSFER"),
//! payee_info: PayeeInfo {
//! identity: String::from("343938938@qq.com"),
//! identity_type: String::from("ALIPAY_LOGON_ID"),
//! name: String::from("陈怀远"),
//! },
//! };
//! let mut client = alipay_rs::Client::new(
//! "20210xxxxxxxxxxx",
//! include_str!("../私钥.txt"),
//! Some(include_str!("../appCertPublicKey_20210xxxxxxxxxxx.crt")),
//! Some(include_str!("../alipayRootCert.crt"))
//! );
//! let data:serde_json::Value = client
//! .post("alipay.fund.trans.uni.transfer", transfer)
//! .await.unwrap();
//! println!("{:?}", data);
//! }
//!
//! // 通过简单封装后的fund_trans_uni_transfer接口来访问支付宝的单笔转账接口, 暂时建议使用client.post来调用支付宝接口
//! async fn fund_transfer() {
//! let transfer = Transfer {
//! out_biz_no: format!("{}", Local::now().timestamp()),
//! trans_amount: String::from("0.1"),
//! product_code: String::from("TRANS_ACCOUNT_NO_PWD"),
//! biz_scene: String::from("DIRECT_TRANSFER"),
//! payee_info: PayeeInfo {
//! identity: String::from("343938938@qq.com"),
//! identity_type: String::from("ALIPAY_LOGON_ID"),
//! name: String::from("陈怀远"),
//! },
//! };
//! let mut client = alipay_rs::Client::new(
//! "20210xxxxxxxxxxx",
//! include_str!("../私钥.txt"),
//! Some(include_str!("../appCertPublicKey_20210xxxxxxxxxxx.crt")),
//! Some(include_str!("../alipayRootCert.crt"))
//! );
//! let api = alipay_rs::api::Fund::new(client);
//! let data: serde_json::Value = api.fund_trans_uni_transfer(client, transfer).await.unwrap();
//! println!("{:?}", data);
//! }
//!
//!
//! // 公共参数
//! #[derive(AlipayParam)]
//! struct PublicParams {
//! app_id: String,
//! method: Option<String>,
//! charset: String,
//! sign_type: String,
//! sign: Option<String>,
//! timestamp: Option<String>,
//! version: String,
//! }
//! // 修改公共参数来访问单笔转账接口
//! async fn fund_transfer_from_public_params() {
//! let transfer = Transfer {
//! out_biz_no: format!("{}", Local::now().timestamp()),
//! trans_amount: String::from("0.1"),
//! product_code: String::from("TRANS_ACCOUNT_NO_PWD"),
//! biz_scene: String::from("DIRECT_TRANSFER"),
//! payee_info: PayeeInfo {
//! identity: String::from("343938938@qq.com"),
//! identity_type: String::from("ALIPAY_LOGON_ID"),
//! name: String::from("陈怀远"),
//! },
//! };
//! let mut client = alipay_rs::Client::new(
//! "20210xxxxxxxxxxx",
//! include_str!("../私钥.txt"),
//! Some(include_str!("../appCertPublicKey_20210xxxxxxxxxxx.crt")),
//! Some(include_str!("../alipayRootCert.crt"))
//! );
//! let public_params = PublicParams {
//! app_id: "20210xxxxxxxxxxx".to_owned(),
//! method: None,
//! charset: "utf-8".to_owned(),
//! sign_type: "RSA2".to_owned(),
//! sign: None,
//! timestamp: None,
//! version: "1.0".to_owned(),
//! };
//! client.set_public_params(public_params);
//! let data:serde_json::Value = client
//! .post("alipay.fund.trans.uni.transfer", transfer)
//! .await.unwrap();
//! println!("{:?}", data);
//! }
//!
//! async fn neo_fund_transfer() {
//! let transfer = Transfer {
//! out_biz_no: format!("{}", Local::now().timestamp()),
//! trans_amount: String::from("0.1"),
//! product_code: String::from("TRANS_ACCOUNT_NO_PWD"),
//! biz_scene: String::from("DIRECT_TRANSFER"),
//! payee_info: PayeeInfo {
//! identity: String::from("343938938@qq.com"),
//! identity_type: String::from("ALIPAY_LOGON_ID"),
//! name: String::from("陈怀远"),
//! },
//! };
//! let mut client = alipay_rs::Client::neo(
//! "20210xxxxxxxxxxx",
//! "私钥.txt",
//! Some("appCertPublicKey_20210xxxxxxxxxxx.crt"),
//! Some("alipayRootCert.crt")
//! );
//! let data:serde_json::Value = client
//! .post("alipay.fund.trans.uni.transfer", transfer)
//! .await.unwrap();
//! println!("{:?}", data);
//! }
//!
//! // 上传图片文件
//! #[derive(AlipayParam)]
//! struct ImageUpload {
//! image_type: String,
//! image_name: String,
//! }
//! async fn image_upload() {
//! let file = std::fs::read("./test.png").unwrap();
//! let image = ImageUpload {
//! image_type: "png".to_owned(),
//! image_name: "test".to_owned(),
//! };
//! let mut client = alipay_rs::Client::new(
//! "20210xxxxxxxxxxx",
//! include_str!("../私钥.txt"),
//! Some(include_str!("../appCertPublicKey_20210xxxxxxxxxxx.crt")),
//! Some(include_str!("../alipayRootCert.crt"))
//! );
//! client.add_public_params(image);
//!
//! let data:serde_json::Value = client.post_file("alipay.offline.material.image.upload", "image_content", "test.png", file.as_ref()).await.unwrap();
//! println!("{:?}", data);
//! }
//! #[tokio::main]
//! async fn main() {
//! // naive_fund_transfer().await;
//! // fund_transfer().await;
//! fund_transfer_from_public_params().await;
//! }
//! ```
//! # Example2:
//! ```rust
//! use alipay_rs::AlipayParam;
//! use chrono::Local;
//! use serde::Serialize;
//! use std::collections::HashMap;
//!
//! #[derive(Serialize, Debug)]
//! struct Transfer {
//! out_biz_no: String,
//! trans_amount: String,
//! product_code: String,
//! biz_scene: String,
//! payee_info: PayeeInfo,
//! }
//! #[derive(Serialize, Debug)]
//! struct PayeeInfo {
//! identity: String,
//! identity_type: String,
//! name: String,
//! }
//!
//! #[derive(Debug, Serialize)]
//! struct QueryParam {
//! operation: String,
//! page_num: i32,
//! page_size: i32,
//! item_id_list: Option<String>
//! }
//!
//! async fn ref_query(client: &mut alipay_rs::Client) {
//! let query = QueryParam {
//! operation: "ITEM_PAGEQUERY".to_owned(),
//! page_num: 1,
//! page_size: 10,
//! item_id_list: None,
//! };
//!
//! let data:serde_json::Value = client
//! .post("alipay.open.mini.item.page.query", query)
//! .await.unwrap();
//! println!("{:?}", data);
//! }
//!
//! async fn ref_fund_transfer(client: &mut alipay_rs::Client) {
//! let transfer = Transfer {
//! out_biz_no: format!("{}", Local::now().timestamp()),
//! trans_amount: String::from("0.1"),
//! product_code: String::from("TRANS_ACCOUNT_NO_PWD"),
//! biz_scene: String::from("DIRECT_TRANSFER"),
//! payee_info: PayeeInfo {
//! identity: String::from("343938938@qq.com"),
//! identity_type: String::from("ALIPAY_LOGON_ID"),
//! name: String::from("陈怀远"),
//! },
//! };
//! let data:serde_json::Value = client
//! .post("alipay.fund.trans.uni.transfer", transfer)
//! .await.unwrap();
//! println!("{:?}", data);
//! }
//! #[tokio::main]
//! async fn main() {
//!
//! let mut client = alipay_rs::Client::new(
//! "2021002199679230",
//! include_str!("../私钥.txt"),
//! Some(include_str!("../appCertPublicKey_2021002199679230.crt")),
//! Some(include_str!("../alipayRootCert.crt"))
//! );
//!
//! ref_query(&mut client).await;
//! ref_fund_transfer(&mut client).await;
//! }
//! ```
use openssl::pkey::{PKey, Public};
use std::cell::RefCell;
use std::collections::HashMap;
#[derive(Debug)]
pub struct Client {
request_params: RefCell<HashMap<String, String>>,
private_key: String,
other_params: RefCell<HashMap<String, String>>,
}
impl Clone for Client {
fn clone(&self) -> Self {
Self {
request_params: self.request_params.clone(),
private_key: self.private_key.clone(),
other_params: self.other_params.clone(),
}
}
}
#[derive(Debug, Clone)]
pub struct SignChecker {
alipay_public_key: PKey<Public>,
}
mod alipay;
mod app_cert_client;
pub mod error;
pub use struct_map::FieldValue;
pub use struct_map::ToHashMap as AlipayParam;