doulianglou0898 2016-01-13 06:01
浏览 38
已采纳

如何在Laravel中添加普通的PHP库?

I am using 2checkout API to integrate payments in my application. I am following their API Tutorial. I made slight modifications to their approach. Below is the unmodified front-end code by 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">

I changed the line <form id="myCCForm" action="payment.php" method="post">to <form id="myCCForm" action="{{route('postPayment)}}" method="post"> and also added CSRF token field in the above form.

This thing is working fine.

The 2nd Part is server side script that I want to handle in Laravel application. In tutorial this is written in payment.php. The script is working fine in payment.php but I am trying to write it in controller as follows:

<?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();
        }
    }


}

The problem is occurring in following lines.

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

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

ERROR

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

What am I doing wrong here?

  • 写回答

1条回答 默认 最新

  • doujin8673 2016-01-13 06:20
    关注

    The problem is that you have a model named Twocheckout that you are importing at the top of your file with:

    use App\TwoCheckout;
    

    This means that all uses of Twocheckout in that controller file will refer to the Eloquent model, which in turn means you can't access the class with the same name coming from the 2checkout library. To fix that you can use an alias for the model class name like so:

    use App\TwoCheckout as TwoCheckoutModel;
    

    And where you need the model class just use that:

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

    That way the class name Twocheckout will refer to the correct library class.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab