smartpay 0.2.18 → 0.5.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: 3897098e68300e9004b822ba8f797040b02c00362428cee023ba598415bd4a54
4
- data.tar.gz: dd42d0ad91f798d88fd765270975e9d61dde01cd3b52143c0d762bb1577f71b6
3
+ metadata.gz: c0cd94594c52188072ee764c3d9abbab864773af324310b3f0a6e5747570e04e
4
+ data.tar.gz: 7fe589e25744b8891af1b91786d26c4f7e67e13fcf4ba80c9d847ea31461181f
5
5
  SHA512:
6
- metadata.gz: 05df4d994fd49a80bd58d56bd58469639aa0ad50f7688d874cce1b412e8366fe5afb82acf6082c00e40b3b0a7491bba4a7242117e65ab22e8fd326cbd0b9e65a
7
- data.tar.gz: ed67c91a5e4f43991a362d77c2c2ac4f430265c14f3c236941c26f482a162683bdddbc10700e82344b9b923c698bbbd9126cd8c1cb7975bc1506807de2888a80
6
+ metadata.gz: 8973913b6c40ddd9fa7449d43696c823b25ac36483f070c3fe1a6cafffe8f030b70171ee2ff7c8ae9876b336a820efaf17b1de85f0bc2b0245b2d6add321f47e
7
+ data.tar.gz: 6b7e1721a942ace0254e701c23186f0b2c0eaa6d2821e9fd5bf4339b782e58b8eaa9d2b6aa39e40b6c1babbb462c199f964f4bf9a9f2ec4664f8d39b9bef25d8
data/.rubocop.yml CHANGED
@@ -1,13 +1,13 @@
1
- AllCops:
2
- TargetRubyVersion: 2.5
3
-
4
- Style/StringLiterals:
5
- Enabled: true
6
- EnforcedStyle: double_quotes
7
-
8
- Style/StringLiteralsInInterpolation:
9
- Enabled: true
10
- EnforcedStyle: double_quotes
11
-
12
- Layout/LineLength:
13
- Max: 120
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/Gemfile CHANGED
@@ -5,6 +5,12 @@ source "https://rubygems.org"
5
5
  # Specify your gem's dependencies in smartpay.gemspec
6
6
  gemspec
7
7
 
8
+ gem "rest-client", "~> 2.1.0"
9
+
10
+ gem "securerandom", "~> 0.2.0"
11
+
12
+ gem "retries", "~> 0.0.5"
13
+
8
14
  gem "rake", "~> 13.0"
9
15
 
10
16
  gem "rspec", "~> 3.0"
