Class: BitPay::Client

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

Overview

This class is used to instantiate a BitPay Client object. It is expected to be thread safe.

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Examples:

# Create a client with a pem file created by the bitpay client:
client = BitPay::Client.new


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bitpay/client.rb', line 21

def initialize(opts={})
  @pem               = opts[:pem] || ENV['BITPAY_PEM'] || KeyUtils.retrieve_or_generate_pem 
  @key               = KeyUtils.create_key @pem
  @priv_key          = KeyUtils.get_private_key @key
  @pub_key           = KeyUtils.get_public_key @key
  @client_id         = KeyUtils.generate_sin_from_pem @pem
  @uri               = URI.parse opts[:api_uri] || API_URI
  @user_agent        = opts[:user_agent] || USER_AGENT
  @https             = Net::HTTP.new @uri.host, @uri.port
  @https.use_ssl     = true
  @https.ca_file     = CA_FILE
  @tokens            = {}

  # Option to disable certificate validation in extraordinary circumstance.  NOT recommended for production use
  @https.verify_mode = opts[:insecure] == true ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
  
  # Option to enable http request debugging
  @https.set_debug_output($stdout) if opts[:debug] == true

end

Instance Method Details

#create_invoice(id:, price:, currency:, facade: 'pos', params: {}) ⇒ Object



48
49
50
51
52
# File 'lib/bitpay/client.rb', line 48

def create_invoice(id:, price:, currency:, facade: 'pos', params:{})
  params.merge!({price: price, currency: currency})
  response = send_request("POST", "invoices", facade: facade, params: params)
  response["data"]
end

#get_public_invoice(id:) ⇒ Object



54
55
56
57
58
# File 'lib/bitpay/client.rb', line 54

def get_public_invoice(id:)
  request = Net::HTTP::Get.new("/invoices/#{id}")
  response = process_request(request)
  response["data"]
end

#pair_pos_client(claimCode) ⇒ Object



42
43
44
45
46
# File 'lib/bitpay/client.rb', line 42

def pair_pos_client(claimCode)
  response = set_pos_token(claimCode)
  get_token 'pos'
  response
end

#send_request(verb, path, facade: 'merchant', params: {}, token: nil) ⇒ Object

Generates REST request to api endpoint



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
91
92
93
# File 'lib/bitpay/client.rb', line 61

def send_request(verb, path, facade: 'merchant', params: {}, token: nil)
  token ||= get_token(facade)

  # Verb-specific logic
  case verb.upcase
    when "GET"
      urlpath = '/' + path + '?nonce=' + KeyUtils.nonce + '&token=' + token
      request = Net::HTTP::Get.new urlpath
      request['X-Signature'] = KeyUtils.sign(@uri.to_s + urlpath, @priv_key)

    when "PUT"

    when "POST"  # Requires a GUID

      urlpath = '/' + path
      request = Net::HTTP::Post.new urlpath
      params[:token] = token
      params[:nonce] = KeyUtils.nonce
      params[:guid]  = SecureRandom.uuid
      params[:id] = @client_id
      request.body = params.to_json
      request['X-Signature'] = KeyUtils.sign(@uri.to_s + urlpath + request.body, @priv_key)

    when "DELETE"
   
      raise(BitPayError, "Invalid HTTP verb: #{verb.upcase}")
  end

  # Build request headers and submit
  request['X-Identity'] = @pub_key
 
  response = process_request(request)
end