smartpay 0.2.2 → 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: 86faee4c994b67bdf3ff9d2454a85d09577b82475917e5048fdb3c373ba1fe30
4
- data.tar.gz: 64babfa347d6f484ec3aea689bf735a3aa294e182c95cf80840fcc0632dca043
3
+ metadata.gz: fd9d36f1c0b628dcafc5308503663e63f628eb9467f237b750c03d7416e565d8
4
+ data.tar.gz: 2c8aa53f89d35801c11657357e1cd01c79a647dcb1adb9a58dbb912538506152
5
5
  SHA512:
6
- metadata.gz: 1d4e5f3a352166fff345a6e6cc3b6e68125b74dae5d0913fc78e780aa37dbe754eb18274ceab76152a991dfd82a883b7d49f8ce93d0a26294d83590f9cb1deb1
7
- data.tar.gz: b18f1f1c5602dca17d548e29f525b9238e9490b0340ed6dc69c8b29e8b22c3e300cecad3a6e658ecf31b333d2c0c2dca660bc1ffd2190d7d55a54e500c99c575
6
+ metadata.gz: 71e24b5936a52552e97675081d462154d527dd9effecf42cba3867dffe18640c6916b48cbffb7140e11c0b9fab50105821ee68271b7746685ad0021ba96b27a4
7
+ data.tar.gz: f2f84b245783023dad0bb44e203c427d5b57f5d9ce14826c7bec5a312535f735bba133295d7e962f2dec7d8400c9d4760b3ca9c3f62262dd18ae08159c504369
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)
@@ -76,7 +141,7 @@ Edit the keys with your own credentials in `config/initializers/smartpay.rb`.
76
141
  ```ruby
77
142
  ...
78
143
  config.public_key = '<YOUR_PUBLIC_KEY>' # the one starts with pk_test_
79
- config.secret_key = '<YOUR_API_SECRET>' # the one starts with sk_test_
144
+ config.secret_key = '<YOUR_SECRET_KEY>' # the one starts with sk_test_
80
145
  ...
81
146
  ```
82
147
 
@@ -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
@@ -13,6 +13,14 @@ module Smartpay
13
13
  URI.escape("#{checkout_url}/login?session-id=#{response[:id]}&public-key=#{public_key}")
14
14
  end
15
15
 
16
+ def as_hash
17
+ @response
18
+ end
19
+
20
+ def as_json
21
+ @response.to_json
22
+ end
23
+
16
24
  private
17
25
 
18
26
  def checkout_url
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Smartpay
4
- VERSION = "0.2.2"
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.2
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-20 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