smartpay 0.2.3 → 0.2.7

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: 0c6d6af18ab67f641631f6e67525ae39c64286ae716819fa2d96658b0571f83b
4
- data.tar.gz: 64a89a61dd9cfec7fffad3735bea54ad22c5387e2b68bc8caa54148803aea394
3
+ metadata.gz: 56d3e609ce7f1842742670dff4b7eac351f6e653e199362cdd75b6715ec60501
4
+ data.tar.gz: 35b80a47568eee0755b4335d3aa68922b5b68767ed8e2371c7a45949783b3269
5
5
  SHA512:
6
- metadata.gz: 7f2ca92bd0867e03a3f2ab3f2c964bfdb1ead71ade148ea753b28aec3ee57aed4d7ab394041620bba30a02055839b214f164e562ed6281e96bf53968a958e6b4
7
- data.tar.gz: 1426c5205f369b614791c2b8277df030f8f693e31b1caf9ac36f8cfef017a95570599ee957c2f615401fb57a6b95f38720d9cdf4296b3cdf8563dc4a5110dde0
6
+ metadata.gz: 32ce2b368f3a40956d8fea8929fcf54f3a442688bdc5cb49d3fcb5d1a5b81bb5ba43adcaebd30c0dd1d004fdefa56aeb7e38687bb9912d23157f5d3ead0a33d7
7
+ data.tar.gz: db3ed60db92f3246636eb173025aefc1aa0f3153e38f67a3a690e4ad381183f6b916d59cc0f88871d0c69544c2f479a85da9eba9d04d06124e9acc1a32b98434
data/README.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  The Smartpay Ruby library offers easy access to Smartpay API from applications written in Ruby.
4
4
 
