Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Braintree integration with Ruby on Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.29k
    Comment on it

    While working on an e-commerce project we need a secure payment system and Braintree framework is one of them.
    With Braintree, we can perform all types of credit card transactions and PayPal transactions.
    The first step is to crate account in sandbox:

    https://www.braintreepayments.com/sandbox

    Once you have created an account in sandbox you will get sandbox API keys like Merchant ID, Public Key etc.
    Add these keys to your config/application.yml

     

    BRAINTREE_ENVIRONMENT: sandbox
     BRAINTREE_MERCHANT_ID: 'merchant_id'
     BRAINTREE_PUBLIC_KEY: 'public key'
     BRAINTREE_PRIVATE_KEY: 'private key'
     BRAINTREE_CLIENT_KEY: 'client key'

    After this add the Braintree javascript.

    %script{:src => "https://js.braintreegateway.com/v2/braintree

    Add the Braintree gem to our Gemfile.

    gem 'braintree'

    In the example below we will allow a customer to make payment using a credit card.
    Create a customer form, that require Cardholder name, Card number, CVV number and expire date.

    <form method="post"  id="paymentForm">
    Payment details
    <ul>
    
    <li>
    <label>Card Number </label>
    <input type="text" name="card_number" id="card_number"  maxlength="20" placeholder="1234 5678 9012 3456"/>
    </li>
    <li>
    <label>Name on Card</label>
    <input type="text" name="card_name" id="card_name" placeholder="Srinivas Tamada"/>
    </li>
    <li class="vertical">
    
    <ul>
    <li>
    <label>Expires</label>
    <input type="text" name="expiry_month" id="expiry_month" maxlength="2" placeholder="MM" />
    <input type="text" name="expiry_year" id="expiry_year" maxlength="2" placeholder="YY" />
    </li>
    <li>
    <label>CVV</label>
    <input type="text" name="cvv" id="cvv" maxlength="3" placeholder="123" />
    </li>
    </ul>
    
    </li>
    <li>
    <input type="submit" id="paymentButton" value="Proceed" disabled="true" class="disable">
    </li>
    </ul>
    </form>


    After creating the form, First create a client token on the serve

    @client_token = Braintree::ClientToken.generate

    The token is needed in the second step where we assign Braintree js to our form.

    braintree.setup("client-token", "custom", {id: "form-id"});

    When we submit the form it will go to the action below. Here we have a payment_method_nonce parameter. That will collect all the fields in our form, and processed it.

    def make_payment
        result = Braintree::Transaction.sale(
          :amount => "100.00",
          :payment_method_nonce => params[:payment_method_nonce]
        )
       if result.success?
          flash[:notice] = "Transaction successful"
          redirect_to root_url
        else
          flash[:danger] = "Transaction error: #{result.transaction.processor_response_text}"
          redirect_to root_url
        end
    end

     

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: