voucherify 0.8.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +206 -808
- data/Rakefile +6 -4
- data/examples/customers.rb +32 -0
- data/examples/utils.rb +17 -19
- data/examples/vouchers.rb +20 -0
- data/lib/voucherify/client.rb +69 -0
- data/lib/voucherify/service/customers.rb +29 -0
- data/lib/voucherify/service/distributions.rb +26 -0
- data/lib/voucherify/service/redemptions.rb +36 -0
- data/lib/voucherify/service/validations.rb +17 -0
- data/lib/voucherify/service/vouchers.rb +52 -0
- data/lib/voucherify/utils.rb +85 -83
- data/lib/voucherify/version.rb +2 -2
- data/lib/voucherify.rb +11 -181
- data/voucherify-ruby-sdk.png +0 -0
- data/voucherify.gemspec +11 -11
- metadata +13 -6
- data/examples/customer.rb +0 -29
- data/examples/validate.rb +0 -15
data/Rakefile
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
3
|
|
4
|
-
RSpec::Core::RakeTask.new(:spec)
|
4
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
5
|
+
t.rspec_opts = '--format documentation'
|
6
|
+
end
|
5
7
|
|
6
|
-
task :default => :spec
|
8
|
+
task :default => :spec
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'voucherify'
|
2
|
+
|
3
|
+
voucherify = Voucherify::Client.new({
|
4
|
+
:applicationId => 'c70a6f00-cf91-4756-9df5-47628850002b',
|
5
|
+
:clientSecretKey => '3266b9f8-e246-4f79-bdf0-833929b1380c'
|
6
|
+
})
|
7
|
+
|
8
|
+
new_customer = voucherify.customers.create({
|
9
|
+
name: 'John Doe',
|
10
|
+
email: 'john@email.com',
|
11
|
+
description: 'Sample description about customer',
|
12
|
+
metadata: {
|
13
|
+
lang: 'en'
|
14
|
+
}
|
15
|
+
})
|
16
|
+
|
17
|
+
puts 'Created Customer:'
|
18
|
+
puts new_customer
|
19
|
+
|
20
|
+
customer = voucherify.customers.get new_customer['id']
|
21
|
+
|
22
|
+
puts 'Retrieved Customer:'
|
23
|
+
puts customer
|
24
|
+
|
25
|
+
customer[:description] = 'Sample description of customer with updates'
|
26
|
+
|
27
|
+
updated_customer = voucherify.customers.update customer
|
28
|
+
|
29
|
+
puts 'Updated Customer:'
|
30
|
+
puts updated_customer
|
31
|
+
|
32
|
+
voucherify.customers.delete updated_customer['id']
|
data/examples/utils.rb
CHANGED
@@ -1,24 +1,22 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
include Utils
|
1
|
+
require 'voucherify/utils'
|
4
2
|
|
5
3
|
base_price = 20.00
|
6
4
|
|
7
|
-
puts
|
5
|
+
puts 'base price: ' + base_price.to_s
|
8
6
|
|
9
|
-
puts
|
7
|
+
puts '===== amount discount ====='
|
10
8
|
amount_voucher = {
|
11
9
|
discount: {
|
12
10
|
amount_off: 1000, # 10.0
|
13
11
|
type: 'AMOUNT'
|
14
12
|
}
|
15
13
|
}
|
16
|
-
puts
|
17
|
-
puts
|
14
|
+
puts 'discount: ' + Voucherify::Utils.calculate_discount(base_price, amount_voucher).to_s
|
15
|
+
puts 'price after discount: ' + Voucherify::Utils.calculate_price(base_price, amount_voucher).to_s
|
18
16
|
puts
|
19
17
|
|
20
18
|
|
21
|
-
puts
|
19
|
+
puts '===== percent discount ===='
|
22
20
|
|
23
21
|
percent_discount_voucher = {
|
24
22
|
discount: {
|
@@ -27,12 +25,12 @@ percent_discount_voucher = {
|
|
27
25
|
}
|
28
26
|
}
|
29
27
|
|
30
|
-
puts
|
31
|
-
puts
|
28
|
+
puts 'discount: ' + Voucherify::Utils.calculate_discount(base_price, percent_discount_voucher).to_s
|
29
|
+
puts 'price after discount: ' + Voucherify::Utils.calculate_price(base_price, percent_discount_voucher).to_s
|
32
30
|
puts
|
33
31
|
|
34
32
|
|
35
|
-
puts
|
33
|
+
puts '===== unit discount ======='
|
36
34
|
|
37
35
|
unit_discount_voucher = {
|
38
36
|
discount: {
|
@@ -41,12 +39,12 @@ unit_discount_voucher = {
|
|
41
39
|
}
|
42
40
|
}
|
43
41
|
|
44
|
-
puts
|
45
|
-
puts
|
42
|
+
puts 'discount: ' + Voucherify::Utils.calculate_discount(base_price, unit_discount_voucher, 5).to_s
|
43
|
+
puts 'price after discount: ' + Voucherify::Utils.calculate_price(base_price, unit_discount_voucher, 5).to_s
|
46
44
|
puts
|
47
45
|
|
48
46
|
|
49
|
-
puts
|
47
|
+
puts '===== gift voucher ========'
|
50
48
|
|
51
49
|
gift_voucher = {
|
52
50
|
gift: {
|
@@ -55,11 +53,11 @@ gift_voucher = {
|
|
55
53
|
}
|
56
54
|
}
|
57
55
|
|
58
|
-
puts
|
59
|
-
puts
|
56
|
+
puts 'discount: ' + Voucherify::Utils.calculate_discount(base_price, gift_voucher).to_s
|
57
|
+
puts 'price after discount: ' + Voucherify::Utils.calculate_price(base_price, gift_voucher).to_s
|
60
58
|
puts
|
61
59
|
|
62
|
-
puts
|
60
|
+
puts '===== gift voucher 2 ======'
|
63
61
|
|
64
62
|
gift_voucher2 = {
|
65
63
|
gift: {
|
@@ -68,6 +66,6 @@ gift_voucher2 = {
|
|
68
66
|
}
|
69
67
|
}
|
70
68
|
|
71
|
-
puts
|
72
|
-
puts
|
69
|
+
puts 'discount: ' + Voucherify::Utils.calculate_discount(base_price, gift_voucher2).to_s
|
70
|
+
puts 'price after discount: ' + Voucherify::Utils.calculate_price(base_price, gift_voucher2).to_s
|
73
71
|
puts
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'voucherify'
|
2
|
+
|
3
|
+
voucherify = Voucherify::Client.new({
|
4
|
+
:applicationId => 'c70a6f00-cf91-4756-9df5-47628850002b',
|
5
|
+
:clientSecretKey => '3266b9f8-e246-4f79-bdf0-833929b1380c'
|
6
|
+
})
|
7
|
+
|
8
|
+
puts voucherify.vouchers.validate('91Ft4U', {
|
9
|
+
tracking_id: 'john@lemon.com',
|
10
|
+
order: {
|
11
|
+
amount: 1000
|
12
|
+
}
|
13
|
+
})
|
14
|
+
|
15
|
+
query = {limit: 10, skip: 20, category: 'API Test'}
|
16
|
+
puts voucherify.vouchers.list(query)
|
17
|
+
|
18
|
+
puts voucherify.vouchers.enable '91Ft4U'
|
19
|
+
|
20
|
+
puts voucherify.vouchers.disable '91Ft4U'
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'rest-client'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Voucherify
|
6
|
+
|
7
|
+
class Client
|
8
|
+
def initialize (options)
|
9
|
+
@options = options
|
10
|
+
@backend_url = 'https://api.voucherify.io/v1'
|
11
|
+
@headers = {
|
12
|
+
'X-App-Id' => @options[:applicationId],
|
13
|
+
'X-App-Token' => @options[:clientSecretKey],
|
14
|
+
'X-Voucherify-Channel' => 'Ruby-SDK',
|
15
|
+
:accept => :json
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
def vouchers
|
20
|
+
Voucherify::Service::Vouchers.new(self)
|
21
|
+
end
|
22
|
+
|
23
|
+
def distributions
|
24
|
+
Voucherify::Service::Distributions.new(self)
|
25
|
+
end
|
26
|
+
|
27
|
+
def validations
|
28
|
+
Voucherify::Service::Validations.new(self)
|
29
|
+
end
|
30
|
+
|
31
|
+
def redemptions
|
32
|
+
Voucherify::Service::Redemptions.new(self)
|
33
|
+
end
|
34
|
+
|
35
|
+
def customers
|
36
|
+
Voucherify::Service::Customers.new(self)
|
37
|
+
end
|
38
|
+
|
39
|
+
def get(path, params = {})
|
40
|
+
url = @backend_url + path
|
41
|
+
response = RestClient.get(url, @headers.merge({:params => params}))
|
42
|
+
JSON.parse(response.body)
|
43
|
+
end
|
44
|
+
|
45
|
+
def put(path, body, params = {})
|
46
|
+
url = @backend_url + path
|
47
|
+
response = RestClient.put(url, body, @headers.merge({:params => params, :content_type => :json}))
|
48
|
+
JSON.parse(response.body)
|
49
|
+
end
|
50
|
+
|
51
|
+
def post(path, body, params = {})
|
52
|
+
url = @backend_url + path
|
53
|
+
response = RestClient.post(url, body, @headers.merge({:params => params, :content_type => :json}))
|
54
|
+
if !response.body.empty?
|
55
|
+
JSON.parse(response.body)
|
56
|
+
else
|
57
|
+
nil
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
def delete(path, params = {})
|
63
|
+
url = @backend_url + path
|
64
|
+
RestClient.delete(url, @headers.merge({:params => params}))
|
65
|
+
nil
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module Voucherify
|
4
|
+
module Service
|
5
|
+
class Customers
|
6
|
+
attr_reader :client
|
7
|
+
|
8
|
+
def initialize(client)
|
9
|
+
@client = client
|
10
|
+
end
|
11
|
+
|
12
|
+
def create(customer)
|
13
|
+
@client.post('/customers/', customer.to_json)
|
14
|
+
end
|
15
|
+
|
16
|
+
def get(customer_id)
|
17
|
+
@client.get('/customers/' + customer_id)
|
18
|
+
end
|
19
|
+
|
20
|
+
def update(customer)
|
21
|
+
@client.put('/customers/' + (customer['id'] || customer[:id]), customer.to_json)
|
22
|
+
end
|
23
|
+
|
24
|
+
def delete(customer_id)
|
25
|
+
@client.delete('/customers/' + customer_id)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module Voucherify
|
4
|
+
module Service
|
5
|
+
class Distributions
|
6
|
+
attr_reader :client
|
7
|
+
|
8
|
+
def initialize(client)
|
9
|
+
@client = client
|
10
|
+
end
|
11
|
+
|
12
|
+
def publish(campaign_name)
|
13
|
+
url = '/vouchers/publish'
|
14
|
+
payload = {}
|
15
|
+
|
16
|
+
if campaign_name.is_a? String
|
17
|
+
url += '?campaign=' + URI.encode(campaign_name)
|
18
|
+
elsif campaign_name.is_a? Hash
|
19
|
+
payload = campaign_name
|
20
|
+
end
|
21
|
+
|
22
|
+
@client.post(url, payload.to_json)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module Voucherify
|
4
|
+
module Service
|
5
|
+
class Redemptions
|
6
|
+
attr_reader :client
|
7
|
+
|
8
|
+
def initialize(client)
|
9
|
+
@client = client
|
10
|
+
end
|
11
|
+
|
12
|
+
def redeem(code, params = {})
|
13
|
+
url = '/vouchers/' + URI.encode(code) + '/redemption'
|
14
|
+
@client.post(url, params.to_json)
|
15
|
+
end
|
16
|
+
|
17
|
+
def list(query = {})
|
18
|
+
@client.get('/redemptions/', query)
|
19
|
+
end
|
20
|
+
|
21
|
+
def get_for_voucher(code)
|
22
|
+
@client.get('/vouchers/' + URI.encode(code) + '/redemption')
|
23
|
+
end
|
24
|
+
|
25
|
+
def rollback(redemption_id, params = {})
|
26
|
+
reason = params['reason'] || params[:reason]
|
27
|
+
url = '/redemptions/'+ URI.encode(redemption_id) + '/rollback'
|
28
|
+
url += '?reason=' + URI.encode(reason) if reason
|
29
|
+
params.delete 'reason'
|
30
|
+
params.delete :reason
|
31
|
+
@client.post(url, params.to_json)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module Voucherify
|
4
|
+
module Service
|
5
|
+
class Validations
|
6
|
+
attr_reader :client
|
7
|
+
|
8
|
+
def initialize(client)
|
9
|
+
@client = client
|
10
|
+
end
|
11
|
+
|
12
|
+
def validate_voucher(code, context = {})
|
13
|
+
@client.post('/vouchers/' + URI.encode(code) + '/validate', context.to_json)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module Voucherify
|
4
|
+
module Service
|
5
|
+
class Vouchers
|
6
|
+
attr_reader :client
|
7
|
+
|
8
|
+
def initialize(client)
|
9
|
+
@client = client
|
10
|
+
end
|
11
|
+
|
12
|
+
def create(code, options = {})
|
13
|
+
url = '/vouchers/'
|
14
|
+
url += URI.encode(code) if code
|
15
|
+
@client.post(url, options.to_json)
|
16
|
+
end
|
17
|
+
|
18
|
+
def get(code)
|
19
|
+
@client.get('/vouchers/' + URI.encode(code))
|
20
|
+
end
|
21
|
+
|
22
|
+
def update(voucher_update)
|
23
|
+
url = '/vouchers/' + URI.encode(voucher_update['code'])
|
24
|
+
@client.put(url, voucher_update.to_json)
|
25
|
+
end
|
26
|
+
|
27
|
+
def list(query)
|
28
|
+
@client.get('/vouchers/', query)
|
29
|
+
end
|
30
|
+
|
31
|
+
def enable(code)
|
32
|
+
url = '/vouchers/' + URI.encode(code) + '/enable'
|
33
|
+
@client.post(url, nil)
|
34
|
+
nil
|
35
|
+
end
|
36
|
+
|
37
|
+
def disable(code)
|
38
|
+
url = '/vouchers/' + URI.encode(code) + '/disable'
|
39
|
+
@client.post(url, nil)
|
40
|
+
nil
|
41
|
+
end
|
42
|
+
|
43
|
+
def delete(code, params = {})
|
44
|
+
force = params['force'] || params[:force]
|
45
|
+
url = '/vouchers/' + URI.encode(code)
|
46
|
+
url += '?force=' + (!!force).to_s
|
47
|
+
@client.delete(url, nil)
|
48
|
+
nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/voucherify/utils.rb
CHANGED
@@ -1,99 +1,101 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
1
|
+
module Voucherify
|
2
|
+
module Utils
|
3
|
+
def self.round_money(value)
|
4
|
+
value.round(2)
|
4
5
|
end
|
5
6
|
|
6
|
-
def validate_percent_discount(discount)
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
def self.validate_percent_discount(discount)
|
8
|
+
if !(discount.is_a? Numeric) || !discount.between?(0, 100)
|
9
|
+
raise 'Invalid voucher, percent discount should be between 0-100.'
|
10
|
+
end
|
10
11
|
end
|
11
12
|
|
12
|
-
def validate_amount_discount(discount)
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
def self.validate_amount_discount(discount)
|
14
|
+
if !(discount.is_a? Numeric) || discount < 0
|
15
|
+
raise 'Invalid voucher, amount discount must be bigger than zero.'
|
16
|
+
end
|
16
17
|
end
|
17
18
|
|
18
|
-
def validate_unit_discount(discount)
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
def self.validate_unit_discount(discount)
|
20
|
+
if !(discount.is_a? Numeric) || discount < 0
|
21
|
+
raise 'Invalid voucher, unit discount must be bigger than zero.'
|
22
|
+
end
|
22
23
|
end
|
23
24
|
|
24
|
-
def calculate_price(base_price, voucher, unit_price = nil)
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
25
|
+
def self.calculate_price(base_price, voucher, unit_price = nil)
|
26
|
+
e = 100.0
|
27
|
+
|
28
|
+
if voucher[:gift]
|
29
|
+
discount = [voucher[:gift][:balance] / e, base_price].min
|
30
|
+
return round_money(base_price - discount)
|
31
|
+
end
|
32
|
+
|
33
|
+
if !voucher[:discount]
|
34
|
+
raise 'Unsupported voucher type.'
|
35
|
+
end
|
36
|
+
|
37
|
+
if voucher[:discount][:type] === 'PERCENT'
|
38
|
+
discount = voucher[:discount][:percent_off]
|
39
|
+
validate_percent_discount(discount);
|
40
|
+
price_discount = base_price * (discount / 100.0)
|
41
|
+
return round_money(base_price - price_discount)
|
42
|
+
|
43
|
+
elsif voucher[:discount][:type] === 'AMOUNT'
|
44
|
+
discount = voucher[:discount][:amount_off] / e
|
45
|
+
validate_amount_discount(discount)
|
46
|
+
new_price = base_price - discount
|
47
|
+
return round_money(new_price > 0 ? (new_price) : 0)
|
35
48
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
price_discount = base_price * (discount / 100.0)
|
40
|
-
return round_money(base_price - price_discount)
|
41
|
-
|
42
|
-
elsif voucher[:discount][:type] === 'AMOUNT'
|
43
|
-
discount = voucher[:discount][:amount_off] / e
|
44
|
-
validate_amount_discount(discount)
|
45
|
-
new_price = base_price - discount
|
46
|
-
return round_money(new_price > 0 ? (new_price) : 0)
|
47
|
-
|
48
|
-
elsif voucher[:discount][:type] === 'UNIT'
|
49
|
-
if !unit_price
|
50
|
-
raise "Missing unit_price argument."
|
51
|
-
end
|
52
|
-
discount = voucher[:discount][:unit_off]
|
53
|
-
validate_unit_discount(discount)
|
54
|
-
new_price = base_price - unit_price * discount
|
55
|
-
return round_money(new_price > 0 ? (new_price) : 0)
|
56
|
-
|
57
|
-
else
|
58
|
-
raise "Unsupported discount type"
|
49
|
+
elsif voucher[:discount][:type] === 'UNIT'
|
50
|
+
if !unit_price
|
51
|
+
raise 'Missing unit_price argument.'
|
59
52
|
end
|
53
|
+
discount = voucher[:discount][:unit_off]
|
54
|
+
validate_unit_discount(discount)
|
55
|
+
new_price = base_price - unit_price * discount
|
56
|
+
return round_money(new_price > 0 ? (new_price) : 0)
|
57
|
+
|
58
|
+
else
|
59
|
+
raise 'Unsupported discount type'
|
60
|
+
end
|
60
61
|
end
|
61
62
|
|
62
|
-
def calculate_discount(base_price, voucher, unit_price = nil)
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
if !voucher[:discount]
|
71
|
-
raise "Unsupported voucher type."
|
72
|
-
end
|
63
|
+
def self.calculate_discount(base_price, voucher, unit_price = nil)
|
64
|
+
e = 100.0
|
65
|
+
|
66
|
+
if voucher[:gift]
|
67
|
+
discount = [voucher[:gift][:balance] / e, base_price].min
|
68
|
+
return round_money(discount)
|
69
|
+
end
|
73
70
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
return round_money(price_discount > base_price ? (base_price) : (price_discount))
|
94
|
-
|
95
|
-
else
|
96
|
-
raise "Unsupported discount type"
|
71
|
+
if !voucher[:discount]
|
72
|
+
raise 'Unsupported voucher type.'
|
73
|
+
end
|
74
|
+
|
75
|
+
if voucher[:discount][:type] === 'PERCENT'
|
76
|
+
discount = voucher[:discount][:percent_off]
|
77
|
+
validate_percent_discount(discount);
|
78
|
+
price_discount = base_price * (discount / 100.0)
|
79
|
+
return round_money(price_discount)
|
80
|
+
|
81
|
+
elsif voucher[:discount][:type] === 'AMOUNT'
|
82
|
+
discount = voucher[:discount][:amount_off] / e
|
83
|
+
validate_amount_discount(discount)
|
84
|
+
new_price = base_price - discount
|
85
|
+
return round_money(new_price > 0 ? (discount) : (base_price))
|
86
|
+
|
87
|
+
elsif voucher[:discount][:type] === 'UNIT'
|
88
|
+
if !unit_price
|
89
|
+
raise 'Missing unit_price argument.'
|
97
90
|
end
|
91
|
+
discount = voucher[:discount][:unit_off]
|
92
|
+
validate_unit_discount(discount)
|
93
|
+
price_discount = unit_price * discount
|
94
|
+
return round_money(price_discount > base_price ? (base_price) : (price_discount))
|
95
|
+
|
96
|
+
else
|
97
|
+
raise 'Unsupported discount type'
|
98
|
+
end
|
98
99
|
end
|
100
|
+
end
|
99
101
|
end
|
data/lib/voucherify/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION =
|
1
|
+
module Voucherify
|
2
|
+
VERSION = '1.0.0'
|
3
3
|
end
|