lib/processout/invoice.rb in processout-1.0.6 vs lib/processout/invoice.rb in processout-1.0.7

- old
+ new

@@ -123,26 +123,26 @@ # +client+:: +ProcessOut+ client instance # +data+:: data that can be used to fill the object def initialize(client, data = {}) @client = client - @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, "") + self.id = data.fetch(:id, nil) + self.project = data.fetch(:project, nil) + self.transaction = data.fetch(:transaction, nil) + self.customer = data.fetch(:customer, nil) + self.subscription = data.fetch(:subscription, nil) + self.url = data.fetch(:url, nil) + self.name = data.fetch(:name, nil) + self.amount = data.fetch(:amount, nil) + self.currency = data.fetch(:currency, nil) + self.metadata = data.fetch(:metadata, nil) + self.request_email = data.fetch(:request_email, nil) + self.request_shipping = data.fetch(:request_shipping, nil) + self.return_url = data.fetch(:return_url, nil) + self.cancel_url = data.fetch(:cancel_url, nil) + self.sandbox = data.fetch(:sandbox, nil) + self.created_at = data.fetch(:created_at, nil) end # Create a new Invoice using the current client def new(data = {}) @@ -151,10 +151,13 @@ # 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.nil? + return self + end if data.include? "id" self.id = data["id"] end if data.include? "project" self.project = data["project"] @@ -203,15 +206,44 @@ end self end + # Prefills the object with the data passed as Parameters + # Params: + # +data+:: +Hash+ of data + def prefill(data) + if data.nil? + return self + end + self.id = data.fetch(:id, self.id) + self.project = data.fetch(:project, self.project) + self.transaction = data.fetch(:transaction, self.transaction) + self.customer = data.fetch(:customer, self.customer) + self.subscription = data.fetch(:subscription, self.subscription) + self.url = data.fetch(:url, self.url) + self.name = data.fetch(:name, self.name) + self.amount = data.fetch(:amount, self.amount) + self.currency = data.fetch(:currency, self.currency) + self.metadata = data.fetch(:metadata, self.metadata) + self.request_email = data.fetch(:request_email, self.request_email) + self.request_shipping = data.fetch(:request_shipping, self.request_shipping) + self.return_url = data.fetch(:return_url, self.return_url) + self.cancel_url = data.fetch(:cancel_url, self.cancel_url) + self.sandbox = data.fetch(:sandbox, self.sandbox) + self.created_at = data.fetch(:created_at, self.created_at) + + self + end + # 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 = {}) + self.prefill(options) + request = Request.new(@client) path = "/invoices/" + CGI.escape(@id) + "/authorize" data = { "source" => source } @@ -231,10 +263,12 @@ # 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 = {}) + self.prefill(options) + request = Request.new(@client) path = "/invoices/" + CGI.escape(@id) + "/capture" data = { "source" => source } @@ -253,10 +287,12 @@ # Get the customer linked to the invoice. # Params: # +options+:: +Hash+ of options def fetch_customer(options = {}) + self.prefill(options) + request = Request.new(@client) path = "/invoices/" + CGI.escape(@id) + "/customers" data = { } @@ -276,10 +312,12 @@ # 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 = {}) + self.prefill(options) + request = Request.new(@client) path = "/invoices/" + CGI.escape(@id) + "/customers" data = { "customer_id" => customer_id } @@ -298,10 +336,12 @@ # Get the transaction of the invoice. # Params: # +options+:: +Hash+ of options def fetch_transaction(options = {}) + self.prefill(options) + request = Request.new(@client) path = "/invoices/" + CGI.escape(@id) + "/transactions" data = { } @@ -320,10 +360,12 @@ # Void the invoice # Params: # +options+:: +Hash+ of options def void(options = {}) + self.prefill(options) + request = Request.new(@client) path = "/invoices/" + CGI.escape(@id) + "/void" data = { } @@ -342,10 +384,12 @@ # Get all the invoices. # Params: # +options+:: +Hash+ of options def all(options = {}) + self.prefill(options) + request = Request.new(@client) path = "/invoices" data = { } @@ -370,10 +414,12 @@ # Create a new invoice. # Params: # +options+:: +Hash+ of options def create(options = {}) + self.prefill(options) + request = Request.new(@client) path = "/invoices" data = { "name" => @name, "amount" => @amount, @@ -402,10 +448,12 @@ # 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 = {}) + self.prefill(options) + request = Request.new(@client) path = "/invoices" data = { "name" => @name, "amount" => @amount, @@ -435,9 +483,11 @@ # Find an invoice by its ID. # Params: # +invoice_id+:: ID of the invoice # +options+:: +Hash+ of options def find(invoice_id, options = {}) + self.prefill(options) + request = Request.new(@client) path = "/invoices/" + CGI.escape(invoice_id) + "" data = { }