-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswipe.rb
63 lines (45 loc) · 1.8 KB
/
swipe.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# A pretty basic abstractor for the Swipe API
# Allows you to set up an adHoc transaction and check for payment
# Quesitons, bugs etc to paul[at]flyingpenguins.co.nz
require 'net/https'
class Swipe
attr_accessor :name, :price, :user_data, :callback_url, :lpn_url, :identifier_id
def initialize(data = {})
self.name ||= data[:name]
self.price ||= data[:price]
self.user_data ||= data[:user_data]
self.callback_url ||= data[:callback_url]
self.lpn_url ||= data[:lpn_url]
@api_url ||= "https://api.swipehq.com/"
@payment_page_url ||= "https://payment.swipehq.com/"
@merchant_id ||= ENV['SWIPE_ID']
@api_key ||= ENV['SWIPE_API_KEY']
self.callback_url ||= ENV['SWIPE_CALLBACK_URL']
self.lpn_url ||= ENV['SWIPE_LPN_URL']
self #Allows chaining
end
def identify(extra_data={})
data = extra_data.merge({td_item: name, td_amount: price, td_user_data: user_data, td_callback_url: callback_url, td_lpn_url: lpn_url})
response = call_api("createTransactionIdentifier.php", data)
if response["response_code"] == 200
self.identifier_id = response["data"]["identifier"]
page = @payment_page_url+"?identifier_id="+response["data"]["identifier"]
else
raise response["message"]
end
end
def accepted?(transaction_id)
response = call_api("verifyTransaction.php", {transaction_id: transaction_id})
response["data"]["transaction_approved"] === 'yes' ? true : false
end
def self.accepted?(transaction_id)
Swipe.new.accepted?(transaction_id)
end
private
def call_api(method, data={})
data = data.merge({merchant_id: @merchant_id, api_key: @api_key})
url = @api_url+method+"?"+data.to_query
response = URI.parse(url).read
JSON.parse(response)
end
end