Stripe Prebuilt hosted checkout with Error (Unexpected token < in JSON at position 0 ) in Opencart

664 Views Asked by At

Recently, I have been testing Stripe Prebuilt Hosted Checkout. So far, by following Official docs , it works without any problem in normal PHP.

https://stripe.com/docs/checkout/integration-builder

However, when i try to test it with Opencart 2.3.0.2 , I got the SyntaxError: Unexpected token < in JSON at position 0 .

test.php

<button id="checkout-button">Checkout</button>

<script type="text/javascript">
// Create an instance of the Stripe object with your publishable API key
var stripe = Stripe("pk_test_1111111");
var checkoutButton = document.getElementById("checkout-button");

checkoutButton.addEventListener("click", function () {
  fetch("catalog/controller/extension/payment/stripe.php", {
    method: "POST",
  })
    .then(function (response) {
      return response.json();
    })
    .then(function (session) {
      return stripe.redirectToCheckout({ sessionId: session.id });
    })
    .then(function (result) {
      // If redirectToCheckout fails due to a browser or network
      // error, you should display the localized error message to your
      // customer using error.message.
      if (result.error) {
        alert(result.error.message);
      }
    })
    .catch(function (error) {
      console.error("Error:", error);
    });
});
</script>

Stripe.php

class ControllerExtensionPaymentStripe extends Controller {
    
    
    public function index() {

        require(DIR_SYSTEM . "library/stripe/init.php");
        
        \Stripe\Stripe::setApiKey('sk_test_2222222');
        
        $response = array( 
            'status' => 0, 
            'error' => array( 
                'message' => 'Invalid Request!'    
            ) 
        ); 
        
        if ($this->request->server['REQUEST_METHOD'] == 'POST') { 
            $input = file_get_contents('php://input'); 
            $request = json_decode($input);     
        } 
         
        if (json_last_error() !== JSON_ERROR_NONE) { 
            http_response_code(400); 
            echo json_encode($response); 
            exit; 
        } 

        //header('Content-Type: application/json');

        $YOUR_DOMAIN = 'http://localhost/oc_stripe';
        $checkout_session = \Stripe\Checkout\Session::create([
          'payment_method_types' => ['card'],
          'line_items' => [[
            'price_data' => [
              'currency' => 'usd',
              'unit_amount' => 2000,
              'product_data' => [
                'name' => 'Stubborn Attachments',
                'images' => ["https://i.imgur.com/EHyR2nP.png"],
              ],
            ],
            'quantity' => 1,
          ]],
          'mode' => 'payment',
          'success_url' => $YOUR_DOMAIN . '/success.php?session_id={CHECKOUT_SESSION_ID}',
          'cancel_url' => $YOUR_DOMAIN . '/cancel.php',
        ]);

        echo json_encode(['id' => $checkout_session->id]);
        
    }
    
}

enter image description here

Above is the screenshot for the error.

After clicking the Button, it shows the Error and didn't redirect to stripe checkout page... Anyone can help? Please advice. Thx!

0

There are 0 best solutions below