lib/khalti/verification.rb in khalti-0.1.4 vs lib/khalti/verification.rb in khalti-0.1.5
- old
+ new
@@ -2,22 +2,16 @@
require 'net/http'
require 'json'
module Khalti
class Verification
+ API_URL = "https://khalti.com/api/payment/verify/"
+ SECRET_KEY = ENV['KHALTI_SECRET_KEY']
def self.verify(token, amount)
- api_url = "https://khalti.com/api/payment/verify/"
- secret_key = ENV['KHALTI_SECRET_KEY']
- headers = {
- Authorization: "Key #{secret_key}"
- }
- uri = URI.parse("#{api_url}")
- https = Net::HTTP.new(uri.host, uri.port)
- https.use_ssl = true
- request = Net::HTTP::Post.new(uri.request_uri, headers)
- request.set_form_data('token' => "#{token}", 'amount' => "#{amount}")
- response = https.request(request)
-
- JSON.parse(response.body) || {}
+ 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)
end
end
end