square_rails 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 02dc33d2fc3eb2eb1a7f5650c05f51a81e341f8f
4
+ data.tar.gz: 421bb48c8b230e7353658defccb56ed430308a92
5
+ SHA512:
6
+ metadata.gz: f7bb3f323d6c6e0d1662a8da40074c3ac4030281ea10d2d22a1fd5da596bdb9dae932cfc867ead3ea1382086f76780db728448c49f528d03d1d4412d73009935
7
+ data.tar.gz: 3cf8a335a2ba47836dce4b0d5f6e454384b0296d3cf13b9b9349afbe38b385a860c934566c09201c587115cf75861abda0ed379c995de559bb9ab3dfd1fa299e
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in square_rails.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Quinn McLaughlin
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.
@@ -0,0 +1,96 @@
1
+ # SquareRails
2
+
3
+ An easy way to work with Square's Connect API from Ruby on Rails.
4
+
5
+ ## Demonstration:
6
+ ### Access a Square Merchant's Account, Locations and Payments
7
+ In four lines of code. After config :)
8
+
9
+ #### 1. Install the square_rails gem
10
+
11
+ Add this line to your application's Gemfile (or create a new blank rails app, then add it):
12
+
13
+ ```ruby
14
+ gem 'square_rails', :git => 'git://github.com/qmclaugh/square_rails.git'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ #### 2. Enter your Square Connect application credentials
22
+
23
+ Execute the following to copy `\config\initalizers\square_rails_init.rb` into your application:
24
+
25
+ $ rake square_rails:init
26
+
27
+ Open `\config\intializers\square_rails_init.rb` in your Rails application and **enter your Square Application ID and Application Secret**. If you don't have these, go to [https://developers.squareup.com/](https://developers.squareup.com/), sign up for a Square account and then go to [Manage Apps](https://developers.squareup.com/apps). Open or create a new Square Connect app there and you'll see both.
28
+
29
+ #### 3. Tell Square Connect where your app is running
30
+
31
+ Continue from your application's page (via the [Manage Apps](https://developers.squareup.com/apps) page). On your application's page there is a field named **Redirect URL**. For now, enter
32
+
33
+ http://localhost:3000/square_callback
34
+
35
+ in that field.
36
+
37
+ #### 4. Connect to a merchant's Square account
38
+
39
+ Start your rails server
40
+
41
+ $ rails server
42
+
43
+ Open [http://localhost:3000/square_authorize](http://localhost:3000/square_authorize). You'll be redirect to a Square, asked to log in if you aren't already and asked to authorize the app you just created. Once you do, you'll be redirected to the URL you entered in #3, which should be your local application. Your access token is shown on this page. **Copy your access token to the clipboard now** so you can use it below.
44
+
45
+ If you have a merchant account that has actual payments and locations in it, log into this on here if you can.
46
+
47
+ You can see your authorized app here: [https://squareup.com/dashboard/apps/my-apps](https://squareup.com/dashboard/apps/my-apps). You can also revoke the access token here. Log out of your Square account here (upper left) if you want to be re-prompted to log in when authorizing at [http://localhost:3000/square_authorize](http://localhost:3000/square_authorize).
48
+
49
+ #### 5. Look up a Merchant's Locations and Payments
50
+
51
+ Open a Rails console:
52
+
53
+ $ rails console
54
+
55
+ Set your access token - you copied it to your clipboard above yes? (if not just [authorize again](http://localhost:3000/square_authorize))
56
+
57
+ ```ruby
58
+ token = 'YOUR ACCESS TOKEN FROM STEP 4'
59
+ ```
60
+ Now pull your merchant's locations:
61
+
62
+ ```ruby
63
+ locations = SquareRails.locations(token)
64
+ ```
65
+ The method above returns an array of locations. So let's use the first one, and grab the payments for it.
66
+
67
+ ```ruby
68
+ location = locations.first
69
+ payments = SquareRails.payments(token,location)
70
+ ```
71
+ Hello Money.
72
+
73
+ Note that money is stored in cents - and there are more things you'll want to know. Read the [Square Connect API reference](https://docs.connect.squareup.com/api/connect/v1/) for the details...
74
+
75
+ ## Usage in your application
76
+
77
+ TODO: Structure and clarify this section.
78
+
79
+ The above demo should make a lot clear. For the initial merchant authentication, make sure to set `SQUARE_AFTER_AUTHORIZE_PATH` in `\config\initalizers\square_rails_init.rb`. Set this to a path within your application and store your user's access token in the database there (it will be in `session[:square_access_token]`). Then there is no text page shown; the user is just redirected back into your application.
80
+
81
+ If you are using the devise gem for authentication, you could migrate the User model to include a new field `square_access_token` and store the Square Connect assigned access token there after the user logs in. Then you can get to locations etc with `current_user.square_access_token`. Don't hit the Connect API every time..!
82
+
83
+ **Once in production** or on staging, make sure to change the Square Connect side **Redirect URL** that you set in Step 3... no localhost here.
84
+
85
+ ## TODO:
86
+
87
+ Lots. Error handling. Add `options` hash for payments. Hook up tons of remaining Square Connect API methods. Add convenience methods, like converting money to decimals and back. Set up for webhooks. Make everything super easy to use. Further Connect API for Dummies style documentation :)
88
+
89
+
90
+ ## Contributing
91
+
92
+ 1. Fork it ( https://github.com/qmclaugh/square_rails/fork )
93
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
94
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
95
+ 4. Push to the branch (`git push origin my-new-feature`)
96
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,55 @@
1
+ module SquareRails
2
+ class SquareIntegrationController < ApplicationController
3
+ def authorize
4
+ redirect_to "#{SQUARE_CONNECT_HOST}/oauth2/authorize?client_id=#{SQUARE_APP_ID}"
5
+ end
6
+
7
+ def callback
8
+
9
+ session[:square_access_token] = nil
10
+ session[:square_auth_response] = nil
11
+ authorization_code = params['code']
12
+
13
+ if authorization_code
14
+
15
+ # Provide the code in a request to the Obtain Token endpoint
16
+ oauth_request_body = {
17
+ 'client_id' => SQUARE_APP_ID,
18
+ 'client_secret' => SQUARE_APP_SECRET,
19
+ 'code' => authorization_code,
20
+ 'scope' => SQUARE_SCOPE
21
+ }
22
+
23
+ response = Unirest.post "#{SQUARE_CONNECT_HOST}/oauth2/token",
24
+ headers: SQUARE_OAUTH_REQUEST_HEADERS,
25
+ parameters: oauth_request_body
26
+
27
+ # Extract the returned access token from the response body
28
+ if response.body.key?('access_token')
29
+
30
+ # Here, instead of printing the access token, your application server should store it securely
31
+ # and use it in subsequent requests to the Connect API on behalf of the merchant.
32
+ auth_response = "Access token received and assigned to session[:square_access_token]. The token is: <br><br>#{response.body['access_token']}"
33
+ session[:square_access_token] = response.body['access_token']
34
+ # The response from the Obtain Token endpoint did not include an access token. Something went wrong.
35
+ else
36
+ auth_response = 'Code exchange failed!'
37
+ end
38
+
39
+ # The request to the Redirect URL did not include an authorization code. Something went wrong.
40
+ else
41
+ auth_response = 'Authorization failed!'
42
+ end
43
+
44
+ if SQUARE_AFTER_AUTHORIZE_PATH.present?
45
+ session[:square_auth_response] = auth_response
46
+ redirect_to SQUARE_AFTER_AUTHORIZE_PATH
47
+ else
48
+ render text: auth_response.html_safe
49
+ end
50
+ end
51
+
52
+ def webhook
53
+ end
54
+ end
55
+ end
File without changes
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "square_rails"
5
+ include SquareRails
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ Rails.application.routes.draw do
2
+ get 'square_authorize' => 'square_rails/square_integration#authorize'
3
+ get 'square_callback' => 'square_rails/square_integration#callback'
4
+ end
5
+
@@ -0,0 +1,39 @@
1
+ require "square_rails/version"
2
+ require "square_rails/engine"
3
+ require 'unirest'
4
+
5
+ module SquareRails
6
+
7
+ #TODO: error handling
8
+
9
+ def self.locations(token)
10
+ @url = "#{SQUARE_CONNECT_HOST}/v1/me/locations"
11
+ @token = token
12
+ self.get_data_from_square
13
+ end
14
+
15
+ def self.payments(token,location)
16
+ if location.is_a?(Hash)
17
+ loc_id = location['id']
18
+ else
19
+ loc_id = location
20
+ end
21
+
22
+ @url = "#{SQUARE_CONNECT_HOST}/v1/#{loc_id}/payments"
23
+ @token =token
24
+ self.get_data_from_square
25
+ end
26
+
27
+ def self.merchant(token)
28
+ @url = "#{SQUARE_CONNECT_HOST}/v1/me"
29
+ @token = token
30
+ self.get_data_from_square
31
+ end
32
+
33
+ def self.get_data_from_square
34
+ headers = {'Authorization' => 'Bearer ' + @token, 'Accept' => 'application/json', "Content-Type"=> "application/json"}
35
+ res = Unirest.get @url, headers: headers
36
+ res.body
37
+ end
38
+
39
+ end
@@ -0,0 +1,17 @@
1
+ module SquareRails
2
+
3
+ class Engine < Rails::Engine
4
+
5
+ # initialize "team_page.load_app_instance_data" do |app|
6
+ # SquareRails.setup do |config|
7
+ # config.app_root = app.root
8
+ # end
9
+ # end
10
+
11
+ # initialize "team_page.load_static_assets" do |app|
12
+ # app.middleware.use ::ActionDispatch::Static, "#{root}/public"
13
+ # end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,11 @@
1
+ require 'square_rails'
2
+ require 'rails'
3
+ module RakePlugin
4
+ class Railtie < Rails::Railtie
5
+ railtie_name :rake_plugin
6
+
7
+ rake_tasks do
8
+ load "lib/tasks/rake_plugin.rake"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module MyPlugin
2
+ require 'railtie' if defined?(Rails)
3
+ end
@@ -0,0 +1,3 @@
1
+ module SquareRails
2
+ VERSION = "0.1.5"
3
+ end
@@ -0,0 +1,19 @@
1
+ namespace :square_rails do
2
+ desc "Prep initializer for square_rails."
3
+ task :init do
4
+ print "Copy the default square_rails_init.rb to the application config/initializers folder?(y/n): "
5
+ option = STDIN.gets.strip
6
+
7
+ case option
8
+ when /[^Yy]/
9
+ abort_message
10
+ end
11
+
12
+ gem_root = Gem.loaded_specs['square_rails'].full_gem_path
13
+ gem_lib = File.join(gem_root, 'lib/tasks')
14
+
15
+ puts "Copying..."
16
+ cp gem_lib + '/square_rails_init.rb', Rails.root.to_s + '/config/initializers', verbose: false
17
+ puts "Done."
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ # Your application's ID and secret, available from your application dashboard.
2
+ SQUARE_APP_ID = 'YOUR APP ID HERE'
3
+ SQUARE_APP_SECRET = 'YOUR APP SECRET HERE'
4
+
5
+ # Headers to provide to OAuth API endpoints
6
+ SQUARE_OAUTH_REQUEST_HEADERS = {
7
+ 'Authorization' => "Client #{SQUARE_APP_SECRET}",
8
+ 'Accept' => 'application/json',
9
+ 'Content-Type' => 'application/json'
10
+ }
11
+
12
+ SQUARE_CONNECT_HOST = 'https://connect.squareup.com'
13
+
14
+ # Set the path you'd like your user to go to after they go to Square's site
15
+ # and authorize your app.
16
+ # Upon redirect, your customer's square access token will be in the session
17
+ # variable session[:square_access_token]
18
+
19
+ SQUARE_AFTER_AUTHORIZE_PATH = nil # path to go to after user authorizes, eg '/home/dashboard'
20
+
21
+ SQUARE_SCOPE = 'PAYMENTS_READ,MERCHANT_PROFILE_READ' # set to desired permissions. See
22
+
23
+ include SquareRails
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'square_rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "square_rails"
8
+ spec.version = SquareRails::VERSION
9
+ spec.authors = ["Quinn McLaughlin"]
10
+ spec.email = ["quinn@coincidence.net"]
11
+
12
+ spec.summary = %q{Gem to simplify getting started with the Square Connect API.}
13
+ spec.description = %q{This gem provides a convenient way for a developer to quickly get to a merchant's data Add your app ID and secret, update your callback location in the Square Developer Portal and off you go. Based on Square's sinatra examples, https://github.com/square/connect-api-examples}
14
+ spec.homepage = "https://github.com/qmclaugh/square_rails"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "rails"
23
+ spec.add_dependency "unirest"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.9"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+
28
+ spec.has_rdoc = false
29
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: square_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.5
5
+ platform: ruby
6
+ authors:
7
+ - Quinn McLaughlin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: unirest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: This gem provides a convenient way for a developer to quickly get to
70
+ a merchant's data Add your app ID and secret, update your callback location in
71
+ the Square Developer Portal and off you go. Based on Square's sinatra examples,
72
+ https://github.com/square/connect-api-examples
73
+ email:
74
+ - quinn@coincidence.net
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - .gitignore
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - app/controllers/square_rails/square_integration_controller.rb
86
+ - app/models/.keep
87
+ - bin/console
88
+ - bin/setup
89
+ - config/routes.rb
90
+ - lib/square_rails.rb
91
+ - lib/square_rails/engine.rb
92
+ - lib/square_rails/railtie.rb
93
+ - lib/square_rails/rake_plugin.rb
94
+ - lib/square_rails/version.rb
95
+ - lib/tasks/rake_plugin.rake
96
+ - lib/tasks/square_rails_init.rb
97
+ - square_rails.gemspec
98
+ homepage: https://github.com/qmclaugh/square_rails
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.4.6
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Gem to simplify getting started with the Square Connect API.
122
+ test_files: []