veritrans 2.1.2 → 2.4.0

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.
Files changed (98) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +10 -1
  3. data/.idea/.gitignore +8 -0
  4. data/.rubocop.yml +35 -0
  5. data/.travis.yml +10 -5
  6. data/CHANGELOG.md +45 -0
  7. data/Gemfile +6 -7
  8. data/Gemfile.lock +134 -146
  9. data/Maintaining.MD +8 -0
  10. data/README.md +266 -226
  11. data/api_reference.md +534 -143
  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 +6 -0
  16. data/example/sinatra/index.erb +202 -0
  17. data/example/sinatra/response.erb +1 -0
  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 +146 -22
  31. data/lib/veritrans/client.rb +48 -12
  32. data/lib/veritrans/config.rb +31 -6
  33. data/lib/veritrans/events.rb +46 -35
  34. data/lib/veritrans/midtrans_error.rb +15 -0
  35. data/lib/veritrans/result.rb +66 -5
  36. data/lib/veritrans/version.rb +1 -1
  37. data/lib/veritrans.rb +121 -12
  38. data/veritrans.gemspec +2 -9
  39. metadata +30 -146
  40. data/.rspec +0 -2
  41. data/Procfile +0 -1
  42. data/Rakefile +0 -16
  43. data/bin/midtrans +0 -3
  44. data/bin/veritrans +0 -68
  45. data/example/README.md +0 -8
  46. data/example/config.ru +0 -6
  47. data/example/index.erb +0 -213
  48. data/example/localization.erb +0 -248
  49. data/example/points.erb +0 -187
  50. data/example/recurring.erb +0 -201
  51. data/example/response.erb +0 -37
  52. data/example/sinatra.rb +0 -188
  53. data/example/style.css +0 -126
  54. data/example/veritrans.yml +0 -11
  55. data/example/widget.erb +0 -51
  56. data/lib/generators/templates/assets/credit_card_form.js +0 -51
  57. data/lib/generators/templates/payments_controller.rb +0 -85
  58. data/lib/generators/templates/veritrans.rb +0 -46
  59. data/lib/generators/templates/veritrans.yml +0 -18
  60. data/lib/generators/templates/views/_credit_card_form.erb +0 -42
  61. data/lib/generators/templates/views/_veritrans_include.erb +0 -10
  62. data/lib/generators/templates/views/payments/create.erb +0 -15
  63. data/lib/generators/templates/views/payments/new.erb +0 -6
  64. data/lib/generators/veritrans/install_generator.rb +0 -32
  65. data/lib/generators/veritrans/payment_form_generator.rb +0 -45
  66. data/lib/veritrans/cli.rb +0 -155
  67. data/lib/veritrans/core_extensions.rb +0 -32
  68. data/spec/cli_spec.rb +0 -83
  69. data/spec/configs/real_key.yml +0 -4
  70. data/spec/configs/veritrans.yml +0 -7
  71. data/spec/configs/veritrans_flat.yml +0 -2
  72. data/spec/configs/veritrans_with_erb.yml +0 -2
  73. data/spec/fixtures/approve_failed.yml +0 -48
  74. data/spec/fixtures/cancel_failed.yml +0 -48
  75. data/spec/fixtures/cancel_success.yml +0 -106
  76. data/spec/fixtures/capture_failed.yml +0 -48
  77. data/spec/fixtures/charge.yml +0 -50
  78. data/spec/fixtures/charge_direct.yml +0 -56
  79. data/spec/fixtures/charge_vtweb.yml +0 -50
  80. data/spec/fixtures/cli_test_1111-not-exists.yml +0 -45
  81. data/spec/fixtures/cli_test_not_exists.yml +0 -45
  82. data/spec/fixtures/cli_test_real_txn.yml +0 -55
  83. data/spec/fixtures/cli_test_unauthorized.yml +0 -47
  84. data/spec/fixtures/events_test_real_txn.yml +0 -55
  85. data/spec/fixtures/expire_failed.yml +0 -50
  86. data/spec/fixtures/expire_success.yml +0 -56
  87. data/spec/fixtures/midtrans_status.yml +0 -117
  88. data/spec/fixtures/status_fail.yml +0 -46
  89. data/spec/fixtures/status_success.yml +0 -109
  90. data/spec/midtrans_rename_spec.rb +0 -27
  91. data/spec/rails_plugin_spec.rb +0 -281
  92. data/spec/spec_helper.rb +0 -61
  93. data/spec/veritrans_client_spec.rb +0 -184
  94. data/spec/veritrans_config_spec.rb +0 -70
  95. data/spec/veritrans_events_spec.rb +0 -72
  96. data/spec/veritrans_logger_spec.rb +0 -46
  97. data/spec/veritrans_snap_spec.rb +0 -39
  98. data/testing_webhooks.md +0 -78
