class Threoze::Payjunction::TransactionService < Threoze::Payjunction::ApplicationService
  def call
    response = JSON.parse(RestClient.post(url, body, headers))
    status = { success: response['response']['approved'] }
    return status if status[:success]

    status.tap { |s| s[:error] = response['response'] }
  rescue RestClient::Exception => e
    { success: false, error: e.response.body }
  end

  private
    def endpoint
      :transactions
    end

    def billing_name
      Threoze::BillingService.call(@school, :show)['billingname']
    end

    def body
      {}.tap do |props|
        props[:action] = :CHARGE
        props[:vaultId] = @params[:vault_id]
        props[:amountBase] = @params[:transaction_amount]
        props[:billingCompanyName] = billing_name
      end
    end
end
