6个版本

使用旧的Rust 2015

0.0.6 2017年6月23日
0.0.5 2017年5月26日

#296 in 财务

每月24次下载

MIT 协议

35KB
630

braintree-rs

Braintree的Rust客户端库

运行示例

如果您在某个地方克隆了存储库,您将在 examples/transaction 中找到一个程序,可以用于执行各种交易操作。示例调用如下

$ cargo run -- create <amount> # Create a transaction
$ cargo run -- find <transaction_id> # Find a transaction
$ cargo run -- void <transaction_id> # Void a transaction
$ cargo run -- settle <transaction_id> # Force a transaction into a settled state
$ cargo run -- refund <transaction_id> # Refund a settled transaction

待办事项

  1. ToXml 特性替换为适当的XML序列化器。目前,Serde 不支持 此操作,但一旦支持,我们应该切换到使用它。

lib.rs:

绑定到Braintree的API。

对于不熟悉Braintree或支付处理的用户,Braintree的主页 是一个了解更多的好地方,以及开发者文档,它提供了可用工具和API的不错概述。

请注意,这是一个非官方库,没有Braintree本身的直接支持。目标是提供一组相对完整的核心功能绑定,但自然会存在很多不完整之处。欢迎提交拉取请求!

您需要做的第一件事是创建一个 沙箱账户,您可以使用它来测试您的集成而无需通过完整的应用程序流程。一旦创建了账户,请按照这些说明 获取您的商户ID、公钥和私钥。一旦有了这些,您应该能够创建您的第一个交易!当然,您需要将这三个值替换以下占位符,并请注意,您 绝对 不应将这些凭证提交到源代码控制

extern crate braintree;

use braintree::{Braintree, CreditCard, Environment};
use braintree::transaction;
use std::error::Error;

fn main() {
    // Create a handle to the Braintree API.
    let bt = Braintree::new(
        Environment::Sandbox,
        "<merchant_id>",
        "<public_key>",
        "<private_key>",
    );

    // Attempt to charge the provided credit card $10.
    let result = bt.transaction().create(transaction::Request{
        amount: String::from("10.00"),
        credit_card: Some(CreditCard{
            number: Some(String::from("4111111111111111")),
            expiration_date: Some(String::from("10/20")),
            ..Default::default()
        }),
        options: Some(transaction::Options{
            submit_for_settlement: Some(true),
            ..Default::default()
        }),
        ..Default::default()
    });

    // Check to see if it worked.
    match result {
        Ok(transaction) => println!("Created transaction: {}", transaction.id),
        Err(err) => println!("Error: {}", err.description()),
    }
}

一旦您确定您的集成可以上线,您需要在Braintree的主站上注册以获取一组单独的生产凭证。记得在切换时将 Environment::Sandbox 更改为 Environment::Production

稳定性说明

该包处于非常初级的alpha状态,因此其API设计可能会更改。您已被提前警告!

依赖项

~5–13MB
~177K SLoC