smartpay 0.2.5 → 0.2.6

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: b85b02e47b248634100a1f5c9cc54aa977ae0306debfa54615f916c17b190d43
4
- data.tar.gz: b001d7cc034fb87d5f80e2d9fafc202d343fc52a03ec2d13bbc774c0dd980fb4
3
+ metadata.gz: fd9d36f1c0b628dcafc5308503663e63f628eb9467f237b750c03d7416e565d8
4
+ data.tar.gz: 2c8aa53f89d35801c11657357e1cd01c79a647dcb1adb9a58dbb912538506152
5
5
  SHA512:
6
- metadata.gz: fc3a2b1c0d41558e07cd4298119446f5fada619c0625e66278437c503c7980d353d1eee2e0948bf3be036e33facbff391354705c11068f3a13dfd226873a1e11
7
- data.tar.gz: c5ae98b31fb8c102bf9ace17467569fba7642f121d00ca2aac64ca9f28cfdca26988c063713fbd43a9aab63914cd9bb3e7107c321589f225ecb0ac7f7ee4bef5
6
+ metadata.gz: 71e24b5936a52552e97675081d462154d527dd9effecf42cba3867dffe18640c6916b48cbffb7140e11c0b9fab50105821ee68271b7746685ad0021ba96b27a4
7
+ data.tar.gz: f2f84b245783023dad0bb44e203c427d5b57f5d9ce14826c7bec5a312535f735bba133295d7e962f2dec7d8400c9d4760b3ca9c3f62262dd18ae08159c504369
@@ -3,66 +3,27 @@ class SmartpaysController < ApplicationController
3
3
  end
4
4
 
5
5
  def create
