jgrant
jgrant

Reputation: 592

Parse curl information in Rails application

This is my first time using curl in my Rails 4 App. I am trying to use Plaid with Stripe. I am able to successful exchange the public token for the stripe bank account token.

Stripe with Plaid ACH

Here's my controller action.

    def create
      results = `curl https://tartan.plaid.com/exchange_token \
        -d client_id="CLIENT_ID" \
        -d secret="SECRET_KEY" \
        -d public_token="#{params[:public_token]}" \
        -d account_id="#{params[:meta][:account_id]}"`
    end

In Terminal with JSON.parse(results)

{"account_id"=>"ACCOUNT_ID", "stripe_bank_account_token"=>"12345678abcd", "sandbox"=>true, "access_token"=>"test_citi"}

How does one get the stripe_bank_account_token into the controller?

UPDATE

I am using the Figaro Gem to hide the params/credentials..

results = 
`curl https://tartan.plaid.com/exchange_token \
  -d client_id="#{ ENV['PLAID_CLIENT_ID'] }" \
  -d secret="#{ ENV['PLAID_SECRET_KEY'] }" \
  -d public_token="#{params[:public_token]}" \
  -d account_id="#{params[:meta][:account_id]}"`

   # here's how I get the stripe_bank_account_token
    break_down = JSON.parse(results)
    x =  break_down.select { |key, val| key == "stripe_bank_account_token" }

Upvotes: 0

Views: 612

Answers (2)

Matthew Arkin
Matthew Arkin

Reputation: 4648

You shouldn't pipe to curl from Ruby code especially when it involves user input.

Rather you should use the built in Ruby HTTP Client, a gem like RestClient, or even better the Plaid Ruby Gem.

gem install plaid

then just

require 'Plaid'
Plaid.config do |p|
  p.client_id = '<<< Plaid provided client ID >>>'
  p.secret = '<<< Plaid provided secret key >>>'
  p.env = :tartan  # or :api for production
end

user = Plaid::User.exchange_token(params[:public_token], params[:meta][:account_id], product: :auth)
user.stripe_bank_account_token

Upvotes: 1

7urkm3n
7urkm3n

Reputation: 6321

Just create new method for plaid, smth like below.

Also, Good to use HTTP client or REST client

HTTP client

REST client

def create
    res = plain_curl(params)
    puts res.inspect #there you will see your respond json obj in rails console.
end

private

def plain_curl(params)
  #it should return you json object, if not just add return before result.
  results = `curl https://tartan.plaid.com/exchange_token \
    -d client_id="CLIENT_ID" \
    -d secret="SECRET_KEY" \
    -d public_token="#{params[:public_token]}" \
    -d account_id="#{params[:meta][:account_id]}"`
end

Upvotes: 0

Related Questions