@@ -0,0 +1,160 @@
1
+ require 'veritrans'
2
+ require 'minitest/autorun'
3
+
4
+ class TestVeritrans < Minitest::Test
5
+
6
+ def setup
7
+ @mt_test = Veritrans.new(
8
+ server_key: "SB-Mid-server-uQmMImQMeo0Ky3Svl90QTUj2",
9
+ client_key: "SB-Mid-client-ArNfhrh7st9bQKmz",
10
+ api_host: "https://api.sandbox.midtrans.com",
11
+ logger: Logger.new(STDOUT),
12
+ file_logger: Logger.new(STDOUT)
13
+ )
14
+ end
15
+
16
+ def test_charge_permata_va
17
+ result = @mt_test.charge(
18
+ {
19
+ "payment_type": "bank_transfer",
20
+ "transaction_details": {
21
+ "gross_amount": 10000,
22
+ "order_id": "ruby-lib-test-#{Time.now.to_i}"
23
+ },
24
+ "bank_transfer": {
25
+ "bank": "permata",
26
+ "va_number": "1234567891"
27
+ }
28
+ }
29
+ )
30
+ assert_equal 201, result.status_code
31
+ assert_equal "pending", result.transaction_status
32
+ assert_equal "Success, PERMATA VA transaction is successful", result.status_message
33
+ end
34
+
35
+ def test_cancel_permata_va
36
+ charge = @mt_test.charge(
37
+ {
38
+ "payment_type": "bank_transfer",
39
+ "transaction_details": {
40
+ "gross_amount": 10000,
41
+ "order_id": "ruby-lib-test-#{Time.now.to_i}"
42
+ },
43
+ "bank_transfer": {
44
+ "bank": "permata",
45
+ "va_number": "1234567891"
46
+ }
47
+ }
48
+ )
49
+ cancel = @mt_test.cancel(charge.transaction_id)
50
+ assert_equal 200, cancel.status_code
51
+ assert_equal "cancel", cancel.transaction_status
52
+ end
53
+
54
+ def test_expire_permata_va
55
+ charge = @mt_test.charge(
56
+ {
57
+ "payment_type": "bank_transfer",
58
+ "transaction_details": {
59
+ "gross_amount": 10000,
60
+ "order_id": "ruby-lib-test-#{Time.now.to_i}"
61
+ },
62
+ "bank_transfer": {
63
+ "bank": "permata",
64
+ "va_number": "1234567891"
65
+ }
66
+ }
67
+ )
68
+ expire = @mt_test.expire(charge.transaction_id)
69
+ assert_equal "Success, transaction has expired", expire.status_message
70
+ assert_equal 407, expire.status_code
71
+ assert_equal "expire", expire.transaction_status
72
+ end
73
+
74
+ def test_get_status_transaction
75
+ charge = @mt_test.charge(
76
+ {
77
+ "payment_type": "bank_transfer",
78
+ "transaction_details": {
79
+ "gross_amount": 10000,
80
+ "order_id": "ruby-lib-test-#{Time.now.to_i}"
81
+ },
82
+ "bank_transfer": {
83
+ "bank": "permata",
84
+ "va_number": "1234567891"
85
+ }
86
+ }
87
+ )
88
+ result = @mt_test.status(charge.transaction_id)
89
+ assert_equal 201, result.status_code
90
+ assert_equal "Success, transaction is found", result.status_message
91
+ end
92
+
93
+ def test_approve_transaction
94
+ card = {
95
+ card_number: 5510111111111115,
96
+ card_cvv: 123,
97
+ card_exp_month: 12,
98
+ card_exp_year: 2025
99
+ }
100
+ get_token = @mt_test.test_token(card)
101
+ charge = @mt_test.charge(
102
+ {
103
+ "payment_type": "credit_card",
104
+ "transaction_details": {
105
+ "gross_amount": 10000,
106
+ "order_id": "ruby-lib-test-#{Time.now.to_i}"
107
+ },
108
+ "credit_card": {
109
+ "token_id": "#{get_token.token_id}",
110
+ "authentication": "false"
111
+ }
112
+ }
113
+ )
114
+ result = @mt_test.approve(charge.transaction_id)
115
+ assert_equal "Success, transaction is approved", result.status_message
116
+ assert_equal 200, result.status_code
117
+ assert_equal "accept", result.fraud_status
118
+ end
119
+
120
+ def test_deny_transaction
121
+ card = {
122
+ card_number: 5510111111111115,
123
+ card_cvv: 123,
124
+ card_exp_month: 12,
125
+ card_exp_year: 2025
126
+ }
127
+ get_token = @mt_test.test_token(card)
128
+ charge = @mt_test.charge(
129
+ {
130
+ "payment_type": "credit_card",
131
+ "transaction_details": {
132
+ "gross_amount": 10000,
133
+ "order_id": "ruby-lib-test-#{Time.now.to_i}"
134
+ },
135
+ "credit_card": {
136
+ "token_id": "#{get_token.token_id}",
137
+ "authentication": "false"
138
+ }
139
+ }
140
+ )
141
+ result = @mt_test.deny(charge.transaction_id)
142
+ assert_equal 200, result.status_code
143
+ assert_equal "Success, transaction is denied", result.status_message
144
+ assert_equal "deny", result.fraud_status
145
+ end
146
+
147
+ def test_fail_refund_transaction
148
+ begin
149
+ param = {
150
+ "refund_key": "reference1",
151
+ "amount": 5000,
152
+ "reason": "for some reason"
153
+ }
154
+ @mt_test.refund("dummy-order-id", param)
155
+ rescue MidtransError => e
156
+ assert_equal "404", e.status
157
+ end
158
+ end
159
+
160
+ end
data/lib/veritrans/api.rb CHANGED
@@ -1,13 +1,12 @@
1
1
  # Veritrans API methods
