-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.rb
40 lines (33 loc) · 943 Bytes
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# coding: utf-8
require "sinatra"
require "sinatra/reloader"
require "json"
require "omise"
require "dotenv"
Dotenv.load
# set your omise test api key
# https://dashboard.omise.co/test/api-keys
Omise.api_key = ENV["OMISE_TEST_SECRET_KEY"]
Omise.api_version = "2015-11-17"
configure do
set :bind, "localhost"
set :port, 3000
end
post "/omise/webhook", provides: :json do
# retrieve the request's body and parse it as JSON
event_json = JSON.parse(request.body.read)
# verify the event by fetching it from Omise
event = Omise::Event.retrieve(event_json["id"])
dir = File.join("log", "api", event["object"])
filepath = File.join(dir, "#{event['id']}.json")
# make directory to save api response as JSON file
FileUtils.mkdir_p(dir) unless FileTest.exist?(dir)
# save api response as JSON file
File.open(filepath, "w") do |f|
f.puts JSON.pretty_generate(event.as_json)
end
status 200
end
get "*" do
":)"
end