paypalrb 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7ae1273de94e65fd45aa8038f0776052d5658f2acdd8828e9230da2ab3a01ac5
4
+ data.tar.gz: 28b3602778a92e0ca0accb1951a5be82cfce028a51c9bba6a53a8f5c4be48787
5
+ SHA512:
6
+ metadata.gz: 60d1ee06802fb3accd5bc8a1eb065b22188901f27cb1657fb4ab072ae76512b1466f6af4a7e26ce50e92a8ae4de114ccf3aaa961e6c9713e97106e2bf1f8eca8
7
+ data.tar.gz: e6a4858704fed4e564d78f13022d0dddd990987125e54e748a8518c421545771f80c1be210a815a80de43aa7e011882049e7681fa232cbaac8eabeb0f656cf59
data/.env.example ADDED
@@ -0,0 +1,3 @@
1
+ CLIENT_ID=
2
+ CLIENT_SECRET=
3
+ ACCESS_TOKEN=
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in youtuberb.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "dotenv"
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ paypalrb (0.1.1)
5
+ faraday (~> 2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ dotenv (2.7.6)
11
+ faraday (2.5.2)
12
+ faraday-net_http (>= 2.0, < 3.1)
13
+ ruby2_keywords (>= 0.0.4)
14
+ faraday-net_http (3.0.0)
15
+ rake (13.0.6)
16
+ ruby2_keywords (0.0.5)
17
+
18
+ PLATFORMS
19
+ x86_64-linux
20
+
21
+ DEPENDENCIES
22
+ dotenv
23
+ paypalrb!
24
+ rake (~> 13.0)
25
+
26
+ BUNDLED WITH
27
+ 2.3.5
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Dean Perry
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # PayPalRB
2
+
3
+ **This library is a work in progress**
4
+
5
+ PayPalRB is a Ruby library for interacting with the PayPal API.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'paypalrb'
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Generate an Access Token
18
+
19
+ Firstly you'll need to generate an Access Token. Set the Client ID and Secret
20
+ An Access Token will be an OAuth2 token generated after authentication.
21
+
22
+
23
+ ```ruby
24
+ @authentication = PayPal::Authentication.new(client_id: "", client_secret: "")
25
+
26
+ @authentication.get_token
27
+ # => #<PayPal::AccessToken access_token="abc123", expires_in=123
28
+ ```
29
+
30
+ Then once you have an access token, set it like so:
31
+
32
+ ```ruby
33
+ @client = PayPal::Client.new(access_token: "abc123")
34
+ ```
35
+
36
+ ### Products
37
+
38
+ ```ruby
39
+ # Retrieve a list of products
40
+ @client.products.list
41
+
42
+ # Retrieve a product by its ID
43
+ @client.products.retrieve id: "123"
44
+
45
+ # Create a product
46
+ # Type should be physical, digital or service
47
+ # Docs: https://developer.paypal.com/docs/api/catalog-products/v1/#products_create
48
+ @client.products.create name: "My Product", type: ""
49
+ ```
50
+
51
+ ### Orders
52
+
53
+ ```ruby
54
+ # Retrieves an Order
55
+ @client.orders.retrieve id: "abc123"
56
+
57
+ # Creates an Order
58
+ # Intent should be either capture or authorize
59
+ # Items is an array of purchase units
60
+ # Docs: https://developer.paypal.com/docs/api/orders/v2/#orders_create
61
+ @client.orders.create intent: "capture", items: []
62
+
63
+ # As above but creates an order for the total of value given
64
+ @client.orders.create_payment intent: "capture", description: "A Payment",
65
+ currency: "GBP", value: "25.00"
66
+
67
+ # As above but creates an order for a single item
68
+ @client.orders.create_single intent: "capture", title: "Item Title",
69
+ description: "Item Description", currency: "GBP", value: "25.00"
70
+
71
+ # Authorizes payment for an order. The buyer must first approve the order.
72
+ @client.orders.authorize id: "123abc"
73
+
74
+ # Captures payment for an order. The buyer must first approve the order.
75
+ @client.orders.capture id: "123abc"
76
+ ```
77
+
78
+ ## Contributing
79
+
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/deanpcmad/paypalrb.
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/bin/console ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "pay_pal"
6
+
7
+ # Load environment variables from .env file
8
+ require 'dotenv/load'
9
+
10
+ # You can add fixtures and/or initialization code here to make experimenting
11
+ # with your gem easier. You can also use a different console, if you like.
12
+
13
+ # (If you use this, don't forget to add pry to your Gemfile!)
14
+ # require "pry"
15
+ # Pry.start
16
+
17
+ @authentication = PayPal::Authentication.new(client_id: ENV["CLIENT_ID"], client_secret: ENV["CLIENT_SECRET"])
18
+
19
+ @client = PayPal::Client.new(access_token: ENV["ACCESS_TOKEN"])
20
+
21
+ require "irb"
22
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ module PayPal
2
+ class Authentication
3
+
4
+ attr_reader :client_id, :client_secret, :sandbox
5
+
6
+ def initialize(client_id:, client_secret:, sandbox: true, adapter: Faraday.default_adapter)
7
+ @client_id = client_id
8
+ @client_secret = client_secret
9
+ @sandbox = sandbox
10
+ @adapter = adapter
11
+ end
12
+
13
+ def get_token
14
+ if @sandbox
15
+ url = "https://api-m.sandbox.paypal.com"
16
+ else
17
+ url = "https://api-m.paypal.com"
18
+ end
19
+
20
+ request_helper = Faraday.new(url: url) do |conn|
21
+ conn.request :authorization, :basic, client_id, client_secret
22
+ end
23
+
24
+ response = request_helper.post "/v1/oauth2/token?grant_type=client_credentials"
25
+
26
+ body = JSON.parse(response.body)
27
+ AccessToken.new(body)
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,36 @@
1
+ module PayPal
2
+ class Client
3
+ attr_reader :access_token, :sandbox, :adapter
4
+
5
+ def initialize(access_token:, sandbox: true, adapter: Faraday.default_adapter, stubs: nil)
6
+ @access_token = access_token
7
+ @sandbox = sandbox
8
+ @adapter = adapter
9
+
10
+ # Test stubs for requests
11
+ @stubs = stubs
12
+ end
13
+
14
+ def products
15
+ ProductsResource.new(self)
16
+ end
17
+
18
+ def orders
19
+ OrdersResource.new(self)
20
+ end
21
+
22
+ def connection
23
+ url = @sandbox ? "https://api-m.sandbox.paypal.com" : "https://api-m.paypal.com"
24
+
25
+ @connection ||= Faraday.new(url) do |conn|
26
+ conn.request :authorization, :Bearer, access_token
27
+ conn.request :json
28
+
29
+ conn.response :json
30
+
31
+ conn.adapter adapter, @stubs
32
+ end
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,24 @@
1
+ module PayPal
2
+ class Collection
3
+ attr_reader :data, :total, :next_page_token, :prev_page_token
4
+
5
+ def self.from_response(response, kind:, type:)
6
+ body = response.body
7
+
8
+ new(
9
+ data: body[kind].map { |attrs| type.new(attrs) },
10
+ total: body["pageInfo"],
11
+ next_page_token: body["nextPageToken"],
12
+ prev_page_token: body["prevPageToken"],
13
+ # cursor: body.dig("pagination", "cursor")
14
+ )
15
+ end
16
+
17
+ def initialize(data:, total:, next_page_token:, prev_page_token:)
18
+ @data = data
19
+ @total = total
20
+ @next_page_token = next_page_token
21
+ @prev_page_token = prev_page_token
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ module PayPal
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,19 @@
1
+ require "ostruct"
2
+
3
+ module PayPal
4
+ class Object < OpenStruct
5
+ def initialize(attributes)
6
+ super to_ostruct(attributes)
7
+ end
8
+
9
+ def to_ostruct(obj)
10
+ if obj.is_a?(Hash)
11
+ OpenStruct.new(obj.map { |key, val| [key, to_ostruct(val)] }.to_h)
12
+ elsif obj.is_a?(Array)
13
+ obj.map { |o| to_ostruct(o) }
14
+ else # Assumed to be a primitive value
15
+ obj
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ module PayPal
2
+ class AccessToken < Object
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ module PayPal
2
+ class Order < Object
3
+
4
+ # def initialize(options = {})
5
+ # options.delete "links"
6
+
7
+ # super options
8
+
9
+ # self.url = "https://www.sandbox.paypal.com/checkoutnow?token=#{options['id']}"
10
+ # end
11
+
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module PayPal
2
+ class Product < Object
3
+
4
+ def initialize(options = {})
5
+ options.delete "links"
6
+
7
+ super options
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,56 @@
1
+ module PayPal
2
+ class Resource
3
+ attr_reader :client
4
+
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ private
10
+
11
+ def get_request(url, params: {}, headers: {})
12
+ handle_response client.connection.get(url, params, headers)
13
+ end
14
+
15
+ def post_request(url, body:, headers: {})
16
+ handle_response client.connection.post(url, body, headers)
17
+ end
18
+
19
+ def patch_request(url, body:, headers: {})
20
+ handle_response client.connection.patch(url, body, headers)
21
+ end
22
+
23
+ def put_request(url, body:, headers: {})
24
+ handle_response client.connection.put(url, body, headers)
25
+ end
26
+
27
+ def delete_request(url, params: {}, headers: {})
28
+ handle_response client.connection.delete(url, params, headers)
29
+ end
30
+
31
+ def handle_response(response)
32
+ case response.status
33
+ when 400
34
+ raise Error, "Error 400: Your request was malformed. '#{response.body}'"
35
+ when 401
36
+ raise Error, "Error 401: You did not supply valid authentication credentials. '#{response.body}'"
37
+ when 403
38
+ raise Error, "Error 403: You are not allowed to perform that action. '#{response.body}'"
39
+ when 404
40
+ raise Error, "Error 404: No results were found for your request. '#{response.body}'"
41
+ when 409
42
+ raise Error, "Error 409: Your request was a conflict. '#{response.body}'"
43
+ when 422
44
+ raise Error, "Error 422: Unprocessable Entity. '#{response.body}'"
45
+ when 429
46
+ raise Error, "Error 429: Your request exceeded the API rate limit. '#{response.body}'"
47
+ when 500
48
+ raise Error, "Error 500: We were unable to perform the request due to server-side problems. '#{response.body}'"
49
+ when 503
50
+ raise Error, "Error 503: You have been rate limited for sending more than 20 requests per second. '#{response.body}'"
51
+ end
52
+
53
+ response
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,65 @@
1
+ module PayPal
2
+ class OrdersResource < Resource
3
+
4
+ def retrieve(id:)
5
+ response = get_request("v2/checkout/orders/#{id}")
6
+ Order.new(response.body)
7
+ end
8
+
9
+ def create(intent:, units:, **params)
10
+ attributes = {intent: intent.upcase, purchase_units: units}
11
+ Order.new post_request("v2/checkout/orders",
12
+ body: attributes.merge(params),
13
+ headers: {"Prefer" => "return=representation"}
14
+ ).body
15
+ end
16
+
17
+ def create_payment(intent:, description:, currency:, value:, **params)
18
+ items = [{
19
+ description: description,
20
+ amount: {
21
+ currency_code: currency, value: value
22
+ }
23
+ }]
24
+
25
+ attributes = {intent: intent.upcase, purchase_units: items}
26
+ Order.new post_request("v2/checkout/orders",
27
+ body: attributes.merge(params),
28
+ headers: {"Prefer" => "return=representation"}
29
+ ).body
30
+ end
31
+
32
+ def create_single(intent:, title:, description:, currency:, value:, **params)
33
+ items = [{
34
+ items: [
35
+ {name: title, description: description, quantity: 1, unit_amount: {currency_code: currency, value: value}}
36
+ ],
37
+ amount: {
38
+ currency_code: currency, value: value,
39
+ breakdown: {
40
+ item_total: {
41
+ currency_code: currency, value: value
42
+ }
43
+ }
44
+ }
45
+ }]
46
+
47
+ attributes = {intent: intent.upcase, purchase_units: items}
48
+ Order.new post_request("v2/checkout/orders",
49
+ body: attributes.merge(params),
50
+ headers: {"Prefer" => "return=representation"}
51
+ ).body
52
+ end
53
+
54
+ def authorize(id:)
55
+ response = post_request("v2/checkout/orders/#{id}/authorize", body: {})
56
+ Order.new(response.body)
57
+ end
58
+
59
+ def capture(id:)
60
+ response = post_request("v2/checkout/orders/#{id}/capture", body: {})
61
+ Order.new(response.body)
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,20 @@
1
+ module PayPal
2
+ class ProductsResource < Resource
3
+
4
+ def list
5
+ response = get_request("v1/catalogs/products")
6
+ Collection.from_response(response, kind: "products", type: Product)
7
+ end
8
+
9
+ def retrieve(id:)
10
+ response = get_request("v1/catalogs/products/#{id}")
11
+ Product.new(response.body)
12
+ end
13
+
14
+ def create(name:, type:, **params)
15
+ attributes = {name: name, type: type}
16
+ Product.new post_request("v1/catalogs/products", body: attributes.merge(params)).body
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module PayPal
2
+ VERSION = "0.1.1"
3
+ end
data/lib/pay_pal.rb ADDED
@@ -0,0 +1,23 @@
1
+ require "faraday"
2
+ require "json"
3
+
4
+ require_relative "pay_pal/version"
5
+
6
+ module PayPal
7
+
8
+ autoload :Client, "pay_pal/client"
9
+ autoload :Collection, "pay_pal/collection"
10
+ autoload :Error, "pay_pal/error"
11
+ autoload :Resource, "pay_pal/resource"
12
+ autoload :Object, "pay_pal/object"
13
+
14
+ autoload :Authentication, "pay_pal/authentication"
15
+
16
+ autoload :ProductsResource, "pay_pal/resources/products"
17
+ autoload :OrdersResource, "pay_pal/resources/orders"
18
+
19
+ autoload :AccessToken, "pay_pal/objects/access_token"
20
+ autoload :Product, "pay_pal/objects/product"
21
+ autoload :Order, "pay_pal/objects/order"
22
+
23
+ end
data/lib/paypalrb.rb ADDED
@@ -0,0 +1 @@
1
+ require "pay_pal"
data/paypalrb.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/pay_pal/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "paypalrb"
7
+ spec.version = PayPal::VERSION
8
+ spec.authors = ["Dean Perry"]
9
+ spec.email = ["dean@deanpcmad.com"]
10
+
11
+ spec.summary = "A Ruby library for interacting with the PayPal API"
12
+ spec.homepage = "https://deanpcmad.com"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/deanpcmad/paypalrb"
18
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "faraday", "~> 2.0"
32
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paypalrb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Dean Perry
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-10-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description:
28
+ email:
29
+ - dean@deanpcmad.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".env.example"
35
+ - Gemfile
36
+ - Gemfile.lock
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - bin/console
41
+ - bin/setup
42
+ - lib/pay_pal.rb
43
+ - lib/pay_pal/authentication.rb
44
+ - lib/pay_pal/client.rb
45
+ - lib/pay_pal/collection.rb
46
+ - lib/pay_pal/error.rb
47
+ - lib/pay_pal/object.rb
48
+ - lib/pay_pal/objects/access_token.rb
49
+ - lib/pay_pal/objects/order.rb
50
+ - lib/pay_pal/objects/product.rb
51
+ - lib/pay_pal/resource.rb
52
+ - lib/pay_pal/resources/orders.rb
53
+ - lib/pay_pal/resources/products.rb
54
+ - lib/pay_pal/version.rb
55
+ - lib/paypalrb.rb
56
+ - paypalrb.gemspec
57
+ homepage: https://deanpcmad.com
58
+ licenses:
59
+ - MIT
60
+ metadata:
61
+ homepage_uri: https://deanpcmad.com
62
+ source_code_uri: https://github.com/deanpcmad/paypalrb
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 2.6.0
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.3.7
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: A Ruby library for interacting with the PayPal API
82
+ test_files: []