veritrans 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (275) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -0
  3. data/.idea/.gitignore +8 -0
  4. data/.rubocop.yml +1 -1
  5. data/.travis.yml +2 -1
  6. data/CHANGELOG.md +13 -0
  7. data/Gemfile +2 -11
  8. data/Gemfile.lock +116 -145
  9. data/Maintaining.MD +8 -0
  10. data/README.md +247 -278
  11. data/api_reference.md +482 -173
  12. data/example/coreapi/core_api_credit_card_example.rb +66 -0
  13. data/example/coreapi/readme.md +4 -0
  14. data/example/sinatra/Gemfile +7 -0
  15. data/example/sinatra/README.md +4 -6
  16. data/example/sinatra/index.erb +180 -192
  17. data/example/sinatra/response.erb +1 -40
  18. data/example/sinatra/snap.erb +33 -0
  19. data/example/sinatra/snap_redirect.erb +10 -0
  20. data/example/sinatra/webapp.rb +113 -0
  21. data/example/snap/readme.md +4 -0
  22. data/example/snap/snap_example.rb +39 -0
  23. data/lib/test/all.rb +1 -0
  24. data/lib/test/api_test.rb +319 -0
  25. data/lib/test/config_test.rb +26 -0
  26. data/lib/test/gopay_tokenization_test.rb +80 -0
  27. data/lib/test/snap_test.rb +79 -0
  28. data/lib/test/subscription_test.rb +116 -0
  29. data/lib/test/transaction_test.rb +160 -0
  30. data/lib/veritrans/api.rb +105 -8
  31. data/lib/veritrans/client.rb +40 -7
  32. data/lib/veritrans/config.rb +26 -3
  33. data/lib/veritrans/midtrans_error.rb +15 -0
  34. data/lib/veritrans/version.rb +1 -1
  35. data/lib/veritrans.rb +5 -6
  36. data/veritrans.gemspec +1 -2
  37. metadata +25 -248
  38. data/.rspec +0 -2
  39. data/Procfile +0 -1
  40. data/Rakefile +0 -16
  41. data/bin/midtrans +0 -3
  42. data/bin/veritrans +0 -68
  43. data/example/rails/cable/Gemfile +0 -48
  44. data/example/rails/cable/Gemfile.lock +0 -184
  45. data/example/rails/cable/README.md +0 -29
  46. data/example/rails/cable/Rakefile +0 -6
  47. data/example/rails/cable/app/assets/config/manifest.js +0 -3
  48. data/example/rails/cable/app/assets/images/.keep +0 -0
  49. data/example/rails/cable/app/assets/javascripts/application.js +0 -15
  50. data/example/rails/cable/app/assets/javascripts/cable.js +0 -13
  51. data/example/rails/cable/app/assets/javascripts/channels/.keep +0 -0
  52. data/example/rails/cable/app/assets/javascripts/chats.js +0 -39
  53. data/example/rails/cable/app/assets/javascripts/rooms.js +0 -55
  54. data/example/rails/cable/app/assets/stylesheets/application.css +0 -16
  55. data/example/rails/cable/app/assets/stylesheets/chatbox.scss +0 -31
  56. data/example/rails/cable/app/assets/stylesheets/peoplebox.scss +0 -6
  57. data/example/rails/cable/app/channels/application_cable/channel.rb +0 -4
  58. data/example/rails/cable/app/channels/application_cable/connection.rb +0 -4
  59. data/example/rails/cable/app/channels/rooms_texting_channel.rb +0 -13
  60. data/example/rails/cable/app/controllers/application_controller.rb +0 -3
  61. data/example/rails/cable/app/controllers/chats_controller.rb +0 -18
  62. data/example/rails/cable/app/controllers/concerns/.keep +0 -0
  63. data/example/rails/cable/app/controllers/pays_controller.rb +0 -28
  64. data/example/rails/cable/app/controllers/rooms_controller.rb +0 -4
  65. data/example/rails/cable/app/helpers/application_helper.rb +0 -2
  66. data/example/rails/cable/app/helpers/chats_helper.rb +0 -2
  67. data/example/rails/cable/app/helpers/rooms_helper.rb +0 -5
  68. data/example/rails/cable/app/jobs/application_job.rb +0 -2
  69. data/example/rails/cable/app/jobs/chat_broadcaster_job.rb +0 -15
  70. data/example/rails/cable/app/mailers/application_mailer.rb +0 -4
  71. data/example/rails/cable/app/models/application_record.rb +0 -3
  72. data/example/rails/cable/app/models/chat.rb +0 -41
  73. data/example/rails/cable/app/models/concerns/.keep +0 -0
  74. data/example/rails/cable/app/models/order.rb +0 -2
  75. data/example/rails/cable/app/models/user.rb +0 -2
  76. data/example/rails/cable/app/views/chats/_chat.html.erb +0 -18
  77. data/example/rails/cable/app/views/layouts/application.html.erb +0 -19
  78. data/example/rails/cable/app/views/layouts/mailer.html.erb +0 -13
  79. data/example/rails/cable/app/views/layouts/mailer.text.erb +0 -1
  80. data/example/rails/cable/app/views/rooms/_chatbox.html.erb +0 -13
  81. data/example/rails/cable/app/views/rooms/_online_people.html.erb +0 -53
  82. data/example/rails/cable/app/views/rooms/index.html.erb +0 -9
  83. data/example/rails/cable/bin/bundle +0 -3
  84. data/example/rails/cable/bin/rails +0 -9
  85. data/example/rails/cable/bin/rake +0 -9
  86. data/example/rails/cable/bin/setup +0 -34
  87. data/example/rails/cable/bin/spring +0 -16
  88. data/example/rails/cable/bin/update +0 -29
  89. data/example/rails/cable/config/application.rb +0 -16
  90. data/example/rails/cable/config/boot.rb +0 -3
  91. data/example/rails/cable/config/cable.yml +0 -9
  92. data/example/rails/cable/config/database.yml +0 -20
  93. data/example/rails/cable/config/environment.rb +0 -5
  94. data/example/rails/cable/config/environments/development.rb +0 -57
  95. data/example/rails/cable/config/environments/production.rb +0 -86
  96. data/example/rails/cable/config/environments/test.rb +0 -42
  97. data/example/rails/cable/config/initializers/application_controller_renderer.rb +0 -6
  98. data/example/rails/cable/config/initializers/assets.rb +0 -11
  99. data/example/rails/cable/config/initializers/backtrace_silencers.rb +0 -7
  100. data/example/rails/cable/config/initializers/cookies_serializer.rb +0 -5
  101. data/example/rails/cable/config/initializers/filter_parameter_logging.rb +0 -4
  102. data/example/rails/cable/config/initializers/inflections.rb +0 -16
  103. data/example/rails/cable/config/initializers/mime_types.rb +0 -4
  104. data/example/rails/cable/config/initializers/new_framework_defaults.rb +0 -24
  105. data/example/rails/cable/config/initializers/session_store.rb +0 -3
  106. data/example/rails/cable/config/initializers/sidekiq.rb +0 -2
  107. data/example/rails/cable/config/initializers/veritrans.rb +0 -46
  108. data/example/rails/cable/config/initializers/wrap_parameters.rb +0 -14
  109. data/example/rails/cable/config/locales/en.yml +0 -23
  110. data/example/rails/cable/config/puma.rb +0 -47
  111. data/example/rails/cable/config/routes.rb +0 -18
  112. data/example/rails/cable/config/secrets.yml +0 -22
  113. data/example/rails/cable/config/spring.rb +0 -6
  114. data/example/rails/cable/config/veritrans.yml +0 -13
  115. data/example/rails/cable/config.ru +0 -5
  116. data/example/rails/cable/db/development.sqlite3 +0 -0
  117. data/example/rails/cable/db/migrate/20161215070044_create_chats.rb +0 -10
  118. data/example/rails/cable/db/migrate/20161215072021_create_users.rb +0 -10
  119. data/example/rails/cable/db/migrate/20161219110219_create_orders.rb +0 -11
  120. data/example/rails/cable/db/schema.rb +0 -37
  121. data/example/rails/cable/db/seeds.rb +0 -41
  122. data/example/rails/cable/db/test.sqlite3 +0 -0
  123. data/example/rails/cable/lib/assets/.keep +0 -0
  124. data/example/rails/cable/lib/tasks/.keep +0 -0
  125. data/example/rails/cable/log/.keep +0 -0
  126. data/example/rails/cable/public/404.html +0 -67
  127. data/example/rails/cable/public/422.html +0 -67
  128. data/example/rails/cable/public/500.html +0 -66
  129. data/example/rails/cable/public/apple-touch-icon-precomposed.png +0 -0
  130. data/example/rails/cable/public/apple-touch-icon.png +0 -0
  131. data/example/rails/cable/public/favicon.ico +0 -0
  132. data/example/rails/cable/public/images/avatar1.jpg +0 -0
  133. data/example/rails/cable/public/images/avatar2.jpg +0 -0
  134. data/example/rails/cable/public/images/avatar3.jpg +0 -0
  135. data/example/rails/cable/public/images/avatar4.jpg +0 -0
  136. data/example/rails/cable/public/images/avatar5.jpg +0 -0
  137. data/example/rails/cable/public/images/avatar6.jpg +0 -0
  138. data/example/rails/cable/public/robots.txt +0 -5
  139. data/example/rails/cable/vendor/assets/javascripts/.keep +0 -0
  140. data/example/rails/cable/vendor/assets/stylesheets/.keep +0 -0
  141. data/example/rails/cable/vendor/assets/stylesheets/photon.css +0 -2333
  142. data/example/rails/simplepay/Gemfile +0 -54
  143. data/example/rails/simplepay/Gemfile.lock +0 -178
  144. data/example/rails/simplepay/README.md +0 -30
  145. data/example/rails/simplepay/Rakefile +0 -6
  146. data/example/rails/simplepay/app/assets/config/manifest.js +0 -3
  147. data/example/rails/simplepay/app/assets/images/.keep +0 -0
  148. data/example/rails/simplepay/app/assets/javascripts/application.js +0 -16
  149. data/example/rails/simplepay/app/assets/javascripts/cable.js +0 -13
  150. data/example/rails/simplepay/app/assets/javascripts/channels/.keep +0 -0
  151. data/example/rails/simplepay/app/assets/javascripts/pay.js +0 -18
  152. data/example/rails/simplepay/app/assets/stylesheets/application.css +0 -15
  153. data/example/rails/simplepay/app/assets/stylesheets/style.css +0 -254
  154. data/example/rails/simplepay/app/channels/application_cable/channel.rb +0 -4
  155. data/example/rails/simplepay/app/channels/application_cable/connection.rb +0 -4
  156. data/example/rails/simplepay/app/controllers/application_controller.rb +0 -3
  157. data/example/rails/simplepay/app/controllers/concerns/.keep +0 -0
  158. data/example/rails/simplepay/app/controllers/home_controller.rb +0 -16
  159. data/example/rails/simplepay/app/controllers/pay_controller.rb +0 -10
  160. data/example/rails/simplepay/app/helpers/application_helper.rb +0 -2
  161. data/example/rails/simplepay/app/helpers/pay_helper.rb +0 -2
  162. data/example/rails/simplepay/app/jobs/application_job.rb +0 -2
  163. data/example/rails/simplepay/app/mailers/application_mailer.rb +0 -4
  164. data/example/rails/simplepay/app/models/application_record.rb +0 -3
  165. data/example/rails/simplepay/app/models/concerns/.keep +0 -0
  166. data/example/rails/simplepay/app/models/order.rb +0 -2
  167. data/example/rails/simplepay/app/views/home/index.html.erb +0 -19
  168. data/example/rails/simplepay/app/views/layouts/application.html.erb +0 -15
  169. data/example/rails/simplepay/app/views/layouts/mailer.html.erb +0 -13
  170. data/example/rails/simplepay/app/views/layouts/mailer.text.erb +0 -1
  171. data/example/rails/simplepay/app/views/pay/notify.html.erb +0 -2
  172. data/example/rails/simplepay/bin/bundle +0 -3
  173. data/example/rails/simplepay/bin/rails +0 -9
  174. data/example/rails/simplepay/bin/rake +0 -9
  175. data/example/rails/simplepay/bin/setup +0 -34
  176. data/example/rails/simplepay/bin/spring +0 -16
  177. data/example/rails/simplepay/bin/update +0 -29
  178. data/example/rails/simplepay/config/application.rb +0 -15
  179. data/example/rails/simplepay/config/boot.rb +0 -3
  180. data/example/rails/simplepay/config/cable.yml +0 -9
  181. data/example/rails/simplepay/config/database.yml +0 -25
  182. data/example/rails/simplepay/config/environment.rb +0 -5
  183. data/example/rails/simplepay/config/environments/development.rb +0 -57
  184. data/example/rails/simplepay/config/environments/production.rb +0 -86
  185. data/example/rails/simplepay/config/environments/test.rb +0 -42
  186. data/example/rails/simplepay/config/initializers/application_controller_renderer.rb +0 -6
  187. data/example/rails/simplepay/config/initializers/assets.rb +0 -11
  188. data/example/rails/simplepay/config/initializers/backtrace_silencers.rb +0 -7
  189. data/example/rails/simplepay/config/initializers/cookies_serializer.rb +0 -5
  190. data/example/rails/simplepay/config/initializers/filter_parameter_logging.rb +0 -4
  191. data/example/rails/simplepay/config/initializers/inflections.rb +0 -16
  192. data/example/rails/simplepay/config/initializers/mime_types.rb +0 -4
  193. data/example/rails/simplepay/config/initializers/new_framework_defaults.rb +0 -24
  194. data/example/rails/simplepay/config/initializers/session_store.rb +0 -3
  195. data/example/rails/simplepay/config/initializers/veritrans.rb +0 -46
  196. data/example/rails/simplepay/config/initializers/wrap_parameters.rb +0 -14
  197. data/example/rails/simplepay/config/locales/en.yml +0 -23
  198. data/example/rails/simplepay/config/puma.rb +0 -47
  199. data/example/rails/simplepay/config/routes.rb +0 -5
  200. data/example/rails/simplepay/config/secrets.yml +0 -22
  201. data/example/rails/simplepay/config/spring.rb +0 -6
  202. data/example/rails/simplepay/config/veritrans.yml +0 -18
  203. data/example/rails/simplepay/config.ru +0 -5
  204. data/example/rails/simplepay/db/development.sqlite3 +0 -0
  205. data/example/rails/simplepay/db/migrate/20161221090855_create_orders.rb +0 -10
  206. data/example/rails/simplepay/db/schema.rb +0 -22
  207. data/example/rails/simplepay/db/seeds.rb +0 -7
  208. data/example/rails/simplepay/db/test.sqlite3 +0 -0
  209. data/example/rails/simplepay/lib/assets/.keep +0 -0
  210. data/example/rails/simplepay/lib/tasks/.keep +0 -0
  211. data/example/rails/simplepay/log/.keep +0 -0
  212. data/example/rails/simplepay/public/404.html +0 -67
  213. data/example/rails/simplepay/public/422.html +0 -67
  214. data/example/rails/simplepay/public/500.html +0 -66
  215. data/example/rails/simplepay/public/apple-touch-icon-precomposed.png +0 -0
  216. data/example/rails/simplepay/public/apple-touch-icon.png +0 -0
  217. data/example/rails/simplepay/public/favicon.ico +0 -0
  218. data/example/rails/simplepay/public/robots.txt +0 -5
  219. data/example/rails/simplepay/vendor/assets/javascripts/.keep +0 -0
  220. data/example/rails/simplepay/vendor/assets/stylesheets/.keep +0 -0
  221. data/example/sinatra/config.ru +0 -9
  222. data/example/sinatra/localization.erb +0 -249
  223. data/example/sinatra/points.erb +0 -188
  224. data/example/sinatra/recurring.erb +0 -201
  225. data/example/sinatra/sinatra.rb +0 -194
  226. data/example/sinatra/style.css +0 -137
  227. data/example/sinatra/veritrans.yml +0 -11
  228. data/example/sinatra/widget.erb +0 -63
  229. data/lib/generators/templates/assets/credit_card_form.js +0 -57
  230. data/lib/generators/templates/payments_controller.rb +0 -85
  231. data/lib/generators/templates/veritrans.rb +0 -46
  232. data/lib/generators/templates/veritrans.yml +0 -18
  233. data/lib/generators/templates/views/_credit_card_form.erb +0 -42
  234. data/lib/generators/templates/views/_veritrans_include.erb +0 -10
  235. data/lib/generators/templates/views/payments/create.erb +0 -15
  236. data/lib/generators/templates/views/payments/new.erb +0 -6
  237. data/lib/generators/veritrans/install_generator.rb +0 -32
  238. data/lib/generators/veritrans/payment_form_generator.rb +0 -45
  239. data/lib/veritrans/cli.rb +0 -161
  240. data/lib/veritrans/testing.rb +0 -156
  241. data/spec/cli_spec.rb +0 -83
  242. data/spec/configs/real_key.yml +0 -4
  243. data/spec/configs/veritrans.yml +0 -7
  244. data/spec/configs/veritrans_flat.yml +0 -2
  245. data/spec/configs/veritrans_with_erb.yml +0 -2
  246. data/spec/fixtures/approve_failed.yml +0 -48
  247. data/spec/fixtures/cancel_failed.yml +0 -48
  248. data/spec/fixtures/cancel_success.yml +0 -126
  249. data/spec/fixtures/capture_failed.yml +0 -48
  250. data/spec/fixtures/charge.yml +0 -50
  251. data/spec/fixtures/charge_direct.yml +0 -56
  252. data/spec/fixtures/charge_vtweb.yml +0 -50
  253. data/spec/fixtures/cli_test_1111-not-exists.yml +0 -45
  254. data/spec/fixtures/cli_test_not_exists.yml +0 -45
  255. data/spec/fixtures/cli_test_real_txn.yml +0 -55
  256. data/spec/fixtures/cli_test_unauthorized.yml +0 -47
  257. data/spec/fixtures/deny_failed.yml +0 -144
  258. data/spec/fixtures/events_test_real_txn.yml +0 -55
  259. data/spec/fixtures/expire_failed.yml +0 -50
  260. data/spec/fixtures/expire_success.yml +0 -56
  261. data/spec/fixtures/midtrans_status.yml +0 -117
  262. data/spec/fixtures/refund_failed.yml +0 -54
  263. data/spec/fixtures/status_fail.yml +0 -48
  264. data/spec/fixtures/status_success.yml +0 -128
  265. data/spec/fixtures/test_token.yml +0 -49
  266. data/spec/midtrans_rename_spec.rb +0 -27
  267. data/spec/rails_plugin_spec.rb +0 -317
  268. data/spec/spec_helper.rb +0 -71
  269. data/spec/veritrans_client_spec.rb +0 -235
  270. data/spec/veritrans_config_spec.rb +0 -70
  271. data/spec/veritrans_events_spec.rb +0 -72
  272. data/spec/veritrans_logger_spec.rb +0 -46
  273. data/spec/veritrans_snap_spec.rb +0 -54
  274. data/spec/veritrans_testing_spec.rb +0 -184
  275. data/testing_webhooks.md +0 -78
