在Laravel 5.3中导入Quickbook库时出错

时间:2016-09-29 05:26:05

标签: php laravel quickbooks

我正在将quickbooks与我的laravel应用程序集成。集成后我得到了这个错误,

  

PHP警告:require_once(../ QuickBooks.php):无法打开流:   在......中没有该文件或目录   第2行/home/vipin/projects/development/Quickbook/config/app.php       PHP致命错误:require_once():打开所需的'../QuickBooks.php'失败   (include_path中=”:在/ usr /共享/ PHP:/首页/ Ubuntu的/项目/开发/的QuickBook /供应商/ consolibyte / QuickBooks的)   在/home/ubuntu/projects/development/Quickbook/config/app.php上线   2

这是我的控制器 Quickbook.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
// require_once '../QuickBooks.php';

use App\Http\Requests;

class QuickBooksController extends Controller
{
private $IntuitAnywhere;
private $context;
private $realm;

public function __construct(){
  if (!\QuickBooks_Utilities::initialized(env('QBO_DSN'))) {
      // Initialize creates the neccessary database schema for queueing up requests and logging
      \QuickBooks_Utilities::initialize(env('QBO_DSN'));
  }
  $this->IntuitAnywhere = new \QuickBooks_IPP_IntuitAnywhere(env('QBO_DSN'), env('QBO_ENCRYPTION_KEY'), env('QBO_OAUTH_CONSUMER_KEY'), env('QBO_CONSUMER_SECRET'), env('QBO_OAUTH_URL'), env('QBO_SUCCESS_URL'));
}

public function  qboConnect(){
  if ($this->IntuitAnywhere->check(env('QBO_USERNAME'), env('QBO_TENANT')) && $this->IntuitAnywhere->test(env('QBO_USERNAME'), env('QBO_TENANT'))) {
      // Set up the IPP instance
      $IPP = new \QuickBooks_IPP(env('QBO_DSN'));
      // Get our OAuth credentials from the database
      $creds = $this->IntuitAnywhere->load(env('QBO_USERNAME'), env('QBO_TENANT'));
      // Tell the framework to load some data from the OAuth store
      $IPP->authMode(
          \QuickBooks_IPP::AUTHMODE_OAUTH,
          env('QBO_USERNAME'),
          $creds);

      if (env('QBO_SANDBOX')) {
          // Turn on sandbox mode/URLs
          $IPP->sandbox(true);
      }
      // This is our current realm
      $this->realm = $creds['qb_realm'];
      // Load the OAuth information from the database
      $this->context = $IPP->context();

      return true;
  } else {
      return false;
  }
}

public function qboOauth(){
  if ($this->IntuitAnywhere->handle(env('QBO_USERNAME'), env('QBO_TENANT')))
  {
      ; // The user has been connected, and will be redirected to QBO_SUCCESS_URL automatically.
  }
  else
  {
      // If this happens, something went wrong with the OAuth handshake
      die('Oh no, something bad happened: ' . $this->IntuitAnywhere->errorNumber() . ': ' . $this->IntuitAnywhere->errorMessage());
  }
}

public function qboSuccess(){
  return view('qbo_success');
}

public function qboDisconnect(){
  $this->IntuitAnywhere->disconnect(env('QBO_USERNAME'), env('QBO_TENANT'),true);
  return redirect()->intended("/yourpath");// afer disconnect redirect where you want

}

public function createCustomer(){

  $CustomerService = new \QuickBooks_IPP_Service_Customer();

  $Customer = new \QuickBooks_IPP_Object_Customer();
  $Customer->setTitle('Ms');
$Customer->setGivenName('Shannon');
$Customer->setMiddleName('B');
$Customer->setFamilyName('Palmer');
$Customer->setDisplayName('Shannon B Palmer ' . mt_rand(0, 1000));
  // Terms (e.g. Net 30, etc.)
  $Customer->setSalesTermRef(4);

  // Phone #
  $PrimaryPhone = new \QuickBooks_IPP_Object_PrimaryPhone();
  $PrimaryPhone->setFreeFormNumber('860-532-0089');
$Customer->setPrimaryPhone($PrimaryPhone);

  // Mobile #
  $Mobile = new \QuickBooks_IPP_Object_Mobile();
  $Mobile->setFreeFormNumber('860-532-0089');
$Customer->setMobile($Mobile);

  // Fax #
  $Fax = new \QuickBooks_IPP_Object_Fax();
  $Fax->setFreeFormNumber('860-532-0089');
$Customer->setFax($Fax);

  // Bill address
  $BillAddr = new \QuickBooks_IPP_Object_BillAddr();
  $BillAddr->setLine1('72 E Blue Grass Road');
$BillAddr->setLine2('Suite D');
$BillAddr->setCity('Mt Pleasant');
$BillAddr->setCountrySubDivisionCode('MI');
$BillAddr->setPostalCode('48858');
$Customer->setBillAddr($BillAddr);

  // Email
  $PrimaryEmailAddr = new \QuickBooks_IPP_Object_PrimaryEmailAddr();
  $PrimaryEmailAddr->setAddress('support@consolibyte.com');
  $Customer->setPrimaryEmailAddr($PrimaryEmailAddr);

  if ($resp = $CustomerService->add($this->context, $this->realm, $Customer))
  {
      //print('Our new customer ID is: [' . $resp . '] (name "' . $Customer->getDisplayName() . '")');
      //return $resp;
      //echo $resp;exit;
      //$resp = str_replace('{','',$resp);
      //$resp = str_replace('}','',$resp);
      //$resp = abs($resp);
      return $this->getId($resp);
  }
  else
  {
      //echo 'Not Added qbo';
      print($CustomerService->lastError($this->context));
  }
}

public function addItem(){
  $ItemService = new \QuickBooks_IPP_Service_Item();

  $Item = new \QuickBooks_IPP_Object_Item();

  $Item->setName('My Item');
$Item->setType('Inventory');
$Item->setIncomeAccountRef('53');

  if ($resp = $ItemService->add($this->context, $this->realm, $Item))
  {
      return $this->getId($resp);
  }
  else
  {
      print($ItemService->lastError($this->context));
  }
}

public function addInvoice($invoiceArray,$itemArray,$customerRef){

  $InvoiceService = new \QuickBooks_IPP_Service_Invoice();

  $Invoice = new \QuickBooks_IPP_Object_Invoice();

  $Invoice = new QuickBooks_IPP_Object_Invoice();

$Invoice->setDocNumber('WEB' . mt_rand(0, 10000));
$Invoice->setTxnDate('2013-10-11');

$Line = new QuickBooks_IPP_Object_Line();
$Line->setDetailType('SalesItemLineDetail');
$Line->setAmount(12.95 * 2);
$Line->setDescription('Test description goes here.');

$SalesItemLineDetail = new QuickBooks_IPP_Object_SalesItemLineDetail();
$SalesItemLineDetail->setItemRef('8');
$SalesItemLineDetail->setUnitPrice(12.95);
$SalesItemLineDetail->setQty(2);

$Line->addSalesItemLineDetail($SalesItemLineDetail);

$Invoice->addLine($Line);

$Invoice->setCustomerRef('67');


  if ($resp = $InvoiceService->add($this->context, $this->realm, $Invoice))
  {
      return $this->getId($resp);
  }
  else
  {
      print($InvoiceService->lastError());
  }
}

public function getId($resp){
  $resp = str_replace('{','',$resp);
  $resp = str_replace('}','',$resp);
  $resp = abs($resp);
  return $resp;
}

}

