Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use braintree with rails and activemerchant

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?

like image 608
yogashi Avatar asked Jul 11 '11 14:07

yogashi


2 Answers

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
like image 63
jjeffus Avatar answered Sep 20 '22 19:09

jjeffus


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.

like image 37
Maran Avatar answered Sep 24 '22 19:09

Maran