smartpay 0.2.15 → 0.3.0

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: e69060f27a2870839a5271eb8718bbb0f7898efee00f70a06231ccc4042643f4
4
- data.tar.gz: 739ddc687f25d13c683b7e651ec5f8b6a569d8dcddee74913bd21afbee055bbf
3
+ metadata.gz: 047abea76f72b96fe9f59b2f7e384ef8871d001ef60ae0e6064f767823f273ba
4
+ data.tar.gz: bd164be4d8ae9747a3331f68192e65e82061bc0627ec4dcdf79389645c9c1828
5
5
  SHA512:
6
- metadata.gz: 33edcef309bce743fc835bf3c812d73db904bac4a6407e85072357279d661292ce2b2e9001bfd672cd14c25e42d3fd0c8956ea57f947fed0133ae99150e48f20
7
- data.tar.gz: 5f0ad3bef86e42f14ce864605e25505e07dab21ef23e1be1f243474f249f327a0145c742a340345cdde1e3c699ae53e8c05bbabcb4657434f906fdae1906d955
6
+ metadata.gz: 635fbaaf5b186283fcb438f34176aa925f013576098b8535d1e7e1a9e59431db1acb16a7a545d77434bc114465ff3675768c763e084a5aabd01dd7df4f47be2f
7
+ data.tar.gz: 053a91da14d3fa5097b3ca70b5106878ab564680769c8ca8a9b70036c5915240980492dd5cecbe7b743c5f843db032311aec0e075b403893b68070e522451358
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.4
2
+ TargetRubyVersion: 2.5
3
3
 
4
4
  Style/StringLiterals:
5
5
  Enabled: true
data/lib/smartpay/api.rb CHANGED
@@ -5,7 +5,7 @@ module Smartpay
5
5
  class << self
6
6
  def create_checkout_session(payload)
7
7
  Responses::CheckoutSession.new(
8
- Client.post('/checkout-sessions', Requests::CheckoutSession.new(payload).as_hash)
8
+ Client.post("/checkout-sessions", Requests::CheckoutSession.new(payload).as_hash)
9
9
  )
10
10
  end
11
11
  end
@@ -1,13 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rest-client'
3
+ require "rest-client"
4
4
 
5
5
  module Smartpay
6
6
  class Client
7
7
  class << self
8
8
  def post(path, payload = {})
9
9
  request_payload = default_payload.merge(payload)
10
- response = RestClient::Request.execute(method: :post, url: api_url(path), headers: headers, timeout: timeout, payload: request_payload.to_json)
10
+ response = RestClient::Request.execute(method: :post, url: api_url(path), headers: headers, timeout: timeout,
11
+ payload: request_payload.to_json)
11
12
  JSON.parse(response.body, symbolize_names: true)
12
13
  end
13
14
 
@@ -6,13 +6,21 @@ module Smartpay
6
6
  attr_writer :post_timeout, :api_url, :checkout_url
7
7
 
8
8
  DEFAULT_TIMEOUT_SETTING = 30
9
- DEFAULT_API_URL = 'https://api.smartpay.co/v1'
10
- DEFAULT_CHECKOUT_URL = 'https://checkout.smartpay.co'
9
+ DEFAULT_API_URL = "https://api.smartpay.co/v1"
10
+ DEFAULT_CHECKOUT_URL = "https://checkout.smartpay.co"
11
11
 
12
12
  def initialize
13
13
  @post_timeout = DEFAULT_TIMEOUT_SETTING
14
- @api_url = ENV['SMARTPAY_API_PREFIX'] || DEFAULT_API_URL
15
- @checkout_url = DEFAULT_CHECKOUT_URL
14
+ @api_url = if in_development_mode?
15
+ ENV["SMARTPAY_API_PREFIX"].downcase || DEFAULT_API_URL
16
+ else
17
+ DEFAULT_API_URL
18
+ end
19
+ @checkout_url = if in_development_mode? && ENV["SMARTPAY_CHECKOUT_URL"].is_a?(String)
20
+ ENV["SMARTPAY_CHECKOUT_URL"].downcase || DEFAULT_CHECKOUT_URL
21
+ else
22
+ DEFAULT_CHECKOUT_URL
23
+ end
16
24
  end
17
25
 
18
26
  def post_timeout
@@ -20,11 +28,21 @@ module Smartpay
20
28
  end
21
29
 
22
30
  def api_url
23
- @api_url || ENV['SMARTPAY_API_PREFIX'] || DEFAULT_API_URL
31
+ if in_development_mode?
32
+ @api_url || ENV["SMARTPAY_API_PREFIX"].downcase || DEFAULT_API_URL
33
+ else
34
+ @api_url || DEFAULT_API_URL
35
+ end
24
36
  end
25
37
 
26
38
  def checkout_url
27
39
  @checkout_url || DEFAULT_CHECKOUT_URL
28
40
  end
