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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据