lib/processout/invoice.rb in processout-0.2.1 vs lib/processout/invoice.rb in processout-0.3.0

- old
+ new

@@ -119,32 +119,38 @@ # Initializes the Invoice object # Params: # +client+:: +ProcessOut+ client instance - def initialize(client) + # +data+:: data that can be used to fill the object + def initialize(client, data = {}) @client = client - @id = "" - @project = nil - @transaction = nil - @customer = nil - @subscription = nil - @url = "" - @name = "" - @amount = "" - @currency = "" - @metadata = Hash.new - @request_email = false - @request_shipping = false - @return_url = "" - @cancel_url = "" - @sandbox = false - @created_at = "" + @id = data.fetch(:id, "") + @project = data.fetch(:project, nil) + @transaction = data.fetch(:transaction, nil) + @customer = data.fetch(:customer, nil) + @subscription = data.fetch(:subscription, nil) + @url = data.fetch(:url, "") + @name = data.fetch(:name, "") + @amount = data.fetch(:amount, "") + @currency = data.fetch(:currency, "") + @metadata = data.fetch(:metadata, Hash.new) + @request_email = data.fetch(:request_email, false) + @request_shipping = data.fetch(:request_shipping, false) + @return_url = data.fetch(:return_url, "") + @cancel_url = data.fetch(:cancel_url, "") + @sandbox = data.fetch(:sandbox, false) + @created_at = data.fetch(:created_at, "") end + # Create a new Invoice using the current client + def new(data = {}) + Invoice.new(@client, data) + end + # Fills the object with data coming from the API # Params: # +data+:: +Hash+ of data coming from the API def fill_with_data(data) if data.include? "id" @@ -201,11 +207,11 @@ # Authorize the invoice using the given source (customer or token) # Params: # +source+:: Source used to authorization the payment. Can be a card, a token or a gateway request # +options+:: +Hash+ of options - def authorize(source, options = nil) + def authorize(source, options = {}) request = Request.new(@client) path = "/invoices/" + CGI.escape(@id) + "/authorize" data = { "source" => source } @@ -213,22 +219,22 @@ response = Response.new(request.post(path, data, options)) return_values = Array.new body = response.body body = body["transaction"] - transaction = Transaction(self._client) + transaction = Transaction.new(@client) return_values.push(transaction.fill_with_data(body)) return_values[0] end # Capture the invoice using the given source (customer or token) # Params: # +source+:: Source used to authorization the payment. Can be a card, a token or a gateway request # +options+:: +Hash+ of options - def capture(source, options = nil) + def capture(source, options = {}) request = Request.new(@client) path = "/invoices/" + CGI.escape(@id) + "/capture" data = { "source" => source } @@ -236,21 +242,21 @@ response = Response.new(request.post(path, data, options)) return_values = Array.new body = response.body body = body["transaction"] - transaction = Transaction(self._client) + transaction = Transaction.new(@client) return_values.push(transaction.fill_with_data(body)) return_values[0] end # Get the customer linked to the invoice. # Params: # +options+:: +Hash+ of options - def customer(options = nil) + def customer(options = {}) request = Request.new(@client) path = "/invoices/" + CGI.escape(@id) + "/customers" data = { } @@ -258,22 +264,22 @@ response = Response.new(request.get(path, data, options)) return_values = Array.new body = response.body body = body["customer"] - customer = Customer(self._client) + customer = Customer.new(@client) return_values.push(customer.fill_with_data(body)) return_values[0] end # Assign a customer to the invoice. # Params: # +customer_id+:: ID of the customer to be linked to the invoice # +options+:: +Hash+ of options - def assign_customer(customer_id, options = nil) + def assign_customer(customer_id, options = {}) request = Request.new(@client) path = "/invoices/" + CGI.escape(@id) + "/customers" data = { "customer_id" => customer_id } @@ -281,21 +287,21 @@ response = Response.new(request.post(path, data, options)) return_values = Array.new body = response.body body = body["customer"] - customer = Customer(self._client) + customer = Customer.new(@client) return_values.push(customer.fill_with_data(body)) return_values[0] end # Get the transaction of the invoice. # Params: # +options+:: +Hash+ of options - def transaction(options = nil) + def transaction(options = {}) request = Request.new(@client) path = "/invoices/" + CGI.escape(@id) + "/transactions" data = { } @@ -303,21 +309,21 @@ response = Response.new(request.get(path, data, options)) return_values = Array.new body = response.body body = body["transaction"] - transaction = Transaction(self._client) + transaction = Transaction.new(@client) return_values.push(transaction.fill_with_data(body)) return_values[0] end # Void the invoice # Params: # +options+:: +Hash+ of options - def void(options = nil) + def void(options = {}) request = Request.new(@client) path = "/invoices/" + CGI.escape(@id) + "/void" data = { } @@ -325,21 +331,21 @@ response = Response.new(request.post(path, data, options)) return_values = Array.new body = response.body body = body["transaction"] - transaction = Transaction(self._client) + transaction = Transaction.new(@client) return_values.push(transaction.fill_with_data(body)) return_values[0] end # Get all the invoices. # Params: # +options+:: +Hash+ of options - def all(options = nil) + def all(options = {}) request = Request.new(@client) path = "/invoices" data = { } @@ -348,11 +354,11 @@ return_values = Array.new a = Array.new body = response.body for v in body['invoices'] - tmp = Invoice(@client) + tmp = Invoice.new(@client) tmp.fill_with_data(v) a.push(tmp) end return_values.push(a) @@ -363,11 +369,11 @@ end # Create a new invoice. # Params: # +options+:: +Hash+ of options - def create(options = nil) + def create(options = {}) request = Request.new(@client) path = "/invoices" data = { "name" => @name, "amount" => @amount, @@ -395,11 +401,11 @@ # Create a new invoice for the given customer ID. # Params: # +customer_id+:: ID of the customer # +options+:: +Hash+ of options - def create_for_customer(customer_id, options = nil) + def create_for_customer(customer_id, options = {}) request = Request.new(@client) path = "/invoices" data = { "name" => @name, "amount" => @amount, @@ -428,10 +434,10 @@ # Find an invoice by its ID. # Params: # +invoice_id+:: ID of the invoice # +options+:: +Hash+ of options - def find(invoice_id, options = nil) + def find(invoice_id, options = {}) request = Request.new(@client) path = "/invoices/" + CGI.escape(invoice_id) + "" data = { }