41
+
42
+ private
43
+
44
+ def in_development_mode?
45
+ ENV["SMARTPAY_API_PREFIX"].downcase.include?("api.smartpay") if ENV["SMARTPAY_API_PREFIX"].is_a?(String)
46
+ end
29
47
  end
30
48
  end
@@ -5,8 +5,8 @@ module Smartpay
5
5
  class CheckoutSession
6
6
  attr_accessor :payload
7
7
 
8
- REQUIREMENT_KEY_NAME = [:successURL, :cancelURL, :customerInfo, :orderData].freeze
9
- CAN_FALLBACK_KEYS = [:customerInfo, :orderData].freeze
8
+ REQUIREMENT_KEY_NAME = [:successUrl, :cancelUrl, :customerInfo, :currency, :items].freeze
9
+ CAN_FALLBACK_KEYS = [:customerInfo].freeze
10
10
 
11
11
  def initialize(raw_payload)
12
12
  @payload = raw_payload.transform_keys(&:to_sym)
@@ -27,24 +27,21 @@ module Smartpay
27
27
  end
28
28
 
29
29
  def normalize_payload
30
- currency = get_currency
31
- total_amount = get_total_amount
30
+ shipping_info = payload.dig(:shippingInfo) || normalize_shipping(payload.dig(:shipping))
31
+ if shipping_info && shipping_info[:feeCurrency].nil? && !(shipping_info[:feeAmount].nil?)
32
+ shipping_info[:feeCurrency] = payload.dig(:currency)
33
+ end
32
34
 
33
35
  {
34
36
  customerInfo: normalize_customer_info(payload.dig(:customerInfo) || payload.dig(:customer) || {}),
35
- orderData: normalize_order_data(payload.dig(:orderData) || {
36
- amount: total_amount,
37
- currency: currency,
38
- captureMethod: payload.dig(:captureMethod),
39
- confirmationMethod: payload.dig(:confirmationMethod),
40
- coupons: payload.dig(:coupons),
41
- shippingInfo: payload.dig(:shippingInfo) || normalize_shipping(payload.dig(:shipping)),
42
- lineItemData: payload.dig(:orderData, :lineItemData) || payload.dig(:items),
43
- description: payload.dig(:orderDescription),
44
- metadata: payload.dig(:orderMetadata)
45
- }),
37
+ amount: payload.dig(:amount),
38
+ captureMethod: payload.dig(:captureMethod),
39
+ currency: payload.dig(:currency),
40
+ description: payload.dig(:description),
41
+ shippingInfo: shipping_info,
42
+ items: normalize_items(payload.dig(:items)),
43
+ metadata: payload.dig(:metadata) || {},
46
44
  reference: payload.dig(:reference),
47
- metadata: payload.dig(:metadata),
48
45
  successUrl: payload.dig(:successURL),
49
46
  cancelUrl: payload.dig(:cancelURL),
50
47
  test: payload.dig(:test) || false
@@ -85,110 +82,39 @@ module Smartpay
85
82
  postalCode: shipping.dig(:postalCode),
86
83
  country: shipping.dig(:country),
87
84
  },
88
- addressType: shipping.dig(:addressType)
89
- }
90
- end
91
-
92
- def normalize_order_data(order)
93
- return if order.nil?
94
- order = order.transform_keys(&:to_sym)
95
- {
96
- amount: order.dig(:amount),
97
- currency: order.dig(:currency),
98
- captureMethod: order.dig(:captureMethod),
99
- confirmationMethod: order.dig(:confirmationMethod),
100
- coupons: order.dig(:coupons),
101
- shippingInfo: order.dig(:shippingInfo),
102
- lineItemData: normalize_line_items(order.dig(:lineItemData) || order.dig(:items))
85
+ addressType: shipping.dig(:addressType),
86
+ feeAmount: shipping.dig(:feeAmount),
87
+ feeCurrency: shipping.dig(:feeCurrency),
103
88
  }
104
89
  end
105
90
 
106
- def normalize_line_items(data)
91
+ def normalize_items(data)
107
92
  return [] if data.nil?
108
93
 
109
94
  data.map do |item|
110
95
  line_item = item.transform_keys(&:to_sym)
111
96
  {
112
- price: line_item.dig(:price),
113
- priceData: normalize_price_data(line_item.dig(:priceData) || {
114
- productData: {
115
- name: line_item.dig(:name),
116
- brand: line_item.dig(:brand),
117
- categories: line_item.dig(:categories),
118
- gtin: line_item.dig(:gtin),
119
- images: line_item.dig(:images),
120
- reference: line_item.dig(:reference),
121
- url: line_item.dig(:url),
122
- description: line_item.dig(:productDescription),
123
- metadata: line_item.dig(:productMetadata)
124
- },
125
- amount: line_item.dig(:amount),
126
- currency: line_item.dig(:currency),
127
- label: line_item.dig(:label),
128
- description: line_item.dig(:priceDescription),
129
- metadata: line_item.dig(:priceMetadata)
130
- }),
131
97
  quantity: line_item.dig(:quantity),
98
+ label: line_item.dig(:label),
99
+ name: line_item.dig(:name),
100
+ description: line_item.dig(:description),
101
+ amount: line_item.dig(:amount),
102
+ currency: line_item.dig(:currency),
103
+ brand: line_item.dig(:brand),
104
+ categories: line_item.dig(:categories),
105
+ gtin: line_item.dig(:gtin),
106
+ images: line_item.dig(:images),
107
+ reference: line_item.dig(:reference),
108
+ url: line_item.dig(:url),
132
109
  description: line_item.dig(:description),
133
- metadata: line_item.dig(:metadata)
110
+ priceDescription: line_item.dig(:priceDescription),
111
+ productDescription: line_item.dig(:productDescription),
112
+ metadata: line_item.dig(:metadata),
113
+ productMetadata: line_item.dig(:productMetadata),
114
+ priceMetadata: line_item.dig(:priceMetadata)
134
115
  }
