Class: Compropago::Client

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

Constant Summary collapse

BASE_URI =
'https://api.compropago.com/v1'

Instance Method Summary collapse

Constructor Details

#initialize(api_key = '', options = {}) ⇒ Client

Returns a new instance of Client.



9
10
11
12
13
14
15
# File 'lib/compropago/client.rb', line 9

def initialize(api_key='', options={})
  @api_key = api_key

  #defaults
  options[:base_uri] ||= BASE_URI
  @base_uri = options[:base_uri]
end

Instance Method Details

#create_charge(product_price, product_name, customer_name, customer_email, payment_type, product_id = nil, image_url = nil) ⇒ Object



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

def create_charge(product_price, product_name, customer_name, customer_email, payment_type, product_id=nil, image_url=nil)
  uri = URI.parse(BASE_URI+'/charges')
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Post.new(uri.request_uri)
  request.basic_auth @api_key, ''
  params = { "product_price" => product_price,
  			     "product_name" => product_name,
  			     "customer_name" => customer_name,
  			     "customer_email" => customer_email,
  			     "payment_type" => payment_type,
  			     "product_id" => product_id,
    		     "image_url" => image_url
  		     }
  request.set_form_data(params)
  http.request(request)
end

#send_payment_instructions(payment_id, customer_phone, customer_company_phone) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/compropago/client.rb', line 46

def send_payment_instructions(payment_id, customer_phone, customer_company_phone)
  uri = URI.parse(BASE_URI+'/charges/'+payment_id.to_s+'/sms')
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Post.new(uri.request_uri)
  request.basic_auth @api_key, ''
  params = { "payment_id" => payment_id.to_s,
  			     "customer_phone" => customer_phone.to_s,
  			     "customer_company_phone" => customer_company_phone
  		     }
  request.set_form_data(params)
  http.request(request)
end

#verify_charge(payment_id) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/compropago/client.rb', line 36

def verify_charge(payment_id)
  uri = URI.parse(BASE_URI+'/charges/'+payment_id)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Get.new(uri.request_uri)
  request.basic_auth @api_key, ''
  http.request(request)
end