khalti 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b20511cc14806c470c53f8ec138616c0863f6b57e5db496ce314f4506ee51184
4
- data.tar.gz: 5acfb0d08b2201176fba040081eaac1b03e1077a47d1dd85826ea708b8915cb8
3
+ metadata.gz: 0d349aa4b31e87cb15894680e1164ffa59a7bab668388ffe98b05eaf3a6bfb44
4
+ data.tar.gz: 825b86c0034a05286bf996a7790b2aa7d427a73b853e49fd1f0a01f412ad1671
5
5
  SHA512:
6
- metadata.gz: ffa68561315dff3dba7c81e1e1449fed9f7036f3fe8c639549237dcd84d66c642b8d9d27947f500f092b2ede9a7334f2bb53f3c6553ed02423b0f33cffa99896
7
- data.tar.gz: 825340bb128d07112998e87120a4dadfefdff52db263bc3a91ade2da2ad93e8b69a86290532b31209d2e0299899d686b9068e7036ec91d4fe35cf6a2d24ea273
6
+ metadata.gz: 6a30ec1cc2a1b44ce39c3fc74eabe373aeb454f897b55476f3f506fef2c154d185f46c4384c9effc25a95f336655106c2ab8b2d88d62cd72aa4bf3d60af03203
7
+ data.tar.gz: 22bc9c2fb9180066fa6d54112aa74de1174e883433b70c33867afa69ff45585db448271d6f50fd04251a95a5ca93c408c45fe2a0201715d9438eace5755769b2
@@ -0,0 +1,2 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
data/Gemfile CHANGED
@@ -1,6 +1,8 @@
1
- source "https://rubygems.org"
1
+ # frozen_string_literal: true
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
6
 
5
7
  # Specify your gem's dependencies in khalti.gemspec
