【问题标题】:How to use non-laravel package at Laravel 5.1如何在 Laravel 5.1 中使用非 laravel 包
【发布时间】:2018-01-17 06:48:29
【问题描述】:

我使用 Laravel 5.1,我们使用 Stripe,但现在我需要更改为 checkout.com Checkout.com 有一个 php 库:https://github.com/checkout/checkout-php-library

我想在我的应用中实现。我首先运行:

composer require checkout/checkout-php-api

所以我安装了库,库在 vendor/checkout 文件夹内

我使用 OrderController 并创建公共功能结帐:

require_once 'vendor\checkout\checkout-php-library\autoload.php';

use com\checkout;

class OrdersController extends Controller
{


    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
public function payment() {

  return view('front.checkout');

}

public function checkout(Request $request) {

  $data = $request->all();


$apiClient = new ApiClient('sk_test_aaaaaa-5116-999-9270-999999999');
// create a charge serive
$charge = $apiClient->chargeService();

try {
    /**  @var ResponseModels\Charge  $ChargeRespons **/
    $ChargeResponse = $charge->verifyCharge($data['cko-card-token']);

} catch (com\checkout\helpers\ApiHttpClientCustomException $e) {
    echo 'Caught exception Message: ',  $e->getErrorMessage(), "\n";
    echo 'Caught exception Error Code: ',  $e->getErrorCode(), "\n";
    echo 'Caught exception Event id: ',  $e->getEventId(), "\n";
}



}

现在当我发出 POST 请求时,我得到:

OrdersController.php 第 26 行中的 FatalErrorException:main():失败 打开所需的 'vendor\checkout\checkout-php-library\autoload.php' (include_path='.;C:\php\pear')

如何将此库集成到我的 Laravel 项目中?

更新: 在前端我有这个代码:

<script src="https://cdn.checkout.com/js/frames.js"></script>
  <form id="payment-form" method="POST" action="{{url()}}/checkout">
  {!! csrf_field() !!}

    <div class="frames-container">
      <!-- form will be added here -->
    </div>
    <!-- add submit button -->
    <button id="pay-now-button" type="submit" disabled>Pay now</button>
  </form>

    <script>
    var paymentForm = document.getElementById('payment-form');
    var payNowButton = document.getElementById('pay-now-button');

    Frames.init({
      publicKey: 'pk_test_aaaaaaaaa-000-41d9-9999-999999999',
      containerSelector: '.frames-container',
      customerName: 'John Smith',
      billingDetails: {
        addressLine1: '623 Slade Street',
        addressLine2: 'Apartment 8',
        postcode: '31313',
        email: 'asd@asd.asd',
        country: 'US',
        city: 'Hinesville',
        phone: { number: '9125084652' }
      },
      cardValidationChanged: function () {
        // if all fields contain valid information, the Pay now
        // button will be enabled and the form can be submitted
        payNowButton.disabled = !Frames.isCardValid();
      },
      cardSubmitted: function () {
        payNowButton.disabled = true;
        // display loader
      }
    });
    paymentForm.addEventListener('submit', function (event) {
      event.preventDefault();
      Frames.submitCard()
        .then(function (data) {
          Frames.addCardToken(paymentForm, data.cardToken);
          paymentForm.submit();
        })
        .catch(function (err) {
          // catch the error
        });
    });
  </script>

【问题讨论】:

  • 我运行 composer dump-autoload 得到了这个文件夹结构:screencast.com/t/dMz7oubLLn
  • 转到包的根目录并运行 composer install
  • 我运行但没有composer.json文件
  • repo 上有一个 composer.json 文件
  • 抱歉,这是我得到的:screencast.com/t/REqxid6Fku

标签: php laravel package integration-testing checkout


【解决方案1】:

不要包含使用include_once 的包。

  1. 因为当您需要在应用程序的其他部分使用它时,它会变得超级混乱。

  2. 将 API 密钥放在源代码中是个坏主意。该密钥可能会留在您的 Git 中,或者转到您不希望它被发现的地方。另外,假设您的密钥发生了变化,您将不得不去寻找您在源代码中使用密钥的所有部分来替换它。


阅读有关服务提供者https://laravel.com/docs/5.1/providers 的信息,您可以在其中为checkout/checkout-php-api 创建一个“包装器”,您将能够在整个 Laravel 应用程序中使用它:

<?php

use AleksPer\Checkout\CheckoutAPI;

public function checkout(Request $request)
{
   $data = $request->all();


  /**
   * There's no need to inject the API key here,
   * Assuming it is injected when the library is bootstrapped.
   * 
   */
   $apiClient = new CheckoutAPI();
   $charge = $apiClient->chargeService();
}

或者,如果将 checkout-php-api 注册/引导为单例,您可以在整个应用程序中将其引用为 app('checkoutApi');,并让您创建的服务提供者注入所需的参数,这让我想到了下一点:

将您的密钥放在项目的.env 例如

CHECKOUT_SECRET_KEY=sk_***************

当然,要在您的服务提供商中加载该环境变量:env('CHECKOUT_SECRET_KEY')


做一些谷歌搜索:

  • '创建作曲家包'
  • '创建 laravel 包'
  • '创建 api 包装 laravel'

你会找到很多答案。

【讨论】:

  • 你能在这里请教一下如何逐步创建 Laravel 提供程序吗?
  • @AleksPer 一旦你完成了至少 1 个简单的“创建 laravel 包”教程,然后使用 Laravel 关于服务提供者的文档,你就可以轻松地做到这一点。我会尝试编写步骤,但不要等待!祝你好运。
【解决方案2】:

不包括包自动加载器。

将库添加到您的项目后,在库的根目录中包含文件 autoload.php。

include 'checkout-php-api/autoload.php';

【讨论】:

  • 现在,当我尝试查看视图时,我得到:OrdersController.php 第 27 行中的 ErrorException:include(checkout-php-api/autoload.php):无法打开流:没有这样的文件或目录
  • 在库的根目录而不是控制器
  • 但在哪个文件中?我需要在哪里添加这一行?
  • delete package 然后 composer update composer clearcache 然后 composer require checkout/checkout-php-api
  • 好吧,我照你说的做……下一步怎么办?
猜你喜欢
  • 1970-01-01
  • 2015-12-25
  • 1970-01-01
  • 2013-02-17
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 2015-05-23
  • 2015-11-15
相关资源
最近更新 更多