首页 > 解决方案 > How to fix Paypal Checkout Order Creation Error

问题描述

I am using Laravel 8 framework for PHP and I am trying to integrate paypal into my the local web. However I am stuck on `create_order_error` even though I have strictly followed some sample snippets provided by paypal I still encounter this pro

References:

Error: SyntaxError: Unexpected token < in JSON at positio…1kLoyti46gxJY-Rl1PH23n49yWhf&currency=PHP:2:79380"
Code:

<script>
    // Render the PayPal button into #paypal-button-container
    paypal.Buttons({
        style: {
            shape:  'pill',
            layout: 'horizontal',
            color:  'blue',
            height: 35
        },
        // Call your server to set up the transaction
        createOrder: function(data, actions) {
            return fetch('/billing/createOrder', {
                method: 'post',
                headers: {
                    'content-type': 'application/json'
                }
            }).then(function(res) {
                return res.json();
            }).then(function(orderData) {
                return orderData.id;
            });
        },

        
    }).render('#paypal-button-container');
</script>

Note: I have removed the onApprove function since I'm stuck on createOrder

Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;
use PayPalHttp\HttpException;

class PaypalCheckoutController extends Controller
{

    private $environment;
    private $client;

    public function __construct()
    {
        $this->environment = new SandboxEnvironment(config('paypal.client_id'), config('paypal.secret'));
        $this->client = new PayPalHttpClient($this->environment);
    }

    public function index(Request $request)
    {
        return view('payment.checkout');
    }

    public function createOrder(Request $request) 
    {
        
        $order = new OrdersCreateRequest();
        $order->prefer('return=representation');

        $order->body = array(
            'intent' => 'CAPTURE',
            'application_context' =>
                array(
                    'return_url' => 'http://dummyweb.test/billing/checkout',
                    'cancel_url' => 'http://dummyweb.test/billing/checkout'
                ),
            'purchase_units' =>
                array(
                    0 =>
                        array(
                            'amount' =>
                                array(
                                    'currency_code' => 'PHP',
                                    'value' => '420.00'
                                )
                        )
                )
        );
        try {
            $result = $this->client->execute($order);
            return $result;
        }
        catch(HttpException $ex) {
            print_r($ex->getMessage());
        }
    }
}

标签: laravelpaypal

解决方案


SyntaxError: Unexpected token < in JSON at position...</p>

当浏览器调用 /billing/createOrder 时,您返回的不是 JSON。您只能返回 JSON。

使用浏览器开发工具中的网络选项卡,或在新选项卡中加载路径,以检查您实际返回的响应正文。

它显然不是 JSON。根据该错误消息,它将以一些 HTML(<字符)开头

只返回 JSON。您需要能够将整个响应正文复制到 JSON 验证器中并使其正常。


推荐阅读