Class: BleumiPay::RequestValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/bleumi_pay_sdk_ruby/api/request_validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.defaultObject



12
13
14
# File 'lib/bleumi_pay_sdk_ruby/api/request_validator.rb', line 12

def self.default
    @@default ||= RequestValidator.new
end

Instance Method Details

#algo_address?(str) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/bleumi_pay_sdk_ruby/api/request_validator.rb', line 21

def algo_address?(str)
    # We use !! to convert the return value to a boolean
    !!(str =~ /^[A-Z2-7+=*]{58}$/)
end

#algo_token?(str) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/bleumi_pay_sdk_ruby/api/request_validator.rb', line 26

def algo_token?(str)
    # We use !! to convert the return value to a boolean
    !!(str =~ /^[0-9]*$/)
end

#check_alg_addr(name, input, is_token) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bleumi_pay_sdk_ruby/api/request_validator.rb', line 52

def check_alg_addr(name, input, is_token)
    if (is_token) 
        if !((algo_token?(input))||(input == "ALGO")) then
            return "`#{name}` is not a valid Algorand token"
        end
    else
        if !(algo_address?(input)) then
            return "`#{name}` is not a valid Algorand address"
        end
    end
    return nil
end

#check_eth_addr(name, input) ⇒ Object



38
39
40
41
42
43
# File 'lib/bleumi_pay_sdk_ruby/api/request_validator.rb', line 38

def check_eth_addr(name, input)
    if !((eth_address?(input))||(input == "ETH")||(input == "XDAI")||(input == "XDAIT")) then
        return "`#{name}` is not a valid Ethereum address"
    end
    return nil
end

#check_network_addr(name, input, chain, mandatory, is_token) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bleumi_pay_sdk_ruby/api/request_validator.rb', line 80

def check_network_addr(name, input, chain, mandatory, is_token)
    if mandatory then
        msg = check_req_param(name, input)
        if is_not_empty(msg) then
            return msg 
        end    
    end
    if is_not_empty(input) then
        msg = nil
        if is_algo_network(chain) then
            msg =  check_alg_addr(name, input, is_token)
        elsif is_rsk_network(chain) then
            msg =  check_rsk_addr(name, input)
        else
            msg =  check_eth_addr(name, input)
        end    
        if is_not_empty(msg) then
            return msg 
        end
    end   
    return nil
end

#check_req_param(name, input) ⇒ Object



73
74
75
76
77
78
# File 'lib/bleumi_pay_sdk_ruby/api/request_validator.rb', line 73

def check_req_param(name, input)
    if (input == nil || input == "")
        return "Missing required parameter `#{name}`"
    end
    return nil
end

#check_rsk_addr(name, input) ⇒ Object



45
46
47
48
49
50
# File 'lib/bleumi_pay_sdk_ruby/api/request_validator.rb', line 45

def check_rsk_addr(name, input)
    if !((eth_address?(input))||(input == "RBTC")) then
        return "`#{name}` is not a valid RSK address"
    end
    return nil
end

#eth_address?(str) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
# File 'lib/bleumi_pay_sdk_ruby/api/request_validator.rb', line 16

def eth_address?(str)
    # We use !! to convert the return value to a boolean
    !!(str =~ /^0x[a-fA-F0-9]{40}$/)
end

#is_algo_network(chain) ⇒ Object



65
66
67
# File 'lib/bleumi_pay_sdk_ruby/api/request_validator.rb', line 65

def is_algo_network(chain)
    return ((chain == "alg_mainnet")||(chain == "alg_testnet")) 
end

#is_not_empty(str) ⇒ Object



31
32
33
34
35
36
# File 'lib/bleumi_pay_sdk_ruby/api/request_validator.rb', line 31

def is_not_empty(str)
    if (str == nil || str == "") then
        return false
    end    
    return true
end

#is_rsk_network(chain) ⇒ Object



69
70
71
# File 'lib/bleumi_pay_sdk_ruby/api/request_validator.rb', line 69

def is_rsk_network(chain)
    return ((chain == "rsk")||(chain == "rsk_testnet")) 
end

#validate_checkout_payment_params(params) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/bleumi_pay_sdk_ruby/api/request_validator.rb', line 193

