smartpay 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c0cd94594c52188072ee764c3d9abbab864773af324310b3f0a6e5747570e04e
4
- data.tar.gz: 7fe589e25744b8891af1b91786d26c4f7e67e13fcf4ba80c9d847ea31461181f
3
+ metadata.gz: 446eb4c4c827e38b97a57ecbceee0b43a932d53cdb11a6bd22215250c5897583
4
+ data.tar.gz: a6e513f1be63893df11793c7b74c66cea109e715423c2f0fbbb25c8869ed5089
5
5
  SHA512:
6
- metadata.gz: 8973913b6c40ddd9fa7449d43696c823b25ac36483f070c3fe1a6cafffe8f030b70171ee2ff7c8ae9876b336a820efaf17b1de85f0bc2b0245b2d6add321f47e
7
- data.tar.gz: 6b7e1721a942ace0254e701c23186f0b2c0eaa6d2821e9fd5bf4339b782e58b8eaa9d2b6aa39e40b6c1babbb462c199f964f4bf9a9f2ec4664f8d39b9bef25d8
6
+ metadata.gz: a274f5a5917cb8bef8bdabe0360f284436be5bc8f8052f7db9b6233eb6f7a18cfd798f4d1a63bd3d0a57e8e8cae1defc8f477e7e7bfe388531f13384e59245ce
7
+ data.tar.gz: 58c21a2d55b9fac38a560ac032d6af7f27f8bc2a61a6477fd1dcd2e884b2791d20f734c79dcd7be8c2b5b49f32eafec8f26604438cc55bf84313a46e5d864a3d
@@ -6,6 +6,7 @@ module Smartpay
6
6
  attr_accessor :payload
7
7
 
8
8
  REQUIREMENT_KEY_NAME = [:successUrl, :cancelUrl, :customer, :currency, :items].freeze
9
+ LINE_ITEM_KINDS = %w[product tax discount].freeze
9
10
  CAN_FALLBACK_KEYS = [:customer].freeze
10
11
 
11
12
  def initialize(raw_payload)
@@ -32,11 +33,8 @@ module Smartpay
32
33
  shipping_info[:feeCurrency] = payload.dig(:currency)
33
34
  end
34
35
 
35
- total_amount = get_total_amount
36
-
37
- {
36
+ normalized = {
38
37
  customerInfo: normalize_customer_info(payload.dig(:customerInfo) || payload.dig(:customer) || {}),
39
- amount: total_amount,
40
38
  captureMethod: payload.dig(:captureMethod),
41
39
  currency: payload.dig(:currency),
42
40
  description: payload.dig(:description),
@@ -47,6 +45,10 @@ module Smartpay
47
45
  successUrl: payload.dig(:successUrl),
48
46
  cancelUrl: payload.dig(:cancelUrl),
49
47
  }
48
+
49
+ normalized[:amount] = get_total_amount(normalized)
50
+
51
+ return normalized
50
52
  end
51
53
 
52
54
  def normalize_customer_info(info)
@@ -111,29 +113,32 @@ module Smartpay
111
113
  productDescription: line_item.dig(:productDescription),
112
114
  metadata: line_item.dig(:metadata),
113
115
  productMetadata: line_item.dig(:productMetadata),
114
- priceMetadata: line_item.dig(:priceMetadata)
116
+ priceMetadata: line_item.dig(:priceMetadata),
117
+ kind: LINE_ITEM_KINDS.include?(line_item.dig(:kind)) ? line_item.dig(:kind) : nil
115
118
  }
116
119
  end
117
120
  end
118
121
 
122
+ def get_total_amount(raw_payload = nil)
123
+ total_amount = raw_payload.dig(:amount) || raw_payload.dig("amount")
124
+ return total_amount if total_amount
119
125
 
120
- def get_total_amount
121
- total_amount = payload.dig(:amount) || payload.dig('amount')
126
+ items = raw_payload.dig(:items)
122
127
 
123
- if total_amount.nil?
124
- items = payload.dig(:items)
125
-
126
- if !items.nil? && items.count > 0
127
- total_amount = items.inject(0) { |sum, item| sum + (item[:amount] || item['amount'] || 0) }
128
- end
129
-
130
- shipping_fee = payload.dig(:shippingInfo, :feeAmount) ||
131
- payload.dig(:shippingInfo, 'feeAmount') ||
132
- 0
133
- total_amount = shipping_fee + (total_amount || 0)
128
+ if !items.nil? && items.count.positive?
129
+ total_amount = items.inject(0) { |sum, item|
130
+ amount = item[:amount] || item["amount"] || 0
131
+ amount = -amount if item[:kind] == "discount"
132
+ sum + amount
133
+ }
134
134
  end
135
135
 
136
- total_amount
136
+ shipping_fee = raw_payload.dig(:shippingInfo, :feeAmount) ||
137
+ raw_payload.dig(:shippingInfo, "feeAmount") ||
138
+ 0
139
+ total_amount += shipping_fee
140
+
141
+ return total_amount
137
142
  end
138
143
  end
139
144
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Smartpay
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smartpay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Smartpay
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-18 00:00:00.000000000 Z
11
+ date: 2022-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.0'
41
- description:
41
+ description:
42
42
  email:
43
43
  - uxe@smartpay.co
44
44
  executables: []
@@ -78,7 +78,7 @@ metadata:
78
78
  homepage_uri: https://smartpay.co
79
79
  source_code_uri: https://github.com/smartpay-co/sdk-ruby
80
80
  changelog_uri: https://github.com/smartpay-co/sdk-ruby/blob/main/CHANGELOG.md
81
- post_install_message:
81
+ post_install_message:
82
82
  rdoc_options: []
83
83
  require_paths:
84
84
  - lib
@@ -93,8 +93,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  requirements: []
96
- rubygems_version: 3.3.7
97
- signing_key:
96
+ rubygems_version: 3.0.3.1
97
+ signing_key:
98
98
  specification_version: 4
99
99
  summary: The Smartpay Ruby SDK offers easy access to Smartpay API from applications
100
100
  written in Ruby.