Class: Vpago::Acleda::Checkout

Inherits:
Base
  • Object
show all
Defined in:
lib/vpago/acleda/checkout.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#acleda_company_name, #acleda_payment_card, #action_url, #app_checkout, #app_checkout?, #error_url, #expiry_time, #host, #login_id, #merchant_id, #merchant_name, #order_number, #other_url, #password, #payment_number, #purchase_date, #signature, #success_url

Methods included from PaymentAmountCalculator

#amount, #amount_with_fee, #transaction_fee, #transaction_fee_fix, #transaction_fee_percentage

Constructor Details

#initialize(payment, options = {}) ⇒ Checkout

Returns a new instance of Checkout.



6
7
8
9
10
11
12
# File 'lib/vpago/acleda/checkout.rb', line 6

def initialize(payment, options = {})
  super

  @error_message = nil
  start_connection
  update_payment_source
end

Instance Attribute Details

#error_messageObject

Returns the value of attribute error_message.



4
5
6
# File 'lib/vpago/acleda/checkout.rb', line 4

def error_message
  @error_message
end

Instance Method Details

#create_sessionObject



36
37
38
39
40
41
42
43
# File 'lib/vpago/acleda/checkout.rb', line 36

def create_session
  con = Faraday::Connection.new(url: host)

  con.post(create_session_path) do |request|
    request.headers['Content-Type'] = 'application/json'
    request.body = create_session_request_body.to_json
  end
end

#create_session_pathObject



78
79
80
# File 'lib/vpago/acleda/checkout.rb', line 78

def create_session_path
  ENV.fetch('ACLEDA_CREATE_SESSION_PATH', nil)
end

#create_session_request_bodyObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vpago/acleda/checkout.rb', line 49

def create_session_request_body
  request_body = {
    loginId: ,
    password:,
    merchantID: merchant_id,
    signature:,
    xpayTransaction: {
      txid: payment_number,
      invoiceid: order_number,
      purchaseAmount: amount_with_fee,
      purchaseCurrency: 'USD',
      purchaseDate: purchase_date,
      purchaseDesc: 'items',
      item: '1',
      quantity: '1',
      expiryTime: expiry_time,
      otherUrl: other_url
    }
  }

  if @payment.payment_method.xpay_mpgs?
    request_body[:xpayTransaction][:paymentCard] = acleda_payment_card if acleda_payment_card.present?
  elsif @payment.payment_method.khqr?
    request_body[:xpayTransaction][:operationType] = '3'
  end

  request_body
end

#failed?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/vpago/acleda/checkout.rb', line 45

def failed?
  @error_message.present?
end

#gateway_paramsObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/vpago/acleda/checkout.rb', line 82

def gateway_params
  return {} if failed?

  {
    merchantID: merchant_id,
    sessionid: json_response['result']['sessionid'],
    paymenttokenid: json_response['result']['xTran']['paymentTokenid'],
    description: 'items',
    expirytime: expiry_time,
    amount: amount_with_fee,
    quantity: '1',
    Item: '1',
    invoiceid: order_number,
    currencytype: 'USD',
    transactionID: payment_number,
    successUrlToReturn: success_url,
    errorUrl: error_url,
    companyName: acleda_company_name,
    companyLogo: ActionController::Base.helpers.image_url('vpago/payway/acleda_merchant_logo_300x300.png')
  }
end

#json_responseObject



32
33
34
# File 'lib/vpago/acleda/checkout.rb', line 32

def json_response
  @json_response ||= JSON.parse(@response.body)
end

#start_connectionObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/vpago/acleda/checkout.rb', line 14

def start_connection
  @response = create_session

  if @response.status != 500 && @response.status != 200
    @error_message = 'Sever Error'
  elsif @response.status == 500 ## mostly when passing wrong parameter name
    @error_message = @response.body
  elsif @response.status == 200 && json_response['result']['code'] != 0 ## mostly when passing invalid payload, wrong loginId, etc.
    @error_message = json_response['result']['errorDetails']
  end
end

#update_payment_sourceObject



26
27
28
29
30
# File 'lib/vpago/acleda/checkout.rb', line 26

def update_payment_source
  return if failed?

  @payment.source.update_column(:transaction_id, json_response['result']['xTran']['paymentTokenid'])
end