Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

elo's avatar
Level 3

Consuming payment gateway API in Laravel controller

Hi guys, I am doing a payment gateway integration for a Laravel 5.8 API project. So below is the logic I have come up with

  1. User confirms they want to pay for a product
  2. They hit this route Route::post('products/{product}/order', 'OrderController@store');
  3. In the store method, I will consume the payment gateway initiate transaction API
  4. Check the status of the transaction in the same controller method
  5. Update the order table based on response from the gateway

Here's a summary version of OrderController

<?php
...
use GuzzleHttp\Client;


class OrderController extends Controller
{
    public function store(Request $request, Product $product)
    {
        // if status is loaded, allow order
        if ($product->status == 'loaded') {

            // redirect user to payment gateway
            $payment = new Client();

            $uri = 'https://sandbox.gateway.com/api/v1/merchant/transactions/init-transaction';

            $paylaod = ["amount" => 100"];

            return $payment->post($uri, $paylaod);

            // do something with returned object
        }
    }
}

When I run my code, I get 500 internal server error

GuzzleHttp\Exception\ClientException: Client error: POST https://sandbox.gateway.com/api/v1/merchant/transactions/init-transaction resulted in a 400 Bad Request

Why am I getting this error?

0 likes
1 reply
audunru's avatar

That error is from sandbox.gateway.com. You should probably dump the full response from the gateway to see if they included an error message.

$response = $payment->post($uri, $paylaod);

$contents = $response->getBody()->getContents();

dump($contents)

Run php artisan dump before hitting the endpoint to see the contents in your console.

Please or to participate in this conversation.