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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