2
2
 
3
- require 'uri'
3
+ require 'erb'
4
4
 
5
5
  class Veritrans
6
6
  module Api
7
7
 
8
- # POST /v2/charge { payment_type: "vtlink" }
9
- # Docs http://docs.veritrans.co.id/vtdirect/integration_cc.html#step2
10
- # Docs http://docs.veritrans.co.id/sandbox/charge.html
8
+ # POST /v2/charge { payment_type: "credit_card" }
9
+ # Docs https://api-docs.midtrans.com/#charge-features
11
10
  #
12
11
  # Example:
13
12
  # Veritrans.charge(
@@ -29,7 +28,7 @@ class Veritrans
29
28
  data[:payment_type] = payment_type if payment_type
30
29
 
31
30
  if data.has_key?(:payment_options)
32
- data[ payment_type.to_sym ] = data.delete(:payment_options)
31
+ data[payment_type.to_sym] = data.delete(:payment_options)
33
32
  end
34
33
 
35
34
  # Rename keys:
@@ -38,54 +37,87 @@ class Veritrans
38
37
  # items -> item_details
39
38
  # customer -> customer_details
40
39
 
41
- data[:transaction_details] = data.delete(:payment) if data[:payment]
42
- data[:transaction_details] = data.delete(:transaction) if data[:transaction]
43
- data[:item_details] = data.delete(:items) if data[:items]
44
- data[:customer_details] = data.delete(:customer) if data[:customer]
40
+ data[:transaction_details] = data.delete(:payment) if data[:payment]
41
+ data[:transaction_details] = data.delete(:transaction) if data[:transaction]
42
+ data[:item_details] = data.delete(:items) if data[:items]
43
+ data[:customer_details] = data.delete(:customer) if data[:customer]
45
44
 