data/README.md CHANGED
@@ -1,181 +1,181 @@
1
- # Smartpay Ruby Library
2
-
3
- The Smartpay Ruby library offers easy access to Smartpay API from applications written in Ruby.
4
-
5
- ## Documentation
6
-
7
- - [Payment Flow](https://docs.smartpay.co/#payment_flow)
8
- - [API Document](https://api-doc.smartpay.co)
9
-
10
- ## Requirements
11
-
12
- - Ruby 2.6+
13
- - Smartpay `API keys & secrets`. You can find your credential at the `settings > credentials` page on your [dashboard](https://dashboard.smartpay.co/settings/credentials).
14
-
15
- ## Installation
16
-
17
- If you use system built-in Ruby, you might need to be the `sudoer` to be able to `sudo` in some of the following steps. We recommend you to use either [rbenv](https://github.com/rbenv/rbenv) or [rvm](https://rvm.io/) to have your own non-global Ruby to avoid potential permission issues.
18
-
19
- Once you have your Ruby in place, add the latest version of Smartpay to your project's dependencies:
20
-
21
- ```sh
22
- gem install smartpay
23
- ```
24
-
25
- If you want to build the gem yourself from source:
26
-
27
- ```sh
28
- gem build smartpay.gemspec
29
- ```
30
-
31
- ### Bundler
32
-
33
- If you are installing via bundler, make sure that you use the `https` resource in your Gemfile to avoid the risk of gems being compromised:
34
-
35
- ```ruby
36
- source 'https://rubygems.org'
37
-
38
- gem 'smartpay'
39
- ```
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
-
101
- ## Use with your favorite frameworks
102
-
103
- ### Ruby on Rails (RoR)
104
-
105
- #### Install Rails
106
-
107
- ```sh
108
- gem install rails
109
- ```
110
-
111
- #### Create your app
112
-
113
- ```sh
114
- rails new app-with-smartpay
115
- ```
116
-
117
- #### Add Smartpay
118
-
119
- ```sh
120
- cd app-with-smartpay
121
- bundle add smartpay
122
- ```
123
-
124
- #### Generator
125
-
126
- ```sh
127
- bundle exec rails generate smartpay:install
128
- ```
129
-
130
- This introduces 4 changes for a pre-built Smartpay Checkout example:
131
-
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.
133
- > 2. A new controller - `app/controllers/smartpays_controller.rb`. This is where you can see how a Checkout session is configured & created.
134
- > 3. A new view - `app/views/smartpays/index.html.erb`. The minimum frontend required.
135
- > 4. A new route in config/routes.rb.
136
-
137
- #### Fill in your API keys
138
-
139
- Edit the keys with your own credentials in `config/initializers/smartpay.rb`.
140
-
141
- ```ruby
142
- ...
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_
145
- ...
146
- ```
147
-
148
- #### Start your server
149
-
150
- ```sh
151
- bundle exec rails server
152
- ```
153
-
154
- ### Test with Checkout Session
155
-
156
- Visit [http://localhost:3000/smartpays](http://localhost:3000/smartpays).
157
-
158
- Click the `checkout` button on the page to be redirected to Smartpay's Checkout.
159
-
160
- To try out different cases, you can use the following test credit cards for different cases:
161
-
162
- - Payment succeeds: `4242 4242 4242 4242`
163
- - Payment is declined: `4100 0000 0000 0019`
164
-
165
- ## Development
166
-
167
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
168
-
169
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
170
-
171
- ## Contributing
172
-
173
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/smartpay. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/smartpay/blob/master/CODE_OF_CONDUCT.md).
174
-
175
- ## License
176
-
177
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
178
-
179
- ## Code of Conduct
180
-
181
- Everyone interacting in the Smartpay project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/smartpay/blob/master/CODE_OF_CONDUCT.md).
1
+ # Smartpay Ruby Library
2
+
3
+ The Smartpay Ruby library offers easy access to Smartpay API from applications written in Ruby.
4
+
5
+ ## Documentation
6
+
7
+ - [Payment Flow](https://docs.smartpay.co/#payment_flow)
8
+ - [API Document](https://api-doc.smartpay.co)
9
+
10
+ ## Requirements
11
+
12
+ - Ruby 2.6+
13
+ - Smartpay `API keys & secrets`. You can find your credential at the `settings > credentials` page on your [dashboard](https://dashboard.smartpay.co/settings/credentials).
14
+
15
+ ## Installation
16
+
17
+ If you use system built-in Ruby, you might need to be the `sudoer` to be able to `sudo` in some of the following steps. We recommend you to use either [rbenv](https://github.com/rbenv/rbenv) or [rvm](https://rvm.io/) to have your own non-global Ruby to avoid potential permission issues.
18
+
19
+ Once you have your Ruby in place, add the latest version of Smartpay to your project's dependencies:
20
+
21
+ ```sh
22
+ gem install smartpay
23
+ ```
24
+
25
+ If you want to build the gem yourself from source:
26
+
27
+ ```sh
28
+ gem build smartpay.gemspec
29
+ ```
30
+
31
+ ### Bundler
32
+
33
+ If you are installing via bundler, make sure that you use the `https` resource in your Gemfile to avoid the risk of gems being compromised:
34
+
35
+ ```ruby
36
+ source 'https://rubygems.org'
37
+
38
+ gem 'smartpay'
39
+ ```
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
+
101
+ ## Use with your favorite frameworks
102
+
103
+ ### Ruby on Rails (RoR)
104
+
105
+ #### Install Rails
106
+
107
+ ```sh
108
+ gem install rails
109
+ ```
110
+
111
+ #### Create your app
112
+
113
+ ```sh
114
+ rails new app-with-smartpay
115
+ ```
116
+
117
+ #### Add Smartpay
118
+
119
+ ```sh
120
+ cd app-with-smartpay
121
+ bundle add smartpay
122
+ ```
123
+
124
+ #### Generator
125
+
126
+ ```sh
127
+ bundle exec rails generate smartpay:install
128
+ ```
129
+
130
+ This introduces 4 changes for a pre-built Smartpay Checkout example:
131
+
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.
133
+ > 2. A new controller - `app/controllers/smartpays_controller.rb`. This is where you can see how a Checkout session is configured & created.
134
+ > 3. A new view - `app/views/smartpays/index.html.erb`. The minimum frontend required.
135
+ > 4. A new route in config/routes.rb.
136
+
137
+ #### Fill in your API keys
138
+
139
+ Edit the keys with your own credentials in `config/initializers/smartpay.rb`.
140
+
141
+ ```ruby
142
+ ...
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_
145
+ ...
146
+ ```
147
+
148
+ #### Start your server
149
+
150
+ ```sh
151
+ bundle exec rails server
152
+ ```
153
+
154
+ ### Test with Checkout Session
155
+
156
+ Visit [http://localhost:3000/smartpays](http://localhost:3000/smartpays).
157
+
158
+ Click the `checkout` button on the page to be redirected to Smartpay's Checkout.
159
+
160
+ To try out different cases, you can use the following test credit cards for different cases:
161
+
162
+ - Payment succeeds: `4242 4242 4242 4242`
163
+ - Payment is declined: `4100 0000 0000 0019`
164
+
165
+ ## Development
166
+
167
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
168
+
169
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
170
+
171
+ ## Contributing
172
+
173
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/smartpay. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/smartpay/blob/master/CODE_OF_CONDUCT.md).
174
+
175
+ ## License
176
+
177
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
178
+
179
+ ## Code of Conduct
180
+
181
+ Everyone interacting in the Smartpay project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/smartpay/blob/master/CODE_OF_CONDUCT.md).
@@ -36,8 +36,8 @@ class SmartpaysController < ApplicationController
36
36
  country: "JP"
37
37
  },
38
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",
39
+ successUrl: "https://docs.smartpay.co/example-pages/checkout-successful",
40
+ cancelUrl: "https://docs.smartpay.co/example-pages/checkout-canceled",
41
41
  test: true
42
42
  })
43
43
  redirect_to session.redirect_url
data/lib/smartpay/api.rb CHANGED
@@ -1,13 +1,56 @@
1
- # frozen_string_literal: true
2
-
3
- module Smartpay
4
- class Api
5
- class << self
6
- def create_checkout_session(payload)
7
- Responses::CheckoutSession.new(
8
- Client.post("/checkout-sessions", Requests::CheckoutSession.new(payload).as_hash)
9
- )
10
- end
11
- end
12
- end
13
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Smartpay
4
+ class Api
5
+ class << self
6
+ def create_checkout_session(payload)
7
+ Responses::CheckoutSession.new(
8
+ Client.post("/checkout-sessions", params: {}, payload: Requests::CheckoutSession.new(payload).as_hash)
9
+ )
10
+ end
11
+
12
+ def get_orders(page_token: nil, max_results: nil, expand: '' )
13
+ Responses::Base.new(Client.get("/orders", params: { pageToken: page_token, maxResults: max_results, expand: expand }))
14
+ end
15
+
16
+ def get_order(id, expand: '' )
17
+ Responses::Base.new(Client.get("/orders/%s" % id, params: { expand: expand }))
18
+ end
19
+
20
+ def cancel_order(id)
21
+ Responses::Base.new(
22
+ Client.put("/orders/%s/cancellation" % id)
23
+ )
24
+ end
25
+
26
+ def create_payment(payload)
27
+ Responses::Base.new(
28
+ Client.post("/payments", params:{}, payload: Requests::Payment.new(payload).as_hash)
29
+ )
30
+ end
31
+
32
+ def capture(payload)
33
+ create_payment(payload)
34
+ end
35
+
36
+ def get_payment(id, expand: '' )
37
+ Responses::Base.new(Client.get("/payments/%s" % id, params: { expand: expand }))
38
+ end
39
+
40
+ def create_refund(payload)
41
+ Responses::Base.new(
42
+ Client.post("/refunds", params:{}, payload: Requests::Refund.new(payload).as_hash)
43
+ )
44
+ end
45
+
46
+ def refund(payload)
47
+ create_refund(payload)
48
+ end
49
+
50
+ def get_refund(id, expand: '' )
51
+ Responses::Base.new(Client.get("/refunds/%s" % id, params: { expand: expand }))
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -1,45 +1,84 @@
1
- # frozen_string_literal: true
2
-
3
- require "rest-client"
4
-
5
- module Smartpay
6
- class Client
7
- class << self
8
- def post(path, payload = {})
9
- request_payload = default_payload.merge(payload)
10
- response = RestClient::Request.execute(method: :post, url: api_url(path), headers: headers, timeout: timeout,
11
- payload: request_payload.to_json)
12
- JSON.parse(response.body, symbolize_names: true)
13
- end
14
-
15
- private
16
-
17
- def api_url(path)
18
- "#{Smartpay.configuration.api_url}#{path}"
19
- end
20
-
21
- def timeout
22
- Smartpay.configuration.post_timeout
23
- end
24
-
25
- def headers
26
- {
27
- accept: :json,
28
- content_type: :json,
29
- Authorization: "Basic #{secret_key}"
30
- }
31
- end
32
-
33
- def secret_key
34
- Smartpay.configuration.secret_key
35
- end
36
-
37
- def default_payload
38
- {
39
- 'dev-lang': :ruby,
40
- 'sdk-version': Smartpay::VERSION
41
- }.freeze
42
- end
43
- end
44
- end
45
- end
1
+ # frozen_string_literal: true
2
+
3
+ require "rest-client"
4
+ require "retries"
5
+ require "securerandom"
6
+
7
+ module Smartpay
8
+ class Client
9
+ class << self
10
+ def get(path, params: {})
11
+ request_params = default_params.merge(params)
12
+ response = RestClient::Request.execute(method: :get, url: api_url(path),
13
+ headers: headers.merge(params: request_params),
14
+ timeout: timeout)
15
+ JSON.parse(response.body, symbolize_names: true)
16
+ end
17
+
18
+ def post(path, params: {}, payload: {})
19
+ request_params = default_params.merge(params)
20
+ request_payload = default_payload.merge(payload)
21
+ idempotency_key = nonce
22
+ response = with_retries(:max_tries => 1, :rescue => [RestClient::BadRequest, RestClient::BadGateway, RestClient::ServiceUnavailable, RestClient::GatewayTimeout]) do
23
+ RestClient::Request.execute(method: :post, url: api_url(path),
24
+ params: request_params,
25
+ headers: headers.merge({Idempotency_Key: idempotency_key}).merge(params: request_params),
26
+ timeout: timeout,
27
+ payload: request_payload.to_json)
28
+ end
29
+ JSON.parse(response.body, symbolize_names: true)
30
+ end
31
+
32
+ def put(path, params: {}, payload: {})
33
+ request_params = default_params.merge(params).merge({'Idempotency-Key': nonce})
34
+ request_payload = default_payload.merge(payload)
35
+ idempotency_key = nonce
36
+ response = with_retries(:max_tries => 1, :rescue => [RestClient::InternalServerError, RestClient::BadGateway, RestClient::ServiceUnavailable, RestClient::GatewayTimeout]) do
37
+ RestClient::Request.execute(method: :put, url: api_url(path),
38
+ params: request_params,
39
+ headers: headers.merge({Idempotency_Key: idempotency_key}).merge(params: request_params),
40
+ timeout: timeout,
41
+ payload: request_payload.to_json)
42
+ end
43
+ JSON.parse(response.body, symbolize_names: true)
44
+ end
45
+
46
+ private
47
+
48
+ def nonce
49
+ SecureRandom.hex
50
+ end
51
+
52
+ def api_url(path)
53
+ "#{Smartpay.configuration.api_url}#{path}"
54
+ end
55
+
56
+ def timeout
57
+ Smartpay.configuration.post_timeout
58
+ end
59
+
60
+ def headers
61
+ {
62
+ accept: :json,
63
+ content_type: :json,
64
+ Authorization: "Basic #{secret_key}"
65
+ }
66
+ end
67
+
68
+ def secret_key
69
+ Smartpay.configuration.secret_key
70
+ end
71
+
72
+ def default_params
73
+ {
74
+ 'dev-lang': :ruby,
75
+ 'sdk-version': Smartpay::VERSION
76
+ }.freeze
77
+ end
78
+
79
+ def default_payload
80
+ {}.freeze
81
+ end
82
+ end
83
+ end
84
+ end
@@ -1,48 +1,48 @@
1
- # frozen_string_literal: true
2
-
3
- module Smartpay
4
- class Configuration
5
- attr_accessor :public_key, :secret_key
6
- attr_writer :post_timeout, :api_url, :checkout_url
7
-
8
- DEFAULT_TIMEOUT_SETTING = 30
9
- DEFAULT_API_URL = "https://api.smartpay.co/v1"
10
- DEFAULT_CHECKOUT_URL = "https://checkout.smartpay.co"
11
-
12
- def initialize
13
- @post_timeout = DEFAULT_TIMEOUT_SETTING
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
24
- end
25
-
26
- def post_timeout
27
- @post_timeout || DEFAULT_TIMEOUT_SETTING
28
- end
29
-
30
- def 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
36
- end
37
-
38
- def checkout_url
39
- @checkout_url || DEFAULT_CHECKOUT_URL
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
47
- end
48
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Smartpay
4
+ class Configuration
5
+ attr_accessor :public_key, :secret_key
6
+ attr_writer :post_timeout, :api_url, :checkout_url
7
+
8
+ DEFAULT_TIMEOUT_SETTING = 30
9
+ DEFAULT_API_URL = "https://api.smartpay.co/v1"
10
+ DEFAULT_CHECKOUT_URL = "https://checkout.smartpay.co"
11
+
12
+ def initialize
13
+ @post_timeout = DEFAULT_TIMEOUT_SETTING
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
24
+ end
25
+
26
+ def post_timeout
27
+ @post_timeout || DEFAULT_TIMEOUT_SETTING
28
+ end
29
+
30
+ def 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
36
+ end
37
+
38
+ def checkout_url
39
+ @checkout_url || DEFAULT_CHECKOUT_URL
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
47
+ end
48
+ end
@@ -1,120 +1,140 @@
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, :currency, :items].freeze
9
- CAN_FALLBACK_KEYS = [:customerInfo].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
- 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
34
-
35
- {
36
- customerInfo: normalize_customer_info(payload.dig(:customerInfo) || payload.dig(:customer) || {}),
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) || {},
44
- reference: payload.dig(:reference),
45
- successUrl: payload.dig(:successURL),
46
- cancelUrl: payload.dig(:cancelURL),
47
- test: payload.dig(:test) || false
48
- }
49
- end
50
-
51
- def normalize_customer_info(info)
52
- return if info.nil?
53
- customer = info.transform_keys(&:to_sym)
54
- {
55
- accountAge: customer.dig(:accountAge),
56
- emailAddress: customer.dig(:emailAddress) || customer.dig(:email),
57
- firstName: customer.dig(:firstName),
58
- lastName: customer.dig(:lastName),
59
- firstNameKana: customer.dig(:firstNameKana),
60
- lastNameKana: customer.dig(:lastNameKana),
61
- address: customer.dig(:address),
62
- phoneNumber: customer.dig(:phoneNumber) || customer.dig(:phone),
63
- dateOfBirth: customer.dig(:dateOfBirth),
64
- legalGender: customer.dig(:legalGender) || customer.dig(:gender),
65
- reference: customer.dig(:reference)
66
- }
67
- end
68
-
69
- def normalize_shipping(shipping)
70
- return if shipping.nil?
71
- shipping= shipping.transform_keys(&:to_sym)
72
- {
73
- address: shipping.dig(:address) || {
74
- line1: shipping.dig(:line1),
75
- line2: shipping.dig(:line2),
76
- line3: shipping.dig(:line3),
77
- line4: shipping.dig(:line4),
78
- line5: shipping.dig(:line5),
79
- subLocality: shipping.dig(:subLocality),
80
- locality: shipping.dig(:locality),
81
- administrativeArea: shipping.dig(:administrativeArea),
82
- postalCode: shipping.dig(:postalCode),
83
- country: shipping.dig(:country),
84
- },
85
- addressType: shipping.dig(:addressType),
86
- feeAmount: shipping.dig(:feeAmount),
87
- feeCurrency: shipping.dig(:feeCurrency),
88
- }
89
- end
90
-
91
- def normalize_items(data)
92
- return [] if data.nil?
93
-
94
- data.map do |item|
95
- line_item = item.transform_keys(&:to_sym)
96
- {
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),
109
- description: line_item.dig(:description),
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)
115
- }
116
- end
117
- end
118
- end
119
- end
120
- end
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, :customer, :currency, :items].freeze
9
+ CAN_FALLBACK_KEYS = [:customer].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
+ 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
34
+
35
+ total_amount = get_total_amount
36
+
37
+ {
38
+ customerInfo: normalize_customer_info(payload.dig(:customerInfo) || payload.dig(:customer) || {}),
39
+ amount: total_amount,
40
+ captureMethod: payload.dig(:captureMethod),
41
+ currency: payload.dig(:currency),
42
+ description: payload.dig(:description),
43
+ shippingInfo: shipping_info,
44
+ items: normalize_items(payload.dig(:items)),
45
+ metadata: payload.dig(:metadata) || {},
46
+ reference: payload.dig(:reference),
47
+ successUrl: payload.dig(:successUrl),
48
+ cancelUrl: payload.dig(:cancelUrl),
49
+ }
50
+ end
51
+
52
+ def normalize_customer_info(info)
53
+ return if info.nil?
54
+ customer = info.transform_keys(&:to_sym)
55
+ {
56
+ accountAge: customer.dig(:accountAge),
57
+ emailAddress: customer.dig(:emailAddress) || customer.dig(:email),
58
+ firstName: customer.dig(:firstName),
59
+ lastName: customer.dig(:lastName),
60
+ firstNameKana: customer.dig(:firstNameKana),
61
+ lastNameKana: customer.dig(:lastNameKana),
62
+ address: customer.dig(:address),
63
+ phoneNumber: customer.dig(:phoneNumber) || customer.dig(:phone),
64
+ dateOfBirth: customer.dig(:dateOfBirth),
65
+ legalGender: customer.dig(:legalGender) || customer.dig(:gender),
66
+ reference: customer.dig(:reference)
67
+ }
68
+ end
69
+
70
+ def normalize_shipping(shipping)
71
+ return if shipping.nil?
72
+ shipping= shipping.transform_keys(&:to_sym)
73
+ {
74
+ address: shipping.dig(:address) || {
75
+ line1: shipping.dig(:line1),
76
+ line2: shipping.dig(:line2),
77
+ line3: shipping.dig(:line3),
78
+ line4: shipping.dig(:line4),
79
+ line5: shipping.dig(:line5),
80
+ subLocality: shipping.dig(:subLocality),
81
+ locality: shipping.dig(:locality),
82
+ administrativeArea: shipping.dig(:administrativeArea),
83
+ postalCode: shipping.dig(:postalCode),
84
+ country: shipping.dig(:country),
85
+ },
86
+ addressType: shipping.dig(:addressType),
87
+ feeAmount: shipping.dig(:feeAmount),
88
+ feeCurrency: shipping.dig(:feeCurrency),
89
+ }
90
+ end
91
+
92
+ def normalize_items(data)
93
+ return [] if data.nil?
94
+
95
+ data.map do |item|
96
+ line_item = item.transform_keys(&:to_sym)
97
+ {
98
+ quantity: line_item.dig(:quantity),
99
+ label: line_item.dig(:label),
100
+ name: line_item.dig(:name),
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),
109
+ description: line_item.dig(:description),
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)
115
+ }
116
+ end
117
+ end
118
+
119
+
120
+ def get_total_amount
121
+ total_amount = payload.dig(:amount) || payload.dig('amount')
122
+
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)
134
+ end
135
+
136
+ total_amount
137
+ end
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Smartpay
4
+ module Requests
5
+ class Payment
6
+ attr_accessor :payload
7
+
8
+ REQUIREMENT_KEY_NAME = [:order, :amount, :currency].freeze
9
+
10
+ def initialize(raw_payload)
11
+ @payload = raw_payload.transform_keys(&:to_sym)
12
+ end
13
+
14
+ def as_hash
15
+ check_requirement!
16
+ normalize_payload
17
+ end
18
+
19
+ private
20
+
21
+ def check_requirement!
22
+ REQUIREMENT_KEY_NAME.each do |key_name|
23
+ raise Errors::InvalidRequestPayloadError, key_name unless payload.include?(key_name)
24
+ end
25
+ end
26
+
27
+ def normalize_payload
28
+ {
29
+ order: payload.dig(:order),
30
+ amount: payload.dig(:amount),
31
+ currency: payload.dig(:currency),
32
+ reference: payload.dig(:reference),
33
+ cancelRemainder: payload.dig(:cancel_remainder),
34
+ description: payload.dig(:description),
35
+ metadata: payload.dig(:metadata) || {},
36
+ }
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Smartpay
4
+ module Requests
5
+ class Refund
6
+ attr_accessor :payload
7
+
8
+ REQUIREMENT_KEY_NAME = [:payment, :amount, :currency].freeze
9
+
10
+ def initialize(raw_payload)
11
+ @payload = raw_payload.transform_keys(&:to_sym)
12
+ end
13
+
14
+ def as_hash
15
+ check_requirement!
16
+ normalize_payload
17
+ end
18
+
19
+ private
20
+
21
+ def check_requirement!
22
+ REQUIREMENT_KEY_NAME.each do |key_name|
23
+ raise Errors::InvalidRequestPayloadError, key_name unless payload.include?(key_name)
24
+ end
25
+ end
26
+
27
+ def normalize_payload
28
+ {
29
+ payment: payload.dig(:payment),
30
+ amount: payload.dig(:amount),
31
+ currency: payload.dig(:currency),
32
+ reason: payload.dig(:reason),
33
+ reference: payload.dig(:reference),
34
+ description: payload.dig(:description),
35
+ metadata: payload.dig(:metadata) || {},
36
+ }
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Smartpay
4
+ module Responses
5
+ class Base
6
+ attr_reader :response
7
+
8
+ def initialize(response)
9
+ @response = response
10
+ end
11
+
12
+ def as_hash
13
+ @response
14
+ end
15
+
16
+ def as_json
17
+ @response.to_json
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,40 +1,30 @@
1
- # frozen_string_literal: true
2
-
3
- module Smartpay
4
- module Responses
5
- class CheckoutSession
6
- attr_reader :response
7
-
8
- def initialize(response)
9
- @response = response
10
- end
11
-
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}"
19
- end
20
-
21
- def as_hash
22
- @response
23
- end
24
-
25
- def as_json
26
- @response.to_json
27
- end
28
-
29
- private
30
-
31
- def checkout_url
32
- Smartpay.configuration.checkout_url
33
- end
34
-
35
- def public_key
36
- Smartpay.configuration.public_key
37
- end
38
- end
39
- end
40
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Smartpay
4
+ module Responses
5
+ class CheckoutSession < Base
6
+
7
+ def redirect_url(options = {})
8
+ url = response[:url]
9
+
10
+ if options && options[:promotionCode]
11
+ qs = "?promotion-code=#{options[:promotionCode]}"
12
+
13
+ return "#{url}#{qs}"
14
+ end
15
+
16
+ url
17
+ end
18
+
19
+ private
20
+
21
+ def checkout_url
22
+ Smartpay.configuration.checkout_url
23
+ end
24
+
25
+ def public_key
26
+ Smartpay.configuration.public_key
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Smartpay
4
- VERSION = "0.2.18"
4
+ VERSION = "0.5.0"
5
5
  end
