如何将纯 PHP 库添加到 Laravel 中?

How to add plain PHP library into Laravel?

我正在使用 2checkout API 将付款集成到我的应用程序中。我正在关注他们的 API Tutorial。我对他们的方法做了细微的修改。下面是教程未修改的前端代码

<form id="myCCForm" action="payment.php" method="post">
<input id="token" name="token" type="hidden" value="">
<div>
    <label>
        <span>Card Number</span>
    </label>
    <input id="ccNo" type="text" size="20" value="" autocomplete="off" required />
</div>
<div>
    <label>
        <span>Expiration Date (MM/YYYY)</span>
    </label>
    <input type="text" size="2" id="expMonth" required />
    <span> / </span>
    <input type="text" size="2" id="expYear" required />
</div>
<div>
    <label>
        <span>CVC</span>
    </label>
    <input id="cvv" size="4" type="text" value="" autocomplete="off" required />
</div>
<input type="submit" value="Submit Payment">

我将行 <form id="myCCForm" action="payment.php" method="post"> 更改为 <form id="myCCForm" action="{{route('postPayment)}}" method="post"> 并且还在上面的表单中添加了 CSRF 令牌字段。

这东西工作正常。

第二部分是我想在 Laravel 应用程序中处理的服务器端脚本。在教程中,这是用 payment.php 编写的。该脚本在 payment.php 中运行良好,但我正尝试将其写入控制器,如下所示:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\TwoCheckout;



class PaymentsController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $twoCheckOut = TwoCheckout::find(1);
        return view('payment')->with('twoCheckOut',$twoCheckOut);
    }

    public function process()
    {
       require_once("2checkout-php/lib/Twocheckout.php");

        Twocheckout::privateKey('6CC34113-3588-4B09-8ECB-958C0B3299F3');
        Twocheckout::sellerId('901303383');
        Twocheckout::sandbox(true);


        try {
            $charge = Twocheckout_Charge::auth(array(
                "sellerId" => "901303383",
                "merchantOrderId" => "123",
                "token" => 'Y2U2OTdlZjMtOGQzMi00MDdkLWJjNGQtMGJhN2IyOTdlN2Ni',
                "currency" => 'USD',
                "total" => '10.00',
                "billingAddr" => array(
                    "name" => 'Testing Tester',
                    "addrLine1" => '123 Test St',
                    "city" => 'Columbus',
                    "state" => 'OH',
                    "zipCode" => '43123',
                    "country" => 'USA',
                    "email" => 'testingtester@2co.com',
                    "phoneNumber" => '555-555-5555'
                ),
                "shippingAddr" => array(
                    "name" => 'Testing Tester',
                    "addrLine1" => '123 Test St',
                    "city" => 'Columbus',
                    "state" => 'OH',
                    "zipCode" => '43123',
                    "country" => 'USA',
                    "email" => 'testingtester@2co.com',
                    "phoneNumber" => '555-555-5555'
                )
            ), 'array');
            if ($charge['response']['responseCode'] == 'APPROVED') {
                echo "Thanks for your Order!";
            }
        } catch (Twocheckout_Error $e) {
            $e->getMessage();
        }
    }


}

问题出现在以下行中。

require_once("2checkout-php/lib/Twocheckout.php");

            Twocheckout::privateKey('6CC34113-3588-4B09-8ECB-958C0B3299F3');
            Twocheckout::sellerId('901303383');
            Twocheckout::sandbox(true);

错误

BadMethodCallException in Builder.php line 2099: Call to undefined method Illuminate\Database\Query\Builder::privateKey()

我做错了什么?

问题是您在文件顶部导入了一个名为 Twocheckout 的模型:

use App\TwoCheckout;

这意味着该控制器文件中对 Twocheckout 的所有使用都将引用 Eloquent 模型,这反过来意味着您无法访问具有相同名称的 class来自 2checkout library。要解决这个问题,您可以为模型 class 名称使用别名,如下所示:

use App\TwoCheckout as TwoCheckoutModel;

在需要模型的地方 class 只需使用它:

public function index()
{
    $twoCheckOut = TwoCheckoutModel::find(1);
    return view('payment')->with('twoCheckOut', $twoCheckOut);
}

这样 class 名称 Twocheckout 将引用正确的库 class。