Welcome Guest, Not a member yet? Register   Sign In
Controller can't return json data
#1
Question 
(This post was last modified: 10-13-2020, 02:03 PM by shoXyRafael.)

Hello, I'm currently working with the PHP framework Codeigniter 4.0.4 and trying to add the PayPal Checkout Intergration with Smart Payment Buttons.

Console error:

Code:
Uncaught SyntaxError: Unexpected token < in JSON at position 0

Error: Unexpected token < in JSON at position 0
    at $t.error (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:59754)
    at Object.<anonymous> (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:67521)
    at JSON.parse (<anonymous>)
    at o (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:67380)
    at cr (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:67533)
    at Cr.u.on (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:72204)
    at Cr (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:72341)
    at https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:78460
    at Function.n.try (https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:14069)
    at https://www.paypal.com/sdk/js?client-id=Af6lu4xavdi1_e_hEFLWQxUj48hq0bndx7o3RGgwNWuChHmenioXFLMnTOKt912F6zmftF1Siv9WsfCp&disable-funding=credit,card:2:78257

Serverside:
PHP Code:
$clientId getenv('paypal.CLIENT_ID');
$clientSecret getenv('paypal.CLIENT_SECRET');

$environment = new SandboxEnvironment($clientId$clientSecret);
$client = new PayPalHttpClient($environment);

$request = new OrdersCreateRequest();
$request->prefer('return=representation');
$request->body = [
    "intent" => "CAPTURE",
    "purchase_units" => [[
        'reference_id' => '123',
        "amount" => [
            "value" => 10,
            "currency_code" => "USD"
        ]
    ]],
    "application_context" => [
        "cancel_url" => base_url() . "/checkout",
        "return_url" => base_url() . "/checkout"
    ]
];

try {
    $response $client->execute($request);
    return json_encode($response);
} catch (
HttpException $ex) {
    echo $ex->statusCode;
    print_r($ex->getMessage());



Clientside:
PHP Code:
<script type="text/javascript">
    paypal.Buttons({
        env'sandbox',

        style: {
            layout'vertical',
            size'responsive',
            shape'pill',
            color'blue',
            label'pay'
        },

        createOrder: function() {
            return fetch('/checkout/paypal', {
                method'post',
                headers: {
                    'content-type''application/json'
                }
            }).then(function(response) {
                return response.json();
            }).then(function(resJson) {
                return resJson.result.id;
            });
        }
    }).render('#paypal-button-container');
</
script

$response outputs the whole page I guess, how I can only return the $response json?
Reply
#2

Try to use this (according to documentation):

PHP Code:
return $this->response->setJSON($data);  // $data - is array 
Reply
#3

i wanted to implement https://demo.paypal.com/au/demo/download...2&lang=PHP
its is simple PHP solution(it working fine standalone)
i've load constants in my constants file and past the index.php code in my view file and also make a new controller.
when i make the
Error when i click the javascript paypal button
Uncaught SyntaxError: Unexpected token < in JSON at position 0

this error come from this code

return fetch(
'<?=URL['services']['orderCreate']?>',
{
method: 'POST',
body: formData
}
).then(function(response) {
return response.json(); ## PRBLEM with this line JSON return issue
// console.warn(xhr.response.json())
}).then(function(resJson) {
console.log('Order ID: '+ resJson.data.id);
return resJson.data.id;
});
},

i've mention above there is an issue in return response.json();
above plase tell how to get he response in json format.
in CI. i'm using CI 3.x.
Reply
#4

maybe you should upgrade to the newest version of CodeIgniter 4.1.1 which has bug fixes.

Also try sending setting a json header to tell it what your sending.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB