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

Peter Wajcowicz's avatar

cURL works but laravel Http facade dont

My question is related to this documentation. I wolud like to create a new order: https://developers.payu.com/europe/pl/api/#tag/Payment-Methods/operation/retrieve-payment-methods

I have curl in php that works:

$ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'https://secure.snd.payu.com/api/v2_1/orders');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);

    curl_setopt($ch, CURLOPT_POST, TRUE);

    $items = [
        [
            "name" => "XXX1!",
            "unitPrice" => "1234",
            "quantity" => 1,
        ]
    ];

    $payload = "{
            \"customerIp\": \"" . request()->ip() . "\",
            \"merchantPosId\": " . env('PAYU_POSID') . ",
            \"description\": \"test\",
            \"currencyCode\": \"PLN\",
            \"totalAmount\": \"1234\",
            \"extOrderId\": \"" . Carbon::now()->timestamp . "\",
            \"products\": [";

    foreach ($items as $item) {
        $payload .= "{
                    \"name\": \"" . $item['name'] . "\",
                    \"unitPrice\": \"" . $item['unitPrice'] . "\",
                    \"quantity\": \"" . $item['quantity'] . "\"
                    },";
    }
    $payload = trim($payload, ',');
    $payload .= "]}";

    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        "Content-Type: application/json",
        "Authorization: Bearer " . $response['access_token']
    ));

    $response2 = curl_exec($ch);
    curl_close($ch);

    $decoded = json_decode($response2);

but when I convert it to laravel facade i got null in response:

$order = Http::withHeaders([
        "Accept" => "application/json",
        "Authorization" => "Bearer " . $response["access_token"]
    ])->post("https://secure.snd.payu.com/api/v2_1/orders", [
        "customerIp" => request()->ip(),
        "merchantPosId" => env("PAYU_POSID"),
        "description" => "XXX!",
        "currencyCode" => "PLN",
        "totalAmount" => "1234",
        "extOrderId" => Carbon::now()->timestamp,
        "buyer" => [
            "email" => "[email protected]"
        ],
        "products" => [
            [
                "name" => "XXX1!",
                "unitPrice" => "1234",
                "quantity" => 1,
            ]
        ],

    ]);

    dd($order->json());

I dont know what is wrong :(

0 likes
1 reply
Braunson's avatar

Have you tried dumping out the status code of the Http request? $response->status()? I'd also suggest dumping the headers and see if you can find out more information.

I also noticed your sending Content-Type in CURL but in Http your sending Accept.

Please or to participate in this conversation.