smartpay 0.3.0 → 0.4.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: 047abea76f72b96fe9f59b2f7e384ef8871d001ef60ae0e6064f767823f273ba
4
- data.tar.gz: bd164be4d8ae9747a3331f68192e65e82061bc0627ec4dcdf79389645c9c1828
3
+ metadata.gz: 794e3de526f66e64a6c05638932f2c3c8a3cb7bcea3708f8df266ad9f02e7d4a
4
+ data.tar.gz: f04b6f575b3be6e2320336a343fa9db842560654c5c7076d6b7bf07b2fcdab70
5
5
  SHA512:
6
- metadata.gz: 635fbaaf5b186283fcb438f34176aa925f013576098b8535d1e7e1a9e59431db1acb16a7a545d77434bc114465ff3675768c763e084a5aabd01dd7df4f47be2f
7
- data.tar.gz: 053a91da14d3fa5097b3ca70b5106878ab564680769c8ca8a9b70036c5915240980492dd5cecbe7b743c5f843db032311aec0e075b403893b68070e522451358
6
+ metadata.gz: 54c1bd5ae7e587b67409e4eb9eed4c50d36690f09958e9003f3b9e9aadf927c429a41dc8c7b9fb18581f5594653f15fa0b3271896486f1276b07c1dd022f8910
7
+ data.tar.gz: 7cefc78c6463a95317d3f87abfc3e9ccc83cf5aeac7f2efed30194fba42faa2069e8e35ce695720ed9a83cdb523cec27a494b0611d51cee90d92bd62ecefd0a9
data/Gemfile CHANGED
@@ -10,3 +10,5 @@ gem "rake", "~> 13.0"
10
10
  gem "rspec", "~> 3.0"
11
11
 
12
12
  gem "rubocop", "~> 1.7"
13
+
14
+ gem "thin"
@@ -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
@@ -8,6 +8,35 @@ module Smartpay
8
8
  Client.post("/checkout-sessions", Requests::CheckoutSession.new(payload).as_hash)
9
9
  )
10
10
  end
11
+
12
+ def get_orders(page_token: nil, max_results: nil, expand: '' )
13
+ Responses::Base.new(Client.get("/orders", { 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, { expand: expand }))
18
+ end
19
+
20
+ def create_payment(payload)
21
+ Responses::Base.new(
22
+ Client.post("/payments", Requests::Payment.new(payload).as_hash)
23
+ )
24
+ end
25
+
26
+ def capture(payload)
27
+ create_payment(payload)
28
+ end
29
+
30
+ def create_refund(payload)
31
+ Responses::Base.new(
32
+ Client.post("/refunds", Requests::Refund.new(payload).as_hash)
33
+ )
34
+ end
35
+
36
+ def refund(payload)
37
+ create_refund(payload)
38
+ end
39
+
11
40
  end
12
41
  end
13
42
  end
@@ -5,6 +5,14 @@ require "rest-client"
5
5
  module Smartpay
6
6
  class Client
7
7
  class << self
8
+ def get(path, payload = {})
9
+ request_payload = default_payload.merge(payload)
10
+ response = RestClient::Request.execute(method: :get, url: api_url(path),
11
+ headers: headers.merge(params: request_payload),
12
+ timeout: timeout)
13
+ JSON.parse(response.body, symbolize_names: true)
14
+ end
15
+
8
16
  def post(path, payload = {})
9
17
  request_payload = default_payload.merge(payload)
10
18
  response = RestClient::Request.execute(method: :post, url: api_url(path), headers: headers, timeout: timeout,
@@ -5,8 +5,8 @@ module Smartpay
5
5
  class CheckoutSession
6
6
  attr_accessor :payload
7
7
 
8
- REQUIREMENT_KEY_NAME = [:successUrl, :cancelUrl, :customerInfo, :currency, :items].freeze
9
- CAN_FALLBACK_KEYS = [:customerInfo].freeze
8
+ REQUIREMENT_KEY_NAME = [:successUrl, :cancelUrl, :customer, :currency, :items].freeze
9
+ CAN_FALLBACK_KEYS = [:customer].freeze
10
10
 
11
11
  def initialize(raw_payload)
12
12
  @payload = raw_payload.transform_keys(&:to_sym)
@@ -32,9 +32,11 @@ module Smartpay
32
32
  shipping_info[:feeCurrency] = payload.dig(:currency)
33
33
  end
34
34
 
35
+ total_amount = get_total_amount
36
+
35
37
  {
36
38
  customerInfo: normalize_customer_info(payload.dig(:customerInfo) || payload.dig(:customer) || {}),
37
- amount: payload.dig(:amount),
39
+ amount: total_amount,
38
40
  captureMethod: payload.dig(:captureMethod),
39
41
  currency: payload.dig(:currency),
40
42
  description: payload.dig(:description),
@@ -42,9 +44,8 @@ module Smartpay
42
44
  items: normalize_items(payload.dig(:items)),
43
45
  metadata: payload.dig(:metadata) || {},
44
46
  reference: payload.dig(:reference),
45
- successUrl: payload.dig(:successURL),
46
- cancelUrl: payload.dig(:cancelURL),
47
- test: payload.dig(:test) || false
47
+ successUrl: payload.dig(:successUrl),
48
+ cancelUrl: payload.dig(:cancelUrl),
48
49
  }
49
50
  end
50
51
 
@@ -97,7 +98,6 @@ module Smartpay
97
98
  quantity: line_item.dig(:quantity),
98
99
  label: line_item.dig(:label),
99
100
  name: line_item.dig(:name),
100
- description: line_item.dig(:description),
101
101
  amount: line_item.dig(:amount),
102
102
  currency: line_item.dig(:currency),
103
103
  brand: line_item.dig(:brand),
@@ -115,6 +115,26 @@ module Smartpay
115
115
  }
116
116
  end
117
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
118
138
  end
119
139
  end
120
140
  end
@@ -0,0 +1,39 @@
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
+ description: payload.dig(:description),
34
+ metadata: payload.dig(:metadata) || {},
35
+ }
36
+ end
37
+ end
38
+ end
39
+ 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
@@ -2,28 +2,18 @@
2
2
 
3
3
  module Smartpay
4
4
  module Responses
5
- class CheckoutSession
6
- attr_reader :response
7
-
8
- def initialize(response)
9
- @response = response
10
- end
5
+ class CheckoutSession < Base
11
6
 
12
7
  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
8
+ url = response[:url]
20
9
 
21
- def as_hash
22
- @response
23
- end
10
+ if options && options[:promotionCode]
11
+ qs = "?promotion-code=#{options[:promotionCode]}"
24
12
 
25
- def as_json
26
- @response.to_json
13
+ return "#{url}#{qs}"
14
+ end
15
+
16
+ url
27
17
  end
28
18
 
29
19
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Smartpay
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/smartpay.rb CHANGED
@@ -8,9 +8,15 @@ require_relative "smartpay/client"
8
8
  require_relative "smartpay/api"
9
9
  require_relative "smartpay/errors/invalid_request_payload_error"
10
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"
11
14
  require_relative "smartpay/responses/checkout_session"
12
15
 
13
16
  module Smartpay
17
+ REJECT_REQUEST_BY_CUSTOMER = 'requested_by_customer'
18
+ REJECT_FRAUDULENT = 'fraudulent'
19
+
14
20
  class << self
15
21
  attr_accessor :configuration
16
22
 
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.3.0
4
+ version: 0.4.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-17 00:00:00.000000000 Z
11
+ date: 2022-03-10 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