def validate_checkout_payment_params(params)
    
    # check if hmac_alg is provided while creating checkout payment parameters request
    msg = check_req_param("HmacAlg", params.hmac_alg)
    if is_not_empty(msg) then
        return msg 
    end
    
    # check if hmac_input is provided while creating checkout payment parameters request
    msg = check_req_param("HmacInput", params.hmac_input)
    if is_not_empty(msg) then
        return msg 
    end
    
    # check if hmac_key_id is provided while creating checkout payment parameters request
    msg = check_req_param("HmacKeyId", params.hmac_key_id)
    if is_not_empty(msg) then
        return msg 
    end
    
    # check if hmac_value is provided while creating checkout payment parameters request
    msg = check_req_param("HmacValue", params.hmac_value)
    if is_not_empty(msg) then
        return msg 
    end
    return nil
end

#validate_create_checkout_url_request(params) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/bleumi_pay_sdk_ruby/api/request_validator.rb', line 136

def validate_create_checkout_url_request(params)
    # check if id is provided while creating checkout url
    msg = check_req_param("Id", params.id)
    if is_not_empty(msg) then
        return msg 
    end
    
    # check if currency is provided while creating checkout url
    msg = check_req_param("Currency", params.currency)
    if is_not_empty(msg) then
        return msg 
    end
    
    # check if amount is provided while creating checkout url
    msg = check_req_param("Amount", params.amount)
    if is_not_empty(msg) then
        return msg 
    end
    
    # check if cancel_url is provided while creating checkout url
    msg = check_req_param("CancelUrl", params.cancel_url)
    if is_not_empty(msg) then
        return msg 
    end
    
    # check if success_url is provided while creating checkout url
    msg = check_req_param("SuccessUrl", params.success_url)
    if is_not_empty(msg) then
        return msg 
    end

    # check if token is valid address in the network (if provided) 
    if is_not_empty(params.token) then
        
        # check if chain is provided in the request, when token is provided, this is required 
        msg = check_req_param("Chain", params.chain)
        if is_not_empty(msg) then
            return msg 
        end

        # check if token is valid address in the network (if provided) 
        msg = check_network_addr("Token", params.token, params.chain, mandatory=false, is_token=true)
        if is_not_empty(msg) then
            return msg 
        end

        # check if transfer_address is valid address in the network (if provided) 
        if is_not_empty(params.transfer_address) then
            msg = check_network_addr("TransferAddress", params.transfer_address, params.chain, mandatory=false, is_token=false)
            if is_not_empty(msg) then
                return msg 
            end
        end    
    end   
    return nil
end

#validate_create_payout_request(params, chain) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/bleumi_pay_sdk_ruby/api/request_validator.rb', line 221

def validate_create_payout_request(params, chain)
    msg = check_req_param("Chain", chain)
    if is_not_empty(msg) then
        return msg 
    end
    # check if token is valid address in the network 
    msg = check_network_addr("Token", params.token, chain, mandatory=true, is_token=true)
    if is_not_empty(msg) then
        return msg 
    end

    payouts = params.payouts
    if payouts.length == 0 then 
        return "Payouts not defined."
    end

    for payout in payouts
        # check if payout.transfer_address is valid address in the network  
        msg = check_network_addr("TransferAddress", payout.transfer_address, chain, mandatory=true, is_token=false)
        if is_not_empty(msg) then
            return msg 
        end

        # check if payout.amount is provided for payout
        msg = check_req_param("Amount", payout.amount)
        if is_not_empty(msg) then
            return msg 
        end

        if is_algo_network(chain) then
            # check if payout.authorization is provided for Algorand payout
            msg = check_req_param("Authorization", payout.authorization)
            if is_not_empty(msg) then
                return msg 
            end
        end 
    end
    return nil
end

#validate_refund_payment_request(params, chain) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/bleumi_pay_sdk_ruby/api/request_validator.rb', line 103

def validate_refund_payment_request(params, chain)
    # check if chain is provided 
    msg = check_req_param("Chain", chain)
    if is_not_empty(msg) then
        return msg 
    end
    # check if token is valid address in the network 
    msg = check_network_addr("Token", params.token, chain, mandatory=true, is_token=true)
    if is_not_empty(msg) then
        return msg 
    end
    return nil
end

#validate_settle_payment_request(params, chain) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/bleumi_pay_sdk_ruby/api/request_validator.rb', line 117

def validate_settle_payment_request(params, chain)
    # check if chain is provided 
    msg = check_req_param("Chain", chain)
    if is_not_empty(msg) then
        return msg 
    end
    # check if token is valid address in the network 
    msg = check_network_addr("Token", params.token, chain, mandatory=true, is_token=true)
    if is_not_empty(msg) then
        return msg 
    end
    # check if amount to settle is provided
    msg = check_req_param("Amount", params.amount)
    if is_not_empty(msg) then
        return msg 
    end
    return nil
end