Skip to content

TestingPays/realex_rails_example_app

Repository files navigation

Realex Rails Example Application

Integrated example application using Realex's Auth API.

Requirements

In order to run this application you will need the following:

  • ruby (version 2.2+)
    • We recommend you use a ruby version manager, such as rvm
    • If you are using windows you can find a ruby installer here

Setup

Firstly pull down the repo.

$ git clone https://github.com/testingpays/realex_rails_example_app.git

Next enter the directory and install the applications dependencies using bundler

$ gem install bundler
$ bundle install

Running the application

Now that we have the application installed and our api keys setup we can start using the application. firstly lets run the tests to make everything is in order.

$ rails test

Your tests should have ran successfully. now to run the application use the following command.

$ rails server

Your application should now be running locally.

API Keys

Insert your Realex test keys to start with

# Set the realex gateway
@@gateway = ActiveMerchant::Billing::RealexGateway.new(
  login: "YOUR-API-KEY-HERE",
  password: "SECRET"
)

Developing with Testing Pays

In order to work with Testing Pays you need to provide your API Key. When working with Realex we replace our login and password fields with your Testing Pays API key. This is done in the realex_handler_module.

Note that we ignore the password field and do not require your actual Realex password.

This application points to the Testing Pays Realex auth API when running in both development and testing modes. This is set in the testing_pays initializer.

# config/initializers/testing_pays.rb
if Rails.env.development? || Rails.env.test?
  ActiveMerchant::Billing::Base.mode = :test

  module ActiveMerchant
    module Billing
      class RealexGateway
        self.live_url = "https://api.testingpays.com/#{ENV["TESTING_PAYS_KEY"]}/realex/v1/auth"
      end
    end
  end
end

Unit Testing with Testing Pays

Testing Pays makes testing many types of responses easy. In order to get a particular response simply pass in the associated response mapping, which is based on the cent part of the amount field:

amount: X.10  # => insufficient funds
amount: X.21  # => bank communications error
amount: X.00   # => success

For a full list of response mappings see the response mappings table under your account.

# test/controllers/charges_controller_test.rb

require 'test_helper'
require 'minitest/mock'

class ChargesControllerTest < ActionController::TestCase

  # Called before every test
  setup do
    @valid_params = {
      amount: 1.10,
      first_name: "John",
      last_name: "Smith",
      number: 4111111111111111,
      cvv: 222,
      year: "2023",
      month: "12"
    }
  end

  test "should create a successful purchase" do
    @valid_params["amount"] = 1.00                  # .00 => tp_success
    post :create, params: @valid_params
    assert_response 200
    json_result = JSON.parse(response.body)
    assert_match "success", json_result["result"]
    assert_match "00", json_result["code"]
  end

  test "should return an insufficent funds message" do
    @valid_params["amount"] = 1.10                  # 10 => tp_insufficient_funds
    post :create, params: @valid_params
    assert_response 402
    json_result = JSON.parse(response.body)

    assert_match "fail", json_result["result"]
    assert_match "101", json_result["code"]
  end

  test "should return invalid card message when number is invalid" do
    @valid_params["number"] = 1234567890
    post :create, params: @valid_params
    assert_response 402
    json_result = JSON.parse(response.body)

    assert_match "credit card invalid", json_result["message"]
    assert_match "error", json_result["code"]
  end

  test "should return bank communication error" do
    @valid_params["amount"] = 1.21
    post :create, params: @valid_params
    assert_response 402
    json_result = JSON.parse(response.body)

    assert_match "We couldn't contact your bank to finailze the transaction, could you try again?", json_result["message"]
    assert_match "205", json_result["code"]
  end
end