[大家的项目] alipay_sdk_rust,Alipay sdk in rust 支付宝支付 SDK

2022-11-28 15:22:39 浏览数 (1)

alipay_sdk_rust

alipay官方没有rust sdk,本人自己实现了一个,已经生产环境可用。基于gostd的http模块。

https://github.com/wandercn/alipay_sdk_rust

Alipay sdk in rust 支付宝支付 SDK。只支持最安全的RSA2,公钥证书方式签名验证方式,默认只支持utf-8编码。

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(统一收单交易创建接口)

代码语言:javascript复制
use std::io::Result;use alipay_sdk_rust::biz::{self, BizContenter};use alipay_sdk_rust::pay::{PayClient, Payer};use alipay_sdk_rust::response::TradeCreateResponse;fn main() -> Result<()> {let out_trade_no = gostd::time::Now().UnixNano().to_string();    let mut biz_content = biz::TradeCreateBiz::new();
    biz_content.set_subject("huawei Mate50");
    biz_content.set_out_trade_no(&out_trade_no); // "1620630871769533112"
    biz_content.set_total_amount("5");
    biz_content.set("seller_id", "2088621955702975");
    biz_content.set_buyer_id("2088102175953034");
    biz_content.set("Timestamp", "2022-07-11 16:09:04");///
    let client = new_pay_client()?;    let res = client.trade_create(&biz_content)?;   println!("{}", serde_json::to_string(&res)?);    Ok(())
}fn new_pay_client() -> Result<impl Payer> {    let client = PayClient::builder()
.api_url("https://openapi.alipaydev.com/gateway.do")
.app_id("2021000117650139")
.alipay_root_cert_sn("687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6")
.alipay_public_key("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoEz4Tegg1Ytfn2N jWzDTOBbUNhcpIXz7dV3K8yIsDzinPwYTzzZhlCLYlKbfJGpSDVU0K3I6TnyGfNPEg R7EC/3ycY1nETh8mxQ4dgrz8irKlLxSRDHVtPg4luncDz2u0hMab9QJdqF8DXD5H r0Pd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CltNTPyLsL3RGUUlsLOPsyT6cFZtUAKJ J6wEqQxM5TrvNHywIDAQAB")
.sign_type_rsa2()
.version_1_0()
.build()?;    Ok(client)
}

output

代码语言:javascript复制
{"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"}

0 人点赞