I'm enthusiastic and passionate about web development and design in all it's forms and love to build application with efficient and simple methods

Thursday, June 18, 2015

Veritrans Rails 4 with dynamic server_key and client_key

1:44 AM Posted by Codeinterpreter No comments
Veritrans is Indonesian Payment Gateway. Veritrans accepts Credit Card, Direct Debit, e-Wallet,Bank Transfer and Convenience Store. That's why this Payment Gateway is famous in Indonesia. Further explanations about Veritrans https://veritrans.co.id/ .

Veritrans has multiple products with different integration method and pricing. About Veritrans Products https://veritrans.co.id/product.html


Veritrans Products:

VT-Link

VT-Link facilitates a payment feature for your online store by redirecting the customer to Veritrans payment page through a link.

VT-Web

VT-Web facilitatess merchant to use Veritrans payment system, by redirecting the customer to the Veritrans payment page.

VT-Direct

VT-Direct is Veritrans product that allows you to use our payment system directly on your website

Integration Tool Kit

  • Rails 4
  • gem veritrans

Configurations

Add gem Veritrans to Gem File and bundle install

    gem 'veritrans';

In Console generate veritrans.yml

rails g veritrans:install

Generate Veritrans Form

rails g veritrans:payment_form

This command will generate controller and view for Veritrans.
Because we will built dynamic key for this integrations. You can keep veritrans.yml configurations like this:

development:
  # Register in sandbox veritrans and get your keys here:
  # https://my.sandbox.veritrans.co.id/settings/config_info
  client_key: ""
  server_key: ""
  api_host: ""

production:
  # Register and get your keys here:
  # https://my.veritrans.co.id/settings/config_info
  client_key: ""
  server_key: ""
  api_host: ""

staging:
  # Register and get your keys here:
  # https://my.veritrans.co.id/settings/config_info
  client_key: ""
  server_key: ""
  api_host: ""

Scripting

VT-Web Integrations

In VT-Web Integrations, you just need to obtain redirect URL to Veritrans . After last check out process, put this function for directing page to veritrans:

@result = Veritrans.charge(
  payment_type: "VTWEB",
  transaction_details: {
    order_id: "order-id",
    gross_amount: 100_000
  }
)
// For Automatically redirect
redirect_to @result.redirect_url

VT-Direct Integrations

VT-Direct Integrations is a little bit complicated because a lot of sensitive data involved in this process.

Preparing The Form

In /views/shared/_credit_card_form   put your last checkout data in here. For example:



<%= form_for @payment, html: {class: "veritrans-payment-form", id:"card_form"} do |f| %>
<% if @settings.client_key.present? && @settings.server_key.present? && @settings.api_host.present? %>
  

Payments Page

<%= render "layouts/notification" %> <%= f.hidden_field :token_id, id:"card_token" %>
<%= label_tag "Length Of Membership" %> <%= number_field_tag :length_of_membership, 1,in: 1..10000 ,size: 5,class:"form-control", id:"length_of_membership",style:"width:10%;float:left;margin-right:10px;" %>
<%= label_tag "Membership Expired" %> <%= text_field_tag :membership_expired, (@company[:membership_expired] + 1.month).strftime('%Y-%m-%d'),size: 5,:readonly => true,class:"form-control", id:"date_expired",style:"width:75%;" %>
<%= f.number_field :amount, id:"gross_amount", size: 25,:readonly => true,class:"form-control",style:"width:75%;" %>
<%= label_tag :credit_card_number %> <%= text_field_tag :credit_card_number, '',:required => true,:placeholder => "4811 1111 1111 1114", name: nil, size: 25,class:"form-control", id:"card_number" %>
<%= label_tag :credit_card_cvv %> <%= text_field_tag :credit_card_cvv, '',:required => true,:placeholder => "123", name: nil,class:"form-control", id:"card_cvc" %>
<%= label_tag :credit_card_expire %> <%= text_field_tag :credit_card_expire, '',:required => true, placeholder: "MM / YY", name: nil,class:"form-control", id:"card_exp" %>
<%= f.label :credit_card_secure, "3D-secure" %> <%= f.check_box :credit_card_secure %>
<%= f.label :notes %> <%= text_area(:notes, :text, class:"form-control", size: "20x30") %>
<%= link_to (image_tag "undo-logo.png", title: :back).html_safe, users_path() %> <%= f.submit "Pay via VT-Direct", class:"btn green-btn" %>
<% else %>
<%= link_to (image_tag "undo-logo.png", title: :back).html_safe, users_path() %>
<% end %> <% end %>

Return Process

In Return Process build this functions

    veritrans_callback = {
      transaction_details: {
        order_id: payment.order_id,
        gross_amount: params[:payment][:amount].presence || @payment.amount
      }
    }
    // Get setting from database
    setting = Setting.first
    // Set key and host for Veritrans
    Veritrans::Config.client_key=(setting.client_key)
    Veritrans::Config.server_key=(setting.server_key)
    Veritrans::Config.api_host=(setting.api_host)

    params[:type] = "Credit Card"
    veritrans_callback[:payment_type] = "credit_card"
    veritrans_callback[:credit_card] = {}
    veritrans_callback[:credit_card][:token_id] = params[:payment][:token_id]
    result = Veritrans.charge(veritrans_callback)

    transaction_params = {}
    transaction_params = {
 order_id:payment.order_id,amounts:result.data[:gross_amount],transaction_status:result.data[:status_message],transaction_id:result.data[:transaction_id],transaction_time:result.data[:transaction_time]
}

    // If Transaction Success
    if result.data[:status_code] == "200"
      // Success Process 
    else
      // Failed Process
    end

That's All. I hope this will help anyone out there. :D



0 comments:

Post a Comment