46
45
  request_with_logging(:post, config.api_host + "/v2/charge", data)
47
46
  end
48
47
 
49
- # POST https://app.sandbox.veritrans.co.id/snap/v1/charge
48
+ def test_token(options = {})
49
+ options[:client_key] = config.client_key
50
+ request_with_logging(:get, config.api_host + '/v2/token', options)
51
+ end
52
+
53
+ alias_method :create_card_token, :test_token
54
+
55
+ # POST https://app.sandbox.midtrans.com/snap/v1/transactions
56
+ # Create Snap payment page, with this version returning full API response
50
57
  def create_snap_token(options = {})
51
- result = request_with_logging(:post, config.api_host.sub('//api.', '//app.') + "/snap/v1/charge", options)
58
+ result = request_with_logging(:post, config.api_host.sub('//api.', '//app.') + "/snap/v1/transactions", options)
52
59
  Veritrans::SnapResult.new(result.response, result.url, result.request_options, result.time)
53
60
  end
54
61
 
55
62
  alias_method :create_widget_token, :create_snap_token
63
+ alias_method :create_snap_redirect_url, :create_snap_token
64
+
65
+ # POST https://app.sandbox.midtrans.com/snap/v1/transactions
66
+ # Create Snap payment page, with this version returning token
67
+ def create_snap_token_string(options = {})
68
+ result = create_snap_token(options)
69
+ result.token
70
+ end
71
+
72
+ # POST https://app.sandbox.midtrans.com/snap/v1/transactions
73
+ # Create Snap payment page, with this version returning redirect url
74
+ def create_snap_redirect_url_str(options = {})
75
+ result = create_snap_token(options)
76
+ result.redirect_url
77
+ end
56
78
 
57
79
  # POST /v2/{id}/cancel
58
- # Docs http://docs.veritrans.co.id/en/api/methods.html#Cancel
80
+ # Docs https://api-docs.midtrans.com/#cancel-transaction
59
81
  def cancel(payment_id, options = {})
60
82
  if !payment_id || payment_id.to_s == ""
61
83
  raise ArgumentError, "parameter payment_id can not be blank (got #{payment_id.class} : #{payment_id.inspect})"
62
84
  end
63
85
 
64
- request_with_logging(:post, config.api_host + "/v2/#{URI.escape(payment_id)}/cancel", options)
86
+ request_with_logging(:post, config.api_host + "/v2/#{ERB::Util.url_encode(payment_id)}/cancel", options)
65
87
  end
66
88
 
67
89
  # POST /v2/{id}/approve
68
- # Docs http://docs.veritrans.co.id/en/api/methods.html#Approve
90
+ # Docs https://api-docs.midtrans.com/#approve-transaction
69
91
  def approve(payment_id, options = {})
70
92
  if !payment_id || payment_id.to_s == ""
71
93
  raise ArgumentError, "parameter payment_id can not be blank (got #{payment_id.class} : #{payment_id.inspect})"
72
94
  end
73
95
 
