5

I have a simple rails website to which I need to add a payments gateway now. I see a railscast on integrating activemerchant with paypal but I wanted to use braintree instead.

I am unable to find any tutorials that show how braintree can be integrated to a rails app end to end. I see that people have good things to say about braintree but how about a tutorial?

Has someone used this payment gateway for their rails application? Would it be similar to the railscasts with paypal...just replace paypal with braintree?

yogashi
  • 273
  • 1
  • 5
  • 11

2 Answers2

16

Active Merchant is a much more flexible choice since it gives your company the freedom to change gateways without significant code changes. The original question was how to integrate it with Active Merchant, not how to use BT's proprietary API. Here's the answer I found after some digging through the code. You can find your public key, private key and merchant id under "Account" -> "My User" -> "API Keys".

gateway = ActiveMerchant::Billing::BraintreeGateway.new(
  :merchant_id => 'Your Merchant ID',
  :public_key  => 'Your Public Key',
  :private_key => 'Your Private Key'
)

creditcard = ActiveMerchant::Billing::CreditCard.new(
  :type       => 'visa',
  :number     => '41111111111111111',
  :month      => 10,
  :year       => 2014,
  :first_name => 'Bob',
  :last_name  => 'Bobsen'
)
response = gateway.purchase(1000, creditcard)
STDERR.puts response.success?
STDERR.puts response.message
STDERR.puts response.authorization
jjeffus
  • 161
  • 1
  • 2
  • 1
    ActiveMerchant is a poor API compared to Braintree's ruby API. It only supports very simple use cases, but your point about changing gateways is valid. – Montdidier Apr 08 '13 at 01:52
  • What can Braintree's ruby API do that ActiveMerchant cannot do? – frediy Mar 16 '15 at 22:54
  • Braintree has some nice features specific to them: HostedFields (so nowhere in your app do you see CC details) OR nonce so only the web page can see it (so provided you trust the JS things are safe). – Michael Noack Jun 04 '15 at 08:02
  • Also 3DS authentication is missing from the Active Merchant implementation. – Sunny Oct 02 '19 at 08:11
6

The guys at Braintree created their own gem based on their API. It's very easy to setup and do actual transactions with. You can view the code on Github and a quick example can be found here. Full projects with Rails integration are located here.

Maran
  • 2,751
  • 15
  • 12