mekari_midtrans_api 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Mekari.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,154 @@
1
+ # midtrans_api_ruby
2
+
3
+ Midtrans API client library for Ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mekari-midtrans-api'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mekari-midtrans-api
20
+
21
+ ## Usage
22
+
23
+ ### Build client
24
+ ```ruby
25
+ require 'midtrans_api'
26
+
27
+ midtrans = MidtransApi::Client.new(
28
+ client_key: 'YOUR-CLIENT-KEY',
29
+ server_key: 'YOUR-SERVER-KEY',
30
+ sandbox: true|false
31
+ notification_url: 'https://example.com/callback'
32
+ )
33
+
34
+ # or
35
+
36
+ midtrans = MidtransApi::Client.new do |client|
37
+ client.client_key = 'YOUR-CLIENT-KEY'
38
+ client.server_key = 'YOUR-SERVER-KEY'
39
+ client.sandbox_mode = true|false
40
+ client.notification_url = 'https://example.com/callback'
41
+ end
42
+ ```
43
+
44
+ ### Basic usage
45
+
46
+ #### Credit Card Online Installment Charge
47
+ ```ruby
48
+ # get credit card token
49
+ credit_card_params = {
50
+ currency: 'IDR',
51
+ gross_amount: 12500,
52
+ card_number: 5573381072196900,
53
+ card_exp_month: 02,
54
+ card_exp_year: 2025,
55
+ card_cvv: 123
56
+ }
57
+
58
+ credit_card_token = midtrans.credit_card_token.get(credit_card_params)
59
+ #=> credit_card_token returns MidtransApi::Model::CreditCard::Token instance
60
+
61
+ charge_params = {
62
+ "payment_type": 'credit_card',
63
+ "transaction_details": {
64
+ "order_id": 'order-with-credit_card_installment',
65
+ "gross_amount": 12500
66
+ },
67
+ "credit_card": {
68
+ "token_id": credit_card_token.token_id,
69
+ "authentication": true,
70
+ "installment_term": 3,
71
+ "bank": 'mandiri',
72
+ "bins": [
73
+ 'mandiri'
74
+ ]
75
+ },
76
+ "customer_details": {
77
+ "first_name": 'Budi',
78
+ "last_name": 'Utomo',
79
+ "email": 'test@midtrans.com',
80
+ "phone": '081111333344'
81
+ },
82
+ "item_details": [
83
+ {
84
+ "id": 'invoice-1',
85
+ "price": 12500,
86
+ "quantity": 1,
87
+ "name": 'Invoice #1'
88
+ }
89
+ ]
90
+ }
91
+
92
+ credit_card_charge = midtrans.credit_card_charge.post(charge_params)
93
+ #=> credit_card_charge returns MidtransApiMidtransApi::Model::CreditCard::Charge instance
94
+ ```
95
+
96
+ #### Gopay Charge
97
+ ```ruby
98
+ charge_params = {
99
+ "payment_type": "gopay",
100
+ "transaction_details": {
101
+ "order_id": "order-with-gopay",
102
+ "gross_amount": 12500
103
+ },
104
+ "item_details": [
105
+ {
106
+ "id": "bluedio-turbine",
107
+ "price": 12500,
108
+ "quantity": 1,
109
+ "name": "Bluedio H+ Turbine Headphone with Bluetooth 4.1 -"
110
+ }
111
+ ],
112
+ "customer_details": {
113
+ "first_name": "Budi",
114
+ "last_name": "Utomo",
115
+ "email": "budi.utomo@midtrans.com",
116
+ "phone": "081223323423"
117
+ },
118
+ "gopay": {
119
+ "enable_callback": true,
120
+ "callback_url": "someapps://callback"
121
+ }
122
+ }
123
+
124
+ gopay_charge = midtrans.gopay_charge.post(charge_params)
125
+ #=> gopay_charge returns MidtransApiMidtransApi::Model::Gopay::Charge instance
126
+ ```
127
+
128
+ #### Check Status Payment
129
+ ```ruby
130
+ dummy_order_id = "order-with-gopay"
131
+ check_status = midtrans.status.get(order_id: dummy_order_id)
132
+ #=> check_status returns MidtransApi::Model::Check::Status
133
+ ```
134
+
135
+ About official documentation api of Midtrans API, see [API doc](https://api-docs.midtrans.com/)
136
+
137
+ ### Errors (Sample)
138
+ MidtransApi can raise conditional Errors explained from Midtrans Documentation.
139
+ About available Status Code, see [Midtrans API doc](https://api-docs.midtrans.com/#status-code)
140
+
141
+ ## Development
142
+
143
+ 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.
144
+
145
+ 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](https://rubygems.org).
146
+
147
+ ## Contributing
148
+
149
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mekari-engineering/midtrans_api_ruby.
150
+
151
+
152
+ ## License
153
+
154
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'midtrans_api'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MidtransApi
4
+ API_PRODUCTION_URL = 'https://api.midtrans.com'
5
+ API_SANDBOX_URL = 'https://api.sandbox.midtrans.com'
6
+ end
7
+
8
+ require 'midtrans_api/errors'
9
+ require 'midtrans_api/client'
10
+ require 'midtrans_api/configure'
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MidtransApi
4
+ module Api
5
+ class Base
6
+ attr_reader :client
7
+
8
+ class << self
9
+ def model_name
10
+ name.sub('Api', 'Model')
11
+ end
12
+
13
+ def model_class
14
+ Object.const_get(model_name)
15
+ end
16
+ end
17
+
18
+ def initialize(client)
19
+ @client = client
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MidtransApi
4
+ module Api
5
+ module Check
6
+ class Status < MidtransApi::Api::Base
7
+ def get(order_id:)
8
+ response = client.get("#{order_id}/status", {})
9
+ MidtransApi::Model::Check::Status.new(response)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MidtransApi
4
+ module Api
5
+ module CreditCard
6
+ class Charge < MidtransApi::Api::Base
7
+ PATH = 'charge'
8
+ PAYMENT_TYPE = 'credit_card'
9
+
10
+ def post(params)
11
+ response = client.post(PATH,
12
+ transaction_details: params[:transaction_details],
13
+ item_details: params[:item_details],
14
+ customer_details: params[:customer_details],
15
+ payment_type: PAYMENT_TYPE,
16
+ credit_card: params[:credit_card])
17
+ MidtransApi::Model::CreditCard::Charge.new(response)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MidtransApi
4
+ module Api
5
+ module CreditCard
6
+ class Token < MidtransApi::Api::Base
7
+ PATH = 'token'
8
+
9
+ def get(params)
10
+ response = client.get(PATH,
11
+ client_key: client.config.client_key,
12
+ currency: params[:currency],
13
+ gross_amount: params[:gross_amount],
14
+ card_number: params[:card_number],
15
+ card_exp_month: params[:card_exp_month],
16
+ card_exp_year: params[:card_exp_year],
17
+ card_cvv: params[:card_cvv])
18
+ MidtransApi::Model::CreditCard::Token.new(response)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MidtransApi
4
+ module Api
5
+ module Gopay
6
+ class Charge < MidtransApi::Api::Base
7
+ PATH = 'charge'
8
+ PAYMENT_TYPE = 'gopay'
9
+
10
+ def post(params)
11
+ gopay_params = {}
12
+
13
+ unless client.config.notification_url.nil?
14
+ gopay_params = {
15
+ enable_callback: true,
16
+ callback_url: client.config.notification_url
17
+ }
18
+ end
19
+
20
+ response = client.post(PATH,
21
+ transaction_details: params[:transaction_details],
22
+ item_details: params[:item_details],
23
+ customer_details: params[:customer_details],
24
+ payment_type: PAYMENT_TYPE,
25
+ gopay: gopay_params)
26
+ MidtransApi::Model::Gopay::Charge.new(response)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+
6
+ require 'midtrans_api/api/base'
7
+ require 'midtrans_api/api/check/status'
8
+ require 'midtrans_api/api/credit_card/charge'
9
+ require 'midtrans_api/api/credit_card/token'
10
+ require 'midtrans_api/api/gopay/charge'
11
+
12
+ require 'midtrans_api/middleware/handle_response_exception'
13
+
14
+ require 'midtrans_api/model/base'
15
+ require 'midtrans_api/model/check/status'
16
+ require 'midtrans_api/model/credit_card/charge'
17
+ require 'midtrans_api/model/credit_card/token'
18
+ require 'midtrans_api/model/gopay/charge'
19
+
20
+ module MidtransApi
21
+ class Client
22
+ attr_reader :config
23
+
24
+ def initialize(options = {})
25
+ @config = MidtransApi::Configure.new(options)
26
+
27
+ yield @config if block_given?
28
+
29
+ @connection = Faraday.new(url: "#{@config.api_url}/#{@config.api_version}/") do |connection|
30
+ connection.basic_auth(@config.server_key, '')
31
+
32
+ unless @config.notification_url.nil?
33
+ connection.headers['X-Override-Notification'] = @config.notification_url
34
+ end
35
+
36
+ connection.request :json
37
+ connection.response :json
38
+
39
+ connection.use MidtransApi::Middleware::HandleResponseException
40
+ connection.adapter Faraday.default_adapter
41
+ end
42
+ end
43
+
44
+ def gopay_charge
45
+ @charge ||= MidtransApi::Api::Gopay::Charge.new(self)
46
+ end
47
+
48
+ def credit_card_token
49
+ @credit_card_token ||= MidtransApi::Api::CreditCard::Token.new(self)
50
+ end
51
+
52
+ def credit_card_charge
53
+ @credit_card_charge ||= MidtransApi::Api::CreditCard::Charge.new(self)
54
+ end
55
+
56
+ def status
57
+ @status ||= MidtransApi::Api::Check::Status.new(self)
58
+ end
59
+
60
+ def get(url, params)
61
+ response = @connection.get(url, params)
62
+ response.body
63
+ end
64
+
65
+ def post(url, params)
66
+ response = @connection.post(url, params)
67
+ response.body
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MidtransApi
4
+ class Configure
5
+ attr_accessor :client_key,
6
+ :server_key,
7
+ :notification_url,
8
+ :sandbox_mode
9
+ attr_reader :api_version
10
+
11
+ def initialize(options = {})
12
+ @client_key = options[:client_key]
13
+ @server_key = options[:server_key]
14
+ @notification_url = options[:notification_url] || nil
15
+ @sandbox_mode = options[:sandbox] || false
16
+ @api_version = :v2
17
+ end
18
+
19
+ def api_url
20
+ return MidtransApi::API_SANDBOX_URL if sandbox_mode
21
+
22
+ MidtransApi::API_PRODUCTION_URL
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MidtransApi
4
+ class Errors
5
+ class ResponseError < StandardError; end
6
+
7
+ class PaymentDenied < ResponseError; end
8
+
9
+ class MovePermanently < ResponseError; end
10
+
11
+ class ValidationError < ResponseError; end
12
+
13
+ class AccessDenied < ResponseError; end
14
+
15
+ class UnauthorizedPayment < ResponseError; end
16
+
17
+ class ForbiddenRequest < ResponseError; end
18
+
19
+ class NotFound < ResponseError; end
20
+
21
+ class HttpNotAllowed < ResponseError; end
22
+
23
+ class DuplicateOrderId < ResponseError; end
24
+
25
+ class ExpiredTransaction < ResponseError; end
26
+
27
+ class WrongDataType < ResponseError; end
28
+
29
+ class ManySameCardNumber < ResponseError; end
30
+
31
+ class MerchantAccountDeactivated < ResponseError; end
32
+
33
+ class ErrorTokenId < ResponseError; end
34
+
35
+ class CannotModifyTransaction < ResponseError; end
36
+
37
+ class MalformedSyntax < ResponseError; end
38
+
39
+ class RefundInvalidAmount < ResponseError; end
40
+
41
+ class InternalServerError < ResponseError; end
42
+
43
+ class FeatureNotAvailable < ResponseError; end
44
+
45
+ class BankConnectionProblem < ResponseError; end
46
+
47
+ class PartnerConnectionIssue < ResponseError; end
48
+
49
+ class FraudDetectionUnavailable < ResponseError; end
50
+
51
+ class PaymentReferenceUnavailable < ResponseError; end
52
+
53
+ class UnknownError < ResponseError; end
54
+ end
55
+ end