alipaysdkrust

crates.io Released API docs GPL3 licensed Downloads of Crates.io Lines of code Build Languages

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 { let client = PayClient::builder() .apiurl("https://openapi.alipaydev.com/gateway.do") .appid("2021000117650139") .alipayrootcertsn("687b59193f3f462dd5336e5abf83c5d802941eef3187dddf3d3b83462e1dfcf6") .alipaypublickey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoEz4Tegg1Ytfn2N+jWzDTOBbUNhcpIXz7dV3K8yIsDzinPwYTzzZhlCLYlKbfJGpSDVU0K3I6TnyGfNPEg+R7EC/3ycY1nETh8mxQ4dgrz8irKlLxSRDHVtPg4luncDz2u0hMab9QJdqF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uTvRs696fPa5wSEshE0RQBcn5iCltNTPyLsL3RGUUlsLOPsyT6cFZtUAKJ+J6wEqQxM5TrvNHywIDAQAB") .signtypersa2() .version10() .build()?; Ok(client) } ```

# 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)
    }
}

} ```

output

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