74
- request_with_logging(:post, config.api_host + "/v2/#{URI.escape(payment_id)}/approve", options)
96
+ request_with_logging(:post, config.api_host + "/v2/#{ERB::Util.url_encode(payment_id)}/approve", options)
97
+ end
98
+
99
+ # POST /v2/{id}/refund
100
+ # Docs https://api-docs.midtrans.com/#refund-transaction
101
+ def refund(payment_id, options = {})
102
+ if !payment_id || payment_id.to_s == ''
103
+ raise ArgumentError, "parameter payment_id can not be blank (got #{payment_id.class} : #{payment_id.inspect})"
104
+ end
105
+
106
+ request_with_logging(:post, config.api_host + "/v2/#{ERB::Util.url_encode(payment_id)}/refund", options)
75
107
  end
76
108
 
77
109
  # GET /v2/{id}/status
78
- # Docs http://docs.veritrans.co.id/en/api/methods.html#Status
110
+ # Docs https://api-docs.midtrans.com/#get-status-transaction
79
111
  def status(payment_id)
80
112
  if !payment_id || payment_id.to_s == ""
81
113
  raise ArgumentError, "parameter payment_id can not be blank (got #{payment_id.class} : #{payment_id.inspect})"
82
114
  end
83
115
 
84
- get(config.api_host + "/v2/#{URI.escape(payment_id)}/status")
116
+ get(config.api_host + "/v2/#{ERB::Util.url_encode(payment_id)}/status")
85
117
  end
86
118
 
87
119
  # POST /v2/capture
88
- # Docs http://docs.veritrans.co.id/en/api/methods.html#Capture
120
+ # Docs https://api-docs.midtrans.com/#capture-transaction
89
121
  def capture(payment_id, gross_amount, options = {})
90
122
  if !payment_id || payment_id.to_s == ""
91
123
  raise ArgumentError, "parameter payment_id can not be blank (got #{payment_id.class} : #{payment_id.inspect})"
@@ -94,13 +126,24 @@ class Veritrans
94
126
  post(config.api_host + "/v2/capture", options.merge(transaction_id: payment_id, gross_amount: gross_amount))
95
127
  end
96
128
 
129
+ # POST /v2/{id}/deny
130
+ # Docs https://api-docs.midtrans.com/#deny-transaction
131
+ def deny(payment_id, options = {})
132
+ if !payment_id || payment_id.to_s == ''
133
+ raise ArgumentError, "parameter payment_id can not be blank (got #{payment_id.class} : #{payment_id.inspect})"
134
+ end
135
+
136
+ request_with_logging(:post, config.api_host + "/v2/#{ERB::Util.url_encode(payment_id)}/deny", options)
137
+ end
138
+
97
139
  # POST /v2/{id}/expire
140
+ # Docs https://api-docs.midtrans.com/#expire-transaction
98
141
  def expire(payment_id)
99
142
  if !payment_id || payment_id.to_s == ""
100
143
  raise ArgumentError, "parameter payment_id can not be blank (got #{payment_id.class} : #{payment_id.inspect})"
101
144
  end
102
145
 
103
- request_with_logging(:post, config.api_host + "/v2/#{URI.escape(payment_id)}/expire", nil)
146
+ request_with_logging(:post, config.api_host + "/v2/#{ERB::Util.url_encode(payment_id)}/expire", nil)
104
147
  end
105
148
 
106
149
  # POST /v2/charge { payment_type: "vtlink" }
@@ -112,7 +155,7 @@ class Veritrans
112
155
 
113
156
  # DELETE /v2/vtlink/{id}
114
157
  def delete_vtlink(id, options)
115
- request_with_logging(:delete, config.api_host + "/v2/vtlink/#{URI.escape(id)}", options)
158
+ request_with_logging(:delete, config.api_host + "/v2/vtlink/#{ERB::Util.url_encode(id)}", options)
116
159
  end
117
160
 
118
161
  # GET /v2/point_inquiry/{token_id}
@@ -123,7 +166,88 @@ class Veritrans
123
166
 
124
167
  request_with_logging(:get, config.api_host + "/v2/point_inquiry/#{token_id}", {})
125
168
  end
169
+
126
170
  alias_method :point_inquiry, :inquiry_points
127
171
 
