JSON - RUBY SETUP

CLICK HERE FOR GITHUB REPOSITORY

Step 1 - Add oauth2.0 GEM

Add gem “oauth2” to your Gemfile for handling authentication to the PayTrace API. Don't forget to bundle install.

@@ -2,6 +2,8 @@ source 'https://rubygems.org'
 
 ruby "2.1.2"
 
+gem "oauth2", '~>1.0'
+
 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
 gem 'rails', '4.1.4'
 # Use sqlite3 as the database for Active Record

Step 2 - Teach your application to set up a PayTrace API token

Get your controller pointed at the PayTrace JSON API at https://api.paytrace.com.

 @@ -2,4 +2,19 @@ class ApplicationController < ActionController::Base
   # Prevent CSRF attacks by raising an exception.
   # For APIs, you may want to use :null_session instead.
   protect_from_forgery with: :exception
+  
+  def self.paytrace_api
+    OAuth2::Client.new(
+      nil, nil,
+      site: 'https://api.paytrace.com',
+    ).password.get_token(
+      *Rails.configuration.paytrace_api_credentials
+    )
+  end
+  
+  protected
+  
+  def paytrace_api
+    self.class.paytrace_api
+  end
 end

Step 3 - Provide username and password in the configuration file.

Provide your PayTrace user credentials in the configuration file in order to authenticate.

@@ -34,4 +34,8 @@
 
   # Raises error for missing translations
   # config.action_view.raise_on_missing_translations = true
+  
+  # Credentials for accessing the API account at api.paytrace.com (username, password)
+  # NOTE: Set this configuration variable in your production environment and mock the connection for testing.
+  config.paytrace_api_credentials = ['demo123', 'demo123']
 end

Step 4 - Build the checkout form

Build your form to capture pertinent information. Please note the client side encryption details in this example. Be sure to include "data-name" and "encrypted:" with the class of "pt=encrypt" for the fields you want encrypted. You will also include the Javascript tag at the bottom referencing our Javascript library for client side encryption, and also the public key you are serving.

+<h1>Checkout</h1>
+
+<%= form_tag(cart_pay_path, method: :post) do %>
+  <div>
+    Cart Total: $<%= "%0.2f" % [@cart_total] %> 
+  </div>
+  <div>
+    <label for="sale_credit_card_number">Credit Card Number:</label>
+    <input type="text" id="sale_credit_card_number" data-name="sale[credit_card][encrypted:number]" class="pt-encrypt" /><br />
+    Card Expiration:<br/>
+    <label for="sale_credit_card_expiration_month">Month</label>
+    <input id="sale_credit_card_expiration_month" name="sale[credit_card][expiration_month]" type="text" size="2" maxlength="2"/>
+    <label for="sale_credit_card_expiration_year">Year</label>
+    <input id="sale_credit_card_expiration_year" name="sale[credit_card][expiration_year]" type="text" size="4" maxlength="4"/>
+  </div>
+  <input name="commit" type="submit" value="Pay" />
+<% end %>
+
+<!--
+
+NOTE:
+  The PEM public key file for your merchant account must be served from the URL referenced by
+  the "data-paytrace-keyurl" attribute below ("/e2ee-public-key.pem" in this case) and it
+  must be a URL on the same server serving this form.  For Rails, put the public key in the
+  "public/e2ee-public-key.pem" file.
+
+-->
+<script src="https://api.paytrace.com/assets/e2ee/paytrace-e2ee.js" data-paytrace-keyurl="/e2ee-public-key.pem"></script>

Step 5 - Process the transaction

This will get your JSON request off to PayTrace from your app controller.

@@ -0,0 +1,36 @@
+class CartController < ApplicationController
+  def checkout
+    # This is where you would look up and render the amount to be paid, along
+    # with any transaction details (line items, tax, etc.).  For the demonstration,
+    # we collect everything except the transaction amount on the form.
+    @cart_total = cart_total
+  end
+
+  def pay
+    # params contains the data posted from the "checkout" form (see the
+    # "checkout" action in this class)
+    
+    request = params[:sale]
+    
+    # This is where you would make any adjustments to the information gathered by
+    # Rails into "request", such as setting request[:amount] based on an invoice
+    # or the current cart balance.
+    request[:amount] = cart_total
+    
+    @request = request.to_json # For display
+    
+    response = paytrace_api.post('/v1/transactions/sale/keyed', body: request)
+    @response_status = response.status
+    @response = response.body
+    
+    # In a real application, you would capture the result of paytrace_api.post
+    # in a model instance, then redirect to a page displaying the information,
+    # obtaining it for display from the model object.
+  end
+  
+  protected
+  
+  def cart_total
+    10.00
+  end
+end

Step 6 - Serve your public key for client side encryption

@@ -0,0 +1,9 @@
+-----BEGIN PUBLIC KEY-----
+MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqr0q5WgXwZH+ZA9Cry2d
+Gh82ZxMa5jRH7DZznqX+ByZxGQkAjT43Cs56WW0nudiIMYSj6VADz+1gSoMWDYE0
+9kSgBTVh0tiqDixjhlkZA5VBsrfhczQZV0LWQNTgDfQiebsjydgwVadjUifw3Afe
+am+R+AK4t8umBhRplpGR04sCZmDTTFPvSb1l3TFn7j+YLSp9ekmIDApEJpj52WDv
+j295FeHgm0SyS9X6pCsWMt/eHCUhjGm9NkDq6B/HMbIeIdURv/aR1ejW0HbrOFB1
+5aT8axPIpQwXHCYnt+L4Zk8yjyV53/iaF30mdiE9yJG1UA0UsRcXwJ30lCim4GSN
+mwIDAQAB
+-----END PUBLIC KEY-----