lib/ideal-mollie.rb in ideal-mollie-0.0.2 vs lib/ideal-mollie.rb in ideal-mollie-0.0.3

- old
+ new

@@ -2,13 +2,16 @@ require "faraday" require "faraday_middleware" require "multi_xml" # Files +require "ideal-mollie/bank" require "ideal-mollie/config" -require "ideal-mollie/ideal_exception" require "ideal-mollie/engine" if defined?(Rails) && Rails::VERSION::MAJOR == 3 +require "ideal-mollie/ideal_exception" +require "ideal-mollie/order" +require "ideal-mollie/order_result" # # IdealMollie Module # module IdealMollie @@ -17,119 +20,65 @@ # # @visibility public # # @example # IdealMollie.banks - # # => [{:id=>"9999", :name=>"TBM Bank"}, ...] # - # @return [Array] list of supported banks. + # @return [Array<IdealMollie::Bank>] list of supported +Bank+'s. def self.banks response = IdealMollie.request("banklist") banks = response["bank"] banks = [banks] unless banks.is_a?Array # to array if it isn't already - banks.map do |bank| - {:id => bank["bank_id"], :name => bank["bank_name"]} - end + banks.inject([]) { |result, bank| result << IdealMollie::Bank.new(bank); result } end # - # Create a new payment + # Create a new order. # # @visibility public # # @param [int] amount The amount of money to transfer (defined in cents). # @param [String] description The description of the payment on the bank transfer. # @param [String] bank_id The id of the bank selected from one of the supported banks. + # @param [String] return_url Optional override of the return url specified in the Config # # @example + # IdealMollie.new_order(1000, "Ordernumber #123: new gadget", "0031") # - # IdealMollie.new_payment(1000, "Ordernumber #123: new gadget", "0031") - # # => { - # # :transaction_id=>"72457c865d4b800eb8b0f3ba928ff495", - # # :amount=>1000, - # # :currency=>"EUR", - # # :URL=>"http://www.mollie.nl/partners/ideal-test-bank?order_nr=M738907M05885973&transaction_id=72457c865d4b800eb8b0f3ba928ff495&trxid=0073890705885973", - # # :message=>"Your iDEAL-payment has successfully been setup. Your customer should visit the given URL to make the payment" - # # } - # - # @return [Hash] the transaction object - def self.new_payment(amount, description, bank_id) - response = IdealMollie.request("fetch", { - :partnerid => Config.partner_id, - :reporturl => Config.report_url, - :returnurl => Config.return_url, - :description => description, - :amount => amount, - :bank_id => bank_id - }) - order = response["order"] + # @example + # IdealMollie.new_order(1000, "Ordernumber #123: new gadget", "0031", "http://override.url/controller/return_action") + # @return [IdealMollie::Order] the +Order+. + def self.new_order(amount, description, bank_id, return_url=nil) + params = new_order_params(amount, description, bank_id, return_url) + response = IdealMollie.request("fetch", params) - result = { - :transaction_id => order["transaction_id"], - :amount => order["amount"].to_i, - :currency => order["currency"], - :url => order["URL"], - :message => order["message"] - } - result + IdealMollie::Order.new(response["order"]) end # - # Check the status of a transaction. + # Check the status of a order. # # @visibility public # # @param [String] transaction_id the transaction to verify. # # @example - # IdealMollie.check_payment("4b99662febb42ce6f889d9c57f5cf3fa") - # # => { - # # :transaction_id => '4b99662febb42ce6f889d9c57f5cf3fa', - # # :amount => 1465, - # # :currency => "EUR", - # # :payed => true, - # # :consumer => { - # # :name => "Hr J Janssen", - # # :account => "P001234567", - # # :city => "Amsterdam" - # # }, - # # :message => "This iDEAL-order has successfuly been payed for, - # # and this is the first time you check it.", - # # :status => "Expired" - # # } + # IdealMollie.check_order("4b99662febb42ce6f889d9c57f5cf3fa") # # @note Once a transaction is payed, only the next time you verify the - # transaction will the value of 'payed' be 'true'. - # Else it will be 'false'. + # transaction will the value of +payed+ be +true+. + # Else it will be +false+. # - # @return [Hash] the status of the transaction. - def self.check_payment(transaction_id) + # @return [IdealMollie::OrderResult] the +OrderResult+. + def self.check_order(transaction_id) response = IdealMollie.request("check", { :partnerid => Config.partner_id, :transaction_id => transaction_id }) - order = response["order"] - - result = { - :transaction_id => order["transaction_id"], - :amount => order["amount"].to_i, - :currency => order["currency"], - :payed => order["payed"] == "true" ? true : false, - :message => order["message"], - :status => order["status"] - } - if order.has_key?("consumer") - consumer = order["consumer"] - result.merge!(:consumer => { - :name => consumer["consumerName"], - :account => consumer["consumerAccount"], - :city => consumer["consumerCity"] - }) - end - result + IdealMollie::OrderResult.new(response["order"]) end class << self MultiXml.parser = :nokogiri @@ -140,11 +89,11 @@ # @param [String] action The action to perform. # @param [Hash] params Additional parameters to send like partner_id # # @raise [IdealMollie::IdealException] When a error is returned by Mollie # - # @return [Hash] the returned XML as a Hash + # @return [Hash] the returned XML as a +Hash+ def request(action, params={}) params.merge!(:a => action) params.merge!(:testmode => Config.test_mode) request = connection.post do |req| req.url("", params) @@ -154,9 +103,31 @@ if response.has_key?("item") error = response["item"] raise IdealMollie::IdealException.new(error["errorcode"], error["message"], error["type"]) end response + end + + # + # Builds a +Hash+ with the parameters, that are needed for making a new order + # Makes sure the +return_url+ is set to the correct value + # + # @param [int] amount The amount of money to transfer (defined in cents). + # @param [String] description The description of the payment on the bank transfer. + # @param [String] bank_id The id of the bank selected from one of the supported banks. + # @param [String] return_url Optional override of the return url specified in the Config + # + # @return [Hash] the parameter +Hash+ for the new order. + def new_order_params(amount, description, bank_id, return_url=nil) + return_url = Config.return_url if return_url.nil? + { + :partnerid => Config.partner_id, + :reporturl => Config.report_url, + :returnurl => return_url, + :description => description, + :amount => amount, + :bank_id => bank_id + } end private def connection