-
Notifications
You must be signed in to change notification settings - Fork 0
/
payment_client.rb
34 lines (26 loc) · 933 Bytes
/
payment_client.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
# frozen_string_literal: true
require 'net/http'
require 'uri'
require 'json'
class PaymentClient
class PaymentClientError < StandardError; end
CREATE_PAYMENT_API_PATH = 'paymentIntents/create'
def create_payment(subscription_id, amount)
uri = URI.parse("#{host}#{CREATE_PAYMENT_API_PATH}")
request = Net::HTTP::Post.new(uri)
request.content_type = 'application/json'
request.body = { 'amount' => amount, 'subscription_id' => subscription_id }.to_json
response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(request)
end
# response = Struct.new(:body).new('{"status": "insufficient_funds"}')
result = JSON.parse(response.body)
result['status']
rescue StandardError => e
raise PaymentClientError, "Payment API error: #{e.message}"
end
private
def host
'http://localhost/' # Can dynamically configure the host based on the environment
end
end