Skip to content

Commit e19af2d

Browse files
Merge pull request #7 from dhanarJkusuma/KRED-474
KRED-474: add expire transaction
2 parents 85032a8 + b5368e7 commit e19af2d

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ bca_virtual_account_charge = midtrans.bca_virtual_account_charge.post(charge_par
174174
#=> bca_virtual_account_charge returns MidtransApiMidtransApi::Model::BcaVirtualAccount::Charge instance
175175
```
176176

177+
#### Expire Transaction
178+
```ruby
179+
expire_response = midtrans.expire_transaction.post(order_id: "eb046679-285a-4136-8977-e4c429cc3254")
180+
#=> expire_response returns MidtransApiMidtransApi::Model::Transaction::Expire instance
181+
```
182+
183+
177184
#### Check Status Payment
178185
```ruby
179186
dummy_order_id = "order-with-gopay"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
module MidtransApi
4+
module Api
5+
module Transaction
6+
class Expire < MidtransApi::Api::Base
7+
8+
def post(order_id:)
9+
response = client.post("#{order_id}/expire",{})
10+
11+
MidtransApi::Model::Transaction::Expire.new(response)
12+
end
13+
end
14+
end
15+
end
16+
end

lib/midtrans_api/client.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require 'midtrans_api/api/credit_card/charge'
1010
require 'midtrans_api/api/credit_card/token'
1111
require 'midtrans_api/api/gopay/charge'
12+
require 'midtrans_api/api/transaction/expire'
1213

1314
require 'midtrans_api/middleware/handle_response_exception'
1415

@@ -18,6 +19,7 @@
1819
require 'midtrans_api/model/credit_card/charge'
1920
require 'midtrans_api/model/credit_card/token'
2021
require 'midtrans_api/model/gopay/charge'
22+
require 'midtrans_api/model/transaction/expire'
2123

2224
module MidtransApi
2325
class Client
@@ -61,6 +63,10 @@ def bca_virtual_account_charge
6163
@charge ||= MidtransApi::Api::BcaVirtualAccount::Charge.new(self)
6264
end
6365

66+
def expire_transaction
67+
@expire ||= MidtransApi::Api::Transaction::Expire.new(self)
68+
end
69+
6470
def gopay_charge
6571
@charge ||= MidtransApi::Api::Gopay::Charge.new(self)
6672
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
module MidtransApi
4+
module Model
5+
module Transaction
6+
class Expire < MidtransApi::Model::Base
7+
resource_attributes :gross_amount,
8+
:order_id,
9+
:payment_type,
10+
:status_code,
11+
:status_message,
12+
:transaction_id,
13+
:transaction_status,
14+
:transaction_time
15+
16+
def resolve_params_attr(attr)
17+
attr.to_s
18+
end
19+
end
20+
end
21+
end
22+
end
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe MidtransApi::Api::Transaction::Expire do
6+
let(:client) do
7+
MidtransApi::Client.new(
8+
client_key: 'client_key',
9+
server_key: 'server_key',
10+
sandbox: true,
11+
notification_url: 'someapps://callback'
12+
)
13+
end
14+
15+
describe '#post' do
16+
context 'with valid params' do
17+
dummy_response = {
18+
"status_code": "407",
19+
"status_message": "Success, transaction is expired",
20+
"transaction_id": "24dd3e24-2b1f-4e09-9176-f4a15df3c869",
21+
"order_id": "eb046679-285a-4136-8977-e4c429cc3254",
22+
"merchant_id": "M045108",
23+
"gross_amount": "1000000.00",
24+
"currency": "IDR",
25+
"payment_type": "bank_transfer",
26+
"transaction_time": "2023-03-15 15:19:23",
27+
"transaction_status": "expire",
28+
"fraud_status": "accept"
29+
}
30+
dummy_order_id = "eb046679-285a-4136-8977-e4c429cc3254"
31+
it 'using described class returns success response' do
32+
stub_request(:post, "#{client.config.api_url}/#{client.config.api_version}/#{dummy_order_id}/expire")
33+
.with(body: {})
34+
.to_return(status: 200, body: dummy_response.to_json)
35+
expire_api = described_class.new(client)
36+
response = expire_api.post(order_id: dummy_order_id)
37+
38+
expect(response).to be_instance_of MidtransApi::Model::Transaction::Expire
39+
expect(response.order_id).to eq dummy_order_id
40+
expect(response.transaction_status).to eq "expire"
41+
end
42+
end
43+
44+
context 'with invalid params' do
45+
it 'raise NotFound; invalid transaction_id' do
46+
dummy_response = {
47+
"status_code": '404',
48+
"status_message": 'Transaction doesn\'t exist.',
49+
"id": 'b19510c2-6ad3-4c5b-92bf-049cfd5846c9'
50+
}
51+
dummy_order_id = "eb046679-28512312323254"
52+
stub_request(:post, "#{client.config.api_url}/#{client.config.api_version}/#{dummy_order_id}/expire")
53+
.with(body: {})
54+
.to_return(status: 200, body: dummy_response.to_json)
55+
expect do
56+
expire_api = described_class.new(client)
57+
expire_api.post(order_id: dummy_order_id)
58+
end.to raise_error(MidtransApi::Errors::NotFound, 'Transaction doesn\'t exist.')
59+
end
60+
61+
it 'raise CannotModifyTransaction; cannot modify transaction' do
62+
dummy_response = {
63+
"status_code": '412',
64+
"status_message": 'Transaction status cannot be updated.',
65+
"id": '95c25db5-08a4-4b3e-b7ce-8d56ebed1dac'
66+
}
67+
dummy_order_id = "2d44562e-6cb6-44f5-8cbd-751acb6e9cd2"
68+
stub_request(:post, "#{client.config.api_url}/#{client.config.api_version}/#{dummy_order_id}/expire")
69+
.with(body: {})
70+
.to_return(status: 200, body: dummy_response.to_json)
71+
expect do
72+
expire_api = described_class.new(client)
73+
expire_api.post(order_id: dummy_order_id)
74+
end.to raise_error(MidtransApi::Errors::CannotModifyTransaction, 'Transaction status cannot be updated.')
75+
end
76+
end
77+
end
78+
end

0 commit comments

Comments
 (0)