afterpay-ruby 0.1.3 → 0.1.4
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.lock +1 -1
- data/README.md +1 -0
- data/lib/afterpay.rb +2 -1
- data/lib/afterpay/address.rb +20 -4
- data/lib/afterpay/discount.rb +10 -4
- data/lib/afterpay/item.rb +1 -1
- data/lib/afterpay/order.rb +22 -40
- data/lib/afterpay/payment.rb +1 -1
- data/lib/afterpay/utils/money.rb +26 -0
- data/lib/afterpay/version.rb +1 -1
- metadata +2 -2
- data/lib/afterpay/money_util.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee47c1b29a922674e59599ea8f81338c88eeea755720f7d96f576d8552639069
|
4
|
+
data.tar.gz: 2205ddca07946fa9cfacf565ef7e91dc968b173ff7e091910f1ddd09f5fd2641
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe4162c069ad19bfca5b1a914c8321ad1fe67e2a7a9c67f0b897d38760c96fef8b1785748a937bc6158a82a7f720503061c13304645eb4f1a7527c0ae6a266ea
|
7
|
+
data.tar.gz: a82bcd5ef816ca4bc48a0dae7e864a219d10b7ad397b2e097294b85e44f3351530172ee6d1727905cca1ae8b145be6c630c8ae87a9687ddd9e4a175548779bed
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -55,6 +55,7 @@ order = Afterpay::Order.create(
|
|
55
55
|
success_url: <String>,
|
56
56
|
cancel_url: <String>,
|
57
57
|
reference: <String>,
|
58
|
+
tax: <Money | optional>,
|
58
59
|
shipping: <Money | optional>,
|
59
60
|
discounts: [<Afterpay::Discount | optional>],
|
60
61
|
billing_address: <Afterpay::Address | optional>,
|
data/lib/afterpay.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "money"
|
4
|
+
require_relative "afterpay/utils/money"
|
5
|
+
|
4
6
|
require_relative "afterpay/version"
|
5
7
|
require_relative "afterpay/client"
|
6
8
|
require_relative "afterpay/config"
|
@@ -11,7 +13,6 @@ require_relative "afterpay/payment"
|
|
11
13
|
require_relative "afterpay/error"
|
12
14
|
require_relative "afterpay/discount"
|
13
15
|
require_relative "afterpay/address"
|
14
|
-
require_relative "afterpay/money_util"
|
15
16
|
|
16
17
|
module Afterpay
|
17
18
|
class << self
|
data/lib/afterpay/address.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module Afterpay
|
4
4
|
class Address
|
5
|
-
attr_accessor :name, :
|
5
|
+
attr_accessor :name, :line_1, :line_2, :suburb, :state, :postcode, :country, :phone
|
6
6
|
|
7
7
|
# Initializes an Order object
|
8
8
|
#
|
@@ -17,8 +17,8 @@ module Afterpay
|
|
17
17
|
# @param phone [String|Number] The phone number
|
18
18
|
def initialize(attributes = {})
|
19
19
|
@name = attributes[:name]
|
20
|
-
@
|
21
|
-
@
|
20
|
+
@line_1 = attributes[:line_1] || ""
|
21
|
+
@line_2 = attributes[:line_2] || ""
|
22
22
|
@suburb = attributes[:suburb] || ""
|
23
23
|
@state = attributes[:state] || ""
|
24
24
|
@postcode = attributes[:postcode]
|
@@ -29,7 +29,8 @@ module Afterpay
|
|
29
29
|
def to_hash
|
30
30
|
{
|
31
31
|
name: name,
|
32
|
-
line1:
|
32
|
+
line1: line_1,
|
33
|
+
line2: line_2,
|
33
34
|
suburb: suburb,
|
34
35
|
state: state,
|
35
36
|
postcode: postcode.to_s,
|
@@ -37,5 +38,20 @@ module Afterpay
|
|
37
38
|
phoneNumber: phone.to_s
|
38
39
|
}
|
39
40
|
end
|
41
|
+
|
42
|
+
def self.from_response(response)
|
43
|
+
return nil if response.nil?
|
44
|
+
|
45
|
+
new(
|
46
|
+
name: response[:name],
|
47
|
+
line_1: response[:line1],
|
48
|
+
line_2: response[:line2],
|
49
|
+
suburb: response[:suburb],
|
50
|
+
state: response[:state],
|
51
|
+
postcode: response[:postcode],
|
52
|
+
country: response[:countryCode],
|
53
|
+
phone: response[:phoneNumber]
|
54
|
+
)
|
55
|
+
end
|
40
56
|
end
|
41
57
|
end
|
data/lib/afterpay/discount.rb
CHANGED
@@ -12,11 +12,17 @@ module Afterpay
|
|
12
12
|
def to_hash
|
13
13
|
{
|
14
14
|
displayName: name,
|
15
|
-
amount:
|
16
|
-
amount: amount.to_f,
|
17
|
-
currency: amount.currency.iso_code
|
18
|
-
}
|
15
|
+
amount: Utils::Money.api_hash(amount)
|
19
16
|
}
|
20
17
|
end
|
18
|
+
|
19
|
+
def self.from_response(response)
|
20
|
+
return nil if response.nil?
|
21
|
+
|
22
|
+
new(
|
23
|
+
name: response[:display_name],
|
24
|
+
amount: Utils::Money.from_response(response[:amount])
|
25
|
+
)
|
26
|
+
end
|
21
27
|
end
|
22
28
|
end
|
data/lib/afterpay/item.rb
CHANGED
data/lib/afterpay/order.rb
CHANGED
@@ -1,24 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "ostruct"
|
4
|
-
require "forwardable"
|
5
|
-
|
6
3
|
module Afterpay
|
7
4
|
# The Order object for creating an order to `/v1/orders`
|
8
5
|
class Order
|
9
|
-
|
6
|
+
attr_accessor :total, :consumer, :items, :shipping, :tax, :discounts,
|
7
|
+
:billing, :shipping_address, :billing_address, :reference,
|
8
|
+
:payment_type, :success_url, :cancel_url
|
10
9
|
|
11
|
-
|
12
|
-
:total,
|
13
|
-
:consumer,
|
14
|
-
:items,
|
15
|
-
:shipping,
|
16
|
-
:tax,
|
17
|
-
:discounts,
|
18
|
-
:billing,
|
19
|
-
:shipping_address,
|
20
|
-
:billing_address,
|
21
|
-
:merchant_reference
|
10
|
+
attr_reader :expiry, :token, :error
|
22
11
|
|
23
12
|
# Finds Order from Afterpay API
|
24
13
|
# @param token [String]
|
@@ -36,14 +25,14 @@ module Afterpay
|
|
36
25
|
return nil if response.nil?
|
37
26
|
|
38
27
|
new(
|
39
|
-
total:
|
28
|
+
total: Utils::Money.from_response(response[:total]),
|
40
29
|
consumer: Consumer.from_response(response[:consumer]),
|
41
30
|
items: response[:items].map { |item| Item.from_response(item) },
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
tax:
|
46
|
-
shipping:
|
31
|
+
billing_address: Address.from_response(response[:billing]),
|
32
|
+
shipping_address: Address.from_response(response[:shipping]),
|
33
|
+
discounts: response[:discounts].map { |discount| Discount.from_response(discount) },
|
34
|
+
tax: Utils::Money.from_response(response[:taxAmount]),
|
35
|
+
shipping: Utils::Money.from_response(response[:shippingAmount])
|
47
36
|
)
|
48
37
|
end
|
49
38
|
|
@@ -55,8 +44,6 @@ module Afterpay
|
|
55
44
|
new(*args).create
|
56
45
|
end
|
57
46
|
|
58
|
-
attr_reader :attributes, :expiry, :token, :error
|
59
|
-
|
60
47
|
# Initializes an Order object
|
61
48
|
#
|
62
49
|
# @overload initialize(total:, items:, consumer:, success_url:, cancel_url:, payment_type:)
|
@@ -71,35 +58,30 @@ module Afterpay
|
|
71
58
|
# @param billing_address [<Afterpay::Address>] optional the billing Address
|
72
59
|
# @param shipping_address [<Afterpay::Address>] optional the shipping Address
|
73
60
|
def initialize(attributes = {})
|
74
|
-
|
75
|
-
@
|
76
|
-
@token
|
77
|
-
@expiry
|
61
|
+
attributes.each { |key, value| instance_variable_set(:"@#{key}", value) }
|
62
|
+
@apayment_type ||= Afterpay.config.type
|
63
|
+
@token ||= nil
|
64
|
+
@expiry ||= nil
|
78
65
|
@error = nil
|
79
66
|
end
|
80
67
|
|
81
68
|
# Builds structure to API specs
|
82
69
|
def to_hash
|
83
70
|
data = {
|
84
|
-
totalAmount:
|
85
|
-
amount: total.to_f,
|
86
|
-
currency: total.currency.iso_code
|
87
|
-
},
|
71
|
+
totalAmount: Utils::Money.api_hash(total),
|
88
72
|
consumer: consumer.to_hash,
|
89
73
|
items: items.map(&:to_hash),
|
90
74
|
merchant: {
|
91
|
-
redirectConfirmUrl:
|
92
|
-
redirectCancelUrl:
|
93
|
-
},
|
94
|
-
merchantReference: attributes.reference,
|
95
|
-
taxAmount: attributes.tax,
|
96
|
-
shippingAmount: {
|
97
|
-
amount: shipping.to_f,
|
98
|
-
currency: shipping.currency.iso_code
|
75
|
+
redirectConfirmUrl: success_url,
|
76
|
+
redirectCancelUrl: cancel_url
|
99
77
|
},
|
100
|
-
|
78
|
+
merchantReference: reference,
|
79
|
+
taxAmount: tax,
|
80
|
+
paymentType: payment_type
|
101
81
|
}
|
102
82
|
|
83
|
+
data[:taxAmount] = Utils::Money.api_hash(tax) if tax
|
84
|
+
data[:shippingAmount] = Utils::Money.api_hash(shipping) if shipping
|
103
85
|
data[:discounts] = discounts.map(&:to_hash) if discounts
|
104
86
|
data[:billing] = billing_address.to_hash if billing_address
|
105
87
|
data[:shipping] = shipping_address.to_hash if shipping_address
|
data/lib/afterpay/payment.rb
CHANGED
@@ -26,7 +26,7 @@ module Afterpay
|
|
26
26
|
@id = attributes[:id]
|
27
27
|
@status = attributes[:status]
|
28
28
|
@created = attributes[:created]
|
29
|
-
@total =
|
29
|
+
@total = Utils::Money.from_response(attributes[:total])
|
30
30
|
@order = Order.from_response(attributes[:orderDetails])
|
31
31
|
@error = Error.new(attributes) if attributes[:errorId]
|
32
32
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Afterpay
|
4
|
+
module Utils
|
5
|
+
class Money
|
6
|
+
# Converts Afterpay response to `Money`
|
7
|
+
# @return [Money]
|
8
|
+
def self.from_response(response)
|
9
|
+
return nil if response.nil?
|
10
|
+
|
11
|
+
::Money.from_amount(
|
12
|
+
response[:amount].to_f,
|
13
|
+
response[:currency]
|
14
|
+
)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Converts <oney to API compatible hash format
|
18
|
+
def self.api_hash(money)
|
19
|
+
{
|
20
|
+
amount: money.dollars.to_f,
|
21
|
+
currency: money.currency.iso_code
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/afterpay/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: afterpay-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eli
|
@@ -172,9 +172,9 @@ files:
|
|
172
172
|
- lib/afterpay/discount.rb
|
173
173
|
- lib/afterpay/error.rb
|
174
174
|
- lib/afterpay/item.rb
|
175
|
-
- lib/afterpay/money_util.rb
|
176
175
|
- lib/afterpay/order.rb
|
177
176
|
- lib/afterpay/payment.rb
|
177
|
+
- lib/afterpay/utils/money.rb
|
178
178
|
- lib/afterpay/version.rb
|
179
179
|
homepage: https://github.com/bluethumbart/afterpay-ruby
|
180
180
|
licenses:
|
data/lib/afterpay/money_util.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Afterpay
|
4
|
-
# Money utility
|
5
|
-
module MoneyUtil
|
6
|
-
# Converts Afterpay response to `Money`
|
7
|
-
# @return [Money]
|
8
|
-
def self.from_response(response)
|
9
|
-
return nil if response.nil?
|
10
|
-
|
11
|
-
Money.from_amount(
|
12
|
-
response[:amount].to_f,
|
13
|
-
response[:currency]
|
14
|
-
)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|