Stripe payments Opencart中带预构建的托管签出,出现错误(位置0处JSON中的意外标记<;)
最近,我一直在测试条带预构建的托管签出。到目前为止,通过遵循官方文档,它在普通PHP中工作没有任何问题Stripe payments Opencart中带预构建的托管签出,出现错误(位置0处JSON中的意外标记<;),stripe-payments,opencart,opencart2.3,Stripe Payments,Opencart,Opencart2.3,最近,我一直在测试条带预构建的托管签出。到目前为止,通过遵循官方文档,它在普通PHP中工作没有任何问题 https://stripe.com/docs/checkout/integration-builder 然而,当我尝试用Opencart 2.3.0.2测试它时,我得到了SyntaxError:Unexpected token
https://stripe.com/docs/checkout/integration-builder
然而,当我尝试用Opencart 2.3.0.2测试它时,我得到了SyntaxError:Unexpected token<在JSON中的位置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>
以上是错误的屏幕截图。
单击按钮后,它显示错误,并且没有重定向到条带签出页面。。。有人能帮忙吗?请给我一些建议。谢谢 您是否确认(通过日志记录)正在从服务器后端获取会话ID?根据您看到的错误,您似乎无法在服务器端创建签出会话,因此当您调用
redirectToCheckout
客户端时,您没有有效的session.ID
。错误是因为“fetch”(“catalog/controller/extension/payment/stripe.php”)@gosulove您是如何修复的?
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]);
}
}