midtrans_api_ruby
Midtrans API client library for Ruby
Installation
Add this line to your application's Gemfile:
gem 'mekari-midtrans-api'
And then execute:
$ bundle
Or install it yourself as:
$ gem install mekari-midtrans-api
Usage
Build client
require 'midtrans_api'
midtrans = MidtransApi::Client.new(
client_key: 'YOUR-CLIENT-KEY',
server_key: 'YOUR-SERVER-KEY',
sandbox: true|false
notification_url: 'https://example.com/callback'
)
# or
midtrans = MidtransApi::Client.new do |client|
client.client_key = 'YOUR-CLIENT-KEY'
client.server_key = 'YOUR-SERVER-KEY'
client.sandbox_mode = true|false
client.notification_url = 'https://example.com/callback'
end
Basic usage
Credit Card Online Installment Charge
credit_card_params = {
currency: 'IDR',
gross_amount: 12500,
card_number: 5573381072196900,
card_exp_month: 02,
card_exp_year: 2025,
card_cvv: 123
}
credit_card_token = midtrans.credit_card_token.get(credit_card_params)
charge_params = {
"payment_type": 'credit_card',
"transaction_details": {
"order_id": 'order-with-credit_card_installment',
"gross_amount": 12500
},
"credit_card": {
"token_id": credit_card_token.token_id,
"authentication": true,
"installment_term": 3,
"bank": 'mandiri',
"bins": [
'mandiri'
]
},
"customer_details": {
"first_name": 'Budi',
"last_name": 'Utomo',
"email": 'test@midtrans.com',
"phone": '081111333344'
},
"item_details": [
{
"id": 'invoice-1',
"price": 12500,
"quantity": 1,
"name": 'Invoice #1'
}
]
}
credit_card_charge = midtrans.credit_card_charge.post(charge_params)
Gopay Charge
charge_params = {
"payment_type": "gopay",
"transaction_details": {
"order_id": "order-with-gopay",
"gross_amount": 12500
},
"item_details": [
{
"id": "bluedio-turbine",
"price": 12500,
"quantity": 1,
"name": "Bluedio H+ Turbine Headphone with Bluetooth 4.1 -"
}
],
"customer_details": {
"first_name": "Budi",
"last_name": "Utomo",
"email": "budi.utomo@midtrans.com",
"phone": "081223323423"
},
"gopay": {
"enable_callback": true,
"callback_url": "someapps://callback"
}
}
gopay_charge = midtrans.gopay_charge.post(charge_params)
Check Status Payment
dummy_order_id = "order-with-gopay"
check_status = midtrans.status.get(order_id: dummy_order_id)
About official documentation api of Midtrans API, see API doc
Errors (Sample)
MidtransApi can raise conditional Errors explained from Midtrans Documentation.
About available Status Code, see Midtrans API doc
Development
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 tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/mekari-engineering/midtrans_api_ruby.
License
The gem is available as open source under the terms of the MIT License.