Skip to content

Commit

Permalink
Merge pull request #11 from mekari-engineering/KRED-749
Browse files Browse the repository at this point in the history
KRED-749 ->  Implement BRI, BNI, CIMB, Permata charge and Mandiri Bill Charge
  • Loading branch information
rikyhidayat21 authored Sep 7, 2023
2 parents 6ea1298 + 92842ed commit 99d4a8e
Show file tree
Hide file tree
Showing 6 changed files with 538 additions and 2 deletions.
110 changes: 108 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ require 'midtrans_api'
midtrans = MidtransApi::Client.new(
client_key: 'YOUR-CLIENT-KEY',
server_key: 'YOUR-SERVER-KEY',
sandbox: true|false
notification_url: 'https://example.com/callback'
sandbox: true|false,
notification_url: 'https://example.com/callback',
timeout: 30 # by default will be 60 (seconds)
)

# or
Expand Down Expand Up @@ -180,6 +181,111 @@ expire_response = midtrans.expire_transaction.post(order_id: "eb046679-285a-4136
#=> expire_response returns MidtransApiMidtransApi::Model::Transaction::Expire instance
```

#### Charge Transaction
Charge Transaction Payment Method: **Bank Transfer**
_Bank Transfer_ is one of the payment methods offered by Midtrans. By using this method, your customers can make a payment via bank transfer and Midtrans will send real time notification when the payment is completed.

A list of bank transfer payment methods supported by Midtrans is given below.

- Permata Virtual Account
- BCA Virtual Account (Please refer to BCA Virtual Account Charge section)
- Mandiri Bill Payment
- BNI Virtual Account
- BRI Virtual Account
- CIMB Virtual Account

##### Charge transaction
```ruby
# "payment_type" => required <"bank_transfer"|"echannel">
# "transaction_details" => required
# "customer_details" => optional
# "item_details" => optional
# "bank_transfer"|"echannel" => required
charge_params_bank_transfer = {
"payment_type": "bank_transfer",
"transaction_details": {
"gross_amount": 10000,
"order_id": "{{$timestamp}}" # specified by you
},
"customer_details": {
"email": "[email protected]",
"first_name": "budi",
"last_name": "utomo",
"phone": "+6281 1234 1234"
},
"item_details": [
{
"id": "1388998298204",
"price": 5000,
"quantity": 1,
"name": "Ayam Zozozo"
},
{
"id": "1388998298205",
"price": 5000,
"quantity": 1,
"name": "Ayam Xoxoxo"
}
],
"bank_transfer":{
"bank": "bni|bri|permata|cimb",
"va_number": "111111"
}
}

charge_params_echannel = {
"payment_type": "echannel",
"transaction_details": {
"order_id": "1388", # specified by you
"gross_amount": 95000
},
"item_details": [
{
"id": "a1",
"price": 50000,
"quantity": 2,
"name": "Apel"
},
{
"id": "a2",
"price": 45000,
"quantity": 1,
"name": "Jeruk"
}
],
"echannel": {
"bill_info1": "Payment For:",
"bill_info2": "debt",
"bill_key": "081211111111"
}
}

charge_response = midtrans.charge_transaction.post(charge_params_bank_transfer, "bank_transfer") # => payment type bank_transfer
charge_response = midtrans.charge_transaction.post(charge_params_echannel, "echannel") # => payment type echannel
#=> charge_response returns MidtransApiMidtransApi::Model::Transaction::Charge instance
```

#### Charge Transaction with Custom Expiry
```ruby
charge_params_with_custom_expiry = {
"payment_type": "bank_transfer",
"bank_transfer": {
"bank": "permata"
},
"transaction_details": {
"order_id": "C17550",
"gross_amount": 145000
},
"custom_expiry": {
"order_time": "2016-12-07 11:54:12 +0700", # If not defined, expiry time starts from transaction time.
"expiry_duration": 60,
"unit": "second|minute|hour|day" # default is minute
}
}

charge_response = midtrans.charge_transaction.post(charge_params_with_custom_expiry, "bank_transfer|echannel")
#=> charge_response returns MidtransApiMidtransApi::Model::Transaction::Charge instance
```

#### Check Status Payment
```ruby
Expand Down
33 changes: 33 additions & 0 deletions lib/midtrans_api/api/transaction/charge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module MidtransApi
module Api
module Transaction
class Charge < MidtransApi::Api::Base
PATH = 'charge'

# @payment_type: "bank_transfer"|"echannel"
def post(params, payment_type)
response = client.post(PATH, build_params(params, payment_type))

MidtransApi::Model::Transaction::Charge.new(response)
end

private

def build_params(params, payment_type)
{
payment_type: payment_type,
transaction_details: params[:transaction_details]
}.tap do |p|
p[:bank_transfer] = params[:bank_transfer] if payment_type == "bank_transfer"
p[:echannel] = params[:echannel] if payment_type == "echannel"
p[:customer_details] = params[:customer_details] unless params[:customer_details].nil?
p[:item_details] = params[:item_details] unless params[:item_details].nil?
p[:custom_expiry] = params[:custom_expiry] unless params[:custom_expiry].nil?
end
end
end
end
end
end
32 changes: 32 additions & 0 deletions lib/midtrans_api/api/transaction/charge_echannel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module MidtransApi
module Api
module Transaction
class ChargeEchannel < MidtransApi::Api::Base
PATH = 'charge'
PAYMENT_TYPE = 'echannel'

def post(params)
response = client.post(PATH, build_params(params))

MidtransApi::Model::Transaction::ChargeEchannel.new(response)
end

private

def build_params(params)
{
payment_type: PAYMENT_TYPE,
transaction_details: params[:transaction_details],
echannel: params[:echannel],
}.tap do |p|
p[:customer_details] = params[:customer_details] unless params[:customer_details].nil?
p[:item_details] = params[:item_details] unless params[:item_details].nil?
p[:custom_expiry] = params[:custom_expiry] unless params[:custom_expiry].nil?
end
end
end
end
end
end
6 changes: 6 additions & 0 deletions lib/midtrans_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'midtrans_api/api/credit_card/token'
require 'midtrans_api/api/gopay/charge'
require 'midtrans_api/api/transaction/expire'
require 'midtrans_api/api/transaction/charge'
require 'midtrans_api/api/check/balance'
require 'midtrans_api/api/disbursement/payout'

Expand All @@ -22,6 +23,7 @@
require 'midtrans_api/model/credit_card/token'
require 'midtrans_api/model/gopay/charge'
require 'midtrans_api/model/transaction/expire'
require 'midtrans_api/model/transaction/charge'
require 'midtrans_api/model/check/balance'
require 'midtrans_api/model/disbursement/payout'

Expand Down Expand Up @@ -72,6 +74,10 @@ def expire_transaction
@expire ||= MidtransApi::Api::Transaction::Expire.new(self)
end

def charge_transaction
@charge ||= MidtransApi::Api::Transaction::Charge.new(self)
end

def gopay_charge
@charge ||= MidtransApi::Api::Gopay::Charge.new(self)
end
Expand Down
31 changes: 31 additions & 0 deletions lib/midtrans_api/model/transaction/charge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module MidtransApi
module Model
module Transaction
class Charge < MidtransApi::Model::Base
resource_attributes :status_code,
:status_message,
:transaction_id,
:order_id,
:gross_amount,
:payment_type,
:transaction_time,
:transaction_status,
:va_numbers,
:fraud_status,
:currency,
:signature_key,
:expiry_time,
:permata_va_number,
:bill_key,
:biller_code,
:approval_code

def resolve_params_attr(attr)
attr.to_s
end
end
end
end
end
Loading

0 comments on commit 99d4a8e

Please sign in to comment.