135
116
  end
136
117
  end
137
-
138
- def normalize_price_data(data)
139
- return if data.nil?
140
- data = data.transform_keys(&:to_sym)
141
- {
142
- productData: normalize_product_data(data.dig(:productData) || {}),
143
- amount: data.dig(:amount),
144
- currency: data.dig(:currency),
145
- metadata: data.dig(:metadata)
146
- }
147
- end
148
-
149
- def normalize_product_data(product)
150
- return if product.nil?
151
- product = product.transform_keys(&:to_sym)
152
- {
153
- name: product.dig(:name),
154
- brand: product.dig(:brand),
155
- categories: product.dig(:categories),
156
- description: product.dig(:description),
157
- gtin: product.dig(:gtin),
158
- images: product.dig(:images),
159
- reference: product.dig(:reference),
160
- url: product.dig(:url),
161
- metadata: product.dig(:metadata)
162
- }
163
- end
164
-
165
- def get_currency
166
- currency = payload.dig(:orderData, :currency) || payload.dig(:orderData, 'currency')
167
- if currency.nil?
168
- items = get_items
169
- if !items.nil? && items.count > 0
170
- currency = items.first.dig(:currency) || items.first.dig('currency')
171
- end
172
- end
173
- currency
174
- end
175
-
176
- def get_total_amount
177
- total_amount = payload.dig(:orderData, :amount) || payload.dig(:orderData, 'amount')
178
- if total_amount.nil?
179
- items = get_items
180
- if !items.nil? && items.count > 0
181
- total_amount = items.inject(0) { |sum, item| sum + (item[:amount] || item['amount'] || 0) }
182
- end
183
- end
184
- total_amount
185
- end
186
-
187
- def get_items
188
- @items ||= payload.dig(:orderData, :lineItemData, :priceData) ||
189
- payload.dig(:orderData, 'lineItemData', 'priceData') ||
190
- payload.dig(:items)
191
- end
192
118
  end
193
119
  end
194
120
  end
@@ -9,8 +9,13 @@ module Smartpay
9
9
  @response = response
10
10
  end
11
11
 
12
- def redirect_url
13
- URI.escape("#{checkout_url}/login?session-id=#{response[:id]}&public-key=#{public_key}")
12
+ def redirect_url(options = {})
13
+ url = "#{checkout_url}/login"
14
+ promotion_code = options[:promotion_code] || response[:metadata][:__promotion_code__] || nil
15
+ qs = "session-id=#{URI.encode_www_form_component(response[:id])}&public-key=#{URI.encode_www_form_component(public_key)}"
16
+ qs = "#{qs}&promotion-code=#{URI.encode_www_form_component(promotion_code)}" if promotion_code
17
+
18
+ "#{url}?#{qs}"
14
19
  end
15
20
 
16
21
  def as_hash
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Smartpay
4
- VERSION = "0.2.15"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/smartpay.rb CHANGED
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json'
3
+ require "json"
4
4
 
5
5
  require_relative "smartpay/version"
6
- require_relative 'smartpay/configuration'
7
- require_relative 'smartpay/client'
8
- require_relative 'smartpay/api'
9
- require_relative 'smartpay/errors/invalid_request_payload_error'
10
- require_relative 'smartpay/requests/checkout_session'
11
- require_relative 'smartpay/responses/checkout_session'
6
+ require_relative "smartpay/configuration"
7
+ require_relative "smartpay/client"
8
+ require_relative "smartpay/api"
9
+ require_relative "smartpay/errors/invalid_request_payload_error"
10
+ require_relative "smartpay/requests/checkout_session"
11
+ require_relative "smartpay/responses/checkout_session"
12
12
 
13
13
  module Smartpay
14
14
  class << self
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.2.15
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Smartpay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-28 00:00:00.000000000 Z
11
+ date: 2022-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -90,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  - !ruby/object:Gem::Version
91
91
  version: '0'
92
92
  requirements: []
93
- rubygems_version: 3.0.9
93
+ rubygems_version: 3.2.32
94
94
  signing_key:
95
95
  specification_version: 4
96
96
  summary: The Smartpay Ruby SDK offers easy access to Smartpay API from applications