Class: PayUnit

Inherits:
Object
  • Object
show all
Defined in:
lib/payunit.rb

Overview

Payunit payment class

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_username, api_password, return_url, notify_url, mode, currency) ⇒ PayUnit

Returns a new instance of PayUnit.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/payunit.rb', line 16

def initialize(api_key, api_username, api_password, return_url, notify_url, mode, currency)
  @api_key = api_key
  @api_username = api_username
  @api_password = api_password
  @return_url = return_url
  @notify_url = notify_url
  @mode = mode
  @currency = currency

  check_api_key
  check_api_username
  check_api_password
  check_api_mode
  check_currency
  check_return_url
  check_notify_url
  check_api_sdk
end

Class Method Details

.transaction_details(transaction_id, api_key, api_username, api_password, mode) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/payunit.rb', line 83

def self.transaction_details(transaction_id, api_key, api_username, api_password, mode)
  auth = "#{api_username}:#{api_password}"
  environment = mode.to_s
  auth_data = Base64.strict_encode64(auth)

  headers = {
    "x-api-key": api_key.to_s,
    "content-type": "application/json",
    "Authorization": "Basic #{auth_data}",
    "mode": environment.downcase.to_s
  }
  begin
    conn = Faraday.new(
      url: "https://app.payunit.net/api/gateway/initialize",
      params: { param: "1" },
      headers: headers
    )

    url = "https://app.payunit.net/api/gateway/transaction/#{transaction_id}"
    response = conn.get(url)
    response = JSON.parse(response&.body || "{}")
    raise response["message"] unless response["statusCode"] == 200

    response
  rescue StandardError
    abort(response["message"])
  end
end

Instance Method Details

#make_payment(amount, transaction_id, purchase_ref = nil, description = nil, name = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
77
78
79
80
81
# File 'lib/payunit.rb', line 35

def make_payment(amount, transaction_id, purchase_ref = nil, description = nil, name = nil)
  return "Invalid transaction amount" if amount <= 0

  @purchase_ref = purchase_ref
  @description = description
  @name = name
  auth = "#{@api_username}:#{@api_password}"
  environment = to_str(@mode)
  auth_data = Base64.strict_encode64(auth)
  @transaction_id = transaction_id

  test_url = "https://app.payunit.net/api/gateway/initialize"

  headers = {
    "x-api-key": to_str(@api_key),
    "content-type": "application/json",
    "Authorization": "Basic #{to_str(auth_data)}",
    "mode": to_str(environment.downcase)
  }

  test_body = {
    "notify_url": to_str(@notify_url),
    "total_amount": to_str(amount),
    "return_url": to_str(@return_url),
    "purchaseRef": to_str(@purchase_ref),
    "description": to_str(@description),
    "name": to_str(@name),
    "currency": to_str(@currency),
    "transaction_id": to_str(@transaction_id)
  }

  begin
    conn = Faraday.new(
      url: "https://app.payunit.net/api/gateway/initialize",
      params: { param: "1" },
      headers: headers
    )
    response = conn.post(test_url, test_body.to_json, headers)
    response = JSON.parse(response&.body || "{}")

    raise response["message"] unless response["statusCode"] == 200

    response
  rescue StandardError
    abort(response["message"])
  end
end