All Files
(99.22%
covered at
2.69
hits/line)
6 files in total.
128 relevant lines.
127 lines covered and
1 lines missed
-
# frozen_string_literal: true
-
-
1
require 'rails/generators'
-
1
module Saman
-
1
module Generators
-
# Install generator class for saman gem using in rails
-
1
class InstallGenerator < Rails::Generators::Base
-
1
source_root File.expand_path('../templates', __FILE__)
-
1
desc 'Creates Saman initializer for your application'
-
-
1
def copy_initializer
-
template 'saman_initializer.rb', 'config/initializers/saman.rb'
-
end
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
require_relative 'saman/authorize'
-
1
require_relative 'saman/verify'
-
1
require_relative 'saman/health_check'
-
1
require_relative 'saman/configuration'
-
1
require_relative 'generators/saman/install_generator'
-
-
1
require 'savon'
-
1
require 'yaml'
-
1
require 'rest-client'
-
1
require 'json'
-
-
# Saman gateway module to handle requests
-
1
module Saman
-
1
attr_accessor :config
-
-
1
def self.authorize(params)
-
7
Saman::Client.new.authorize(params)
-
end
-
-
1
def self.verify(params)
-
2
Saman::Client.new.verify(params)
-
end
-
-
1
def self.up?
-
3
Saman::Client.new.health_check
-
end
-
end
-
# frozen_string_literal: true
-
-
# Saman module for gateway communications
-
1
module Saman
-
# extend Client class to handle authorize method
-
1
class Client
-
1
def authorize(params)
-
7
config = Saman.configuration
-
7
request_parameters = create_authorize_parameters(params)
-
7
response = send_rest_requests(
-
config.authorize_address,
-
request_parameters,
-
config.retry_count
-
)
-
7
raise response[:error] unless response.is_a? RestClient::Response
-
5
authorize_result(response)
-
end
-
-
1
private
-
-
1
def authorize_result(response)
-
5
token = parse_authorize_result(response)
-
4
{
-
method: 'POST',
-
fields: {
-
Token: token
-
},
-
url: Saman.configuration.authorize_address
-
}
-
end
-
-
1
def create_authorize_parameters(params)
-
7
{
-
Action: 'Token',
-
Amount: calculate_the_amount(params),
-
Wage: calculate_the_wage(params),
-
TerminalId: get_terminal_id(params),
-
ResNum: params[:order_id],
-
RedirectUrl: params[:redirect_url],
-
CellNumber: params[:customer][:mobile_number]
-
}
-
end
-
-
1
def get_terminal_id(params)
-
7
case params[:split_amount]&.count
-
when nil
-
4
Saman.configuration.terminal_id_default
-
when 1
-
return Saman.configuration.terminal_id_wage_only if \
-
2
params[:split_amount].key?(:wage)
-
1
Saman.configuration.terminal_id_default
-
else
-
1
Saman.configuration.terminal_id_with_wage
-
end
-
end
-
-
1
def calculate_the_wage(params)
-
7
return 0 unless multiple_accounts?(params)
-
3
return 0 if params[:split_amount].count == 1
-
-
1
params[:split_amount][:wage] || 0
-
end
-
-
1
def calculate_the_amount(params)
-
7
return params[:amount] unless multiple_accounts?(params)
-
3
return params[:amount] if params[:split_amount].count == 1
-
-
1
params[:split_amount][:amount] || 0
-
end
-
-
1
def multiple_accounts?(params)
-
14
params[:split_amount]&.count&.positive?
-
end
-
-
1
def parse_authorize_result(response)
-
5
token_result = JSON.parse(response.body)
-
5
token = token_result['token'] if token_result['status'].to_i == 1
-
raise "Code ##{token_result['errorCode']}, #{token_result['errorDesc']}" \
-
5
if token.nil?
-
-
4
token
-
end
-
-
1
def send_rest_requests(url, parameters, retry_to)
-
RestClient.proxy = Saman.configuration.proxy unless \
-
11
Saman.configuration.proxy.blank?
-
11
return RestClient.post(url, parameters.to_json, content_type: :json)
-
rescue RestClient::Exceptions::OpenTimeout => exception
-
3
retry if (retry_to -= 1).positive?
-
return { error: 'Saman is not available right now,
-
1
calling web service got time out' }
-
rescue StandardError => exception
-
3
retry if (retry_to -= 1).positive?
-
1
return { error: exception.message }
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
# Saman module for gateway communications
-
1
module Saman
-
1
class << self
-
1
attr_accessor :configuration
-
end
-
-
1
def self.configure
-
12
self.configuration ||= Configuration.new
-
12
yield(configuration)
-
end
-
-
# configure class
-
1
class Configuration
-
1
attr_accessor :username
-
1
attr_accessor :password
-
1
attr_accessor :proxy
-
1
attr_accessor :terminal_id_default
-
1
attr_accessor :terminal_id_with_wage
-
1
attr_accessor :terminal_id_wage_only
-
1
attr_writer :retry_count
-
1
attr_writer :authorize_wsdl
-
1
attr_writer :authorize_address
-
1
attr_writer :verify_wsdl
-
-
1
def authorize_address
-
22
@authorize_address || 'https://sep.shaparak.ir/MobilePG/MobilePayment'
-
end
-
-
1
def verify_wsdl
-
10
@verify_wsdl || 'https://verify.sep.ir/payments/referencepayment.asmx?wsdl'
-
end
-
-
1
def authorize_wsdl
-
7
@authorize_wsdl || 'https://sep.shaparak.ir/payments/initpayment.asmx?wsdl'
-
end
-
-
1
def retry_count
-
7
@retry_count || 3
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
module Saman
-
# Client class to handle requests
-
1
class Client
-
1
def health_check
-
3
authorize_up? && verify_up?
-
end
-
-
1
def verify_up?
-
2
verify_client = Savon.client(wsdl: Saman.configuration.verify_wsdl)
-
2
expected_operations_for_verify = %i[
-
verify_transaction
-
verify_transaction1
-
]
-
-
2
(expected_operations_for_verify - verify_client.operations).empty?
-
rescue StandardError
-
1
return false
-
end
-
-
1
def authorize_up?
-
3
authorize_client = Savon.client(wsdl: Saman.configuration.authorize_wsdl)
-
3
expected_operations_for_authorize = %i[
-
request_token
-
request_multi_settle_type_token
-
]
-
-
3
(expected_operations_for_authorize - authorize_client.operations).empty?
-
rescue StandardError
-
1
return false
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
# Saman module for gateway communications
-
1
module Saman
-
# extend Client class to handle verify method
-
1
class Client
-
1
def verify(params)
-
2
return false unless params[:State] == 'OK'
-
2
verify_the_payment(params)
-
end
-
-
1
private
-
-
1
def verify_the_payment(params)
-
2
savon_client = create_savon_client_for_verify
-
2
request_parameters = {
-
String_1: params[:RefNum],
-
String_2: params[:TerminalId]
-
}
-
2
response = send_verify_request(savon_client, request_parameters)
-
2
raise response[:error] if response.to_hash.include? :error
-
1
last_response = response.body[:verify_transaction_response][:result].to_i
-
1
last_response.positive?
-
end
-
-
1
def create_savon_client_for_verify
-
2
config = Saman.configuration
-
2
options = {
-
wsdl: config.verify_wsdl,
-
convert_request_keys_to: :none
-
}
-
2
options['proxy'] = config.proxy unless config.proxy.blank?
-
-
2
Savon.client(options)
-
end
-
-
1
def send_verify_request(client, parameters, retry_to = 3)
-
4
client.call(:verify_transaction, message: parameters)
-
rescue StandardError => exception
-
3
retry if (retry_to -= 1).positive?
-
1
return { error: exception.message }
-
end
-
end
-
end