20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
82
83
84
85
86
87
88
89
90
|
# File 'lib/naspay_rails/transactions.rb', line 20
def self.create_new(endpoint, params, token)
route_suffix = '/api/v1/transactions'
target_url = "#{endpoint}#{route_suffix}"
request_uri = URI.parse(target_url)
request = Net::HTTP::Post.new(request_uri)
request["Authorization"] = "Bearer #{token}"
request["Content-Type"] = "application/json"
request["Accept"] = "application/json"
request_body =
case params.keys.sort
when %i[intent amount currency transaction_id description].sort
{
"intent" => params[:intent],
"amount" => params[:amount],
"currency" => params[:currency],
"merchantTransactionId" => params[:transaction_id],
"description" => params[:description]
}
when %i[intent amount currency transaction_id description customer].sort
{
"intent" => params[:intent],
"amount" => params[:amount],
"currency" => params[:currency],
"merchantTransactionId" => params[:transaction_id],
"description" => params[:description],
"customer" => {
"email" => params[:customer].try(:[], :email),
"phone" => params[:customer].try(:[], :phone),
"merchantCustomerId" => params[:customer].try(:[], :customer_id),
"locale" => params[:customer].try(:[], :locale),
"ip" => params[:customer].try(:[], :ip)
}
}
when %i[intent amount currency transaction_id description customer shipping_address].sort
{
"intent" => params[:intent],
"amount" => params[:amount],
"currency" => params[:currency],
"merchantTransactionId" => params[:transaction_id],
"description" => params[:description],
"customer" => {
"email" => params[:customer].try(:[], :email),
"phone" => params[:customer].try(:[], :phone),
"merchantCustomerId" => params[:customer].try(:[], :customer_id),
"locale" => params[:customer].try(:[], :locale),
"ip" => params[:customer].try(:[], :ip)
},
"shippingAddress" => {
"line1" => params[:shipping_address].try(:[], :line1),
"line2" => params[:shipping_address].try(:[], :line2),
"city" => params[:shipping_address].try(:[], :city),
"countryCode" => params[:shipping_address].try(:[], :country_code),
"postalCode" => params[:shipping_address].try(:[], :postal_code),
"state" => params[:shipping_address].try(:[], :state),
"phone" => params[:shipping_address].try(:[], :phone),
"recipientName" => params[:shipping_address].try(:[], :recipient_name)
}
}
else
return 'please check the keys provided in params Hash!'
end
request.body = JSON.dump(request_body)
req_options = {
use_ssl: request_uri.scheme == "https",
}
Net::HTTP.start(request_uri.hostname, request_uri.port, req_options) { |http| http.request(request) }
end
|