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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5ca1e1c47d6b509844aedbf008e7af90c6b04534fbf163cb526b70891dcfa8f0
4
- data.tar.gz: '072138f089151b6adc36c88eb37267a2c14fb3adff566777ef6af5d8fe158392'
3
+ metadata.gz: ee47c1b29a922674e59599ea8f81338c88eeea755720f7d96f576d8552639069
4
+ data.tar.gz: 2205ddca07946fa9cfacf565ef7e91dc968b173ff7e091910f1ddd09f5fd2641
5
5
  SHA512:
6
- metadata.gz: a6243f64a347561daabde5135449521dffee10fb1c3b1faff2f0a34156ffc53fa358bb32851dcd59b608c02f1a8bfd1843c332e9af2a5a5fd0fd123b69841c64
7
- data.tar.gz: 2d561c95396fa24a42afa375748e8e61b8d3a03454d40ec82827a38f00762ad832ea4890812a91cee41f8e26a843e95c6465426aa4bf2314f781f6c0a4fabf38
6
+ metadata.gz: fe4162c069ad19bfca5b1a914c8321ad1fe67e2a7a9c67f0b897d38760c96fef8b1785748a937bc6158a82a7f720503061c13304645eb4f1a7527c0ae6a266ea
7
+ data.tar.gz: a82bcd5ef816ca4bc48a0dae7e864a219d10b7ad397b2e097294b85e44f3351530172ee6d1727905cca1ae8b145be6c630c8ae87a9687ddd9e4a175548779bed
data/.rubocop.yml CHANGED
@@ -29,3 +29,5 @@ Metrics/MethodLength:
29
29
  Naming/FileName:
30
30
  Exclude:
31
31
  - "lib/afterpay-ruby.rb"
32
+ Naming/VariableNumber:
33
+ Enabled: false
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- afterpay-ruby (0.1.3)
4
+ afterpay-ruby (0.1.4)
5
5
  faraday (>= 0.8, < 1.0)
6
6
  faraday_middleware (~> 0.13.1)
7
7
  money (>= 6.7.1, < 7.0.0)
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Afterpay
4
4
  class Address
5
- attr_accessor :name, :line1, :line2, :suburb, :state, :postcode, :country, :phone
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
- @line1 = attributes[:line_1] || ""
21
- @line2 = attributes[:line_2] || ""
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: 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
@@ -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
@@ -33,7 +33,7 @@ module Afterpay
33
33
  name: response[:name],
34
34
  sku: response[:sku],
35
35
  quantity: response[:quantity],
36
- price: MoneyUtil.from_response(response[:price])
36
+ price: Utils::Money.from_response(response[:price])
37
37
  )
38
38
  end
39
39
  end
@@ -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
- extend Forwardable
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
- def_delegators :@attributes,
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: MoneyUtil.from_response(response[: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
- # billing: response[:billing],
43
- # shipping: response[:shipping],
44
- # discounts: Discount.from_response(response[:discounts]),
45
- tax: MoneyUtil.from_response(response[:taxAmount]),
46
- shipping: MoneyUtil.from_response(response[:shippingAmount])
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
- @attributes = OpenStruct.new(attributes)
75
- @attributes.payment_type ||= Afterpay.config.type
76
- @token = @attributes.token || nil
77
- @expiry = nil
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: attributes.success_url,
92
- redirectCancelUrl: attributes.cancel_url
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
- paymentType: attributes.payment_type
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
@@ -26,7 +26,7 @@ module Afterpay
26
26
  @id = attributes[:id]
27
27
  @status = attributes[:status]
28
28
  @created = attributes[:created]
29
- @total = MoneyUtil.from_response(attributes[: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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Afterpay
4
- VERSION = "0.1.3".freeze
4
+ VERSION = "0.1.4".freeze
5
5
  end
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.3
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:
@@ -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