ActiveMerchantSquare for Square’s e-commerce API
A gem for Ruby developer's to use in their eCommerce application
We released ActiveMerchantSquare for developers who want to use Square’s e-commerce API with the ActiveMerchant rubygem. If you are a ruby developer you may use the popular ActiveMerchant gem as a common abstraction layer (in production use since 2006) to work with many payments gateways.
ActiveMerchantSquare depends on and enhances the ActiveMerchant gem so that you can use it in conjunction with the existing use of ActiveMerchant. Call the standard ActiveMerchant API methods using a card_nonce from the payment form instead of raw card numbers. Square’s ecommerce API removes you from PCI scope as your servers do not ever see raw payment card numbers.
Other features (in our V1 and V2 APIs) such as the newly released Catalog API is only available in our connect-ruby-sdk. You can use both gems side-by-side if you want to. As with all our client libraries, we hope to help you decrease the development time required to integrate with Square.
Trying it out
You can use the ActiveMerchantSquare by adding it to your Gemfile:
gem ‘active_merchant_square’, ‘~> 1.0’
This simple example demonstrates how a purchase can be made after getting a card nonce.
require 'active_merchant_square'
# Get your login and password by going to: https://connect.squareup.com/apps
credentials = {
login: 'APPLICATION_ID',
password: 'APPLICATION_SECRET',
# How to get your location ID, see: https://docs.connect.squareup.com/articles/faq-lookup-my-location-id
location_id: 'LOCATION_ID',
}
amount_cents = 1000 # $10.00
gateway = ActiveMerchant::Billing::SquareGateway.new(credentials)
response = gateway.purchase(amount_cents, card_nonce, {
:description => 'Store Purchase Note'})
if response.success?
puts "Successfully charged $#{sprintf("%.2f", amount_cents / 100)}"
else
raise StandardError, response.message
end
In addition to calling purchase, you can also call these methods, exactly the same as you can in ActiveMerchant. (Details in the rubydoc).
def purchase(money, card_nonce, options={})
def authorize(money, card_nonce, options={})
def capture(ignored_money, txn_id, ignored_options={})
def refund(money, txn_id, options={})
def void(txn_id, options={})
def verify(card_nonce, options={})
def store(card_nonce, options = {})
def update(customer_id, card_id, options = {})
def update_customer(customer_id, options = {})
def unstore(card_id, options = {}, deprecated_options = {})
You can take a look at our working example application here. And let us know other ways you use ActiveMerchantSquare to save development time when integrating your apps with Square.