The Smartpay Ruby library offers easy access to Smartpay API from applications written in Ruby.
- Ruby 2.6+
- Smartpay
API keys & secrets
. You can find your credential at thesettings > credentials
page on your dashboard.
If you use system built-in Ruby, you might need to be the sudoer
to be able to sudo
in some of the following steps. We recommend you to use either rbenv or rvm to have your own non-global Ruby to avoid potential permission issues.
Once you have your Ruby in place, add the latest version of Smartpay to your project's dependencies:
gem install smartpay
If you want to build the gem yourself from source:
gem build smartpay.gemspec
If you are installing via bundler, make sure that you use the https
resource in your Gemfile to avoid the risk of gems being compromised:
source 'https://rubygems.org'
gem 'smartpay'
The package needs to be configured with your own API keys, you can find them on your dashboard.
Smartpay.configure do |config|
config.public_key = '<YOUR_PUBLIC_KEY>' # the one starts with pk_test_
config.secret_key = '<YOUR_SECRET_KEY>' # the one starts with sk_test_
end
You can find the description and requirement for request payload in API Document.
payloaad = {
currency: "JPY",
items: [
{
name: "オリジナルス STAN SMITH",
amount: 1000,
currency: "JPY",
quantity: 1
},
{
currency: "JPY",
amount: 500,
name: "Merchant special discount",
kind: "discount"
},
{
currency: "JPY",
amount: 100,
name: "explicit taxes",
kind: "tax"
}
],
customerInfo: {
accountAge: 20,
email: "merchant-support@smartpay.co",
firstName: "田中",
lastName: "太郎",
firstNameKana: "たなか",
lastNameKana: "たろう",
address: {
line1: "北青山 3-6-7",
line2: "青山パラシオタワー 11階",
subLocality: "",
locality: "港区",
administrativeArea: "東京都",
postalCode: "107-0061",
country: "JP"
},
dateOfBirth: "1985-06-30",
gender: "male"
},
shippingInfo: {
address: {
line1: "北青山 3-6-7",
line2: "青山パラシオタワー 11階",
subLocality: "",
locality: "港区",
administrativeArea: "東京都",
postalCode: "107-0061",
country: "JP"
},
feeAmount: 100,
feeCurrency: "JPY"
},
captureMethod: "manual",
reference: "order_ref_1234567",
successUrl: "https://docs.smartpay.co/example-pages/checkout-successful",
cancelUrl: "https://docs.smartpay.co/example-pages/checkout-canceled"
}
Create a checkout session by using Smartpay::Api.create_checkout_session
with your request payload.
session = Smartpay::Api.create_checkout_session(payload)
Then, you can redirect your customer to the session url by calling redirect_url
:
session.redirect_url
gem install rails
rails new app-with-smartpay
cd app-with-smartpay
bundle add smartpay
bundle exec rails generate smartpay:install
This introduces 4 changes for a pre-built Smartpay Checkout example:
- A new initializer -
config/initializers/smartpay.rb
. You will have to update theconfig.public_key
andconfig.secret_key
with your own credentials to make this work.- A new controller -
app/controllers/smartpays_controller.rb
. This is where you can see how a Checkout session is configured & created.- A new view -
app/views/smartpays/index.html.erb
. The minimum frontend required.- A new route in config/routes.rb.
Edit the keys with your own credentials in config/initializers/smartpay.rb
.
...
config.public_key = '<YOUR_PUBLIC_KEY>' # the one starts with pk_test_
config.secret_key = '<YOUR_SECRET_KEY>' # the one starts with sk_test_
...
bundle exec rails server
Visit http://localhost:3000/smartpays.
Click the checkout
button on the page to be redirected to Smartpay's Checkout.
To try out different cases, you can use the following test credit cards for different cases:
- Payment succeeds:
4242 4242 4242 4242
- Payment is declined:
4100 0000 0000 0019
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and the created tag, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/smartpay-co/sdk-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Smartpay project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.