lib/docdata/payment.rb in docdata-0.0.2 vs lib/docdata/payment.rb in docdata-0.0.5

- old
+ new

@@ -51,13 +51,13 @@ attr_accessor :bank_id attr_accessor :prefered_payment_method attr_accessor :line_items attr_accessor :key attr_accessor :default_act + attr_accessor :canceled - # # Initializer to transform a +Hash+ into an Payment object # # @param [Hash] args def initialize(args=nil) @@ -82,29 +82,48 @@ # def create # if there are any line items, they should all be valid. validate_line_items - # puts - # make the SOAP API call - response = Docdata.client.call(:create, xml: xml) + response = Docdata.client.call(:create, xml: create_xml) response_object = Docdata::Response.parse(:create, response) if response_object.success? self.key = response_object.key end + + # set `self` as the value of the `payment` attribute in the response object + response_object.payment = self + response_object.url = redirect_url + return response_object end - # @return [String] the xml to send in the SOAP API - def xml - xml_file = "#{File.dirname(__FILE__)}/xml/create.xml.erb" - template = File.read(xml_file) - namespace = OpenStruct.new(payment: self, shopper: shopper) - xml = ERB.new(template).result(namespace.instance_eval { binding }) + # + # This calls the 'cancel' method of the SOAP API + # It cancels the payment and returns a Docdata::Response object + def cancel + # make the SOAP API call + response = Docdata.client.call(:cancel, xml: cancel_xml) + response_object = Docdata::Response.parse(:cancel, response) + if response_object.success? + self.key = response_object.key + end + + # set `self` as the value of the `payment` attribute in the response object + response_object.payment = self + self.canceled = true + return true end + # This method makes it possible to find and cancel a payment with only the key + # It combines + def self.cancel(api_key) + p = self.find(api_key) + p.cancel + end + # Initialize a Payment object with the key set def self.find(api_key) p = self.new(key: api_key) if p.status.success return p @@ -133,39 +152,61 @@ # @return [String] The URI where the consumer can be redirected to in order to pay def redirect_url url = {} - base_url = Docdata.return_url - if Docdata.test_mode + base_url = Docdata::Config.return_url + if Docdata::Config.test_mode redirect_base_url = 'https://test.docdatapayments.com/ps/menu' else redirect_base_url = 'https://secure.docdatapayments.com/ps/menu' end url[:command] = "show_payment_cluster" url[:payment_cluster_key] = key - url[:merchant_name] = Docdata.username + url[:merchant_name] = Docdata::Config.username # only include return URL if present if base_url.present? url[:return_url_success] = "#{base_url}/success?key=#{url[:payment_cluster_key]}" url[:return_url_pending] = "#{base_url}/pending?key=#{url[:payment_cluster_key]}" url[:return_url_canceled] = "#{base_url}/canceled?key=#{url[:payment_cluster_key]}" url[:return_url_error] = "#{base_url}/error?key=#{url[:payment_cluster_key]}" end - url[:client_language] = shopper.language_code + if shopper && shopper.language_code + url[:client_language] = shopper.language_code + end if default_act - url[:default_act] = true + url[:default_act] = "yes" end if bank_id.present? url[:ideal_issuer_id] = bank_id url[:default_pm] = "IDEAL" end params = URI.encode_www_form(url) uri = "#{redirect_base_url}?#{params}" end + alias_method :url, :redirect_url private + + + # @return [String] the xml to send in the SOAP API + def create_xml + xml_file = "#{File.dirname(__FILE__)}/xml/create.xml.erb" + template = File.read(xml_file) + namespace = OpenStruct.new(payment: self, shopper: shopper) + xml = ERB.new(template).result(namespace.instance_eval { binding }) + end + + + # @return [String] the xml to send in the SOAP API + def cancel_xml + xml_file = "#{File.dirname(__FILE__)}/xml/cancel.xml.erb" + template = File.read(xml_file) + namespace = OpenStruct.new(payment: self) + xml = ERB.new(template).result(namespace.instance_eval { binding }) + end + # In case there are any line_items, validate them all and # raise an error for the first invalid LineItem def validate_line_items if @line_items.any?