128
- end
129
- end
172
+ # POST /v2/pay/account
173
+ # param create pay account request (more params detail refer to: https://api-docs.midtrans.com/#create-pay-account)
174
+ def link_payment_account(param)
175
+ if param == nil
176
+ raise ArgumentError, "parameter Payment_type gopay cannot be blank"
177
+ end
178
+
179
+ request_with_logging(:post, config.api_host + "/v2/pay/account", param)
180
+ end
181
+
182
+ # GET /v2/pay/account/{account_id}
183
+ # account_id (more params detail refer to: https://api-docs.midtrans.com/#get-pay-account)
184
+ def get_payment_account(account_id)
185
+ if account_id == nil || account_id == ''
186
+ raise ArgumentError, "account_id cannot be blank"
187
+ end
188
+
189
+ request_with_logging(:get, config.api_host + "/v2/pay/account/#{account_id}", {})
190
+ end
191
+
192
+ # POST /v2/pay/account/{account_id}/unbind
193
+ # account_id (more params detail refer to: https://api-docs.midtrans.com/#unbind-pay-account)
194
+ def unlink_payment_account(account_id)
195
+ if account_id == nil || account_id == ''
196
+ raise ArgumentError, "parameter account_id cannot be blank"
197
+ end
198
+
199
+ request_with_logging(:post, config.api_host + "/v2/pay/account/#{account_id}/unbind", {})
200
+ end
201
+
202
+ # POST /v1/subscription
203
+ # param create subscription request (more params detail refer to: https://api-docs.midtrans.com/#create-subscription)
204
+ def create_subscription(param)
205
+ if param == nil
206
+ raise ArgumentError, "parameter subscription cannot be blank"
207
+ end
208
+
209
+ request_with_logging(:post, config.api_host + "/v1/subscriptions", param)
210
+ end
211
+
212
+ # GET /v1/subscription/{subscription_id}
213
+ # get subscription request (more params detail refer to: https://api-docs.midtrans.com/#get-subscription)
214
+ def get_subscription(subscription_id)
215
+ if subscription_id == nil || subscription_id == ''
216
+ raise ArgumentError, "subscription_id cannot be blank"
217
+ end
218
+
219
+ request_with_logging(:get, config.api_host + "/v1/subscriptions/#{subscription_id}", {})
220
+ end
221
+
222
+ # POST /v1/subscription/{subscription_id}/disable
223
+ # disable subscription request (more params detail refer to: https://api-docs.midtrans.com/#disable-subscription)
224
+ def disable_subscription(subscription_id)
225
+ if subscription_id == nil || subscription_id == ''
226
+ raise ArgumentError, "subscription_id cannot be blank"
227
+ end
228
+
229
+ request_with_logging(:post, config.api_host + "/v1/subscriptions/#{subscription_id}/disable", {})
230
+ end
231
+
232
+ # POST /v1/subscription/{subscription_id}/disable
233
+ # enable subscription request (more params detail refer to: https://api-docs.midtrans.com/#enable-subscription)
234
+ def enable_subscription(subscription_id)
235
+ if subscription_id == nil || subscription_id == ''
236
+ raise ArgumentError, "subscription_id cannot be blank"
237
+ end
238
+
239
+ request_with_logging(:post, config.api_host + "/v1/subscriptions/#{subscription_id}/enable", {})
240
+ end
241
+
242
+ # PATCH /v1/subscription/{subscription_id}
243
+ # update subscription request (more params detail refer to: https://api-docs.midtrans.com/#update-subscription)
244
+ def update_subscription(subscription_id, param)
245
+ if subscription_id && param == nil || subscription_id && param == ''
246
+ raise ArgumentError, "subscription_id and param cannot be blank"
247
+ end
248
+
249
+ request_with_logging(:patch, config.api_host + "/v1/subscriptions/#{subscription_id}", param)
250
+ end
251
+ end
252
+
253
+ end
@@ -7,7 +7,8 @@ require 'excon'
7
7
  class Veritrans
8
8
  module Client
9
9
 
