khalti 0.1.4 → 0.1.5

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: 309f5628dd3b199d9eca368e1bc05b27ae09ee77e507588256a6985ffa698283
4
- data.tar.gz: 68580e107ed53666784aebb19da5be2a98d8f08ad74ace6c8e366b7b04b4c1a9
3
+ metadata.gz: b20511cc14806c470c53f8ec138616c0863f6b57e5db496ce314f4506ee51184
4
+ data.tar.gz: 5acfb0d08b2201176fba040081eaac1b03e1077a47d1dd85826ea708b8915cb8
5
5
  SHA512:
6
- metadata.gz: 58f20eebbd9a41e48a8bced44bdbb5d608dc75e53658d366545d3b841107432cbd1d366d221028c893ce649549e9b3c084fa05fd0b0158b9149feaa9387affef
7
- data.tar.gz: c6494c81d7d5ed477ad241fb2ff804471b6493b7e6bf9b0227502a616d3fdfaf37dd3e9e6c04172a8d8fb742923160bab7976633ad0e31ba5e43291d05ea9d15
6
+ metadata.gz: ffa68561315dff3dba7c81e1e1449fed9f7036f3fe8c639549237dcd84d66c642b8d9d27947f500f092b2ede9a7334f2bb53f3c6553ed02423b0f33cffa99896
7
+ data.tar.gz: 825340bb128d07112998e87120a4dadfefdff52db263bc3a91ade2da2ad93e8b69a86290532b31209d2e0299899d686b9068e7036ec91d4fe35cf6a2d24ea273
data/.travis.yml CHANGED
@@ -4,4 +4,5 @@ rvm:
4
4
  - 2.5.0
5
5
  before_install: gem install bundler -v 1.16.1
6
6
  script:
7
+ - export KHALTI_SECRET_KEY=your_secret_key
7
8
  - bundle exec rspec spec
data/Gemfile.lock CHANGED
@@ -1,17 +1,23 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- khalti (0.1.3)
4
+ khalti (0.1.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  addressable (2.5.2)
10
10
  public_suffix (>= 2.0.2, < 4.0)
11
+ codecov (0.1.10)
12
+ json
13
+ simplecov
14
+ url
11
15
  crack (0.4.3)
12
16
  safe_yaml (~> 1.0.0)
13
17
  diff-lcs (1.3)
18
+ docile (1.3.0)
14
19
  hashdiff (0.3.7)
20
+ json (2.1.0)
15
21
  public_suffix (3.0.2)
16
22
  rake (10.5.0)
17
23
  rspec (3.7.0)
@@ -28,6 +34,12 @@ GEM
28
34
  rspec-support (~> 3.7.0)
29
35
  rspec-support (3.7.1)
30
36
  safe_yaml (1.0.4)
37
+ simplecov (0.16.1)
38
+ docile (~> 1.1)
39
+ json (>= 1.8, < 3)
40
+ simplecov-html (~> 0.10.0)
41
+ simplecov-html (0.10.2)
42
+ url (0.3.2)
31
43
  webmock (3.3.0)
32
44
  addressable (>= 2.3.6)
33
45
  crack (>= 0.3.2)
@@ -38,6 +50,7 @@ PLATFORMS
38
50
 
39
51
  DEPENDENCIES
40
52
  bundler (~> 1.16)
53
+ codecov (~> 0.1)
41
54
  khalti!
42
55
  rake (~> 10.0)
43
56
  rspec (~> 3.0)
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)
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)
2
2
 
3
3
  # Khalti
4
4
 
data/khalti.gemspec CHANGED
@@ -34,4 +34,5 @@ Gem::Specification.new do |spec|
34
34
  spec.add_development_dependency "rake", "~> 10.0"
35
35
  spec.add_development_dependency "rspec", "~> 3.0"
36
36
  spec.add_development_dependency "webmock", "~> 3.3"
37
+ spec.add_development_dependency "codecov", "~> 0.1"
37
38
  end
