veritrans 2.3.0 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +4 -0
- data/.idea/.gitignore +8 -0
- data/.rubocop.yml +1 -1
- data/.travis.yml +2 -1
- data/CHANGELOG.md +13 -0
- data/Gemfile +2 -11
- data/Gemfile.lock +116 -145
- data/Maintaining.MD +8 -0
- data/README.md +247 -278
- data/api_reference.md +482 -173
- data/example/coreapi/core_api_credit_card_example.rb +66 -0
- data/example/coreapi/readme.md +4 -0
- data/example/sinatra/Gemfile +7 -0
- data/example/sinatra/README.md +4 -6
- data/example/sinatra/index.erb +180 -192
- data/example/sinatra/response.erb +1 -40
- data/example/sinatra/snap.erb +33 -0
- data/example/sinatra/snap_redirect.erb +10 -0
- data/example/sinatra/webapp.rb +113 -0
- data/example/snap/readme.md +4 -0
- data/example/snap/snap_example.rb +39 -0
- data/lib/test/all.rb +1 -0
- data/lib/test/api_test.rb +319 -0
- data/lib/test/config_test.rb +26 -0
- data/lib/test/gopay_tokenization_test.rb +80 -0
- data/lib/test/snap_test.rb +79 -0
- data/lib/test/subscription_test.rb +116 -0
- data/lib/test/transaction_test.rb +160 -0
- data/lib/veritrans/api.rb +105 -8
- data/lib/veritrans/client.rb +40 -7
- data/lib/veritrans/config.rb +26 -3
- data/lib/veritrans/midtrans_error.rb +15 -0
- data/lib/veritrans/version.rb +1 -1
- data/lib/veritrans.rb +5 -6
- data/veritrans.gemspec +1 -2
- metadata +25 -248
- data/.rspec +0 -2
- data/Procfile +0 -1
- data/Rakefile +0 -16
- data/bin/midtrans +0 -3
- data/bin/veritrans +0 -68
- data/example/rails/cable/Gemfile +0 -48
- data/example/rails/cable/Gemfile.lock +0 -184
- data/example/rails/cable/README.md +0 -29
- data/example/rails/cable/Rakefile +0 -6
- data/example/rails/cable/app/assets/config/manifest.js +0 -3
- data/example/rails/cable/app/assets/images/.keep +0 -0
- data/example/rails/cable/app/assets/javascripts/application.js +0 -15
- data/example/rails/cable/app/assets/javascripts/cable.js +0 -13
- data/example/rails/cable/app/assets/javascripts/channels/.keep +0 -0
- data/example/rails/cable/app/assets/javascripts/chats.js +0 -39
- data/example/rails/cable/app/assets/javascripts/rooms.js +0 -55
- data/example/rails/cable/app/assets/stylesheets/application.css +0 -16
- data/example/rails/cable/app/assets/stylesheets/chatbox.scss +0 -31
- data/example/rails/cable/app/assets/stylesheets/peoplebox.scss +0 -6
- data/example/rails/cable/app/channels/application_cable/channel.rb +0 -4
- data/example/rails/cable/app/channels/application_cable/connection.rb +0 -4
- data/example/rails/cable/app/channels/rooms_texting_channel.rb +0 -13
- data/example/rails/cable/app/controllers/application_controller.rb +0 -3
- data/example/rails/cable/app/controllers/chats_controller.rb +0 -18
- data/example/rails/cable/app/controllers/concerns/.keep +0 -0
- data/example/rails/cable/app/controllers/pays_controller.rb +0 -28
- data/example/rails/cable/app/controllers/rooms_controller.rb +0 -4
- data/example/rails/cable/app/helpers/application_helper.rb +0 -2
- data/example/rails/cable/app/helpers/chats_helper.rb +0 -2
- data/example/rails/cable/app/helpers/rooms_helper.rb +0 -5
- data/example/rails/cable/app/jobs/application_job.rb +0 -2
- data/example/rails/cable/app/jobs/chat_broadcaster_job.rb +0 -15
- data/example/rails/cable/app/mailers/application_mailer.rb +0 -4
- data/example/rails/cable/app/models/application_record.rb +0 -3
- data/example/rails/cable/app/models/chat.rb +0 -41
- data/example/rails/cable/app/models/concerns/.keep +0 -0
- data/example/rails/cable/app/models/order.rb +0 -2
- data/example/rails/cable/app/models/user.rb +0 -2
- data/example/rails/cable/app/views/chats/_chat.html.erb +0 -18
- data/example/rails/cable/app/views/layouts/application.html.erb +0 -19
- data/example/rails/cable/app/views/layouts/mailer.html.erb +0 -13
- data/example/rails/cable/app/views/layouts/mailer.text.erb +0 -1
- data/example/rails/cable/app/views/rooms/_chatbox.html.erb +0 -13
- data/example/rails/cable/app/views/rooms/_online_people.html.erb +0 -53
- data/example/rails/cable/app/views/rooms/index.html.erb +0 -9
- data/example/rails/cable/bin/bundle +0 -3
- data/example/rails/cable/bin/rails +0 -9
- data/example/rails/cable/bin/rake +0 -9
- data/example/rails/cable/bin/setup +0 -34
- data/example/rails/cable/bin/spring +0 -16
- data/example/rails/cable/bin/update +0 -29
- data/example/rails/cable/config/application.rb +0 -16
- data/example/rails/cable/config/boot.rb +0 -3
- data/example/rails/cable/config/cable.yml +0 -9
- data/example/rails/cable/config/database.yml +0 -20
- data/example/rails/cable/config/environment.rb +0 -5
- data/example/rails/cable/config/environments/development.rb +0 -57
- data/example/rails/cable/config/environments/production.rb +0 -86
- data/example/rails/cable/config/environments/test.rb +0 -42
- data/example/rails/cable/config/initializers/application_controller_renderer.rb +0 -6
- data/example/rails/cable/config/initializers/assets.rb +0 -11
- data/example/rails/cable/config/initializers/backtrace_silencers.rb +0 -7
- data/example/rails/cable/config/initializers/cookies_serializer.rb +0 -5
- data/example/rails/cable/config/initializers/filter_parameter_logging.rb +0 -4
- data/example/rails/cable/config/initializers/inflections.rb +0 -16
- data/example/rails/cable/config/initializers/mime_types.rb +0 -4
- data/example/rails/cable/config/initializers/new_framework_defaults.rb +0 -24
- data/example/rails/cable/config/initializers/session_store.rb +0 -3
- data/example/rails/cable/config/initializers/sidekiq.rb +0 -2
- data/example/rails/cable/config/initializers/veritrans.rb +0 -46
- data/example/rails/cable/config/initializers/wrap_parameters.rb +0 -14
- data/example/rails/cable/config/locales/en.yml +0 -23
- data/example/rails/cable/config/puma.rb +0 -47
- data/example/rails/cable/config/routes.rb +0 -18
- data/example/rails/cable/config/secrets.yml +0 -22
- data/example/rails/cable/config/spring.rb +0 -6
- data/example/rails/cable/config/veritrans.yml +0 -13
- data/example/rails/cable/config.ru +0 -5
- data/example/rails/cable/db/development.sqlite3 +0 -0
- data/example/rails/cable/db/migrate/20161215070044_create_chats.rb +0 -10
- data/example/rails/cable/db/migrate/20161215072021_create_users.rb +0 -10
- data/example/rails/cable/db/migrate/20161219110219_create_orders.rb +0 -11
- data/example/rails/cable/db/schema.rb +0 -37
- data/example/rails/cable/db/seeds.rb +0 -41
- data/example/rails/cable/db/test.sqlite3 +0 -0
- data/example/rails/cable/lib/assets/.keep +0 -0
- data/example/rails/cable/lib/tasks/.keep +0 -0
- data/example/rails/cable/log/.keep +0 -0
- data/example/rails/cable/public/404.html +0 -67
- data/example/rails/cable/public/422.html +0 -67
- data/example/rails/cable/public/500.html +0 -66
- data/example/rails/cable/public/apple-touch-icon-precomposed.png +0 -0
- data/example/rails/cable/public/apple-touch-icon.png +0 -0
- data/example/rails/cable/public/favicon.ico +0 -0
- data/example/rails/cable/public/images/avatar1.jpg +0 -0
- data/example/rails/cable/public/images/avatar2.jpg +0 -0
- data/example/rails/cable/public/images/avatar3.jpg +0 -0
- data/example/rails/cable/public/images/avatar4.jpg +0 -0
- data/example/rails/cable/public/images/avatar5.jpg +0 -0
- data/example/rails/cable/public/images/avatar6.jpg +0 -0
- data/example/rails/cable/public/robots.txt +0 -5
- data/example/rails/cable/vendor/assets/javascripts/.keep +0 -0
- data/example/rails/cable/vendor/assets/stylesheets/.keep +0 -0
- data/example/rails/cable/vendor/assets/stylesheets/photon.css +0 -2333
- data/example/rails/simplepay/Gemfile +0 -54
- data/example/rails/simplepay/Gemfile.lock +0 -178
- data/example/rails/simplepay/README.md +0 -30
- data/example/rails/simplepay/Rakefile +0 -6
- data/example/rails/simplepay/app/assets/config/manifest.js +0 -3
- data/example/rails/simplepay/app/assets/images/.keep +0 -0
- data/example/rails/simplepay/app/assets/javascripts/application.js +0 -16
- data/example/rails/simplepay/app/assets/javascripts/cable.js +0 -13
- data/example/rails/simplepay/app/assets/javascripts/channels/.keep +0 -0
- data/example/rails/simplepay/app/assets/javascripts/pay.js +0 -18
- data/example/rails/simplepay/app/assets/stylesheets/application.css +0 -15
- data/example/rails/simplepay/app/assets/stylesheets/style.css +0 -254
- data/example/rails/simplepay/app/channels/application_cable/channel.rb +0 -4
- data/example/rails/simplepay/app/channels/application_cable/connection.rb +0 -4
- data/example/rails/simplepay/app/controllers/application_controller.rb +0 -3
- data/example/rails/simplepay/app/controllers/concerns/.keep +0 -0
- data/example/rails/simplepay/app/controllers/home_controller.rb +0 -16
- data/example/rails/simplepay/app/controllers/pay_controller.rb +0 -10
- data/example/rails/simplepay/app/helpers/application_helper.rb +0 -2
- data/example/rails/simplepay/app/helpers/pay_helper.rb +0 -2
- data/example/rails/simplepay/app/jobs/application_job.rb +0 -2
- data/example/rails/simplepay/app/mailers/application_mailer.rb +0 -4
- data/example/rails/simplepay/app/models/application_record.rb +0 -3
- data/example/rails/simplepay/app/models/concerns/.keep +0 -0
- data/example/rails/simplepay/app/models/order.rb +0 -2
- data/example/rails/simplepay/app/views/home/index.html.erb +0 -19
- data/example/rails/simplepay/app/views/layouts/application.html.erb +0 -15
- data/example/rails/simplepay/app/views/layouts/mailer.html.erb +0 -13
- data/example/rails/simplepay/app/views/layouts/mailer.text.erb +0 -1
- data/example/rails/simplepay/app/views/pay/notify.html.erb +0 -2
- data/example/rails/simplepay/bin/bundle +0 -3
- data/example/rails/simplepay/bin/rails +0 -9
- data/example/rails/simplepay/bin/rake +0 -9
- data/example/rails/simplepay/bin/setup +0 -34
- data/example/rails/simplepay/bin/spring +0 -16
- data/example/rails/simplepay/bin/update +0 -29
- data/example/rails/simplepay/config/application.rb +0 -15
- data/example/rails/simplepay/config/boot.rb +0 -3
- data/example/rails/simplepay/config/cable.yml +0 -9
- data/example/rails/simplepay/config/database.yml +0 -25
- data/example/rails/simplepay/config/environment.rb +0 -5
- data/example/rails/simplepay/config/environments/development.rb +0 -57
- data/example/rails/simplepay/config/environments/production.rb +0 -86
- data/example/rails/simplepay/config/environments/test.rb +0 -42
- data/example/rails/simplepay/config/initializers/application_controller_renderer.rb +0 -6
- data/example/rails/simplepay/config/initializers/assets.rb +0 -11
- data/example/rails/simplepay/config/initializers/backtrace_silencers.rb +0 -7
- data/example/rails/simplepay/config/initializers/cookies_serializer.rb +0 -5
- data/example/rails/simplepay/config/initializers/filter_parameter_logging.rb +0 -4
- data/example/rails/simplepay/config/initializers/inflections.rb +0 -16
- data/example/rails/simplepay/config/initializers/mime_types.rb +0 -4
- data/example/rails/simplepay/config/initializers/new_framework_defaults.rb +0 -24
- data/example/rails/simplepay/config/initializers/session_store.rb +0 -3
- data/example/rails/simplepay/config/initializers/veritrans.rb +0 -46
- data/example/rails/simplepay/config/initializers/wrap_parameters.rb +0 -14
- data/example/rails/simplepay/config/locales/en.yml +0 -23
- data/example/rails/simplepay/config/puma.rb +0 -47
- data/example/rails/simplepay/config/routes.rb +0 -5
- data/example/rails/simplepay/config/secrets.yml +0 -22
- data/example/rails/simplepay/config/spring.rb +0 -6
- data/example/rails/simplepay/config/veritrans.yml +0 -18
- data/example/rails/simplepay/config.ru +0 -5
- data/example/rails/simplepay/db/development.sqlite3 +0 -0
- data/example/rails/simplepay/db/migrate/20161221090855_create_orders.rb +0 -10
- data/example/rails/simplepay/db/schema.rb +0 -22
- data/example/rails/simplepay/db/seeds.rb +0 -7
- data/example/rails/simplepay/db/test.sqlite3 +0 -0
- data/example/rails/simplepay/lib/assets/.keep +0 -0
- data/example/rails/simplepay/lib/tasks/.keep +0 -0
- data/example/rails/simplepay/log/.keep +0 -0
- data/example/rails/simplepay/public/404.html +0 -67
- data/example/rails/simplepay/public/422.html +0 -67
- data/example/rails/simplepay/public/500.html +0 -66
- data/example/rails/simplepay/public/apple-touch-icon-precomposed.png +0 -0
- data/example/rails/simplepay/public/apple-touch-icon.png +0 -0
- data/example/rails/simplepay/public/favicon.ico +0 -0
- data/example/rails/simplepay/public/robots.txt +0 -5
- data/example/rails/simplepay/vendor/assets/javascripts/.keep +0 -0
- data/example/rails/simplepay/vendor/assets/stylesheets/.keep +0 -0
- data/example/sinatra/config.ru +0 -9
- data/example/sinatra/localization.erb +0 -249
- data/example/sinatra/points.erb +0 -188
- data/example/sinatra/recurring.erb +0 -201
- data/example/sinatra/sinatra.rb +0 -194
- data/example/sinatra/style.css +0 -137
- data/example/sinatra/veritrans.yml +0 -11
- data/example/sinatra/widget.erb +0 -63
- data/lib/generators/templates/assets/credit_card_form.js +0 -57
- data/lib/generators/templates/payments_controller.rb +0 -85
- data/lib/generators/templates/veritrans.rb +0 -46
- data/lib/generators/templates/veritrans.yml +0 -18
- data/lib/generators/templates/views/_credit_card_form.erb +0 -42
- data/lib/generators/templates/views/_veritrans_include.erb +0 -10
- data/lib/generators/templates/views/payments/create.erb +0 -15
- data/lib/generators/templates/views/payments/new.erb +0 -6
- data/lib/generators/veritrans/install_generator.rb +0 -32
- data/lib/generators/veritrans/payment_form_generator.rb +0 -45
- data/lib/veritrans/cli.rb +0 -161
- data/lib/veritrans/testing.rb +0 -156
- data/spec/cli_spec.rb +0 -83
- data/spec/configs/real_key.yml +0 -4
- data/spec/configs/veritrans.yml +0 -7
- data/spec/configs/veritrans_flat.yml +0 -2
- data/spec/configs/veritrans_with_erb.yml +0 -2
- data/spec/fixtures/approve_failed.yml +0 -48
- data/spec/fixtures/cancel_failed.yml +0 -48
- data/spec/fixtures/cancel_success.yml +0 -126
- data/spec/fixtures/capture_failed.yml +0 -48
- data/spec/fixtures/charge.yml +0 -50
- data/spec/fixtures/charge_direct.yml +0 -56
- data/spec/fixtures/charge_vtweb.yml +0 -50
- data/spec/fixtures/cli_test_1111-not-exists.yml +0 -45
- data/spec/fixtures/cli_test_not_exists.yml +0 -45
- data/spec/fixtures/cli_test_real_txn.yml +0 -55
- data/spec/fixtures/cli_test_unauthorized.yml +0 -47
- data/spec/fixtures/deny_failed.yml +0 -144
- data/spec/fixtures/events_test_real_txn.yml +0 -55
- data/spec/fixtures/expire_failed.yml +0 -50
- data/spec/fixtures/expire_success.yml +0 -56
- data/spec/fixtures/midtrans_status.yml +0 -117
- data/spec/fixtures/refund_failed.yml +0 -54
- data/spec/fixtures/status_fail.yml +0 -48
- data/spec/fixtures/status_success.yml +0 -128
- data/spec/fixtures/test_token.yml +0 -49
- data/spec/midtrans_rename_spec.rb +0 -27
- data/spec/rails_plugin_spec.rb +0 -317
- data/spec/spec_helper.rb +0 -71
- data/spec/veritrans_client_spec.rb +0 -235
- data/spec/veritrans_config_spec.rb +0 -70
- data/spec/veritrans_events_spec.rb +0 -72
- data/spec/veritrans_logger_spec.rb +0 -46
- data/spec/veritrans_snap_spec.rb +0 -54
- data/spec/veritrans_testing_spec.rb +0 -184
- data/testing_webhooks.md +0 -78
data/README.md
CHANGED
@@ -1,356 +1,329 @@
|
|
1
|
-
#
|
1
|
+
# Midtrans Ruby library
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
Midtrans ❤️ Ruby!
|
4
|
+
|
5
|
+
This is the Official Ruby API client/library for Midtrans Payment API. Visit [https://midtrans.com](https://midtrans.com). More information about the product and see documentation at [http://docs.midtrans.com](https://docs.midtrans.com) for more technical details.
|
5
6
|
|
6
7
|
[![Gem Version](https://badge.fury.io/rb/veritrans.svg)](http://badge.fury.io/rb/veritrans)
|
7
8
|
[![Build Status](https://travis-ci.org/veritrans/veritrans-ruby.svg?branch=master)](https://travis-ci.org/veritrans/veritrans-ruby)
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
1. Sinatra, which demonstrate in as succint code as possible. Please [have a look here](https://github.com/veritrans/veritrans-ruby/tree/master/example/sinatra)
|
12
|
-
2. Simplepay, demonstrated how to integrate Midtrans with a simple, succint Rails project. [Have a look](https://github.com/veritrans/veritrans-ruby/tree/master/example/rails/simplepay).
|
13
|
-
3. Cable, demonstrate a chat-commerce app where the payment is handled with Midtrans. [Have a look](https://github.com/veritrans/veritrans-ruby/tree/master/example/rails/cable).
|
14
|
-
|
15
|
-
## How to use (Rails)
|
16
|
-
|
17
|
-
### Add gem veritrans to Gemfile
|
10
|
+
## 1. Installation
|
18
11
|
|
12
|
+
### Using Gemfile
|
13
|
+
Add gem veritrans to Gemfile
|
19
14
|
```ruby
|
20
15
|
gem 'veritrans'
|
21
16
|
```
|
17
|
+
Run this command in your terminal
|
18
|
+
```ruby
|
19
|
+
gem install veritrans
|
20
|
+
```
|
21
|
+
```ruby
|
22
|
+
bundle install
|
23
|
+
```
|
22
24
|
|
23
|
-
|
25
|
+
## 2. Usage
|
26
|
+
### 2.1 Choose Product/Method
|
24
27
|
|
25
|
-
|
28
|
+
We have [3 different products](https://docs.midtrans.com/en/welcome/index.html) of payment that you can use:
|
29
|
+
- [Snap](#22A-snap) - Customizable payment popup will appear on **your web/app** (no redirection). [doc ref](https://snap-docs.midtrans.com/)
|
30
|
+
- [Snap Redirect](#22B-snap-redirect) - Customer need to be redirected to payment url **hosted by midtrans**. [doc ref](https://snap-docs.midtrans.com/)
|
31
|
+
- [Core API (VT-Direct)](#22C-core-api-vt-direct) - Basic backend implementation, you can customize the frontend embedded on **your web/app** as you like (no redirection). [doc ref](https://api-docs.midtrans.com/)
|
26
32
|
|
27
|
-
|
33
|
+
Choose one that you think best for your unique needs.
|
28
34
|
|
29
|
-
###
|
35
|
+
### 2.2 Client Initialization and Configuration
|
30
36
|
|
31
|
-
|
37
|
+
Get your client key and server key from [Midtrans Dashboard](https://dashboard.midtrans.com)
|
32
38
|
|
33
|
-
|
39
|
+
Create instance of Midtrans client
|
34
40
|
|
35
|
-
```
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
41
|
+
```ruby
|
42
|
+
require 'veritrans'
|
43
|
+
|
44
|
+
mt_client = Midtrans.new(
|
45
|
+
server_key: "your server key",
|
46
|
+
client_key: "your client key",
|
47
|
+
api_host: "https://api.sandbox.midtrans.com", # default
|
48
|
+
http_options: { }, # optional
|
49
|
+
logger: Logger.new(STDOUT), # optional
|
50
|
+
file_logger: Logger.new(STDOUT), # optional
|
51
|
+
)
|
52
|
+
|
53
|
+
mt_client.status("order-id-123456")
|
54
|
+
```
|
55
|
+
|
56
|
+
Alternatively, you can also set config by declaring each one like below:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
Midtrans.config.server_key = "your server key"
|
60
|
+
Midtrans.config.client_key = "your client key"
|
61
|
+
Midtrans.config.api_host = "https://api.sandbox.midtrans.com"
|
40
62
|
```
|
41
63
|
|
42
|
-
|
64
|
+
### 2.2.A Snap
|
65
|
+
You can see Snap example [with Sinatra](example/sinatra) and [without framework](example/snap).
|
43
66
|
|
44
|
-
|
67
|
+
```ruby
|
68
|
+
# Create Snap payment page, with this version returning full API response
|
69
|
+
create_snap_token(parameter)
|
45
70
|
|
46
|
-
|
71
|
+
# Create Snap payment page, with this version returning token
|
72
|
+
create_snap_token_string(parameter)
|
47
73
|
|
48
|
-
|
74
|
+
# Create Snap payment page, with this version returning redirect url
|
75
|
+
create_snap_redirect_url_str(parameter)
|
76
|
+
```
|
77
|
+
`parameter` is Object or String of JSON of [SNAP Parameter](https://snap-docs.midtrans.com/#json-objects)
|
49
78
|
|
50
|
-
|
51
|
-
and as detailed as you wish to.
|
79
|
+
#### Get Snap Token
|
52
80
|
|
53
81
|
```ruby
|
54
|
-
|
82
|
+
result = Midtrans.create_snap_token(
|
55
83
|
transaction_details: {
|
56
|
-
order_id:
|
57
|
-
gross_amount:
|
84
|
+
order_id: "test-transaction-order-123",
|
85
|
+
gross_amount: 100000,
|
86
|
+
secure: true
|
58
87
|
}
|
59
88
|
)
|
60
|
-
|
61
|
-
@snap_token = response.token
|
89
|
+
@token = result.token
|
62
90
|
```
|
63
91
|
|
64
|
-
|
92
|
+
#### Initialize Snap JS when customer click pay button
|
65
93
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
94
|
+
On frontend / html:
|
95
|
+
Replace `PUT_TRANSACTION_TOKEN_HERE` with `transactionToken` acquired above
|
96
|
+
```html
|
97
|
+
<html>
|
98
|
+
<body>
|
99
|
+
<button id="pay-button">Pay!</button>
|
100
|
+
<pre><div id="result-json">JSON result will appear here after payment:<br></div></pre>
|
101
|
+
|
102
|
+
<!-- TODO: Remove ".sandbox" from script src URL for production environment. Also input your client key in "data-client-key" -->
|
103
|
+
<script src="https://app.sandbox.midtrans.com/snap/snap.js" data-client-key="<Set your ClientKey here>"></script>
|
104
|
+
<script type="text/javascript">
|
105
|
+
document.getElementById('pay-button').onclick = function(){
|
106
|
+
// SnapToken acquired from previous step
|
107
|
+
snap.pay('PUT_TRANSACTION_TOKEN_HERE', {
|
108
|
+
// Optional
|
109
|
+
onSuccess: function(result){
|
110
|
+
/* You may add your own js here, this is just example */ document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2);
|
111
|
+
},
|
112
|
+
// Optional
|
113
|
+
onPending: function(result){
|
114
|
+
/* You may add your own js here, this is just example */ document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2);
|
115
|
+
},
|
116
|
+
// Optional
|
117
|
+
onError: function(result){
|
118
|
+
/* You may add your own js here, this is just example */ document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2);
|
119
|
+
}
|
120
|
+
});
|
121
|
+
};
|
122
|
+
</script>
|
123
|
+
</body>
|
124
|
+
</html>
|
89
125
|
```
|
90
126
|
|
91
|
-
|
127
|
+
### 2.2.B Snap Redirect
|
128
|
+
You can see Snap example [with Sinatra](example/sinatra) and [without framework](example/snap).
|
92
129
|
|
93
|
-
|
130
|
+
#### Get Redirection URL of a Payment Page
|
94
131
|
|
95
132
|
```ruby
|
96
|
-
|
133
|
+
result = Midtrans.create_snap_redirect_url(
|
97
134
|
transaction_details: {
|
98
|
-
order_id: "
|
99
|
-
gross_amount:
|
135
|
+
order_id: "test-transaction-order-123",
|
136
|
+
gross_amount: 100000,
|
137
|
+
secure: true
|
100
138
|
}
|
101
139
|
)
|
102
|
-
|
103
|
-
redirect_to @result.redirect_url
|
140
|
+
@redirecturl = result.redirect_url
|
104
141
|
```
|
105
142
|
|
106
|
-
|
143
|
+
### 2.2.C Core API (VT-Direct)
|
144
|
+
You can see some Core API examples [with Sinatra](example/sinatra) and [without framework](example/coreapi).
|
107
145
|
|
108
|
-
|
146
|
+
Available methods for `CoreApi` class
|
109
147
|
|
110
|
-
|
148
|
+
```ruby
|
149
|
+
# charge : Do `/charge` API request to Midtrans Core API
|
150
|
+
def charge(payment_type, data = nil)
|
111
151
|
|
112
|
-
|
152
|
+
# test_token : Do `/token` API request to Midtrans Core API
|
153
|
+
def test_token(options = {})
|
154
|
+
|
155
|
+
# point_inquiry : Do `/point_inquiry/{tokenId}` API request to Midtrans Core API
|
156
|
+
def point_inquiry(token_id)
|
113
157
|
|
114
|
-
|
115
|
-
|
116
|
-
payment_type: "VTWEB",
|
117
|
-
transaction_details: {
|
118
|
-
order_id: "my-unique-order-id",
|
119
|
-
gross_amount: 100_000
|
120
|
-
}
|
121
|
-
)
|
158
|
+
# status : Do `/{orderId}/status` API request to Midtrans Core API
|
159
|
+
def status(payment_id)
|
122
160
|
|
123
|
-
|
124
|
-
|
161
|
+
# approve : Do `/{orderId}/approve` API request to Midtrans Core API
|
162
|
+
def approve(payment_id, options = {})
|
125
163
|
|
126
|
-
|
164
|
+
# deny : Do `/{orderId}/deny` API request to Midtrans Core API
|
165
|
+
def deny(payment_id, options = {})
|
127
166
|
|
128
|
-
|
129
|
-
|
167
|
+
# cancel : Do `/{orderId}/cancel` API request to Midtrans Core API
|
168
|
+
def cancel(payment_id, options = {})
|
130
169
|
|
131
|
-
|
170
|
+
# expire : Do `/{orderId}/expire` API request to Midtrans Core API
|
171
|
+
def expire(payment_id)
|
172
|
+
|
173
|
+
# refund : Do `/{orderId}/refund` API request to Midtrans Core API
|
174
|
+
def refund(payment_id, options = {})
|
132
175
|
|
133
|
-
|
176
|
+
# capture : Do `/{orderId}/capture` API request to Midtrans Core API
|
177
|
+
def capture(payment_id, gross_amount, options = {})
|
134
178
|
|
135
|
-
|
136
|
-
|
179
|
+
# link_payment_account : Do `/pay/account` API request to Midtrans Core API
|
180
|
+
def link_payment_account(param)
|
137
181
|
|
138
|
-
|
139
|
-
|
140
|
-
Veritrans.client_key = "<%= Veritrans.config.client_key %>";
|
141
|
-
</script>
|
142
|
-
```
|
182
|
+
# get_payment_account : Do `/pay/account/{account_id}` API request to Midtrans Core API
|
183
|
+
def get_payment_account(account_id)
|
143
184
|
|
144
|
-
|
145
|
-
|
146
|
-
```erb
|
147
|
-
<%= form_tag "/charge_vtdirect", id: "card_form" do %>
|
148
|
-
<%= hidden_field_tag :token_id, nil, id: "card_token" %>
|
149
|
-
<%= hidden_field_tag :gross_amount, 30000 %>
|
150
|
-
<p>
|
151
|
-
<%= label_tag "card_number", "Card number" %>
|
152
|
-
<%= text_field_tag :card_number, "4811 1111 1111 1114", name: nil, style: "width: 150px" %>
|
153
|
-
</p>
|
154
|
-
<p>
|
155
|
-
<%= label_tag "card_cvc", "Security Code" %>
|
156
|
-
<%= text_field_tag :card_cvc, "123", name: nil, style: "width: 30px", placeholder: "cvc" %>
|
157
|
-
</p>
|
158
|
-
<p>
|
159
|
-
<%= label_tag "card_exp", "Expiration date" %>
|
160
|
-
<%= text_field_tag :card_exp, "12 / 18", name: nil, placeholder: "MM / YY" %>
|
161
|
-
</p>
|
162
|
-
<%= submit_tag "Make payment", id: "submit_btn" %>
|
163
|
-
<% end %>
|
164
|
-
<iframe id="3d-secure-iframe" style="display: none; width: 500px; height: 600px"></iframe>
|
165
|
-
```
|
185
|
+
# unlink_payment_account : Do `/pay/account/{account_id}/unbind` API request to Midtrans Core API
|
186
|
+
def unlink_payment_account(account_id)
|
166
187
|
|
167
|
-
|
188
|
+
# create_subscription : Do `/subscription` API request to Midtrans Core API
|
189
|
+
def create_subscription(param)
|
168
190
|
|
169
|
-
|
170
|
-
|
171
|
-
<input type="hidden" name="token_id" id="card_token">
|
172
|
-
<input type="hidden" id="gross_amount" value="30000">
|
173
|
-
<p>
|
174
|
-
<label for="card_number">Card number</label>
|
175
|
-
<input type="text" id="card_number" style="width: 150px" value="4811 1111 1111 1114">
|
176
|
-
</p>
|
177
|
-
<p>
|
178
|
-
<label for="card_cvc">Security Code</label>
|
179
|
-
<input type="text" id="card_cvc" style="width: 30px" placeholder="cvc" value="123">
|
180
|
-
</p>
|
181
|
-
<p>
|
182
|
-
<label for="card_exp">Expiration date</label>
|
183
|
-
<input type="text" id="card_exp" placeholder="MM / YY" value="12 / 18">
|
184
|
-
</p>
|
185
|
-
<p>
|
186
|
-
<label for="card_secure">3D-secure</label>
|
187
|
-
<input id="card_secure" name="card_secure" type="checkbox" value="1" />
|
188
|
-
</p>
|
189
|
-
<input id="submit_btn" type="submit">
|
190
|
-
</form>
|
191
|
-
<iframe id="3d-secure-iframe" style="display: none; width: 500px; height: 600px"></iframe>
|
192
|
-
```
|
191
|
+
# get_subscription : Do `/subscription/{subscription_id}` API request to Midtrans Core API
|
192
|
+
def get_subscription(subscription_id)
|
193
193
|
|
194
|
-
|
195
|
-
|
196
|
-
```js
|
197
|
-
$(document).ready(function () {
|
198
|
-
// function to prepare our credit card data before send
|
199
|
-
function createTokenData() {
|
200
|
-
return {
|
201
|
-
card_number: $('#card_number').val(),
|
202
|
-
card_cvv: $('#card_cvc').val(),
|
203
|
-
card_exp_month: $('#card_exp').val().match(/(\d+) \//)[1],
|
204
|
-
card_exp_year: '20' + $('#card_exp').val().match(/\/ (\d+)/)[1],
|
205
|
-
gross_amount: $('#gross_amount').val(),
|
206
|
-
secure: $('#card_secure')[0].checked
|
207
|
-
};
|
208
|
-
}
|
209
|
-
// Add custom event for form submition
|
210
|
-
$('#card_form').on('submit', function (event) {
|
211
|
-
var form = this;
|
212
|
-
event.preventDefault();
|
213
|
-
|
214
|
-
Veritrans.token(createTokenData, function (data) {
|
215
|
-
console.log('Token data:', data);
|
216
|
-
// when you making 3D-secure transaction,
|
217
|
-
// this callback function will be called again after user confirm 3d-secure
|
218
|
-
// but you can also redirect on server side
|
219
|
-
if (data.redirect_url) {
|
220
|
-
// if we get url then it's 3d-secure transaction
|
221
|
-
// so we need to open that page
|
222
|
-
$('#3d-secure-iframe').attr('src', data.redirect_url).show();
|
223
|
-
// if no redirect_url and we have token_id then just make charge request
|
224
|
-
} else if (data.token_id) {
|
225
|
-
$('#card_token').val(data.token_id);
|
226
|
-
form.submit();
|
227
|
-
// if no redirect_url and no token_id, then it should be error
|
228
|
-
} else {
|
229
|
-
alert(data.validation_messages ? data.validation_messages.join("\n") : data.status_message);
|
230
|
-
}
|
231
|
-
});
|
232
|
-
});
|
233
|
-
});
|
234
|
-
```
|
194
|
+
# disable_subscription : Do `/subscription/{subscription_id}/disable` API request to Midtrans Core API
|
195
|
+
def disable_subscription(subscription_id)
|
235
196
|
|
236
|
-
|
197
|
+
# enable_subscription : Do `/subscription/{subscription_id}/enable` API request to Midtrans Core API
|
198
|
+
def enable_subscription(subscription_id)
|
237
199
|
|
238
|
-
|
239
|
-
|
240
|
-
payment_type: "credit_card",
|
241
|
-
credit_card: { token_id: params[:token_id] },
|
242
|
-
transaction_details: {
|
243
|
-
order_id: @payment.order_id,
|
244
|
-
gross_amount: @payment.amount
|
245
|
-
}
|
246
|
-
)
|
247
|
-
if @result.success?
|
248
|
-
puts "Success"
|
249
|
-
end
|
200
|
+
# update_subscription : Do `/subscription/{subscription_id}` API request to Midtrans Core API
|
201
|
+
def update_subscription(subscription_id, param)
|
250
202
|
```
|
251
203
|
|
252
|
-
|
204
|
+
#### Credit Card Get Token
|
253
205
|
|
254
|
-
|
206
|
+
Get token should be handled on Frontend please refer to [API docs](https://docs.midtrans.com/en/core-api/credit-card).
|
207
|
+
Further example to demonstrate Core API card integration (including get card token on frontend), available on [Sinatra example](/example/sinatra)
|
255
208
|
|
256
|
-
|
257
|
-
|
258
|
-
For Core API / VT-Direct you have to specify payment method (without get token step, credit card token required only for credit card transactions).
|
209
|
+
#### Credit Card Charge
|
259
210
|
|
260
211
|
```ruby
|
261
|
-
|
262
|
-
payment_type: "
|
263
|
-
|
212
|
+
result = Midtrans.charge(
|
213
|
+
payment_type: "credit_card",
|
214
|
+
credit_card: {
|
215
|
+
token_id: "CREDIT_CARD_TOKEN", # change with your card token,
|
216
|
+
authentication: true
|
217
|
+
},
|
264
218
|
transaction_details: {
|
265
|
-
order_id:
|
266
|
-
gross_amount:
|
267
|
-
}
|
268
|
-
|
269
|
-
puts
|
219
|
+
order_id: "test-transaction-12345",
|
220
|
+
gross_amount: 20000
|
221
|
+
})
|
222
|
+
# result.data this will be Hash representation of the API JSON response:
|
223
|
+
puts result.data
|
270
224
|
```
|
271
225
|
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
## STEP 3: Receive notification callback
|
226
|
+
#### Credit Card 3DS Authentication
|
276
227
|
|
277
|
-
|
228
|
+
The credit card charge result may contains `redirect_url` for 3DS authentication. 3DS Authentication should be handled on Frontend please refer to [API docs](https://api-docs.midtrans.com/#card-features-3d-secure)
|
278
229
|
|
279
|
-
|
230
|
+
For full example on Credit Card 3DS transaction refer to:
|
231
|
+
- [Sinatra example](/example/sinatra) that implement Snap & Core Api
|
280
232
|
|
281
|
-
For testing in development phase please read our [Testing webhooks tutorial](https://github.com/veritrans/veritrans-ruby/blob/master/testing_webhooks.md) and [command line tool](#command-line-tool)
|
282
233
|
|
234
|
+
### 2.3 Handle HTTP Notification
|
235
|
+
> **IMPORTANT NOTE**: To update transaction status on your backend/database, **DO NOT** solely rely on frontend callbacks! For security reason to make sure the status is authentically coming from Midtrans, only update transaction status based on HTTP Notification or API Get Status.
|
283
236
|
|
284
|
-
|
237
|
+
Create separated web endpoint (notification url) to receive HTTP POST notification callback/webhook.
|
238
|
+
HTTP notification will be sent whenever transaction status is changed.
|
239
|
+
Example also available [here](example/sinatra)
|
285
240
|
|
286
241
|
```ruby
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
242
|
+
post_body = JSON.parse(request.body.read)
|
243
|
+
notification = Midtrans.status(post_body['transaction_id'])
|
244
|
+
|
245
|
+
order_id = notification.data[:order_id]
|
246
|
+
payment_type = notification.data[:payment_type]
|
247
|
+
transaction_status = notification.data[:transaction_status]
|
248
|
+
fraud_status = notification.data[:fraud_status]
|
249
|
+
|
250
|
+
puts "Transaction order_id: #{order_id}"
|
251
|
+
puts "Payment type: #{payment_type}"
|
252
|
+
puts "Transaction status: #{transaction_status}"
|
253
|
+
puts "Fraud status: #{fraud_status}"
|
254
|
+
|
255
|
+
return "Transaction notification received. Order ID: #{order_id}. Transaction status: #{transaction_status}. Fraud status: #{fraud_status}"
|
256
|
+
|
257
|
+
# Sample transactionStatus handling logic
|
258
|
+
if transaction_status == "capture" && fraud_status == "challange"
|
259
|
+
# TODO set transaction status on your databaase to 'challenge'
|
260
|
+
elsif transaction_status == "capture" && fraud_status == "success"
|
261
|
+
# TODO set transaction status on your databaase to 'success'
|
262
|
+
elsif transaction_status == "settlement"
|
263
|
+
# TODO set transaction status on your databaase to 'success'
|
264
|
+
elsif transaction_status == "deny"
|
265
|
+
# TODO you can ignore 'deny', because most of the time it allows payment retries
|
266
|
+
elsif transaction_status == "cancel" || transaction_status == "expire"
|
267
|
+
# TODO set transaction status on your databaase to 'failure'
|
268
|
+
elsif transaction_status == "pending"
|
269
|
+
# Todo set transaction status on your databaase to 'pending' / waiting payment
|
310
270
|
end
|
311
271
|
```
|
312
272
|
|
313
|
-
|
314
|
-
|
315
|
-
#### Veritrans::Events
|
273
|
+
### 2.4 Transaction Action
|
274
|
+
For full example on transaction action refer to: [Api Reference](api_reference.md)
|
316
275
|
|
317
|
-
|
276
|
+
## 3. Handling Error / Exception
|
277
|
+
When using function that result in Midtrans API call e.g: `Midtrans.charge(...)` or `Midtrans.create_snap_token(...)`
|
278
|
+
there's a chance it may throw error (`MidtransError` object), the error object will contains below properties that can be used as information to your error handling logic:
|
318
279
|
|
319
280
|
```ruby
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
#
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
events.subscribe('payment.success') do |payment|
|
329
|
-
# payment variable is hash with params recieved from Veritrans
|
330
|
-
# assuming you have model Order in your project
|
331
|
-
Order.find_by(order_id: payment.order_id).mark_paid!(payment.masked_card)
|
332
|
-
end
|
333
|
-
|
334
|
-
events.subscribe('payment.failed', 'payment.challenge') do |payment|
|
335
|
-
# payment variable is hash with params recieved from Veritrans
|
336
|
-
# assuming you have model Order in your project
|
337
|
-
Order.find_by(order_id: payment.order_id) ...
|
338
|
-
end
|
281
|
+
begin
|
282
|
+
Midtrans.create_snap_token(parameter)
|
283
|
+
rescue MidtransError => e
|
284
|
+
puts e.message # Basic error message string
|
285
|
+
puts e.http_status_code # HTTP status code e.g: 400, 401, etc.
|
286
|
+
puts e.api_response # API response body in String
|
287
|
+
puts e.raw_http_client_data # Raw HTTP client response
|
339
288
|
end
|
340
289
|
```
|
341
290
|
|
342
|
-
|
291
|
+
## 4. Advanced Usage
|
292
|
+
### Override Notification URL
|
293
|
+
|
294
|
+
You can opt to change or add custom notification urls on every transaction. It can be achieved by adding additional HTTP headers into charge request.
|
295
|
+
```ruby
|
296
|
+
# Add new notification url(s) alongside the settings on Midtrans Dashboard Portal (MAP)
|
297
|
+
Midtrans.config.append_notif_url = "https://example.com/test1,https://example.com/test2"
|
298
|
+
# Use new notification url(s) disregarding the settings on Midtrans Dashboard Portal (MAP)
|
299
|
+
Midtrans.config.override_notif_url = "https://example.com/test1"
|
300
|
+
```
|
301
|
+
|
302
|
+
[More details](https://api-docs.midtrans.com/#override-notification-url)
|
303
|
+
> **Note:** When both `appendNotifUrl` and `overrideNotifUrl` are used together then only `overrideNotifUrl` will be used.
|
343
304
|
|
344
|
-
|
305
|
+
> Both header can only receive up to maximum of **3 urls**.
|
306
|
+
|
307
|
+
### Idempotency-Key
|
308
|
+
Is a unique value that is put on header on API request. Midtrans API accept Idempotency-Key on header to safely handle retry request
|
309
|
+
without performing the same operation twice. This is helpful for cases where merchant didn't receive the response because of network issue or other unexpected error.
|
310
|
+
You can opt to add idempotency key by adding additional HTTP headers into charge request.
|
311
|
+
```ruby
|
312
|
+
Midtrans.config.idempotency_key = "Unique-ID"
|
313
|
+
```
|
314
|
+
[More details](http://api-docs.midtrans.com/#idempotent-requests)
|
345
315
|
|
346
|
-
|
316
|
+
### Log Configuration
|
317
|
+
By default if you are using Rails, gem Veritrans will show information via Rails logger and in addition save important information to `RAILS_APP/log/Midtrans.log` <br>
|
318
|
+
You can configure it like example below:
|
347
319
|
|
348
320
|
```ruby
|
349
|
-
|
350
|
-
|
321
|
+
Midtrans.logger = Rails.logger
|
322
|
+
# To set custom logger
|
323
|
+
Midtrans.file_logger = Logger.new("./log/midtrans.log")
|
351
324
|
```
|
352
325
|
|
353
|
-
`
|
326
|
+
`Midtrans.file_logger` save information about:
|
354
327
|
|
355
328
|
* "charge", "cancel", "approve" api calls
|
356
329
|
* Validation errors for "charge", "cancel", "approve"
|
@@ -359,25 +332,21 @@ Veritrans.file_logger = Logger.new("/my/important_logs/veritrans.log")
|
|
359
332
|
|
360
333
|
----
|
361
334
|
|
362
|
-
|
363
|
-
|
364
|
-
**Installation**
|
365
|
-
|
366
|
-
$ gem install veritrans
|
367
|
-
|
368
|
-
**Usage**
|
335
|
+
### To see it in action, we have made example:
|
369
336
|
|
370
|
-
|
337
|
+
Sinatra, which demonstrate in as succint code as possible. Please [have a look here](https://github.com/veritrans/veritrans-ruby/tree/master/example/sinatra)
|
371
338
|
|
372
|
-
$ veritrans testhook http://localhost:3000/vt_events
|
373
|
-
$ veritrans testhook -o my-order-1 -c ~/path/to/veritrans.yml http://localhost:3000/vt_events
|
374
339
|
|
375
|
-
|
376
|
-
|
377
|
-
#### Get help
|
340
|
+
### Get help
|
378
341
|
|
379
342
|
* [Veritrans gem reference](https://github.com/veritrans/veritrans-ruby/blob/master/api_reference.md)
|
380
343
|
* [Midtrans login](https://account.midtrans.com/login)
|
381
344
|
* [Midtrans registration](https://account.midtrans.com/register)
|
382
345
|
* [Midtrans documentation](http://docs.midtrans.com)
|
383
346
|
* Technical support [support@midtrans.com](mailto:support@midtrans.com)
|
347
|
+
|
348
|
+
## Important Changes
|
349
|
+
|
350
|
+
### v2.4.0
|
351
|
+
- API client methods will now raise `MidtransError` when getting unexpected API response. You may need to update your error handling. [Handling Error / Exception](#3-Handling-Error--Exception)
|
352
|
+
- Removed features: CLI, TestingLib. Mainly removed due to no longer relevant/essential to this library's purpose.
|