lib/litle/models/litle_response.rb in killbill-litle-1.9.6 vs lib/litle/models/litle_response.rb in killbill-litle-1.9.7

- old
+ new

@@ -82,10 +82,56 @@ def to_refund_response to_killbill_response :refund end + # VisibleForTesting + def self.search_query(api_call, search_key, offset = nil, limit = nil) + t = self.arel_table + + # Exact matches only + where_clause = t[:params_litleonelineresponse_saleresponse_id].eq(search_key) + .or(t[:params_litleonelineresponse_saleresponse_litle_txn_id].eq(search_key)) + .or(t[:params_litleonelineresponse_saleresponse_order_id].eq(search_key)) + .or(t[:params_litleonelineresponse_saleresponse_auth_code].eq(search_key)) + + # Only search successful payments and refunds + where_clause = where_clause.and(t[:api_call].eq(api_call)) + .and(t[:success].eq(true)) + + query = t.where(where_clause) + .order(t[:id]) + + if offset.blank? and limit.blank? + # true is for count distinct + query.project(t[:id].count(true)) + else + query.skip(offset) unless offset.blank? + query.take(limit) unless limit.blank? + query.project(t[Arel.star]) + # Not chainable + query.distinct + end + query + end + + def self.search(search_key, offset = 0, limit = 100, type = :payment) + api_call = type == :payment ? 'charge' : 'refund' + pagination = Killbill::Plugin::Model::Pagination.new + pagination.current_offset = offset + pagination.total_nb_records = self.count_by_sql(self.search_query(api_call, search_key)) + pagination.max_nb_records = self.where(:api_call => api_call, :success => true).count + pagination.next_offset = (!pagination.total_nb_records.nil? && offset + limit >= pagination.total_nb_records) ? nil : offset + limit + # Reduce the limit if the specified value is larger than the number of records + actual_limit = [pagination.max_nb_records, limit].min + pagination.iterator = StreamyResultSet.new(actual_limit) do |offset,limit| + self.find_by_sql(self.search_query(api_call, search_key, offset, limit)) + .map { |x| type == :payment ? x.to_payment_response : x.to_refund_response } + end + pagination + end + private def to_killbill_response(type) if litle_transaction.nil? amount_in_cents = nil @@ -95,39 +141,42 @@ second_payment_reference_id = nil else amount_in_cents = litle_transaction.amount_in_cents currency = litle_transaction.currency created_date = litle_transaction.created_at - first_payment_reference_id = params_litleonelineresponse_saleresponse_id - second_payment_reference_id = litle_transaction.litle_txn_id + first_reference_id = params_litleonelineresponse_saleresponse_id + second_reference_id = litle_transaction.litle_txn_id end effective_date = params_litleonelineresponse_saleresponse_response_time || created_date gateway_error = message || params_litleonelineresponse_saleresponse_message gateway_error_code = params_litleonelineresponse_saleresponse_response if type == :payment p_info_plugin = Killbill::Plugin::Model::PaymentInfoPlugin.new - p_info_plugin.amount = BigDecimal.new(amount_in_cents.to_s) / 100.0 if amount_in_cents + p_info_plugin.kb_payment_id = kb_payment_id + p_info_plugin.amount = Money.new(amount_in_cents, currency).to_d if currency p_info_plugin.currency = currency p_info_plugin.created_date = created_date p_info_plugin.effective_date = effective_date p_info_plugin.status = (success ? :PROCESSED : :ERROR) p_info_plugin.gateway_error = gateway_error p_info_plugin.gateway_error_code = gateway_error_code - p_info_plugin.first_payment_reference_id = first_payment_reference_id - p_info_plugin.second_payment_reference_id = second_payment_reference_id + p_info_plugin.first_payment_reference_id = first_reference_id + p_info_plugin.second_payment_reference_id = second_reference_id p_info_plugin else r_info_plugin = Killbill::Plugin::Model::RefundInfoPlugin.new - r_info_plugin.amount = BigDecimal.new(amount_in_cents.to_s) / 100.0 if amount_in_cents + r_info_plugin.kb_payment_id = kb_payment_id + r_info_plugin.amount = Money.new(amount_in_cents, currency).to_d if currency r_info_plugin.currency = currency r_info_plugin.created_date = created_date r_info_plugin.effective_date = effective_date r_info_plugin.status = (success ? :PROCESSED : :ERROR) r_info_plugin.gateway_error = gateway_error r_info_plugin.gateway_error_code = gateway_error_code - r_info_plugin.reference_id = first_payment_reference_id + r_info_plugin.first_refund_reference_id = first_reference_id + r_info_plugin.second_refund_reference_id = second_reference_id r_info_plugin end end def self.extract(response, key1, key2=nil, key3=nil)