6
8
  gemspec
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- khalti (0.1.5)
4
+ khalti (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Build Status](https://travis-ci.org/degendra/khalti-ruby.png)](https://travis-ci.org/degendra/khalti-ruby) [![codecov](https://codecov.io/gh/degendra/khalti-ruby/branch/master/graph/badge.svg)](https://codecov.io/gh/degendra/khalti-ruby) [![Gem Version](https://badge.fury.io/rb/khalti.svg)](https://badge.fury.io/rb/khalti) [![Open Source Love](https://badges.frapsoft.com/os/v2/open-source.png?v=103)](https://github.com/ellerbrock/open-source-badges/) [![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)
1
+ [![Build Status](https://travis-ci.org/bajratech/khalti-ruby.png)](https://travis-ci.org/bajratech/khalti-ruby) [![codecov](https://codecov.io/gh/bajratech/khalti-ruby/branch/master/graph/badge.svg)](https://codecov.io/gh/bajratech/khalti-ruby) [![Gem Version](https://badge.fury.io/rb/khalti.svg)](https://badge.fury.io/rb/khalti) [![Open Source Love](https://badges.frapsoft.com/os/v2/open-source.png?v=103)](https://github.com/ellerbrock/open-source-badges/) [![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)
2
2
 
3
3
  # Khalti
4
4
 
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "khalti"
4
+ require 'bundler/setup'
5
+ require 'khalti'
5
6
 
6
7
  # You can add fixtures and/or initialization code here to make experimenting
7
8
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +11,5 @@ require "khalti"
10
11
  # require "pry"
11
12
  # Pry.start
12
13
 
13
- require "irb"
14
+ require 'irb'
14
15
  IRB.start(__FILE__)
@@ -1,38 +1,38 @@
1
+ # frozen_string_literal: true
1
2
 
2
- lib = File.expand_path("../lib", __FILE__)
3
+ lib = File.expand_path('lib', __dir__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "khalti/version"
5
+ require 'khalti/version'
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = "khalti"
8
+ spec.name = 'khalti'
8
9
  spec.version = Khalti::VERSION
9
- spec.authors = ["Degendra Sivakoti"]
10
- spec.email = ["degendra.sivakoti@gmail.com"]
11
-
12
- spec.summary = %q{Ruby gem for Khalti online transaction platform.}
13
- spec.description = %q{Welcome to the khalti ruby gem. It is an API wrapper for Khalti payment gateway. For more info visit Khalti Gateway Docs.}
14
- spec.homepage = "https://github.com/bajratech/khalti-ruby"
15
- spec.license = "MIT"
16
-
17
- # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
- # to allow pushing to a single host or delete this section to allow pushing to any host.
10
+ spec.authors = ['Degendra Sivakoti']
11
+ spec.email = ['degendra.sivakoti@gmail.com']
12
+ spec.summary = 'Ruby gem for Khalti online transaction platform.'
13
+ spec.description = 'https://github.com/bajratech/khalti-ruby'
14
+ spec.homepage = 'https://github.com/bajratech/khalti-ruby'
15
+ spec.license = 'MIT'
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either
17
+ # set the 'allowed_push_host' to allow pushing to a single host or
18
+ # delete this section to allow pushing to any host.
19
19
  if spec.respond_to?(:metadata)
20
- # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+ # spec.metadata["allowed_push_host"] = "Set to 'http://mygemserver.com'"
21
21
  else
22
- raise "RubyGems 2.0 or newer is required to protect against " \
23
- "public gem pushes."
22
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
23
+ 'public gem pushes.'
24
24
  end
25
25
 
26
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
27
  f.match(%r{^(test|spec|features)/})
28
28
  end
29
- spec.bindir = "exe"
29
+ spec.bindir = 'exe'
30
30
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
- spec.require_paths = ["lib"]
31
+ spec.require_paths = ['lib']
32
32
 
33
- spec.add_development_dependency "bundler", "~> 1.16"
34
- spec.add_development_dependency "rake", "~> 10.0"
35
- spec.add_development_dependency "rspec", "~> 3.0"
36
- spec.add_development_dependency "webmock", "~> 3.3"
37
- spec.add_development_dependency "codecov", "~> 0.1"
33
+ spec.add_development_dependency 'bundler', '~> 1.16'
34
+ spec.add_development_dependency 'codecov', '~> 0.1'
35
+ spec.add_development_dependency 'rake', '~> 10.0'
36
+ spec.add_development_dependency 'rspec', '~> 3.0'
37
+ spec.add_development_dependency 'webmock', '~> 3.3'
38
38
  end
@@ -1,9 +1,12 @@
1
- require_relative "khalti/version"
2
- require_relative "khalti/verification"
3
- require_relative "khalti/transaction"
4
- require_relative "khalti/request_helper"
5
- require_relative "khalti/errors"
1
+ # frozen_string_literal: true
6
2
 
3
+ require_relative 'khalti/version'
4
+ require_relative 'khalti/verification'
5
+ require_relative 'khalti/transaction'
6
+ require_relative 'khalti/request_helper'
7
+ require_relative 'khalti/errors'
8
+
9
+ # Khalti payment gateway API wrapper
7
10
  module Khalti
8
11
  # Your code goes here...
9
12
  end
@@ -1,23 +1,20 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Khalti
2
4
  module Errors
3
- class KhaltiError < RuntimeError
4
- attr_reader :data
5
- def initialize(data)
6
- @data = data
7
- super
8
- end
9
- end
5
+ # Custom error class to handle RuntimeError
6
+ class KhaltiError < RuntimeError; end
10
7
 
11
- class BlankError < RuntimeError
12
- end
8
+ # Custom error class to handle blank variables
9
+ class BlankError < KhaltiError; end
13
10
 
14
- class InvalidAmountError < RuntimeError
15
- end
11
+ # Custom error class to handle invalid amount
12
+ class InvalidAmountError < KhaltiError; end
16
13
 
17
- class InvalidTokenError < RuntimeError
18
- end
14
+ # Custom error class to handle invalid token
15
+ class InvalidTokenError < KhaltiError; end
19
16
 
20
- class InvalidResponseError < RuntimeError
21
- end
17
+ # Custom error class to handle invalid response data
18
+ class InvalidResponseError < KhaltiError; end
22
19
  end
23
20
  end
@@ -1,48 +1,55 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Khalti
4
+ # Handles API calls
2
5
  module RequestHelper
3
- SECRET_KEY = ENV['KHALTI_SECRET_KEY']
4
- def self.get(path)
5
- self.validate_secret_key
6
- uri = URI.parse(path)
7
- req = Net::HTTP::Get.new(uri)
8
- req['authorization'] = "Key #{SECRET_KEY}"
9
- res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) {|http|
10
- http.request(req)
11
- }
12
- case res
13
- when Net::HTTPSuccess
14
- self.validate_content_type(res['content-type'])
15
- JSON.parse res.body
16
- else
17
- raise Errors::KhaltiError.new(res.message)
6
+ class << self
7
+ SECRET_KEY = ENV['KHALTI_SECRET_KEY']
8
+ def get(path)
9
+ validate_secret_key
10
+ uri = URI.parse(path)
11
+ req = Net::HTTP::Get.new(uri)
12
+ req['authorization'] = "Key #{SECRET_KEY}"
13
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
14
+ http.request(req)
15
+ end
16
+ extract_response(res)
18
17
  end
19
- end
20
18
 
21
- def self.post(path, params)
22
- self.validate_secret_key
23
- uri = URI.parse(path)
24
- req = Net::HTTP::Post.new(uri)
25
- req['authorization'] = "Key #{SECRET_KEY}"
26
- req.set_form_data(params)
27
- res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) {|http|
28
- http.request(req)
29
- }
30
- case res
19
+ def post(path, params)
20
+ validate_secret_key
21
+ uri = URI.parse(path)
22
+ req = Net::HTTP::Post.new(uri)
23
+ req['authorization'] = "Key #{SECRET_KEY}"
24
+ req.set_form_data(params)
25
+ res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
26
+ http.request(req)
27
+ end
28
+ extract_response(res)
29
+ end
30
+
31
+ private
32
+
33
+ def validate_secret_key
34
+ return unless SECRET_KEY.nil? || SECRET_KEY.strip.empty?
35
+ raise Errors::BlankError, 'Ensure KHALTI_SECRET_KEY is not blank.'
36
+ end
37
+
38
+ def validate_content_type(content_type)
39
+ return if content_type == 'application/json'
40
+ raise Errors::InvalidResponseError,
41
+ 'Content-type is not application/json.'
42
+ end
43
+
44
+ def extract_response(res)
45
+ case res
31
46
  when Net::HTTPSuccess
32
- self.validate_content_type(res['content-type'])
47
+ validate_content_type(res['content-type'])
33
48
  JSON.parse res.body
34
49
  else
35
- raise Errors::KhaltiError.new(res.message)
50
+ raise Errors::KhaltiError, res.message
51
+ end
36
52
  end
37
53
  end
38
-
39
- private
40
- def self.validate_secret_key
41
- raise Errors::BlankError.new('Ensure KHALTI_SECRET_KEY is not blank.') if SECRET_KEY.nil? || SECRET_KEY.strip.empty?
42
- end
43
-
44
- def self.validate_content_type(content_type)
45
- Errors::InvalidResponseError.new('Content-type is not application/json.') unless content_type ==='application/json'
46
- end
47
54
  end
48
55
  end
@@ -1,12 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'uri'
2
4
  require 'net/http'
3
5
  require 'json'
4
6
 
5
7
  module Khalti
8
+ # Khalti Transaction API wrapper
6
9
  class Transaction
7
10
  API_URL = 'https://khalti.com/api/merchant-transaction'
8
11
  def self.find(idx)
9
- raise Errors::BlankError.new('Ensure idx is not blank.') if idx.nil? || idx.strip.empty?
12
+ raise Errors::BlankError, 'Ensure idx is not blank.' \
13
+ if idx.nil? || idx.strip.empty?
10
14
  RequestHelper.get("#{API_URL}/#{idx}/")
11
15
  end
12
16
 
@@ -1,17 +1,39 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'uri'
2
4
  require 'net/http'
3
5
  require 'json'
4
6
 
5
7
  module Khalti
8
+ # Khalti verification API wrapper
6
9
  class Verification
7
- API_URL = "https://khalti.com/api/payment/verify/"
10
+ API_URL = 'https://khalti.com/api/payment/verify/'
8
11
  SECRET_KEY = ENV['KHALTI_SECRET_KEY']
9
- def self.verify(token, amount)
10
- raise Errors::BlankError.new('Ensure token is not blank.') if token.nil? || token.strip.empty?
11
- raise Errors::InvalidTokenError.new('Ensure token has at least 22 characters.') if token.strip.size < 22
12
- raise Errors::InvalidAmountError.new('Ensure amount is greate than 0 paisa.') if Integer(amount) < 0
13
- params = {'token': token, 'amount': Integer(amount)}
14
- RequestHelper.post(API_URL, params)
12
+ class << self
13
+ def verify(token, amount)
14
+ validate_blank_token(token)
15
+ validate_token_length(token)
16
+ validate_ammount(amount)
17
+ params = { 'token': token, 'amount': amount.to_i }
18
+ RequestHelper.post(API_URL, params)
19
+ end
20
+
21
+ def validate_blank_token(val)
22
+ return unless val.nil? || val.strip.empty?
23
+ raise Errors::BlankError, 'Ensure token is not blank.'
24
+ end
25
+
26
+ def validate_token_length(val)
27
+ return if val.strip.size >= 22
28
+ raise Errors::InvalidTokenError, 'Ensure token has at least ' \
29
+ '22 characters.'
30
+ end
31
+
32
+ def validate_ammount(val)
33
+ return if Integer(val).positive?
34
+ raise Errors::InvalidAmountError, 'Ensure amount is ' \
35
+ 'greater than 0 paisa.'
36
+ end
15
37
  end
16
38
  end
17
39
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Khalti
2
- VERSION = "0.1.5"
4
+ VERSION = '0.2.0'
3
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: khalti
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Degendra Sivakoti
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-02 00:00:00.000000000 Z
11
+ date: 2018-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -25,63 +25,62 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.16'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: codecov
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '0.1'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '0.1'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rspec
42
+ name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: '10.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.0'
54
+ version: '10.0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: webmock
56
+ name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '3.3'
61
+ version: '3.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '3.3'
68
+ version: '3.0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: codecov
70
+ name: webmock
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0.1'
75
+ version: '3.3'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0.1'
83
- description: Welcome to the khalti ruby gem. It is an API wrapper for Khalti payment
84
- gateway. For more info visit Khalti Gateway Docs.
82
+ version: '3.3'
83
+ description: https://github.com/bajratech/khalti-ruby
85
84
  email:
86
85
  - degendra.sivakoti@gmail.com
87
86
  executables: []
@@ -90,6 +89,7 @@ extra_rdoc_files: []
90
89
  files:
91
90
  - ".gitignore"
92
91
  - ".rspec"
92
+ - ".rubocop.yml"
93
93
  - ".travis.yml"
94
94
  - CODE_OF_CONDUCT.md
95
95
  - Gemfile