lib/khalti/verification.rb in khalti-0.1.5 vs lib/khalti/verification.rb in khalti-0.2.0
- old
+ new
@@ -1,17 +1,39 @@
+# frozen_string_literal: true
+
require 'uri'
require 'net/http'
require 'json'
module Khalti
+ # Khalti verification API wrapper
class Verification
- API_URL = "https://khalti.com/api/payment/verify/"
+ API_URL = 'https://khalti.com/api/payment/verify/'
SECRET_KEY = ENV['KHALTI_SECRET_KEY']
- def self.verify(token, amount)
- raise Errors::BlankError.new('Ensure token is not blank.') if token.nil? || token.strip.empty?
- raise Errors::InvalidTokenError.new('Ensure token has at least 22 characters.') if token.strip.size < 22
- raise Errors::InvalidAmountError.new('Ensure amount is greate than 0 paisa.') if Integer(amount) < 0
- params = {'token': token, 'amount': Integer(amount)}
- RequestHelper.post(API_URL, params)
+ class << self
+ def verify(token, amount)
+ validate_blank_token(token)
+ validate_token_length(token)
+ validate_ammount(amount)
+ params = { 'token': token, 'amount': amount.to_i }
+ RequestHelper.post(API_URL, params)
+ end
+
+ def validate_blank_token(val)
+ return unless val.nil? || val.strip.empty?
+ raise Errors::BlankError, 'Ensure token is not blank.'
+ end
+
+ def validate_token_length(val)
+ return if val.strip.size >= 22
+ raise Errors::InvalidTokenError, 'Ensure token has at least ' \
+ '22 characters.'
+ end
+
+ def validate_ammount(val)
+ return if Integer(val).positive?
+ raise Errors::InvalidAmountError, 'Ensure amount is ' \
+ 'greater than 0 paisa.'
+ end
end
end
end