6
- session = Smartpay::Api.create_checkout_session(
7
- {
8
- "customerInfo": {
9
- "emailAddress": "success@smartpay.co",
10
- "firstName": nil,
11
- "lastName": nil,
12
- "firstNameKana": nil,
13
- "lastNameKana": nil,
14
- "address": nil,
15
- "phoneNumber": nil,
16
- "dateOfBirth": nil,
17
- "legalGender": nil,
18
- "reference": nil
6
+ session = Smartpay::Api.create_checkout_session({
7
+ items: [
8
+ {
9
+ name: "レブロン 18 LOW",
10
+ amount: 250,
11
+ currency: "JPY",
12
+ quantity: 1,
19
13
  },
20
- "orderData": {
21
- "amount": 250,
22
- "currency": "JPY",
23
- "captureMethod": nil,
24
- "confirmationMethod": nil,
25
- "coupons": nil,
26
- "shippingInfo": {
27
- "address": {
28
- "line1": "line1",
29
- "line2": nil,
30
- "line3": nil,
31
- "line4": nil,
32
- "line5": nil,
33
- "subLocality": nil,
34
- "locality": "locality",
35
- "administrativeArea": nil,
36
- "postalCode": "123",
37
- "country": "JP"},
38
- "addressType": nil},
39
- "lineItemData": [{
40
- "price": nil,
41
- "priceData": {
42
- "productData": {
43
- "name": "レブロン 18 LOW",
44
- "brand": nil,
45
- "categories": nil,
46
- "description": nil,
47
- "gtin": nil,
48
- "images": nil,
49
- "reference": nil,
50
- "url": nil,
51
- "metadata": nil},
52
- "amount": 250,
53
- "currency": "JPY",
54
- "metadata": nil},
55
- "quantity": 1,
56
- "description": nil,
57
- "metadata": nil
58
- }]
59
- },
60
- "reference": "order_ref_1234567",
61
- "metadata": nil,
62
- "successUrl": "https://docs.smartpay.co/example-pages/checkout-successful",
63
- "cancelUrl": "https://docs.smartpay.co/example-pages/checkout-canceled",
64
- "test": true
65
- })
14
+ ],
15
+
16
+ shipping: {
17
+ line1: "line1",
18
+ locality: "locality",
19
+ postalCode: "123",
20
+ country: "JP",
21
+ },
22
+ reference: "order_ref_1234567",
23
+ successURL: "https://docs.smartpay.co/example-pages/checkout-successful",
24
+ cancelURL: "https://docs.smartpay.co/example-pages/checkout-canceled",
25
+ test: true,
26
+ })
66
27
  redirect_to session.redirect_url
67
28
  end
68
29
  end
data/lib/smartpay/api.rb CHANGED
@@ -4,7 +4,9 @@ module Smartpay
4
4
  class Api
5
5
  class << self
6
6
  def create_checkout_session(payload)
7
- Responses::CheckoutSession.new(Client.post('/checkout/sessions', payload))
7
+ Responses::CheckoutSession.new(
8
+ Client.post('/checkout/sessions', Requests::CheckoutSession.new(payload).as_hash)
9
+ )
8
10
  end
9
11
  end
10
12
  end
@@ -0,0 +1,16 @@
1
+ module Smartpay
2
+ module Errors
3
+ class InvalidRequestPayloadError < ArgumentError
4
+ attr_accessor :key_name
5
+
6
+ def initialize(key_name)
7
+ @key_name = key_name
8
+ super
9
+ end
10
+
11
+ def message
12
+ "#{key_name} can't be blank."
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,170 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Smartpay
4
+ module Requests
5
+ class CheckoutSession
6
+ attr_accessor :payload
7
+
8
+ REQUIREMENT_KEY_NAME = [:successURL, :cancelURL, :customerInfo, :orderData].freeze
9
+ CAN_FALLBACK_KEYS = [:customerInfo, :orderData].freeze
10
+
11
+ def initialize(raw_payload)
12
+ @payload = raw_payload.transform_keys(&:to_sym)
13
+ end
14
+
15
+ def as_hash
16
+ check_requirement!
17
+ normalize_payload
18
+ end
19
+
20
+ private
21
+
22
+ def check_requirement!
23
+ REQUIREMENT_KEY_NAME.each do |key_name|
24
+ next if CAN_FALLBACK_KEYS.include?(key_name)
25
+ raise Errors::InvalidRequestPayloadError, key_name unless payload.include?(key_name)
26
+ end
27
+ end
28
+
29
+ def normalize_payload
30
+ currency = nil
31
+ total_amount = 0
32
+ items = payload.dig(:lineItemData) || payload.dig(:items)
33
+ if items.count > 0
34
+ total_amount = items.inject(0) { |sum, item| sum + (item[:amount] || 0) }
35
+ currency = items.first.dig(:currency)
36
+ end
37
+
38
+ {
39
+ customerInfo: normalize_customer_info(payload.dig(:customerInfo) || payload.dig(:customer) || {}),
40
+ orderData: normalize_order_data(payload.dig(:orderData) || {
41
+ amount: total_amount,
42
+ currency: currency,
43
+ captureMethod: payload.dig(:captureMethod),
44
+ confirmationMethod: payload.dig(:confirmationMethod),
45
+ coupons: payload.dig(:coupons),
46
+ shippingInfo: payload.dig(:shippingInfo) || normalize_shipping(payload.dig(:shipping)),
47
+ lineItemData: items,
48
+ description: payload.dig(:orderDescription),
49
+ metadata: payload.dig(:orderMetadata)
50
+ }),
51
+ reference: payload.dig(:reference),
52
+ metadata: payload.dig(:metadata),
53
+ successUrl: payload.dig(:successURL),
54
+ cancelUrl: payload.dig(:cancelURL),
55
+ test: payload.dig(:test) || false
56
+ }
57
+ end
58
+
59
+ def normalize_customer_info(info)
60
+ return if info.nil?
61
+ customer = info.transform_keys(&:to_sym)
62
+ {
63
+ accountAge: customer.dig(:accountAge),
64
+ emailAddress: customer.dig(:emailAddress) || customer.dig(:email),
65
+ firstName: customer.dig(:firstName),
66
+ lastName: customer.dig(:lastName),
67
+ firstNameKana: customer.dig(:firstNameKana),
68
+ lastNameKana: customer.dig(:lastNameKana),
69
+ address: customer.dig(:address),
70
+ phoneNumber: customer.dig(:phoneNumber) || customer.dig(:phone),
71
+ dateOfBirth: customer.dig(:dateOfBirth),
72
+ legalGender: customer.dig(:legalGender) || customer.dig(:gender),
73
+ reference: customer.dig(:reference)
74
+ }
75
+ end
76
+
77
+ def normalize_shipping(shipping)
78
+ return if shipping.nil?
79
+ {
80
+ address: shipping.dig(:address) || {
81
+ line1: shipping.dig(:line1),
82
+ line2: shipping.dig(:line2),
83
+ line3: shipping.dig(:line3),
84
+ line4: shipping.dig(:line4),
85
+ line5: shipping.dig(:line5),
86
+ subLocality: shipping.dig(:subLocality),
87
+ locality: shipping.dig(:locality),
88
+ administrativeArea: shipping.dig(:administrativeArea),
89
+ postalCode: shipping.dig(:postalCode),
90
+ country: shipping.dig(:country),
91
+ },
92
+ addressType: shipping.dig(:addressType)
93
+ }
94
+ end
95
+
96
+ def normalize_order_data(order)
97
+ return if order.nil?
98
+ order = order.transform_keys(&:to_sym)
99
+ {
100
+ amount: order.dig(:amount),
101
+ currency: order.dig(:currency),
102
+ captureMethod: order.dig(:captureMethod),
103
+ confirmationMethod: order.dig(:confirmationMethod),
104
+ coupons: order.dig(:coupons),
105
+ shippingInfo: order.dig(:shippingInfo),
106
+ lineItemData: normalize_line_items(order.dig(:lineItemData) || order.dig(:items))
107
+ }
108
+ end
109
+
110
+ def normalize_line_items(data)
111
+ return [] if data.nil?
112
+
113
+ data.map do |item|
114
+ line_item = item.transform_keys(&:to_sym)
115
+ {
116
+ price: line_item.dig(:price),
117
+ priceData: normalize_price_data(line_item.dig(:priceData) || {
118
+ productData: {
119
+ name: line_item.dig(:name),
120
+ brand: line_item.dig(:brand),
121
+ categories: line_item.dig(:categories),
122
+ gtin: line_item.dig(:gtin),
123
+ images: line_item.dig(:images),
124
+ reference: line_item.dig(:reference),
125
+ url: line_item.dig(:url),
126
+ description: line_item.dig(:productDescription),
127
+ metadata: line_item.dig(:productMetadata)
128
+ },
129
+ amount: line_item.dig(:amount),
130
+ currency: line_item.dig(:currency),
131
+ label: line_item.dig(:label),
132
+ description: line_item.dig(:priceDescription),
133
+ metadata: line_item.dig(:priceMetadata)
134
+ }),
135
+ quantity: line_item.dig(:quantity),
136
+ description: line_item.dig(:description),
137
+ metadata: line_item.dig(:metadata)
138
+ }
139
+ end
140
+ end
141
+
142
+ def normalize_price_data(data)
143
+ return if data.nil?
144
+ data = data.transform_keys(&:to_sym)
145
+ {
146
+ productData: normalize_product_data(data.dig(:productData) || {}),
147
+ amount: data.dig(:amount),
148
+ currency: data.dig(:currency),
149
+ metadata: data.dig(:metadata)
150
+ }
151
+ end
152
+
153
+ def normalize_product_data(product)
154
+ return if product.nil?
155
+ product = product.transform_keys(&:to_sym)
156
+ {
157
+ name: product.dig(:name),
158
+ brand: product.dig(:brand),
159
+ categories: product.dig(:categories),
160
+ description: product.dig(:description),
161
+ gtin: product.dig(:gtin),
162
+ images: product.dig(:images),
163
+ reference: product.dig(:reference),
164
+ url: product.dig(:url),
165
+ metadata: product.dig(:metadata)
166
+ }
167
+ end
168
+ end
169
+ end
170
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Smartpay
4
- VERSION = "0.2.5"
4
+ VERSION = "0.2.6"
5
5
  end
data/lib/smartpay.rb CHANGED
@@ -6,6 +6,8 @@ require_relative "smartpay/version"
6
6
  require_relative 'smartpay/configuration'
7
7
  require_relative 'smartpay/client'
8
8
  require_relative 'smartpay/api'
9
+ require_relative 'smartpay/errors/invalid_request_payload_error'
10
+ require_relative 'smartpay/requests/checkout_session'
9
11
  require_relative 'smartpay/responses/checkout_session'
10
12
 
11
13
  module Smartpay
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.5
4
+ version: 0.2.6
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-22 00:00:00.000000000 Z
11
+ date: 2021-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -64,6 +64,8 @@ files:
64
64
  - lib/smartpay/api.rb
65
65
  - lib/smartpay/client.rb
66
66
  - lib/smartpay/configuration.rb
67
+ - lib/smartpay/errors/invalid_request_payload_error.rb
68
+ - lib/smartpay/requests/checkout_session.rb
67
69
  - lib/smartpay/responses/checkout_session.rb
68
70
  - lib/smartpay/version.rb
69
71
  homepage: https://smartpay.co