Class: PiNetwork

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, wallet_private_key, options = {}) ⇒ PiNetwork

Returns a new instance of PiNetwork.



15
16
17
18
19
20
21
22
# File 'lib/pi_network.rb', line 15

def initialize(api_key, wallet_private_key, options = {})
  validate_private_seed_format!(wallet_private_key)
  @api_key = api_key
  @account = (wallet_private_key)
  @base_url = options[:base_url] || "https://api.minepi.com"

  @open_payments = {}
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



11
12
13
# File 'lib/pi_network.rb', line 11

def 
  @account
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



9
10
11
# File 'lib/pi_network.rb', line 9

def api_key
  @api_key
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



12
13
14
# File 'lib/pi_network.rb', line 12

def base_url
  @base_url
end

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/pi_network.rb', line 10

def client
  @client
end

#from_addressObject (readonly)

Returns the value of attribute from_address.



13
14
15
# File 'lib/pi_network.rb', line 13

def from_address
  @from_address
end

Instance Method Details

#cancel_payment(identifier) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/pi_network.rb', line 95

def cancel_payment(identifier)
  response = Faraday.post(
    base_url + "/v2/payments/#{identifier}/cancel",
    {}.to_json,
    http_headers,
  )

  handle_http_response(response, "An unknown error occurred while cancelling the payment")
end

#complete_payment(identifier, txid) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pi_network.rb', line 83

def complete_payment(identifier, txid)
  body = {"txid": txid}

  response = Faraday.post(
    base_url + "/v2/payments/#{identifier}/complete",
    body.to_json,
    http_headers
  )

  handle_http_response(response, "An unknown error occurred while completing the payment")
end

#create_payment(payment_data) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pi_network.rb', line 38

def create_payment(payment_data)
  validate_payment_data!(payment_data, {amount: true, memo: true, metadata: true, uid: true})

  request_body = {
    payment: payment_data,
  }

  response = Faraday.post(
    base_url + "/v2/payments",
    request_body.to_json,
    http_headers,
  )

  parsed_response = handle_http_response(response, "An unknown error occurred while creating a payment")
  
  identifier = parsed_response["identifier"]
  @open_payments[identifier] = parsed_response

  return identifier
end

#get_incomplete_server_paymentsObject



105
106
107
108
109
110
111
112
113
114
# File 'lib/pi_network.rb', line 105

def get_incomplete_server_payments
  response = Faraday.get(
    base_url + "/v2/payments/incomplete_server_payments",
    {},
    http_headers,
  )

  res = handle_http_response(response, "An unknown error occurred while fetching incomplete payments")
  res["incomplete_server_payments"]
end

#get_payment(payment_id) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pi_network.rb', line 24

def get_payment(payment_id)
  response = Faraday.get(
    base_url + "/v2/payments/#{payment_id}",
    {},
    http_headers,
  )

  if response.status == 404
    raise Errors::PaymentNotFoundError.new("Payment not found", payment_id)
  end

  handle_http_response(response, "An unknown error occurred while fetching the payment")
end

#submit_payment(payment_id) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pi_network.rb', line 59

def submit_payment(payment_id)
  payment = @open_payments[payment_id]

  if payment.nil?
    payment = get_payment(payment_id)
  end

  set_horizon_client(payment["network"])
  @from_address = payment["from_address"]

  transaction_data = {
    amount: payment["amount"],
    identifier: payment["identifier"],
    recipient: payment["to_address"]
  }

  transaction = build_a2u_transaction(transaction_data)
  txid = submit_transaction(transaction)

  @open_payments.delete(payment_id)

  return txid
end