Git Product home page Git Product logo

connect-ruby-sdk's Introduction

Square logo

Square Connect Ruby SDK - RETIRED


Build Status Gem Version Apache-2 license

NOTICE: Square Connect Ruby SDK retired

The Square Connect Ruby SDK is retired (EOL) as of 2019-08-15 and will no longer receive bug fixes or product updates. To continue receiving API and SDK improvements, please follow the instructions below to migrate to the new Square Ruby SDK gem.

The old Connect SDK documentation is available under the /docs folder.





Migrate to the Square Ruby SDK

Follow the instructions below to migrate your apps from the deprecated square_connect gem to the new square.rb gem.

Update your bundle

  1. Find the line in your Gemfile starting with gem: 'square_connect' and change the entire line to gem: 'square.rb'.
  2. Run bundle to update your Gemfile.lock.

Update your code

  1. Change all instances of require 'square_connect' to require 'square'.
  2. Replace models with plain Ruby Hash equivalents.
  3. Update client instantiation to follow the method outlined below.
  4. Update code for accessing response data to follow the method outlined below.
  5. Check response.success? or response.error? rather than rescuing exceptions for flow control.

To simplify your code, we also recommend that you use method chaining to access APIs instead of explicitly instantiating multiple clients.

Client instantiation

require 'square'

square = Square::Client.new(access_token: 'YOUR ACCESS TOKEN')

response = square.API.ENDPOINT(body: BODY)

Accessing response data

if response.success?
  p response.data
else
  warn response.errors
end



Example code migration

As a specific example, consider the following code for creating a new customer from the following Hash:

new_customer = {
 given_name: 'Ava',
 address: {
   address_line_1: '555 Electric Ave',
   locality: 'Los Angeles',
   country: 'US'
 }
}

With the deprecated square_connect gem, this is how you instantiate a client for the Customers API, format the request, and call the endpoint:

require 'square_connect'

# Instantiate the client
SquareConnect.configure do |config|
  config.access_token = 'YOUR ACCESS TOKEN'
end

api_instance = SquareConnect::CustomersApi.new

# Create the models
address = SquareConnect::Address.new(new_customer[:address])

body = SquareConnect::CreateCustomerRequest.new(
  given_name: new_customer[:given_name],
  address: address
)

begin
  # Call the endpoint
  response = api_instance.create_customer(body)

  # Handle the response and warn on errors
  p response.customer.to_hash
rescue SquareConnect::ApiError
  warn response.errors
end

Now consider equivalent code using the new square.rb gem:

require 'square'

# Instantiate the client
square = Square::Client.new(access_token: 'YOUR ACCESS TOKEN')

# Call the endpoint
response = square.customers.create_customer(body: new_customer)

# Handle the response and warn on errors
if response.success?
  p response.data
else
  warn response.errors
end

That's it!

What was once a multi-block process can be handled in 2 lines of code and an if/else block. Migrating to the square.rb gem reduces boilerplate and lets you focus on the parts of your code that really matter.




Ask the community

Please join us in our Square developer community if you have any questions!

connect-ruby-sdk's People

Contributors

alecholmes avatar bunnyc1986 avatar dange42 avatar frojasg avatar havenwood avatar hukid avatar jguinta avatar jlabanca avatar mikekono avatar shaofu88 avatar square-sdk-deployer avatar sseaman avatar tristansokol avatar yonpols avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

connect-ruby-sdk's Issues

Why was authorization removed from individual API call?

We use Square on behalf of multiple merchants and our access_tokens are different for each merchant.

Removing the authorization from individual calls and making it VM wide global makes it impossible for us to migrate to versions > 2.0.x.

How do you recommend those who access Square on behalf of multiple merchants deal with this?

libcurl.dll not found when using Ruby

Hi I was trying out Ruby SDK but encountered a really obnoxious error. I successfully installed square.rb but when I tried to run the code:

# Load the gem

require 'square_connect'

# Setup authorization
SquareConnect.configure do |config|
# Configure OAuth2 access token for authorization: oauth2
config.access_token = 'YOUR ACCESS TOKEN'
end

# The SDK throws an exception if a Connect endpoint responds with anything besides 200 (success).
# This block catches any exceptions that occur from the request.
locations_api = SquareConnect::LocationsApi.new
begin
locations = locations_api.list_locations.locations
rescue SquareConnect::ApiError => e
puts "Error encountered while loading the locations: #{e.message}"
end

This error occurs.

C:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/ffi-1.11.1-x86-mingw32/lib/ffi/library.rb:145:in 'block in ffi_lib' (LoadError)open library 'libcurl': The specified module could not be found.
.
Could not open library 'libcurl.dll': The specified module could not be found.
.
Could not open library 'libcurl.so.4': The specified module could not be found.

I tried installing libcurl by following the suggestions found on:
stackoverflow

I downloaded the latest build for windows and I even added the PATH coming from my

Git/mingw64/bin

since it does have curl already. It might need a specific version that I don't know. I will really appreciate any guidance

How to add `Tips` through the Square API

Hi, I am using the Square's OrderAPI + TransactionAPI to handle my eCommerce checkout and everything is working great so far.
However, I've just received a requirement from my client to add the tip section to the checkout, where customers will inform how much tip they will give. Is it something doable using the current Square's Api?

Square API errors

Hello.

When I'm using RenewTokenRequest, I'm getting this error:

SquareConnect::ApiError:
Bad Request - {
  "message": "cannot renew revoked token",
  "type": "bad_request"
}

Doesn't RenewTokenResponse (and other response objects) supposed to handle such cases and contain some Error with coresponding code, e.g. ACCESS_TOKEN_REVOKED?
Because for now, I can only parse response for this message, which seems like a bad solution to me.
Or is there a better way to handle token revocation form Square dashboard, without getting it on the runtime?

Also, Error#handlingerrors refers to nowhere.

Invalid Enum for OrderServiceChargeCalculationPhase

I keep on getting this error for calculation phase. Tried "TOTAL_PHASE" and SquareConnect::OrderServiceChargeCalculationPhase::TOTAL_PHASE (which gives "TOTAL_PHASE") but still getting the error below.

Bad Request - {"errors":[{"category":"INVALID_REQUEST_ERROR","code":"INVALID_ENUM_VALUE","detail":"TOTAL_PHASE is not a valid enum value for order.order.service_charges[0].calculation_phase.","field":"order.order.service_charges[0].calculation_phase"}]}

Redirect url format

# The URL to redirect to after checkout is completed with `checkoutId`, Square's `orderId`, `transactionId`, and `referenceId` appended as URL parameters. For example, if the provided redirect_url is `http://www.example.com/order-complete`, a successful transaction redirects the customer to: `http://www.example.com/order-complete?checkoutId=xxxxxx&orderId=xxxxxx&referenceId=xxxxxx&transactionId=xxxxxx` If you do not provide a redirect URL, Square Checkout will display an order confirmation page on your behalf; however Square strongly recommends that you provide a redirect URL so you can verify the transaction results and finalize the order through your existing/normal confirmation workflow. Default: none; only exists if explicitly set.
attr_accessor :redirect_url

Is this documentation still correct? I have tested it in the sandbox. The redirec_url, however, only containscheckoutId and transactionId.

Allow upgrade to Ruby 2.4 and JSON 2

json 2 is pretty much required now for full JSON RFC compliance and compatibility with ruby 2.4.

Most rails gems now support either 1.8 or 2.0.

Keeping the ceiling at 1.8 is preventing us from upgrading other gems and to ruby 2.4/2.5 versions. Can we be bit more generous with json dependency? This is similar to: Azure/autorest#476

Ruby 2.5 ships with json 2.1 and, Ruby 2.4 ships with json 2.0.x.

It would be better to skip the dependency altogether or support higher versions.

Allow use of 2 API key/accounts in a single app

At the moment is seems that SquareConnect.configure will change the config class, making it very difficult to have two SquareConnect instances that use different tokens.

Do you guys have any suggestion as to how best architect something like this?

Thank you.

Api initializers should fail to do so when no configuration is provided

The following code:

api = SquareConnect::TransactionsApi.new
api.list_locations('LOCATION_ID')

fails with:

{
  "errors": [
    {
      "category": "AUTHENTICATION_ERROR",
      "code": "UNAUTHORIZED",
      "detail": "The `Authorization` http header of your request was malformed. The header value is expected to be of the format \"Bearer TOKEN\" (without quotation marks), where TOKEN is to be replaced with your access token (e.g. \"Bearer ABC123def456GHI789jkl0\"). For more information, see https://docs.connect.squareup.com/api/connect/v2/#requestandresponseheaders. If you are seeing this error message while using one of our officially supported SDKs, please report this to [email protected]."
    }
  ]
}

when no configuration has be set

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.