gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:58:20 25 4
gpt4 key购买 nike

我正在使用 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 token 字段。

这东西工作正常。

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

<?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;

这意味着该 Controller 文件中对 Twocheckout 的所有使用都将引用 Eloquent 模型,这反过来意味着您无法访问来自 2checkout library 的同名类。 .要解决这个问题,您可以像这样为模型类名称使用别名:

use App\TwoCheckout as TwoCheckoutModel;

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

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

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

关于php - 如何将纯 PHP 库添加到 Laravel 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34759449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com