配置/ app.php

<?php
require_once '../QuickBooks.php';

return [


    'qbo_token' => env('QUICKBOOK_TOKEN'),
    'qbo_consumer_key' => env('QBO_OAUTH_CONSUMER_KEY'),
    'qbo_consumer_secret' => env('QBO_CONSUMER_SECRET'),
    'qbo_sandbox' => env('QBO_SANDBOX'),
    'qbo_encryption_key' => env('QBO_ENCRYPTION_KEY'),
    'qbo_username' => env('QBO_USERNAME'),
    'qbo_tenant' => env('QBO_TENANT'),
    'qbo_auth_url' => 'http://app.localhost:8000/qbo/oauth',
    'qbo_success_url' => 'http://app.localhost:8000/qbo/success',
    'qbo_mysql_connection' => 'mysqli://'. env('DB_USERNAME') .':'. env('DB_PASSWORD') .'@'. env('DB_HOST') .'/'. env('DB_DATABASE'),

3 个答案:

答案 0 :(得分:0)

在现代框架中(通常在现代PHP中)使用require而不是自动加载器是一种不好的做法。我强烈建议使用包管理器(例如composer)将模块正确添加到项目中。

例如,要使用composer将 quickbooks 库添加到项目中,您只需运行一个命令:

composer require consolibyte/quickbooks

答案 1 :(得分:0)

这里有几个方面需要改进,给定的代码和方法

  1. 作为Anton correctly points out,您不应该直接要求任何quickbooks库文件。如果您已通过Composer加载它们,那么它们将自动加载,因为Composer自动加载器将从供应商加载QuickBooks文件。这对于Laravel以及基于Composer的通用应用程序来说是正确的 - 与Laravel的唯一区别在于没有为此SDK编写的特定Laravel包ServiceProvider,但这并不重要。
  2. QuickBooks库尝试跳过自动加载任何以“QuickBooks”开头的类,因此您最好为控制器类创建QuickBooks 文件夹。这更像是一个'陷阱'并且一直是pointed out in the repo issues
  3. 您收到Driver/.php错误的原因是您没有指定QBO_DSN,或者错误地执行了此错误 - 您正在运行此初始化的DSN环境变量正在运行通过SDK代码中的parse_url(),出现falsenull并打破自动加载器以进行初始化。如果将其设置为正确的连接字符串(例如mysqli://username:password@host:port/database并注意port 必须是数字或被视为格式错误),它将正确处理DSN并继续加载页面。请注意,初始化将尝试解析并获取主机的网络地址,因此您不能只在其中放置虚拟值并期望它可以工作 - 这需要先存在。
  4. 您正在混合环境变量和应用程序配置,而无需正确使用其中任何一个。如果您希望将数据库连接字符串(a.k.a。QBO_DSN)构建为应用程序配置设置qbo_mysql_connection的特定方式,那么在尝试初始化/加载/等时应该是using the configuration setting。您应该使用env('QBO_DSN')从应用设置中加载构建的版本,而不是使用config('app.qbo_mysql_connection')。通常,您根本不会将如此多的环境变量加载到控制器中 - 这应该由应用程序处理,然后控制器调用应用程序配置,因此它不知道如何定义它们。
  5. 您不应该在应用程序配置文件中require任何内容 - 该文件仅用于设置配置变量。
  6. 由于QuickBooks SDK没有正确命名空间(yet),因此没有一种很好的PSR-4方式加载(和use - )这些类,但它仍然是很好的做法在文件顶部使用use子句(例如use QuickBooks_Utilities;),以便您可以使用这些类而不必忘记前面的反斜杠(即不再有\QuickBooks_Utilities,只需{{1在使用中) - 在给定的代码中有几个实例已被遗忘,并且因为Laravel应用程序是命名空间并且将在QuickBooks_Utilities命名空间中查找这些类(例如“不能”找到班级App\Http\Controllers“)。
  7. 缩进 - 选择一种样式(例如制表符,2空格,PSR-2等),然后坚持下去。在提交到您的存储库或在SO上发布之前,在所有代码上运行phpcs或其他一些清理工具 - 可读性非常重要!

答案 2 :(得分:0)

将此行添加到Config/app.php页脚

require_once '../QuickBooks.php';