dane5890's avatar

Integrating Shippo With Laravel 8

Hi all, I'm building an ecommerce app with Laravel 8. Everything is working fine, except for the shipping rates. I've installed Shippo, but I keep getting errors when I try to input some code in my checkout controller. Does anybody have any experience with Shippo?

0 likes
4 replies
martinbean's avatar

@dane5890 No one can help unless you share what errors you’re getting, or some example code.

dane5890's avatar

@martinbean

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Models\ShipDivision; use App\Models\ShipDistrict; use App\Models\ShipState; use App\Models\Product; use App\Services\Shipping_New; use App\Models\User; use Auth; use\App\Http\Controllers\Frontend\CartController; use Gloudemans\Shoppingcart\Facades\Cart; use Carbon\Carbon;

class CheckoutController extends Controller {

/**

  • @var Shipping */ private $shipping;

/**

  • @var UserRepository */ private $user;

/**

  • CheckoutController constructor.
  • @param UserRepository $user
  • @param Shipping $shipping */

public function DistrictGetAjax($division_id) {

$ship = ShipDistrict::where('division_id',$division_id)->orderBy('district_name', 'ASC')->get();

return json_encode($ship);

}

public function StateGetAjax($district_id) {

$ship = ShipState::where('district_id',$district_id)->orderBy('state_name', 'ASC')->get();

return json_encode($ship);

}

public function CheckoutStore(Request $request) {

$data = array();
$data['name'] = $request->name;
$data['shipping_last_name'] = $request->shipping_last_name;
$data['email'] = $request->email;
$data['phone'] = $request->phone;
$data['zip'] = $request->zip;
$data['state'] = $request->state;
$data['street1'] = $request->street1;
$data['company'] = $request->company;
$data['city'] = $request->city;
$data['country'] = $request->country;
$data['notes'] = $request->notes;
$data['state_id'] = $request->state_id;
$data['district_id'] = $request->district_id;
$data['division_id'] = $request->division_id;
$cartTotal = Cart::total();


if ($request->payment_method == 'stripe') {

  return view('shop.payment.stripe', compact('data','cartTotal'));


}

  elseif ($request->payment_method == 'paypal') {

    $carts = Cart::content();
    $cartQty = Cart::count();
    $cartTotal = Cart::total();

    return view('shop.payment.paypal', compact('data', 'cartTotal'));

  }

  else {

    return 'cash';

  }

}

public function RatesIndex() {

  // Grabbed the logged in user.
  $user = auth()->user();

  // Here we are faking a product and this would typically
  // come from the cart and then look it up from the database
  $product = new Product([
    'weight' => $request->weight,
    'length' => $request->length,
    'height' => $request->height,
    'width' => $request->width,
    'distance_unit' => $request->distance_unit,
    'mass_unit' => $request->mass_unit,
  ]);

  // Now that we have all the data lets try to
  // get a list of shipping providers and pricing
  $rates = $this->shipping->rates($user, $product);

  // The rates is a complete object but for this we
  // only need the rates_list items and will pass that.
  return view('shop.checkout.index', compact('rates'));


}

}

The errors I receive are either too few arguemetns ($rates) or undefined variable ($rates)

martinbean's avatar

The errors I receive are either too few arguemetns ($rates) or undefined variable ($rates)

@dane5890 Well, without reading through that massive chunk of (poorly formatted) code, I’m going to take a guess that you’re passing two few arguments, and trying to use a variable that you’ve not actually defined.

Error messages seldom lie. They’re there to tell you what’s gone wrong and a hint as to how to fix the issue. So work backwards. If you’re getting an error saying two few arguments, look at the method it’s complaining about, look at the arguments you are passing versus the arguments you should be passing.

Please or to participate in this conversation.