6 releases

Uses old Rust 2015

0.0.6 Jun 23, 2017
0.0.5 May 26, 2017

#298 in Finance

24 downloads per month

MIT license

35KB
630 lines

braintree-rs

A Rust client library for Braintree

Running the Example

If you clone the repository somewhere, you'll find a program in examples/transaction that can be used to perform various operations on transactions. Example invocations are:

$ 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

TODO

  1. Replace the ToXml trait with a proper XML serializer. Serde currently does not support this, but once it does we should switch to using that.

lib.rs:

Bindings to Braintree's API.

For those unfamiliar with Braintree or payments processing, Braintree's homepage is a good place to start to learn more, along with the developer documentation which provides a good overview of the available tools and API's.

Note that this is an unofficial library, with no direct support from Braintree themselves. The goal is to provide a set of reasonably-complete bindings to core functionality, but naturally a lot of it will be incomplete. Pull requests are welcome!

The first thing you'll need to do is create a sandbox account, which you can use to test your integration without needing to go through the full application process. Once you've created an account, follow these instructions to retrieve your Merchant ID, Public Key, and Private Key. Once you have those, you should be able to create your first transaction! Naturally you'll need to substitute those three values in for the placeholders below, and it bears repeating that you should never commit those credentials to source control:

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()),
    }
}

Once you've decided that your integration is good to go live, you'll need to get a separate set of production credentials by signing up on Braintree's main site. Remember to also change Environment::Sandbox to Environment::Production when you make the switch.

Stability Note

This crate is very much in a pre-alpha state, and as such the design of its API is subject to change. You have been forewarned!

Dependencies

~5–13MB
~182K SLoC