<commit_before>require 'signer'
require 'savon'

module Transbank
  class Api

    def client
      @client ||= Transbank::Client.new
    end

    def config
      Transbank::Webpay.configuration
    end

    def init_data(amount, buyOrder, sessionId, returnURL = nil, finalURL = nil)

      commerce_code = config.commerce_code
      returnURL = returnURL || "http://localhost:3000?action=result"
      finalURL = finalURL || "http://localhost:3000?action=end"

      return {
        "wsInitTransactionInput" => {
          "wSTransactionType" => "TR_NORMAL_WS",
          "buyOrder" => buyOrder,
          "sessionId" => sessionId,
          "returnURL" => returnURL,
          "finalURL" => finalURL,
          "transactionDetails" => {
            "amount" => amount,
            "commerceCode" => commerce_code,
            "buyOrder" => buyOrder
          }
        }
      }
    end

    def init_transaction amount, buyOrder, sessionId, return_url = nil, final_url = nil
      input = init_data(amount, buyOrder, sessionId, return_url, final_url)
      document = client.make_request(:init_transaction, input)
      return document if document.to_s == "INVALID"
      response = Transbank::Document.get_xml_values(['url', 'token'], document)
      show_log("init_transaction response", response)
      return response
    end

    def get_transaction_result token
      input = {"tokenInput" => token}
      document = client.make_request(:get_transaction_result, input)
      keys = ["paymenttypecode", "vci", "signaturevalue", "keyinfo", "securitytokenreference", "buyorder", "carddetail", "cardnumber", "amount", "authorizationcode",
        "responsecode", "sessionid", "transactiondate", "sharesnumber", "urlredirection"]
      response = Transbank::Document.get_xml_values(keys, document)
      show_log("get_transaction_result response", response)
      return response
    end<commit_msg>Update api.rb<commit_after>require 'signer'
require 'savon'

module Transbank
  class Api

    def client
      @client ||= Transbank::Client.new
    end

    def config
      Transbank::Webpay.configuration
    end

    def init_data(amount, buyOrder, sessionId, returnURL = nil, finalURL = nil)

      commerce_code = config.commerce_code
      returnURL = returnURL || "http://localhost:3000?action=result"
      finalURL = finalURL || "http://localhost:3000?action=end"

      return {
        "wsInitTransactionInput" => {
          "wSTransactionType" => "TR_NORMAL_WS",
          "buyOrder" => buyOrder,
          "sessionId" => sessionId,
          "returnURL" => returnURL,
          "finalURL" => finalURL,
          "transactionDetails" => {
            "amount" => amount,
            "commerceCode" => commerce_code,
            "buyOrder" => buyOrder
          }
        }
      }
    end

    def init_transaction amount, buyOrder, sessionId, return_url = nil, final_url = nil
      input = init_data(amount, buyOrder, sessionId, return_url, final_url)
      document = client.make_request(:init_transaction, input)
      return document if document.to_s == "INVALID"
      response = Transbank::Document.get_xml_values(['url', 'token'], document)
      show_log("init_transaction response", response)
      return response
    end

    def get_transaction_result token
      input = {"tokenInput" => token}
      document = client.make_request(:get_transaction_result, input)
      return document if document.to_s == "INVALID"
      keys = ["paymenttypecode", "vci", "signaturevalue", "keyinfo", "securitytokenreference", "buyorder", "carddetail", "cardnumber", "amount", "authorizationcode",
        "responsecode", "sessionid", "transactiondate", "sharesnumber", "urlredirection"]
      response = Transbank::Document.get_xml_values(keys, document)
      show_log("get_transaction_result response", response)
      return response
    end
{'fraction_non_alphanumeric': 0.11496633868461936, 'fraction_numerical': 0.004142931123770067, 'mean_word_length': 3.127136752136752, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4471374', 'n_tokens_mistral': 1106, 'n_tokens_neox': 1077, 'n_words': 245}