Module: Bayarcash::ManualBankTransfer

Included in:
Client
Defined in:
lib/bayarcash/manual_bank_transfer.rb

Overview

Manual (offline) bank transfer operations.

These calls hit a different base URL than the versioned API (no /v2 or /v3 segment) and support a multipart proof_of_payment file upload.

Constant Summary collapse

REQUIRED_MANUAL_TRANSFER_FIELDS =
%w[
  portal_key buyer_name buyer_email order_amount order_no payment_gateway
  merchant_bank_name merchant_bank_account merchant_bank_account_holder
  bank_transfer_type bank_transfer_notes
].freeze
FILE_CONTENT_TYPES =
{
  "jpg"  => "image/jpeg",
  "jpeg" => "image/jpeg",
  "png"  => "image/png",
  "gif"  => "image/gif",
  "pdf"  => "application/pdf"
}.freeze

Instance Method Summary collapse

Instance Method Details

#create_manual_bank_transfer(data, allow_redirect = false) ⇒ Hash, String

Create a manual bank transfer payment.

Parameters:

  • data (Hash)

    payment and customer details (see README for the field list)

  • allow_redirect (Boolean) (defaults to: false)

    whether to auto-follow HTTP redirects

Returns:

  • (Hash, String)

    response hash or raw string depending on the API response

Raises:

  • (ArgumentError)

    when required fields are missing or invalid

  • (Bayarcash::Error)

    when the API request fails



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bayarcash/manual_bank_transfer.rb', line 30

def create_manual_bank_transfer(data, allow_redirect = false)
  data = stringify_keys(data)

  validate_manual_transfer_data(data)

  data["bank_transfer_date"] ||= Date.today.strftime("%Y-%m-%d")

  post_fields = prepare_manual_transfer_post_fields(data)

  response = execute_manual_transfer_request(post_fields, allow_redirect)

  process_manual_transfer_response(response[:body], response[:http_code], allow_redirect)
end

#parse_manual_bank_transfer_response(html_response) ⇒ Hash{String => String}

Extract structured data from an HTML form response.

Parameters:

  • html_response (String)

Returns:

  • (Hash{String => String})


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/bayarcash/manual_bank_transfer.rb', line 79

def parse_manual_bank_transfer_response(html_response)
  data = {}

  if (form_id = html_response[/id="([^"]+)"/, 1])
    data["form_id"] = form_id
  end

  if (return_url = html_response[/action="([^"]+)"/, 1])
    data["return_url"] = return_url
  end

  html_response.scan(/<input name="([^"]+)" type="hidden" value="([^"]*)">/).each do |name, value|
    data[name] = value
  end

  data
end

#update_manual_bank_transfer_status(ref_no, status, amount) ⇒ Object

Update the status of an existing manual bank transfer.

Parameters:

  • ref_no (String)

    transaction reference number

  • status (String)

    new status code

  • amount (String)

    transaction amount

Returns:

  • (Object)

    decoded API response

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bayarcash/manual_bank_transfer.rb', line 51

def update_manual_bank_transfer_status(ref_no, status, amount)
  data = { "ref_no" => ref_no, "status" => status, "amount" => amount }

  conn = Faraday.new do |faraday|
    faraday.request :url_encoded
    faraday.adapter Faraday.default_adapter
  end

  url = "#{manual_transfer_base_url}/manual-bank-transfer/update-status"

  begin
    response = conn.post(url) do |req|
      req.headers["Accept"] = "application/json"
      req.headers["Authorization"] = "Bearer #{@token}"
      req.headers["Content-Type"] = "application/x-www-form-urlencoded"
      req.body = data
    end
  rescue Faraday::Error => e
    raise Bayarcash::Error, "Connection failed: #{e.message}"
  end

  handle_api_response(response.body.to_s, response.status)
end