Search code examples
ruby-on-railsstripe-paymentsplaid

Plaid with Stripe Rails app


I have an application successfully using Stripe to charge users for access to an application. Building on to this, I would like to implement Plaid in my checkout process. Since this was my first time implementing Stripe, and now my first time with Plaid, I am a bit lost regarding the directions and how to push this feature forward.

I installed the plaid-ruby gem, and added my Plaid secret_key and client_id via figaro but now I am lost.

Plaid.rb:

   Plaid.configuration.stripe = {
    p.client_id = ENV['PLAID_CLIENT_ID'],
    p.secret = ENV['PLAID_SECRET_KEY'],
    p.env = :tartan  # or :production
  end  

The Plaid Link:

       <button id='linkButton'>Open Plaid Link</button>
        <script src="https://cdn.plaid.com/link/stable/link-initialize.js"></script>
        <script>
        var linkHandler = Plaid.create({
          env: 'tartan',
          clientName: 'ACCR',
          key: '<<< MY_PUBLIC_KEY >>>',
          product: 'auth',
          selectAccount: true,
          env: 'tartan',
          onSuccess: function(public_token, metadata) {
            // Send the public_token and account ID to your app server.
            console.log('public_token: ' + public_token);
            console.log('account ID: ' + metadata.account_id);
          },
        });

        // Trigger the Link UI
        document.getElementById('linkButton').onclick = function() {
          linkHandler.open();
        };
        </script>

Here's what I have in my controller for Stripe:

    def new 
    @stripe_btn_data = {
     key: "#{ Rails.configuration.stripe[:publishable_key] }",
    }   
    end

  #1 Create a charge
      customer = if current_user.stripe_id?
                    Stripe::Customer.retrieve(current_user.stripe_id)
                  else
                    Stripe::Customer.create(email: :stripeEmail)
                  end

      current_user.update(
        stripe_id: customer.id,
      )

       stripe_charge = Stripe::Charge.create(
         customer:    customer.id,
         amount:      (current_order.subtotal * 100).to_i,
         currency:    'usd',
       )
... more information to create an order then send email after charge. 

What do I need to include in my controller, or elsewhere, to create a charge via Plaid and Stripe?


Solution

  • Plaid has a writeup on how to wire this up - though the provided example is in Node.js - and Stripe has a guide too.

    That said, you'll need to add code in the onSuccess handler to send the public_token and metadata.account_id to your server's 'token exchange' endpoint and once you've got that, you can exchange those for the Stripe token, and finally, you'll need to attach that token to the Customer.

    So, something like this:

    plaid_user = Plaid::User.exchange_token(
      public_token,
      metadata_dot_account_id,
      product: :auth
    )
    
    puts plaid_user.stripe_bank_account_token
    
    customer = Stripe::Customer.retrieve("<customer-id>")
    
    customer.sources.create({
      :source => plaid_user.stripe_bank_account_token
    })
    

    And then you can do your Stripe::Charge.create().