10
- # Failback for activesupport
10
+ # If you using Rails then it will call ActiveSupport::JSON.encode
11
+ # Otherwise JSON.pretty_generate
11
12
  def self._json_encode(params)
12
13
  if defined?(ActiveSupport) && defined?(ActiveSupport::JSON)
13
14
  ActiveSupport::JSON.encode(params)
@@ -21,6 +22,8 @@ class Veritrans
21
22
  Veritrans::Client._json_encode(params)
22
23
  end
23
24
 
25
+ # If you using Rails then it will call ActiveSupport::JSON.decode
26
+ # Otherwise JSON.parse
24
27
  def self._json_decode(params)
25
28
  if defined?(ActiveSupport) && defined?(ActiveSupport::JSON)
26
29
  ActiveSupport::JSON.decode(params)
@@ -65,27 +68,38 @@ class Veritrans
65
68
  make_request(:post, url, params)
66
69
  end
67
70
 
71
+ def patch(url, params)
72
+ make_request(:patch, url, params)
73
+ end
74
+
68
75
  def make_request(method, url, params, auth_header = nil)
69
76
  if !config.server_key || config.server_key == ''
70
- raise "Please add server_key to config/veritrans.yml"
77
+ raise "Please configure your server key"
71
78
  end
72
79
 
73
80
  method = method.to_s.upcase
74
- logger.info "Veritrans: #{method} #{url} #{_json_encode(params)}"
75
- logger.info "Veritrans: Using server key: #{config.server_key}"
81
+ logger.info "Midtrans: #{method} #{url} #{_json_encode(params)}"
82
+ #logger.info "Veritrans: Using server key: #{config.server_key}"
76
83
  #puts "Veritrans: #{method} #{url} #{_json_encode(params)}"
77
84
 
78
85
  default_options = config.http_options || {}
79
86
 
87
+ idempotency_key = config.idempotency_key
88
+ append_notif_url = config.append_notif_url
89
+ override_notif_url = config.override_notif_url
90
+
80
91
  # Add authentication and content type
81
- # Docs http://docs.veritrans.co.id/sandbox/introduction.html
92
+ # Docs https://api-docs.midtrans.com/#http-s-header
82
93
  request_options = {
83
94
  :path => URI.parse(url).path,
84
95
  :headers => {
85
- :Authorization => auth_header || basic_auth_header(config.server_key),
86
- :Accept => "application/json",
87
- :"Content-Type" => "application/json",
88
- :"User-Agent" => "Veritrans ruby gem #{Veritrans::VERSION}"
96
+ "Authorization" => auth_header || basic_auth_header(config.server_key),
97
+ "Accept" => "application/json",
98
+ "Content-Type" => "application/json",
99
+ "User-Agent" => "Veritrans ruby gem #{Veritrans::VERSION}",
100
+ "Idempotency-Key" => "#{idempotency_key}",
101
+ "X-Append-Notification" => "#{append_notif_url}",
102
+ "X-Override-Notification" => "#{override_notif_url}"
89
103
  }
90
104
  }
91
105
 
@@ -99,19 +113,41 @@ class Veritrans
99
113
  read_timeout: 120,
100
114
  write_timeout: 120,
101
115
  connect_timeout: 120
102
- }.deep_merge(default_options)
116
+ }.merge(default_options)
103
117
 
104
118
  s_time = Time.now
105
119
  request = Excon.new(url, connection_options)
106
120
 
107
121
  response = request.send(method.downcase.to_sym, request_options)
108
122
 