@@ -0,0 +1,23 @@
1
+ module Khalti
2
+ module Errors
3
+ class KhaltiError < RuntimeError
4
+ attr_reader :data
5
+ def initialize(data)
6
+ @data = data
7
+ super
8
+ end
9
+ end
10
+
11
+ class BlankError < RuntimeError
12
+ end
13
+
14
+ class InvalidAmountError < RuntimeError
15
+ end
16
+
17
+ class InvalidTokenError < RuntimeError
18
+ end
19
+
20
+ class InvalidResponseError < RuntimeError
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,48 @@
1
+ module Khalti
2
+ 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)
18
+ end
19
+ end
20
+
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
31
+ when Net::HTTPSuccess
32
+ self.validate_content_type(res['content-type'])
33
+ JSON.parse res.body
34
+ else
35
+ raise Errors::KhaltiError.new(res.message)
36
+ end
37
+ 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
+ end
48
+ end
@@ -2,37 +2,16 @@ require 'uri'
2
2
  require 'net/http'
3
3
  require 'json'
4
4
 
5
- API_URL = 'https://khalti.com/api/merchant-transaction'
6
-
7
5
  module Khalti
8
6
  class Transaction
7
+ API_URL = 'https://khalti.com/api/merchant-transaction'
9
8
  def self.find(idx)
10
- secret_key = ENV['KHALTI_SECRET_KEY'] #test_secret_key_f59e8b7d18b4499ca40f68195a846e9b
11
- headers = {
12
- Authorization: "Key #{secret_key}"
13
- }
14
- uri = URI.parse("#{API_URL}/#{idx}/")
15
- puts uri
16
- https = Net::HTTP.new(uri.host, uri.port)
17
- https.use_ssl = true
18
- request = Net::HTTP::Get.new(uri.request_uri, headers)
19
- response = https.request(request)
20
-
21
- JSON.parse(response.body) || {}
9
+ raise Errors::BlankError.new('Ensure idx is not blank.') if idx.nil? || idx.strip.empty?
10
+ RequestHelper.get("#{API_URL}/#{idx}/")
22
11
  end
23
12
 
24
13
  def self.all
25
- secret_key = ENV['KHALTI_SECRET_KEY'] #test_secret_key_f59e8b7d18b4499ca40f68195a846e9b
26
- headers = {
27
- Authorization: "Key #{secret_key}"
28
- }
29
- uri = URI.parse("#{API_URL}/")
30
- https = Net::HTTP.new(uri.host, uri.port)
31
- https.use_ssl = true
32
- request = Net::HTTP::Get.new(uri.request_uri, headers)
33
- response = https.request(request)
34
-
35
- JSON.parse(response.body) || {}
14
+ RequestHelper.get("#{API_URL}/")
36
15
  end
37
16
  end
38
17
  end
@@ -4,20 +4,14 @@ require 'json'
4
4
 
5
5
  module Khalti
6
6
  class Verification
7
+ API_URL = "https://khalti.com/api/payment/verify/"
8
+ SECRET_KEY = ENV['KHALTI_SECRET_KEY']
7
9
  def self.verify(token, amount)
8
- api_url = "https://khalti.com/api/payment/verify/"
9
- secret_key = ENV['KHALTI_SECRET_KEY']
10
- headers = {
11
- Authorization: "Key #{secret_key}"
12
- }
13
- uri = URI.parse("#{api_url}")
14
- https = Net::HTTP.new(uri.host, uri.port)
15
- https.use_ssl = true
16
- request = Net::HTTP::Post.new(uri.request_uri, headers)
17
- request.set_form_data('token' => "#{token}", 'amount' => "#{amount}")
18
- response = https.request(request)
19
-
20
- JSON.parse(response.body) || {}
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)
21
15
  end
22
16
  end
23
17
  end
@@ -1,3 +1,3 @@
1
1
  module Khalti
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/khalti.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require_relative "khalti/version"
2
2
  require_relative "khalti/verification"
3
3
  require_relative "khalti/transaction"
4
+ require_relative "khalti/request_helper"
5
+ require_relative "khalti/errors"
4
6
 
5
7
  module Khalti
6
8
  # Your code goes here...
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.4
4
+ version: 0.1.5
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-03-31 00:00:00.000000000 Z
11
+ date: 2018-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: codecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.1'
69
83
  description: Welcome to the khalti ruby gem. It is an API wrapper for Khalti payment
70
84
  gateway. For more info visit Khalti Gateway Docs.
71
85
  email:
@@ -87,6 +101,8 @@ files:
87
101
  - bin/setup
88
102
  - khalti.gemspec
89
103
  - lib/khalti.rb
104
+ - lib/khalti/errors.rb
105
+ - lib/khalti/request_helper.rb
90
106
  - lib/khalti/transaction.rb
91
107
  - lib/khalti/verification.rb
92
108
  - lib/khalti/version.rb