Class: Vpago::PaywayV2::TransactionStatus

Inherits:
Base
  • Object
show all
Defined in:
lib/vpago/payway_v2/transaction_status.rb

Instance Method Summary collapse

Methods inherited from Base

#amount, #api_key, #continue_success_url, #email, #first_name, #hash_data, #hash_hmac, #host, #initialize, #last_name, #merchant_id, #order_jwt_token, #payment_option, #payout, #phone, #public_key, #req_time, #return_deeplink, #return_deeplink_url, #return_params, #return_url, #transaction_fee, #transaction_fee_fix, #transaction_fee_percentage, #transaction_id, #type, #view_type

Constructor Details

This class inherits a constructor from Vpago::PaywayV2::Base

Instance Method Details

#callObject

status: 0 – Approved, PRE_AUTH, PREAUTH_APPROVED 1 – Created 2 – Pending 3 – Declined 4 – Refunded 5 – Wrong Hash 7 - Cancelled 11 – Other Server-side Error



15
16
17
# File 'lib/vpago/payway_v2/transaction_status.rb', line 15

def call
  @response = check_remote_status
end

#check_remote_statusObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vpago/payway_v2/transaction_status.rb', line 37

def check_remote_status
  conn = Faraday::Connection.new do |faraday|
    faraday.request :url_encoded
  end

  data = {
    req_time: req_time,
    merchant_id: merchant_id,
    tran_id: transaction_id,
    hash: checker_hmac
  }

  conn.post(check_transaction_url, data)
end

#check_transaction_urlObject



80
81
82
# File 'lib/vpago/payway_v2/transaction_status.rb', line 80

def check_transaction_url
  "#{host}#{ENV.fetch('PAYWAY_CHECK_TRANSACTION_PATH', nil)}"
end

#checker_hmacObject



72
73
74
75
76
77
78
# File 'lib/vpago/payway_v2/transaction_status.rb', line 72

def checker_hmac
  data = "#{req_time}#{merchant_id}#{transaction_id}"
  hash = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha512'), api_key, data))

  # somehow php counter part are not able to decode if the \n present.
  hash.delete("\n")
end

#error_messageObject



60
61
62
# File 'lib/vpago/payway_v2/transaction_status.rb', line 60

def error_message
  json_response['description'].presence
end

#failed?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/vpago/payway_v2/transaction_status.rb', line 33

def failed?
  %w[3 4 5 7].include?(status)
end

#json_responseObject



64
65
66
67
68
69
70
# File 'lib/vpago/payway_v2/transaction_status.rb', line 64

def json_response
  @json_response ||= begin
    JSON.parse(@response.body)
  rescue JSON::ParserError
    {}
  end
end

#payout_totalObject



52
53
54
55
56
57
58
# File 'lib/vpago/payway_v2/transaction_status.rb', line 52

def payout_total
  payouts_response = json_response['payout']

  return nil if payouts_response.nil? || !payouts_response.is_a?(Array) || payouts_response.empty?

  payouts_response.map { |payout| payout['amt'].to_f || 0 }.sum
end

#pending?Boolean

request failed does not mean payment failed. but it failed when status is failed.

Returns:

  • (Boolean)


29
30
31
# File 'lib/vpago/payway_v2/transaction_status.rb', line 29

def pending?
  %w[1 2].include?(status)
end

#statusObject



19
20
21
# File 'lib/vpago/payway_v2/transaction_status.rb', line 19

def status
  json_response['status']&.to_s
end

#success?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/vpago/payway_v2/transaction_status.rb', line 23

def success?
  status == '0'
end