YooKassa API Ruby Client
Installation
Add this line to your application's Gemfile:
gem 'yookassa'
And then execute:
$ bundle
Or install it yourself as:
$ gem install yookassa
Usage
Configuration
First of all you need to setup credentials to use Yookassa. You can configure your instance of Yookassa once in a application booting time. Ex. if you use rails, just put these lines into initializer file
# config/initializers/yookassa.rb
Yookassa.configure do |config|
config.shop_id = ENV.fetch('YOOKASSA_SHOP_ID') # or put your shop_id and api_key here directly
config.api_key = ENV.fetch('YOOKASSA_API_KEY') # can be taken from Rails.credentials too
end
There are some cases, when you need to connect to different Yookassa accounts (say, your clients need to connect to Yookassa). That probably means that you run a marketplace or multitenant system. There is a solution for this one from Yookassa, see https://yookassa.ru/en/developers/special-solutions/checkout-for-platforms/basics or https://yookassa.ru/en/developers/partners-api/basics
If that is not your case, and you still have multiple shop_ids and api_keys, and need to handle all of them under one application, then you need to instantiate clients inline
client1 = Yookassa::Client.new(shop_id: 'shop_1', api_key: '123')
client2 = Yookassa::Client.new(shop_id: 'shop_2', api_key: '456')
Making Payments
Creating payment
payload = {
amount: {
value: 100,
currency: 'RUB'
},
capture: true,
confirmation: {
type: 'redirect',
return_url: return_url
}
}
payment = Yookassa.payments.create(payment: payload)
# or
client = Yookassa::Client.new(shop_id: 'shop_1', api_key: '123')
payment = client.payments.create(payment: payload)
Other payment requests
Yookassa.payments.find(payment_id: '12345')
Yookassa.payments.capture(payment_id: '12345')
Yookassa.payments.cancel(payment_id: '12345')
Refunds
payment = Yookassa.refunds.create(payment: payload)
# or
client = Yookassa::Client.new(shop_id: 'shop_1', api_key: '123')
payment = client.refunds.create(payment: payload)
Contributing
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
License
The gem is available as open source under the terms of the MIT License.