109
- logger.info "Veritrans: got #{(Time.now - s_time).round(3)} sec #{response.status} #{response.body}"
123
+ if defined?(response.body)
124
+ response_body = JSON.parse(response.body)
125
+ if response_body.has_key? 'status_code'
126
+ status_code = Integer(response_body["status_code"])
127
+ if status_code >= 400 && status_code != 407
128
+ raise MidtransError.new(
129
+ "Midtrans API is returning API error. HTTP status code: #{status_code}",
130
+ "#{status_code}",
131
+ "#{response_body}",
132
+ "#{response}")
133
+ end
134
+ end
135
+ end
136
+
137
+ if response.status >= 400
138
+ raise MidtransError.new(
139
+ "Midtrans API is returning API error. HTTP status code: #{response.status} API response: #{response.body}",
140
+ "#{response.status}",
141
+ "#{response.body}",
142
+ "#{response}")
143
+ end
144
+
145
+ logger.info "Midtrans: got #{(Time.now - s_time).round(3)} sec #{response.status} #{response.body}"
110
146
 
111
147
  Result.new(response, url, request_options, Time.now - s_time)
112
148
 
113
149
  rescue Excon::Errors::SocketError => error
114
- logger.info "Veritrans: socket error, can not connect"
150
+ logger.info "Veritrans: socket error, can not connect (#{error.message})"
115
151
  error_response = Excon::Response.new(
116
152
  body: '{"status_code": "500", "status_message": "Internal server error, no response from backend. Try again later"}',
117
153
  status: '500'
@@ -7,7 +7,7 @@ class Veritrans
7
7
  class Config
8
8
 
9
9
  def initialize(options = nil)
10
- @api_host = "https://api.sandbox.veritrans.co.id"
10
+ @api_host = "https://api.sandbox.midtrans.com"
11
11
  apply(options) if options
12
12
  end
13
13
 
@@ -20,7 +20,6 @@ class Veritrans
20
20
  @client_key = value
21
21
  end
22
22
 
23
-
24
23
  def client_key
25
24
  @client_key
26
25
  end
@@ -41,9 +40,9 @@ class Veritrans
41
40
  ##
42
41
  # API Server hostname, this allow to switch between production and sandbox
43
42
  #
44
- # Should be "https://api.sandbox.veritrans.co.id" or "https://api.veritrans.co.id"
43
+ # Should be "https://api.sandbox.midtrans.com" or "https://api.midtrans.com"
45
44
  #
46
- # Default is "https://api.sandbox.veritrans.co.id"
45
+ # Default is "https://api.sandbox.midtrans.com"
47
46
  #
48
47
  def api_host=(value)
49
48
  @api_host = value
@@ -72,13 +71,37 @@ class Veritrans
72
71
  diff = options.keys.map(&:to_sym) - Excon::VALID_CONNECTION_KEYS
73
72
  if diff.size > 0
74
73
  raise ArgumentError,
75
- "http_options contain unsupported keys: #{diff.inspect}\n" +
76
- "Supported keys are: #{Excon::VALID_CONNECTION_KEYS.inspect}"
74
+ "http_options contain unsupported keys: #{diff.inspect}\n" +
75
+ "Supported keys are: #{Excon::VALID_CONNECTION_KEYS.inspect}"
77
76
  end
78
77
 
79
78
  @http_options = options
80
79
  end
81
80
 
81
+ def idempotency_key=(value)
82
+ @idempotency_key = value
83
+ end
84
+
85
+ def idempotency_key
86
+ @idempotency_key
87
+ end
88
+
89
+ def append_notif_url=(value)
90
+ @append_notif_url = value
91
+ end
92
+
93
+ def append_notif_url
94
+ @append_notif_url
95
+ end
96
+
97
+ def override_notif_url=(value)
98
+ @override_notif_url = value
99
+ end
100
+
101
+ def override_notif_url
102
+ @override_notif_url
103
+ end
104
+
82
105
  def http_options
83
106
  @http_options
84
107
  end
@@ -93,6 +116,8 @@ class Veritrans
93
116
  #
94
117
  # Veritrans.setup do
95
118
  # config.load_yml "#{Rails.root.to_s}/config/veritrans.yml#development"
119
+ # # or
120
+ # config.load_yml "#{Rails.root.to_s}/config/veritrans.yml", :development
96
121
  # end
97
122
  #
98
123
  def load_config(filename, yml_section = nil)