merchant-paypal 0.0.2 → 0.0.3

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: fafb219995ad1ea99a7cfdc2fd98c150f95e9d465f49af48dae413f53ae28db7
4
- data.tar.gz: c77faaac7ab11fca83ca653a9cf18248a3ac1d30266de1e83e1412d9028dbf42
3
+ metadata.gz: af420e90d6c2f33c1d7125d91288ad1fb19f7f047e7465c427e335f001f56b49
4
+ data.tar.gz: 3a58efbc723b57db059b1e23ef876e8d10737ce90cb5c5b41f27ef3c36254c44
5
5
  SHA512:
6
- metadata.gz: 229e92148ecafa0ceb15c6d1ea1a21eab0c932216bc369e75ff801912bf52a801a12623cc824e52dd8105bc6a4e3b046b59b9529d22be2e234b45f0c49daf092
7
- data.tar.gz: cb3fd490ece01f155a0614c7dcdb75c8bdbf7dbbe30e281ea2a2e7e1e263a3cd4e996becbc325d32e5c19dfd90f2c1a7db4bc7435bb189d93fbafa74d3891c79
6
+ metadata.gz: 282a10d292486eb105ce49246e0cbcda2ebe7b8d4b60bbdb1f501971ab846e34623185aea780522db46071115253778891776d127205aa3a3ef851f9b573be61
7
+ data.tar.gz: 60ac8fb74b0c640d4fd8445f64d41fde0088c2fc2e35d9296d8596a00c461047754ef898f1ba9d32bbcad6cd4bc5eeabe70bb8e7897f1fd04db63eeca0da57e5
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 DevHub
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/bin/console ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "irb"
4
+ require_relative "../lib/merchant-paypal"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ IRB.start(__FILE__)
@@ -0,0 +1,9 @@
1
+ module MerchantPaypal
2
+ class ApiResource
3
+ include MerchantPaypal::Operations::Request
4
+
5
+ def self.class_name
6
+ self.name.split('::')[-1]
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,109 @@
1
+ require "forwardable"
2
+
3
+ module MerchantPaypal
4
+ class Client
5
+ extend Forwardable
6
+
7
+ DEFAULT_API_MODE = :live
8
+ API_MODES = [:live, :sandbox]
9
+ DEFAULT_API_ENDPOINT = {
10
+ sandbox: "https://api-m.sandbox.paypal.com",
11
+ live: "https://api-m.paypal.com"
12
+ }
13
+
14
+ attr_reader :config
15
+
16
+ def_delegators :@config, :client_id, :client_id=
17
+ def_delegators :@config, :client_secret, :client_secret=
18
+ def_delegators :@config, :mode, :mode=
19
+ def_delegators :@config, :access_token, :access_token=
20
+
21
+ def initialize(config = {})
22
+ @config = case config
23
+ when Hash
24
+ MerchantPaypal.config.reverse_duplicate_merge(config)
25
+ when MerchantPaypal::Configuration
26
+ config
27
+ else
28
+ MerchantPaypal.config.dup
29
+ end
30
+ end
31
+
32
+ def api_mode
33
+ if API_MODES.include?(mode&.to_sym)
34
+ mode.to_sym
35
+ else
36
+ DEFAULT_API_MODE
37
+ end
38
+ end
39
+
40
+ def api_base
41
+ DEFAULT_API_ENDPOINT[api_mode]
42
+ end
43
+
44
+ def execute_request(method, url, params = {}, opts = {})
45
+ retries = 2
46
+
47
+ begin
48
+ request_opts = opts
49
+ headers = {
50
+ "Content-Type" => "application/json",
51
+ "Authorization" => "Bearer #{access_token}",
52
+ "Prefer" => "return=minimal",
53
+ }
54
+
55
+ headers.merge!({ "PayPal-Request-Id" => opts[:request_id] }) if opts[:request_id].present?
56
+
57
+ request_url = api_base + url.to_s
58
+
59
+ case method.to_s.downcase.to_sym
60
+ when :get, :head, :delete
61
+ # Make params into GET parameters
62
+ request_url += "#{URI.parse(url).query ? '&' : '?'}#{Util.encode_parameters(params)}" if params && params.any?
63
+ payload = nil
64
+ else
65
+ payload = params
66
+ end
67
+
68
+ payload = payload.to_json if payload.is_a?(Hash)
69
+
70
+ request_opts.update(method: method,
71
+ headers: headers,
72
+ payload: payload,
73
+ url: request_url)
74
+
75
+ response = RestClient::Request.execute(request_opts)
76
+
77
+ JSON.parse(response.body, object_class: OpenStruct)
78
+ rescue RestClient::Unauthorized
79
+ if retries.positive?
80
+ retries -= 1
81
+ authenticate!
82
+ retry
83
+ end
84
+ raise
85
+ rescue => e
86
+ puts e.response
87
+ raise
88
+ end
89
+ end
90
+
91
+ def authenticate!
92
+ request_opts = {
93
+ method: :post,
94
+ url: api_base + "/v1/oauth2/token",
95
+ payload: { grant_type: "client_credentials" },
96
+ user: client_id,
97
+ password: client_secret,
98
+ }
99
+
100
+ response = RestClient::Request.execute(request_opts)
101
+ result = JSON.parse(response.body, object_class: OpenStruct)
102
+
103
+ MerchantPaypal.access_token = result.access_token
104
+ self.access_token = result.access_token
105
+
106
+ result
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,28 @@
1
+ module MerchantPaypal
2
+ class Configuration
3
+ attr_accessor :client_id
4
+ attr_accessor :client_secret
5
+ attr_accessor :mode
6
+ attr_accessor :access_token
7
+
8
+ def self.setup
9
+ new.tap do |instance|
10
+ yield(instance) if block_given?
11
+ end
12
+ end
13
+
14
+ # Create a new config based off an existing one. This is useful when the
15
+ # caller wants to override the global configuration
16
+ def reverse_duplicate_merge(hash)
17
+ dup.tap do |instance|
18
+ hash.each do |option, value|
19
+ instance.public_send("#{option}=", value)
20
+ end
21
+ end
22
+ end
23
+
24
+ def initialize
25
+ @mode = :live
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ module MerchantPaypal
2
+ module Operations
3
+ module Create
4
+ def create(params = {}, opts = {})
5
+ request(:post, resource_url, params, opts)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module MerchantPaypal
2
+ module Operations
3
+ module Get
4
+ def get(id, params = {})
5
+ request(:get, "#{resource_url}/#{CGI.escape(id)}", params)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ module MerchantPaypal
2
+ module Operations
3
+ module Request
4
+ module ClassMethods
5
+ def request(method, url, params = {}, opts = {})
6
+ MerchantPaypal::Client.new.execute_request(method, url, params, opts)
7
+ end
8
+ end
9
+
10
+ def self.included(base)
11
+ base.extend(ClassMethods)
12
+ end
13
+
14
+ protected
15
+
16
+ def request(method, url, params = {}, opts = {})
17
+ opts = @opts.merge(opts)
18
+ self.class.request(method, url, params, opts)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,48 @@
1
+ module MerchantPaypal
2
+ class Order < ApiResource
3
+ extend MerchantPaypal::Operations::Get
4
+
5
+ def self.resource_url
6
+ "/v2/checkout/#{CGI.escape(class_name.tableize)}"
7
+ end
8
+
9
+ def self.create(params = {}, opts = {})
10
+ payload = {
11
+ intent: "CAPTURE",
12
+ processing_instruction: "NO_INSTRUCTION",
13
+ purchase_units: [{
14
+ amount: {
15
+ currency_code: params[:currency] || "AUD",
16
+ value: params[:amount],
17
+ }
18
+ }],
19
+ application_context: {
20
+ landing_page: "NO_PREFERENCE",
21
+ user_action: "PAY_NOW",
22
+ return_url: params[:return_url],
23
+ cancel_url: params[:cancel_url],
24
+ }
25
+ }
26
+
27
+ request(:post, resource_url, payload, opts)
28
+ end
29
+
30
+ def self.create_for_redirection_url(params = {}, opts = {})
31
+ response = self.create(params, opts)
32
+ response.links.find { |link| link.rel == "approve"}&.href
33
+ end
34
+
35
+ def self.capture(id, opts = {})
36
+ payload = {
37
+ payment_source: {
38
+ token: {
39
+ id: id,
40
+ type: "BILLING_AGREEMENT",
41
+ }
42
+ }
43
+ }
44
+
45
+ request(:post, "#{resource_url}/#{CGI.escape(id)}/capture", payload, opts)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,57 @@
1
+ module MerchantPaypal
2
+ module Util
3
+ # Encodes a hash of parameters in a way that's suitable for use as query
4
+ # parameters in a URI or as form parameters in a request body. This mainly
5
+ # involves escaping special characters from parameter keys and values (e.g.
6
+ # `&`).
7
+ def self.encode_parameters(params)
8
+ Util.flatten_params(params).
9
+ map { |k,v| "#{url_encode(k)}=#{url_encode(v)}" }.join('&')
10
+ end
11
+
12
+ # Encodes a string in a way that makes it suitable for use in a set of
13
+ # query parameters in a URI or in a set of form parameters in a request
14
+ # body.
15
+ def self.url_encode(key)
16
+ CGI.escape(key.to_s).
17
+ # Don't use strict form encoding by changing the square bracket control
18
+ # characters back to their literals. This is fine by the server, and
19
+ # makes these parameter strings easier to read.
20
+ gsub('%5B', '[').gsub('%5D', ']')
21
+ end
22
+
23
+ def self.flatten_params(params, parent_key=nil)
24
+ result = []
25
+
26
+ # do not sort the final output because arrays (and arrays of hashes
27
+ # especially) can be order sensitive, but do sort incoming parameters
28
+ params.each do |key, value|
29
+ calculated_key = parent_key ? "#{parent_key}[#{key}]" : "#{key}"
30
+ if value.is_a?(Hash)
31
+ result += flatten_params(value, calculated_key)
32
+ elsif value.is_a?(Array)
33
+ check_array_of_maps_start_keys!(value)
34
+ result += flatten_params_array(value, calculated_key)
35
+ else
36
+ result << [calculated_key, value]
37
+ end
38
+ end
39
+
40
+ result
41
+ end
42
+
43
+ def self.flatten_params_array(value, calculated_key)
44
+ result = []
45
+ value.each do |elem|
46
+ if elem.is_a?(Hash)
47
+ result += flatten_params(elem, "#{calculated_key}[]")
48
+ elsif elem.is_a?(Array)
49
+ result += flatten_params_array(elem, calculated_key)
50
+ else
51
+ result << ["#{calculated_key}[]", elem]
52
+ end
53
+ end
54
+ result
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module MerchantPaypal
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,38 @@
1
+ require "forwardable"
2
+
3
+ require "merchant-paypal/operations/request"
4
+ require "merchant-paypal/operations/create"
5
+ require "merchant-paypal/operations/get"
6
+ require "merchant-paypal/api_resource"
7
+ require "merchant-paypal/configuration"
8
+ require "merchant-paypal/version"
9
+ require "merchant-paypal/util"
10
+
11
+ module MerchantPaypal
12
+ autoload :Client, "merchant-paypal/client"
13
+ autoload :Authentication, "merchant-paypal/authentication"
14
+ autoload :Order, "merchant-paypal/order"
15
+
16
+ @config = Configuration.setup
17
+
18
+ class << self
19
+ extend Forwardable
20
+
21
+ attr_reader :config
22
+
23
+ # User configurable options
24
+ def_delegators :@config, :client_id, :client_id=
25
+ def_delegators :@config, :client_secret, :client_secret=
26
+ def_delegators :@config, :mode, :mode=
27
+ def_delegators :@config, :access_token, :access_token=
28
+
29
+ def configure(client_id: nil, client_secret: nil, mode: :live)
30
+ @config = Configuration.setup.reverse_duplicate_merge(
31
+ client_id: client_id,
32
+ client_secret: client_secret,
33
+ mode: mode,
34
+ access_token: nil,
35
+ )
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,29 @@
1
+ $LOAD_PATH.unshift(::File.join(::File.dirname(__FILE__), "lib"))
2
+
3
+ require "merchant-paypal/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "merchant-paypal"
7
+ s.version = MerchantPaypal::VERSION
8
+ s.authors = ["Devhub"]
9
+ s.email = ["info@devhub.co"]
10
+ s.summary = "Ruby Library for Paypal API"
11
+ s.description = "Merchant Paypal provides Ruby APIs dealing with Palpal transactions."
12
+ s.homepage = "https://github.com/DevHubCo/merchant-paypal"
13
+ s.license = 'MIT'
14
+
15
+ ignored = Regexp.union(
16
+ /\A\.editorconfig/,
17
+ /\A\.git/,
18
+ /\A\.rubocop/,
19
+ /\A\.travis.yml/,
20
+ /\A\.vscode/,
21
+ /\Atest/
22
+ )
23
+ s.files = `git ls-files`.split("\n").reject { |f| ignored.match(f) }
24
+ s.executables = `git ls-files -- bin/*`.split("\n")
25
+ .map { |f| ::File.basename(f) }
26
+ s.require_paths = ["lib"]
27
+
28
+ s.add_dependency 'rest-client', '~> 2.0', '>= 2.0.0'
29
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merchant-paypal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Devhub
@@ -33,12 +33,27 @@ dependencies:
33
33
  description: Merchant Paypal provides Ruby APIs dealing with Palpal transactions.
34
34
  email:
35
35
  - info@devhub.co
36
- executables: []
36
+ executables:
37
+ - console
37
38
  extensions: []
38
39
  extra_rdoc_files: []
39
- files: []
40
+ files:
41
+ - LICENSE
42
+ - bin/console
43
+ - lib/merchant-paypal.rb
44
+ - lib/merchant-paypal/api_resource.rb
45
+ - lib/merchant-paypal/client.rb
46
+ - lib/merchant-paypal/configuration.rb
47
+ - lib/merchant-paypal/operations/create.rb
48
+ - lib/merchant-paypal/operations/get.rb
49
+ - lib/merchant-paypal/operations/request.rb
50
+ - lib/merchant-paypal/order.rb
51
+ - lib/merchant-paypal/util.rb
52
+ - lib/merchant-paypal/version.rb
53
+ - merchant-paypal.gemspec
40
54
  homepage: https://github.com/DevHubCo/merchant-paypal
41
- licenses: []
55
+ licenses:
56
+ - MIT
42
57
  metadata: {}
43
58
  post_install_message:
44
59
  rdoc_options: []