3

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?

Hassan Saqib
  • 2,597
  • 7
  • 28
  • 51

1 Answers1

4

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.

Bogdan
  • 43,166
  • 12
  • 128
  • 129
  • changed the name of model from TwoCheckout to TwoCheckoutAccount... another error is for line `Twocheckout::privateKey('6CC34113-3588-4B09-8ECB-958C0B3299F3');` *Class 'App\Http\Controllers\Twocheckout' not found* – Hassan Saqib Jan 13 '16 at 06:25
  • That because it's resolving inside the controller namespace, which is `App\Http\Controllers`. You can add an additional `use Twocheckout;` statement to specify that the class should be resolved in the global namespace. – Bogdan Jan 13 '16 at 06:31