5
+ ## Documentation
6
+
7
+ - [Payment Flow](https://docs.smartpay.co/#payment_flow)
8
+ - [API Document](https://api-doc.smartpay.co)
9
+
5
10
  ## Requirements
6
11
 
7
12
  - Ruby 2.6+
@@ -33,6 +38,66 @@ source 'https://rubygems.org'
33
38
  gem 'smartpay'
34
39
  ```
35
40
 
41
+ ## Usage
42
+
43
+ The package needs to be configured with your own API keys, you can find them on your [dashboard](https://dashboard.smartpay.co/settings/credentials).
44
+
45
+ ```ruby
46
+ Smartpay.configure do |config|
47
+ config.public_key = '<YOUR_PUBLIC_KEY>' # the one starts with pk_test_
48
+ config.secret_key = '<YOUR_SECRET_KEY>' # the one starts with sk_test_
49
+ end
50
+ ```
51
+
52
+ ### Create Checkout session
53
+
54
+ You can find the description and requirement for request payload in [API Document](https://api-doc.smartpay.co/#8a3538b1-530c-448c-8bae-4a41cdf0b8fd).
55
+
56
+ ```ruby
57
+ payloaad = {
58
+ "customerInfo": {
59
+ "emailAddress": "success@smartpay.co",
60
+ },
61
+ "orderData": {
62
+ "amount": 250,
63
+ "currency": "JPY",
64
+ "shippingInfo": {
65
+ "address": {
66
+ "line1": "line1",
67
+ "locality": "locality",
68
+ "postalCode": "123",
69
+ "country": "JP"
70
+ },
71
+ },
72
+ "lineItemData": [{
73
+ "priceData": {
74
+ "productData": {
75
+ "name": "レブロン 18 LOW",
76
+ },
77
+ "amount": 250,
78
+ "currency": "JPY",
79
+ },
80
+ "quantity": 1
81
+ }]
82
+ },
83
+ "reference": "order_ref_1234567",
84
+ "successUrl": "https://docs.smartpay.co/example-pages/checkout-successful",
85
+ "cancelUrl": "https://docs.smartpay.co/example-pages/checkout-canceled"
86
+ }
87
+ ```
88
+
89
+ Create a checkout session by using `Smartpay::Api.create_checkout_session` with your request payload.
90
+
91
+ ```ruby
92
+ session = Smartpay::Api.create_checkout_session(payload)
93
+ ```
94
+
95
+ Then, you can redirect your customer to the session url by calling `redirect_url`:
96
+
97
+ ```ruby
98
+ session.redirect_url
99
+ ```
100
+
36
101
  ## Use with your favorite frameworks
37
102
 
38
103
  ### Ruby on Rails (RoR)
@@ -64,7 +129,7 @@ bundle exec rails generate smartpay:install
64
129
 
65
130
  This introduces 4 changes for a pre-built Smartpay Checkout example:
66
131
 
67
- > 1. A new initializer - `config/initializers/smartpay.rb`. You will have to update the `config.public_api_key` and `config.private_api_key` with your own credentials to make this work.
132
+ > 1. A new initializer - `config/initializers/smartpay.rb`. You will have to update the `config.public_key` and `config.secret_key` with your own credentials to make this work.
68
133
  > 2. A new controller - `app/controllers/smartpays_controller.rb`. This is where you can see how a Checkout session is configured & created.
69
134
  > 3. A new view - `app/views/smartpays/index.html.erb`. The minimum frontend required.
70
135
  > 4. A new route in config/routes.rb.
@@ -75,8 +140,8 @@ Edit the keys with your own credentials in `config/initializers/smartpay.rb`.
75
140
 
76
141
  ```ruby
77
142
  ...
78
- config.public_api_key = '<YOUR_PUBLIC_API_KEY>' # the one starts with pk_test_
79
- config.private_api_key = '<YOUR_PRIVATE_API_KEY>' # the one starts with sk_test_
143
+ config.public_key = '<YOUR_PUBLIC_KEY>' # the one starts with pk_test_
144
+ config.secret_key = '<YOUR_SECRET_KEY>' # the one starts with sk_test_
80
145
  ...
81
146
  ```
82
147
 
@@ -3,66 +3,43 @@ 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
+ name: "オリジナルス STAN SMITH",
9
+ amount: 250,
10
+ currency: "JPY",
11
+ quantity: 1
12
+ }],
13
+ customer: {
14
+ accountAge: 20,
15
+ email: "merchant-support@smartpay.co",
16
+ firstName: "田中",
17
+ lastName: "太郎",
18
+ firstNameKana: "たなか",
19
+ lastNameKana: "たろう",
20
+ address: {
21
+ line1: "3-6-7",
22
+ line2: "青山パラシオタワー 11階",
23
+ subLocality: "",
24
+ locality: "港区北青山",
25
+ administrativeArea: "東京都",
26
+ postalCode: "107-0061",
27
+ country: "JP"
19
28
  },
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
- })
29
+ dateOfBirth: "1985-06-30",
30
+ gender: "male"
31
+ },
32
+ shipping: {
33
+ line1: "line1",
34
+ locality: "locality",
35
+ postalCode: "123",
36
+ country: "JP"
37
+ },
38
+ reference: "order_ref_1234567",
39
+ successURL: "https://docs.smartpay.co/example-pages/checkout-successful",
40
+ cancelURL: "https://docs.smartpay.co/example-pages/checkout-canceled",
41
+ test: true
42
+ })
66
43
  redirect_to session.redirect_url
67
44
  end
68
45
  end
@@ -3,6 +3,6 @@
3
3
  Smartpay.configure do |config|
4
4
  config.api_url = 'https://api.smartpay.co/smartpayments'
5
5
  config.checkout_url = 'https://checkout.smartpay.co'
6
- config.public_api_key = 'pk_test_'
7
- config.private_api_key = 'sk_test_'
6
+ config.public_key = 'pk_test_'
7
+ config.secret_key = 'sk_test_'
8
8
  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
@@ -24,12 +24,12 @@ module Smartpay
24
24
  {
25
25
  accept: :json,
26
26
  content_type: :json,
27
- Authorization: "Basic #{private_api_key}"
27
+ Authorization: "Basic #{secret_key}"
28
28
  }
29
29
  end
30
30
 
31
- def private_api_key
32
- Smartpay.configuration.private_api_key
31
+ def secret_key
32
+ Smartpay.configuration.secret_key
33
33
  end
34
34
  end
35
35
  end
@@ -3,7 +3,7 @@
3
3
  module Smartpay
4
4
  class Configuration
5
5
  attr_accessor :post_timeout
6
- attr_accessor :public_api_key, :private_api_key, :api_url, :checkout_url
6
+ attr_accessor :public_key, :secret_key, :api_url, :checkout_url
7
7
 
8
8
  DEFAULT_TIMEOUT_SETTING = 30
9
9
  DEFAULT_API_URL = 'https://api.smartpay.co'
@@ -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,187 @@
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 = get_currency
31
+ total_amount = get_total_amount
32
+
33
+ {
34
+ 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
+ }),
46
+ reference: payload.dig(:reference),
47
+ metadata: payload.dig(:metadata),
48
+ successUrl: payload.dig(:successURL),
49
+ cancelUrl: payload.dig(:cancelURL),
50
+ test: payload.dig(:test) || false
51
+ }
52
+ end
53
+
54
+ def normalize_customer_info(info)
55
+ return if info.nil?
56
+ customer = info.transform_keys(&:to_sym)
57
+ {
58
+ accountAge: customer.dig(:accountAge),
59
+ emailAddress: customer.dig(:emailAddress) || customer.dig(:email),
60
+ firstName: customer.dig(:firstName),
61
+ lastName: customer.dig(:lastName),
62
+ firstNameKana: customer.dig(:firstNameKana),
63
+ lastNameKana: customer.dig(:lastNameKana),
64
+ address: customer.dig(:address),
65
+ phoneNumber: customer.dig(:phoneNumber) || customer.dig(:phone),
66
+ dateOfBirth: customer.dig(:dateOfBirth),
67
+ legalGender: customer.dig(:legalGender) || customer.dig(:gender),
68
+ reference: customer.dig(:reference)
69
+ }
70
+ end
71
+
72
+ def normalize_shipping(shipping)
73
+ return if shipping.nil?
74
+ {
75
+ address: shipping.dig(:address) || {
76
+ line1: shipping.dig(:line1),
77
+ line2: shipping.dig(:line2),
78
+ line3: shipping.dig(:line3),
79
+ line4: shipping.dig(:line4),
80
+ line5: shipping.dig(:line5),
81
+ subLocality: shipping.dig(:subLocality),
82
+ locality: shipping.dig(:locality),
83
+ administrativeArea: shipping.dig(:administrativeArea),
84
+ postalCode: shipping.dig(:postalCode),
85
+ country: shipping.dig(:country),
86
+ },
87
+ addressType: shipping.dig(:addressType)
88
+ }
89
+ end
90
+
91
+ def normalize_order_data(order)
92
+ return if order.nil?
93
+ order = order.transform_keys(&:to_sym)
94
+ {
95
+ amount: order.dig(:amount),
96
+ currency: order.dig(:currency),
97
+ captureMethod: order.dig(:captureMethod),
98
+ confirmationMethod: order.dig(:confirmationMethod),
99
+ coupons: order.dig(:coupons),
100
+ shippingInfo: order.dig(:shippingInfo),
101
+ lineItemData: normalize_line_items(order.dig(:lineItemData) || order.dig(:items))
102
+ }
103
+ end
104
+
105
+ def normalize_line_items(data)
106
+ return [] if data.nil?
107
+
108
+ data.map do |item|
109
+ line_item = item.transform_keys(&:to_sym)
110
+ {
111
+ price: line_item.dig(:price),
112
+ priceData: normalize_price_data(line_item.dig(:priceData) || {
113
+ productData: {
114
+ name: line_item.dig(:name),
115
+ brand: line_item.dig(:brand),
116
+ categories: line_item.dig(:categories),
117
+ gtin: line_item.dig(:gtin),
118
+ images: line_item.dig(:images),
119
+ reference: line_item.dig(:reference),
120
+ url: line_item.dig(:url),
121
+ description: line_item.dig(:productDescription),
122
+ metadata: line_item.dig(:productMetadata)
123
+ },
124
+ amount: line_item.dig(:amount),
125
+ currency: line_item.dig(:currency),
126
+ label: line_item.dig(:label),
127
+ description: line_item.dig(:priceDescription),
128
+ metadata: line_item.dig(:priceMetadata)
129
+ }),
130
+ quantity: line_item.dig(:quantity),
131
+ description: line_item.dig(:description),
132
+ metadata: line_item.dig(:metadata)
133
+ }
134
+ end
135
+ end
136
+
137
+ def normalize_price_data(data)
138
+ return if data.nil?
139
+ data = data.transform_keys(&:to_sym)
140
+ {
141
+ productData: normalize_product_data(data.dig(:productData) || {}),
142
+ amount: data.dig(:amount),
143
+ currency: data.dig(:currency),
144
+ metadata: data.dig(:metadata)
145
+ }
146
+ end
147
+
148
+ def normalize_product_data(product)
149
+ return if product.nil?
150
+ product = product.transform_keys(&:to_sym)
151
+ {
152
+ name: product.dig(:name),
153
+ brand: product.dig(:brand),
154
+ categories: product.dig(:categories),
155
+ description: product.dig(:description),
156
+ gtin: product.dig(:gtin),
157
+ images: product.dig(:images),
158
+ reference: product.dig(:reference),
159
+ url: product.dig(:url),
160
+ metadata: product.dig(:metadata)
161
+ }
162
+ end
163
+
164
+ def get_currency
165
+ currency = payload.dig(:orderData, :currency) || payload.dig(:orderData, 'currency')
166
+ if currency.nil?
167
+ items = payload.dig(:orderData, :lineItemData, :priceData) || payload.dig(:items)
168
+ if !items.nil? && items.count > 0
169
+ currency = items.first.dig(:currency)
170
+ end
171
+ end
172
+ currency
173
+ end
174
+
175
+ def get_total_amount
176
+ total_amount = payload.dig(:orderData, :amount) || payload.dig(:orderData, 'amount')
177
+ if total_amount.nil?
178
+ items = payload.dig(:orderData, :lineItemData, :priceData) || payload.dig(:items)
179
+ if !items.nil? && items.count > 0
180
+ total_amount = items.inject(0) { |sum, item| sum + (item[:amount] || 0) }
181
+ end
182
+ end
183
+ total_amount
184
+ end
185
+ end
186
+ end
187
+ end
@@ -10,7 +10,15 @@ module Smartpay
10
10
  end
11
11
 
12
12
  def redirect_url
13
- URI.escape("#{checkout_url}/login?session-id=#{response[:id]}&public-key=#{public_api_key}")
13
+ URI.escape("#{checkout_url}/login?session-id=#{response[:id]}&public-key=#{public_key}")
14
+ end
15
+
16
+ def as_hash
17
+ @response
18
+ end
19
+
20
+ def as_json
21
+ @response.to_json
14
22
  end
15
23
 
16
24
  private
@@ -19,8 +27,8 @@ module Smartpay
19
27
  Smartpay.configuration.checkout_url
20
28
  end
21
29
 
22
- def public_api_key
23
- Smartpay.configuration.public_api_key
30
+ def public_key
31
+ Smartpay.configuration.public_key
24
32
  end
25
33
  end
26
34
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Smartpay
4
- VERSION = "0.2.3"
4
+ VERSION = "0.2.7"
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.3
4
+ version: 0.2.7
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-21 00:00:00.000000000 Z
11
+ date: 2021-10-26 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