data/README.md CHANGED
@@ -1,356 +1,329 @@
1
- # Veritrans ruby library
1
+ # Midtrans Ruby library
2
2
 
3
- Veritrans gem is the library that will help you to integrate seamlessly with
4
- Midtrans (formerly known as Veritrans Indonesia).
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
- To see it in action, we have made 3 examples:
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
- bundle install
25
+ ## 2. Usage
26
+ ### 2.1 Choose Product/Method
24
27
 
25
- ### Generate veritrans.yml
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
- rails g veritrans:install
33
+ Choose one that you think best for your unique needs.
28
34
 
29
- ### Create simple payment form (optional)
35
+ ### 2.2 Client Initialization and Configuration
30
36
 
31
- rails g veritrans:payment_form
37
+ Get your client key and server key from [Midtrans Dashboard](https://dashboard.midtrans.com)
32
38
 
33
- ### Edit api keys in config/veritrans.yml
39
+ Create instance of Midtrans client
34
40
 
35
- ```yml
36
- # config/veritrans.yml
37
- development:
38
- client_key: # your api client key
39
- server_key: # your api client key
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
- ## STEP 1: Process credit cards
64
+ ### 2.2.A Snap
65
+ You can see Snap example [with Sinatra](example/sinatra) and [without framework](example/snap).
43
66
 
44
- #### Snap
67
+ ```ruby
68
+ # Create Snap payment page, with this version returning full API response
69
+ create_snap_token(parameter)
45
70
 
46
- ##### Pop-up
71
+ # Create Snap payment page, with this version returning token
72
+ create_snap_token_string(parameter)
47
73
 
48
- This will result in payment page being a pop-up(iframe) inside your own web page, no need redirection, similar to our [demo](http://demo.midtrans.com)
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
- First, generate token in the back end provided with enough details as necessary
51
- and as detailed as you wish to.
79
+ #### Get Snap Token
52
80
 
53
81
  ```ruby
54
- response = Veritrans.create_widget_token(
82
+ result = Midtrans.create_snap_token(
55
83
  transaction_details: {
56
- order_id: 'THE-ITEM-ORDER-ID',
57
- gross_amount: 200000
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
- Then on the front end, the token is saved somewhere probably in the hidden field.
92
+ #### Initialize Snap JS when customer click pay button
65
93
 
66
- ```
67
- <div class='cart'>
68
- <!-- some other codes -->
69
- <input type='hidden' name='snap-token' id='snap-token' value='<%= %>'>
70
- <%= hidden_field_tag 'snap_token', @snap_token %>
71
- <a href='#' class='order-button'>Order</a>
72
- </div>
73
- ```
74
-
75
- Then JavaScript can be used to invoke the SNAP dialog upon click on the
76
- order button.
77
-
78
- ```javascript
79
- var token = jQuery("#snap_token").val();
80
-
81
- jQuery(".order-button").on("click", function() {
82
- snap.pay(token, {
83
- onSuccess: function(res) { console.log("SUCCESS RESPONSE", res); },
84
- // you may also implement:
85
- // onPending
86
- // onError
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
- ##### Redirection
127
+ ### 2.2.B Snap Redirect
128
+ You can see Snap example [with Sinatra](example/sinatra) and [without framework](example/snap).
92
129
 
93
- This will result in redirect_url, you can redirect customer to the url, payment page is securely hosted by Midtrans.
130
+ #### Get Redirection URL of a Payment Page
94
131
 
95
132
  ```ruby
96
- @result = Veritrans.create_snap_redirect_url(
133
+ result = Midtrans.create_snap_redirect_url(
97
134
  transaction_details: {
98
- order_id: "my-unique-order-id",
99
- gross_amount: 100_000
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
- > This is similar feature as old VT-Web
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
- #### VT-Web
146
+ Available methods for `CoreApi` class
109
147
 
110
- > !!! WARNING NOTE: VT-Web is deprecated, please use [Snap](#Snap) instead, it has better previous VT-Web feature and many more improvements, including redirect_url.
148
+ ```ruby
149
+ # charge : Do `/charge` API request to Midtrans Core API
150
+ def charge(payment_type, data = nil)
111
151
 
112
- *If you want to use VT-Web, add `payment_type: "VTWEB"`*
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
- ```ruby
115
- @result = Veritrans.charge(
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
- redirect_to @result.redirect_url
124
- ```
161
+ # approve : Do `/{orderId}/approve` API request to Midtrans Core API
162
+ def approve(payment_id, options = {})
125
163
 
126
- #### VT-Direct / Core API
164
+ # deny : Do `/{orderId}/deny` API request to Midtrans Core API
165
+ def deny(payment_id, options = {})
127
166
 
128
- It's little more complicated, because credit_card is sensitive data,
129
- you need put credit card number in our safe storage first using `midtrans.min.js` library, then send received token to with other payment details.
167
+ # cancel : Do `/{orderId}/cancel` API request to Midtrans Core API
168
+ def cancel(payment_id, options = {})
130
169
 
131
- We don't want you to send credit card number to your server, especially for websites not using https.
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
- File: "app/views/shared/_veritrans_include.erb"
176
+ # capture : Do `/{orderId}/capture` API request to Midtrans Core API
177
+ def capture(payment_id, gross_amount, options = {})
134
178
 
135
- ```html
136
- <script src="https://api.midtrans.com/v2/assets/js/midtrans.min.js"></script>
179
+ # link_payment_account : Do `/pay/account` API request to Midtrans Core API
180
+ def link_payment_account(param)
137
181
 
138
- <script type="text/javascript">
139
- Veritrans.url = "<%= Veritrans.config.api_host %>/v2/token";
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
- Payment form: (same as if you use `rails g veritrans:payment_form`)
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
- For sinatra:
188
+ # create_subscription : Do `/subscription` API request to Midtrans Core API
189
+ def create_subscription(param)
168
190
 
169
- ```html
170
- <form action="/charge_vtdirect" method="post" id="card_form">
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
- Sending "get-token" request:
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
- On a server side:
197
+ # enable_subscription : Do `/subscription/{subscription_id}/enable` API request to Midtrans Core API
198
+ def enable_subscription(subscription_id)
237
199
 
238
- ```ruby
239
- @result = Veritrans.charge(
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
- ## STEP 2: Process non credit cards payment
204
+ #### Credit Card Get Token
253
205
 
254
- We provide many payment channels to accept payment, but the API call is almost the same.
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
- For Snap / VT-Web in only one request, payment page will display all available payment options.
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
- @result = Veritrans.charge(
262
- payment_type: "bank_transfer",
263
- bank_transfer: { bank: 'permata' },
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: @payment.order_id,
266
- gross_amount: @payment.amount
267
- }
268
- )
269
- puts "Please transfer fund to account no. #{@result.permata_va_number} in bank Permata"
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
- See [our documentation](https://api-docs.midtrans.com/#charge-features) for other available options.
273
-
274
-
275
- ## STEP 3: Receive notification callback
226
+ #### Credit Card 3DS Authentication
276
227
 
277
- For every transaction success and failed we will send you HTTP POST notification (aka webhook)
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
- First you should set callback url in our dashboard https://dashboard.sandbox.midtrans.com/settings/vtweb_configuration
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
- For rails:
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
- # config/routes.rb
288
- match "/payments/receive_webhook" => "payments#receive_webhook", via: [:post]
289
-
290
- # app/controllers/payments_controller.rb
291
- def receive_webhook
292
- post_body = request.body.read
293
- callback_params = Veritrans.decode_notification_json(post_body)
294
-
295
- verified_data = Veritrans.status(callback_params['transaction_id'])
296
-
297
- if verified_data.status_code != 404
298
- puts "--- Transaction callback ---"
299
- puts "Payment: #{verified_data.data[:order_id]}"
300
- puts "Payment type: #{verified_data.data[:payment_type]}"
301
- puts "Payment status: #{verified_data.data[:transaction_status]}"
302
- puts "Fraud status: #{verified_data.data[:fraud_status]}" if verified_data.data[:fraud_status]
303
- puts "Payment amount: #{verified_data.data[:gross_amount]}"
304
- puts "--- Transaction callback ---"
305
-
306
- render text: "ok"
307
- else
308
- render text: "ok", :status => :not_found
309
- end
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
- Other option to handle callbacks is our rack-based handler
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
- # config/routes.rb
321
- mount Veritrans::Events.new => '/vt_events'
322
-
323
- # config/initalizers/veritrans.rb
324
- Veritrans.setup do
325
- config.server_key = "..."
326
- config.client_key = "..."
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
- #### Logging
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
- By default gem veritrans will show information via rails' logger. And in addition save important information to `RAILS_APP/log/veritrans.log`
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
- It's configurable.
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
- Veritrans.logger = Rails.logger
350
- Veritrans.file_logger = Logger.new("/my/important_logs/veritrans.log")
321
+ Midtrans.logger = Rails.logger
322
+ # To set custom logger
323
+ Midtrans.file_logger = Logger.new("./log/midtrans.log")
351
324
  ```
352
325
 
353
- `Veritrans.file_logger` save information about:
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
- #### Command line tool
363
-
364
- **Installation**
365
-
366
- $ gem install veritrans
367
-
368
- **Usage**
335
+ ### To see it in action, we have made example:
369
336
 
370
- Testing http notification:
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.