Alipay sdk in rust 支付宝支付 SDK。只支持最安全的RSA2,公钥证书方式签名验证方式,默认只支持utf-8编码和JSON格式。目前只支持商户直接接入模式
RSA2密钥生成请参考https://opendocs.alipay.com/common/02kipl 中的公钥证书方式生成,使用CSR文件申请,密钥格式必须使用PKCS1(非java适用)
# Example ## apidoc https://opendocs.alipay.com/apis/api_1/alipay.trade.create
## alipay.trade.create(统一收单交易创建接口)
注意:开发环境使用沙箱环境下的CSR公钥证书配置调试代码,生产环境需要切换正式申请的公钥证书配置。沙箱环境配置参考https://opendocs.alipay.com/common/02kkv7
(如下的Example使用的是沙箱环境下本人申请的自定义公钥证书)
```rust use std::io::Result; use alipaysdkrust::biz::{self, BizContenter}; use alipaysdkrust::pay::{PayClient, Payer}; use alipaysdkrust::response::TradeCreateResponse; fn main() -> Result<()> { let outtradeno = gostd::time::Now().UnixNano().tostring(); let mut bizcontent = biz::TradeCreateBiz::new(); bizcontent.setsubject("huawei Mate50"); bizcontent.setouttradeno(&outtradeno); // "1620630871769533112" bizcontent.settotalamount("5"); bizcontent.set("sellerid", "2088621955702975"); bizcontent.setbuyerid("2088102175953034"); bizcontent.set("Timestamp", "2022-07-11 16:09:04");/// let client = newpayclient()?; let res = client.tradecreate(&bizcontent)?; println!("{}", serdejson::to_string(&res)?); Ok(()) }
fn newpayclient() -> Result
# output
{"response":{"code":"10000","msg":"Success","trade_no":"2022071322001453030502038801","out_trade_no":"1620630871769533112"},"sign":"NgDoQ8wIjV0MY3/hA3BDvHOz3Jw7y6CTdGPD+Q4GBsvrAHDDRwbdki+jSGr66zutKtklUYJsizKVIGbmJmKKBhVSNdxWCRI++keWfHjWDLjy59hiRix0l8oFh+dhnXaQqjAXEjqOxjtd6WGgO9FhgX1Kz6GAZ8NJobwzXKor8fotA0E5ztpcPcRRF4KmdVioofAdSf0o9UTpM24uFmGuBwi0Cfae70jctpmn0CMXJ36g2FEe3pcZIWm/KWDAXwGH6daQccULwVjUYN01OyeM93wKuLXJwEhvIeLpJeW4AiXpU21W/qNgYINPkjRA/h/HmG6ooG14VfdHNXPjuQ0/sg==","alipay_cert_sn":"28e0499cc4ef722406edd30274314430"}
```rust use simplelog::{ ColorChoice, CombinedLogger, ConfigBuilder, LevelFilter, TermLogger, TerminalMode, ThreadLogMode, };
fn initlog() { let config = ConfigBuilder::new() .settimeformatrfc3339() .setthreadmode(ThreadLogMode::Names) .build(); CombinedLogger::init(vec![TermLogger::new( LevelFilter::Debug, config, TerminalMode::Mixed, ColorChoice::Always, )]) .expect("init log faield!"); }
use alipaysdkrust::cert;
const APPCERTSNFILE: &str = "/Users/xxx/Documents/appCertPublicKey2021000117650139.crt"; const ALIPAYROOTCERTFILE: &str = "/Users/xxxx/Documents/alipayRootCert.crt"; const ALIPAYCERTPUBLICKEYRSA2FILE: &str = "/Users/xxx/Documents/alipayCertPublicKeyRSA2.crt"; fn main() { initlog(); match cert::getcertsn(APPCERTSNFILE) { Ok(sn) => { println!("appcertsn: {}", sn) } Err(err) => { println!("get appcert_sn faild: {}", err) } }
match cert::get_root_cert_sn(ALIPAY_ROOT_CERT_FILE) {
Ok(sn) => {
println!("alipay_root_cert_sn : {}", sn)
}
Err(err) => {
println!("get alipay_root_cert_sn faild: {}", err)
}
}
match cert::get_public_key_with_path(ALIPAY_CERT_PUBLIC_KEY_RSA2_FILE) {
Ok(sn) => {
println!("alipay_cert_public_key : {}", sn)
}
Err(err) => {
println!("faild: {}", err)
}
}
} ```
2022-07-20T10:21:18.9427Z [DEBUG] (main) alipay_sdk_rust::cert: cert_path: /Users/xxx/Documents/appCertPublicKey_2021000117650139.crt
app_cert_sn: 8c68b9753e5b9e0bb7704a981936ecce
2022-07-20T10:21:18.945293Z [DEBUG] (main) alipay_sdk_rust::cert: root_cert_path: "/Users/xxx/Documents/alipayRootCert.crt"
alipay_root_cert_sn : 687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6
2022-07-20T10:21:18.948418Z [DEBUG] (main) alipay_sdk_rust::cert: alipay_cert_path: "/Users/xxx/Documents/alipayCertPublicKey_RSA2.crt"
alipay_cert_public_key : MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoEz4Tegg1Ytfn2N+jWzDTOBbUNhcpIXz7dV3K8yIsDzinPwYTzzZhlCLYlKbfJGpSDVU0K3I6TnyGfNPEg+R7EC/3ycY1nETh8mxQ4dgrz8irKlLxSRDHVtPg4luncDz2u0hMab9QJdqF8DXD5H+r0Pdt6CSJgKJqLa0awPV3/8cTQZbhZ4ontnRdcwWWcp4/TunkEc891Aa5FmzWp4hgBYcu3BGcazS1HQA4r6wTwRkiqKsCwZeVTag4CiOeqe/vRFTxTMKF4gjRzdhTapUfcBCmXblEA3i8/7OILEyNHceRAxxxIpUjTyoRJ4/2xd0kWbw1gmkLFM0Fzee0eVgoQIDAQABs