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 +4 -4
- data/.rubocop.yml +2 -0
- data/Gemfile +4 -2
- data/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/Rakefile +5 -3
- data/bin/console +4 -3
- data/khalti.gemspec +24 -24
- data/lib/khalti.rb +8 -5
- data/lib/khalti/errors.rb +12 -15
- data/lib/khalti/request_helper.rb +44 -37
- data/lib/khalti/transaction.rb +5 -1
- data/lib/khalti/verification.rb +29 -7
- data/lib/khalti/version.rb +3 -1
- metadata +16 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d349aa4b31e87cb15894680e1164ffa59a7bab668388ffe98b05eaf3a6bfb44
|
4
|
+
data.tar.gz: 825b86c0034a05286bf996a7790b2aa7d427a73b853e49fd1f0a01f412ad1671
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a30ec1cc2a1b44ce39c3fc74eabe373aeb454f897b55476f3f506fef2c154d185f46c4384c9effc25a95f336655106c2ab8b2d88d62cd72aa4bf3d60af03203
|
7
|
+
data.tar.gz: 22bc9c2fb9180066fa6d54112aa74de1174e883433b70c33867afa69ff45585db448271d6f50fd04251a95a5ca93c408c45fe2a0201715d9438eace5755769b2
|
data/.rubocop.yml
ADDED
data/Gemfile
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
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
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
[![Build Status](https://travis-ci.org/
|
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
data/bin/console
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
|
-
require
|
4
|
-
require
|
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
|
14
|
+
require 'irb'
|
14
15
|
IRB.start(__FILE__)
|
data/khalti.gemspec
CHANGED
@@ -1,38 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
|
-
lib = File.expand_path(
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
5
|
+
require 'khalti/version'
|
5
6
|
|
6
7
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
8
|
+
spec.name = 'khalti'
|
8
9
|
spec.version = Khalti::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
|
12
|
-
spec.
|
13
|
-
spec.
|
14
|
-
spec.
|
15
|
-
|
16
|
-
|
17
|
-
#
|
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"] = "
|
20
|
+
# spec.metadata["allowed_push_host"] = "Set to 'http://mygemserver.com'"
|
21
21
|
else
|
22
|
-
raise
|
23
|
-
|
22
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
23
|
+
'public gem pushes.'
|
24
24
|
end
|
25
25
|
|
26
|
-
spec.files
|
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 =
|
29
|
+
spec.bindir = 'exe'
|
30
30
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
-
spec.require_paths = [
|
31
|
+
spec.require_paths = ['lib']
|
32
32
|
|
33
|
-
spec.add_development_dependency
|
34
|
-
spec.add_development_dependency
|
35
|
-
spec.add_development_dependency
|
36
|
-
spec.add_development_dependency
|
37
|
-
spec.add_development_dependency
|
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
|
data/lib/khalti.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
|
-
|
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
|
data/lib/khalti/errors.rb
CHANGED
@@ -1,23 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Khalti
|
2
4
|
module Errors
|
3
|
-
class
|
4
|
-
|
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
|
12
|
-
end
|
8
|
+
# Custom error class to handle blank variables
|
9
|
+
class BlankError < KhaltiError; end
|
13
10
|
|
14
|
-
class
|
15
|
-
end
|
11
|
+
# Custom error class to handle invalid amount
|
12
|
+
class InvalidAmountError < KhaltiError; end
|
16
13
|
|
17
|
-
class
|
18
|
-
end
|
14
|
+
# Custom error class to handle invalid token
|
15
|
+
class InvalidTokenError < KhaltiError; end
|
19
16
|
|
20
|
-
class
|
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
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
-
|
47
|
+
validate_content_type(res['content-type'])
|
33
48
|
JSON.parse res.body
|
34
49
|
else
|
35
|
-
raise Errors::KhaltiError
|
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
|
data/lib/khalti/transaction.rb
CHANGED
@@ -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
|
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
|
|
data/lib/khalti/verification.rb
CHANGED
@@ -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 =
|
10
|
+
API_URL = 'https://khalti.com/api/payment/verify/'
|
8
11
|
SECRET_KEY = ENV['KHALTI_SECRET_KEY']
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
data/lib/khalti/version.rb
CHANGED
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.
|
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-
|
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:
|
28
|
+
name: codecov
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
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: '
|
40
|
+
version: '0.1'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
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: '
|
54
|
+
version: '10.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '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.
|
68
|
+
version: '3.0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: webmock
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
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: '
|
83
|
-
description:
|
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
|