data/lib/smartpay.rb CHANGED
@@ -1,25 +1,31 @@
1
- # frozen_string_literal: true
2
-
3
- require "json"
4
-
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"
12
-
13
- module Smartpay
14
- class << self
15
- attr_accessor :configuration
16
-
17
- def configuration
18
- @configuration ||= Smartpay::Configuration.new
19
- end
20
-
21
- def configure
22
- yield(configuration)
23
- end
24
- end
25
- end
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
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/requests/payment"
12
+ require_relative "smartpay/requests/refund"
13
+ require_relative "smartpay/responses/base"
14
+ require_relative "smartpay/responses/checkout_session"
15
+
16
+ module Smartpay
17
+ REJECT_REQUEST_BY_CUSTOMER = 'requested_by_customer'
18
+ REJECT_FRAUDULENT = 'fraudulent'
19
+
20
+ class << self
21
+ attr_accessor :configuration
22
+
23
+ def configuration
24
+ @configuration ||= Smartpay::Configuration.new
25
+ end
26
+
27
+ def configure
28
+ yield(configuration)
29
+ end
30
+ end
31
+ 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.2.18
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Smartpay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-15 00:00:00.000000000 Z
11
+ date: 2022-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -66,6 +66,9 @@ files:
66
66
  - lib/smartpay/configuration.rb
67
67
  - lib/smartpay/errors/invalid_request_payload_error.rb
68
68
  - lib/smartpay/requests/checkout_session.rb
69
+ - lib/smartpay/requests/payment.rb
70
+ - lib/smartpay/requests/refund.rb
71
+ - lib/smartpay/responses/base.rb
69
72
  - lib/smartpay/responses/checkout_session.rb
70
73
  - lib/smartpay/version.rb
71
74
  homepage: https://smartpay.co
@@ -90,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
93
  - !ruby/object:Gem::Version
91
94
  version: '0'
92
95
  requirements: []
93
- rubygems_version: 3.0.9
96
+ rubygems_version: 3.3.7
94
97
  signing_key:
95
98
  specification_version: 4
96
99
  summary: The Smartpay Ruby SDK offers easy access to Smartpay API from applications