STRIPE 结帐中的 php 函数无法识别项目数组
items array is not being recognized by php function in STRIPE checkout
我正在使用此处找到的 Stripe 快速入门代码 https://stripe.com/docs/payments/quickstart
我已将问题缩小到 checkout.js 中的“项目”数组未被识别或未正确构造或 create.php 文件中的任何内容。
我的客户只会以不同的美元金额购买一种类型的商品,因此此功能
function calculateOrderAmount(array $items): int {
// Replace this constant with a calculation of the order's amount
// Calculate the order total on the server to prevent
// people from directly manipulating the amount on the client
return 1400;
}
对我没用,金额就可以进去
$paymentIntent = \Stripe\PaymentIntent::创建([
'amount' => calculateOrderAmount($jsonObj->items)
,
'currency' => 'eur',
'automatic_payment_methods' => [
'enabled' => 正确,
],
]);
作为总数。
我已经尝试将 calculateOrderAmount($jsonObj->items)
替换为 array_values($$items)[0] 以及我能想到的所有其他内容。条纹支持没有帮助。
提前谢谢你
HTML: 价格是从“价值”开始的美分
<select id="item-options"">
<option value="">---- SELECT----</option>
<option value="9000">300 ITEMS - $90.00 USD</option>
<option value="8000">200 ITEMS - $80.00 USD</option>
<option value="5000">100 ITEMS - $50.00 USD</option>
<option value="3000">50 ITEMS - $30.00 USD</option>
<option value="1750">25 ITEMS - $17.50 USD</option>
<option value="800">10 ITEMS - $8.00 USD</option>
<option value="450">5 ITEMS - $4.50 USD</option>
<option value="100">1 ITEMS - $1.00 USD</option>
</select>
完整javascript:
var price = $('#item-options option:selected').val();
// This is your test publishable API key.
const stripe = Stripe("pk_test_xxxxxxxxxxx....");
//顾客要买的商品我把这个改成(金额:价格)
const items = [{ id: "xl-tshirt" }];
let elements;
initialize();
checkStatus();
document
.querySelector("#payment-form")
.addEventListener("submit", handleSubmit);
// Fetches a payment intent and captures the client secret
async function initialize() {
const { clientSecret } = await fetch("/create.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ items }),
}).then((r) => r.json());
elements = stripe.elements({ clientSecret });
const paymentElement = elements.create("payment");
paymentElement.mount("#payment-element");
}
async function handleSubmit(e) {
e.preventDefault();
setLoading(true);
const { error } = await stripe.confirmPayment({
elements,
confirmParams: {
// Make sure to change this to your payment completion page
return_url: "http://localhost:4242/public/checkout.html",
receipt_email: document.getElementById("email").value,
},
});
// This point will only be reached if there is an immediate error when
// confirming the payment. Otherwise, your customer will be redirected to
// your `return_url`. For some payment methods like iDEAL, your customer will
// be redirected to an intermediate site first to authorize the payment, then
// redirected to the `return_url`.
if (error.type === "card_error" || error.type === "validation_error") {
showMessage(error.message);
} else {
showMessage("An unexpected error occured.");
}
setLoading(false);
}
// Fetches the payment intent status after payment submission
async function checkStatus() {
const clientSecret = new URLSearchParams(window.location.search).get(
"payment_intent_client_secret"
);
if (!clientSecret) {
return;
}
const { paymentIntent } = await stripe.retrievePaymentIntent(clientSecret);
switch (paymentIntent.status) {
case "succeeded":
showMessage("Payment succeeded!");
break;
case "processing":
showMessage("Your payment is processing.");
break;
case "requires_payment_method":
showMessage("Your payment was not successful, please try again.");
break;
default:
showMessage("Something went wrong.");
break;
}
}
// ------- UI helpers -------
function showMessage(messageText) {
const messageContainer = document.querySelector("#payment-message");
messageContainer.classList.remove("hidden");
messageContainer.textContent = messageText;
setTimeout(function () {
messageContainer.classList.add("hidden");
messageText.textContent = "";
}, 4000);
}
// Show a spinner on payment submission
function setLoading(isLoading) {
if (isLoading) {
// Disable the button and show a spinner
document.querySelector("#submit").disabled = true;
document.querySelector("#spinner").classList.remove("hidden");
document.querySelector("#button-text").classList.add("hidden");
} else {
document.querySelector("#submit").disabled = false;
document.querySelector("#spinner").classList.add("hidden");
document.querySelector("#button-text").classList.remove("hidden");
}
}
php :
<?php
require 'vendor/autoload.php';
// This is your test secret API key.
\Stripe\Stripe::setApiKey('sk_test_XXXXXX.........');
function calculateOrderAmount(array $items): int {
// Replace this constant with a calculation of the order's amount
// Calculate the order total on the server to prevent
// people from directly manipulating the amount on the client
return 1400;
}
header('Content-Type: application/json');
try {
// retrieve JSON from POST body
$jsonStr = file_get_contents('php://input');
$jsonObj = json_decode($jsonStr);
// Create a PaymentIntent with amount and currency
$paymentIntent = \Stripe\PaymentIntent::create([
'amount' => calculateOrderAmount($jsonObj->items),
'currency' => 'eur',
'automatic_payment_methods' => [
'enabled' => true,
],
]);
$output = [
'clientSecret' => $paymentIntent->client_secret,
];
echo json_encode($output);
} catch (Error $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}
为防止在客户端操纵价格,您应该将产品的 PHP 脚本 ID 传递给您,而不是直接传递价格。通过了解产品 ID,您可以在服务器端 search for prices。这就是 calculateOrderAmount
函数在您的示例代码中的用途。
由于您只打算销售一种类型的产品,其价格将取决于数量,因此您可能希望从客户那里发送多件商品。然后查看 tiered pricing 并使代码与您的定价模型相匹配。
我通过在 php 文件中执行 var_dump($items) 解决了这个问题,console.log 将其显示为 stdClass 对象。使用“$amount = $items[0]->amount;”在“函数 calculateOrderAmount(array $items)”中,现在为我提供了“$paymentIntent = \Stripe\PaymentIntent::create([
'amount' => calculateOrderAmount($jsonObj->items),
谢谢贾斯汀回答我的问题。
我正在使用此处找到的 Stripe 快速入门代码 https://stripe.com/docs/payments/quickstart 我已将问题缩小到 checkout.js 中的“项目”数组未被识别或未正确构造或 create.php 文件中的任何内容。 我的客户只会以不同的美元金额购买一种类型的商品,因此此功能
function calculateOrderAmount(array $items): int {
// Replace this constant with a calculation of the order's amount
// Calculate the order total on the server to prevent
// people from directly manipulating the amount on the client
return 1400;
}
对我没用,金额就可以进去
$paymentIntent = \Stripe\PaymentIntent::创建([
'amount' => calculateOrderAmount($jsonObj->items)
,
'currency' => 'eur',
'automatic_payment_methods' => [
'enabled' => 正确,
],
]);
作为总数。
我已经尝试将 calculateOrderAmount($jsonObj->items)
替换为 array_values($$items)[0] 以及我能想到的所有其他内容。条纹支持没有帮助。
提前谢谢你
HTML: 价格是从“价值”开始的美分
<select id="item-options"">
<option value="">---- SELECT----</option>
<option value="9000">300 ITEMS - $90.00 USD</option>
<option value="8000">200 ITEMS - $80.00 USD</option>
<option value="5000">100 ITEMS - $50.00 USD</option>
<option value="3000">50 ITEMS - $30.00 USD</option>
<option value="1750">25 ITEMS - $17.50 USD</option>
<option value="800">10 ITEMS - $8.00 USD</option>
<option value="450">5 ITEMS - $4.50 USD</option>
<option value="100">1 ITEMS - $1.00 USD</option>
</select>
完整javascript:
var price = $('#item-options option:selected').val();
// This is your test publishable API key.
const stripe = Stripe("pk_test_xxxxxxxxxxx....");
//顾客要买的商品我把这个改成(金额:价格) const items = [{ id: "xl-tshirt" }];
let elements;
initialize();
checkStatus();
document
.querySelector("#payment-form")
.addEventListener("submit", handleSubmit);
// Fetches a payment intent and captures the client secret
async function initialize() {
const { clientSecret } = await fetch("/create.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ items }),
}).then((r) => r.json());
elements = stripe.elements({ clientSecret });
const paymentElement = elements.create("payment");
paymentElement.mount("#payment-element");
}
async function handleSubmit(e) {
e.preventDefault();
setLoading(true);
const { error } = await stripe.confirmPayment({
elements,
confirmParams: {
// Make sure to change this to your payment completion page
return_url: "http://localhost:4242/public/checkout.html",
receipt_email: document.getElementById("email").value,
},
});
// This point will only be reached if there is an immediate error when
// confirming the payment. Otherwise, your customer will be redirected to
// your `return_url`. For some payment methods like iDEAL, your customer will
// be redirected to an intermediate site first to authorize the payment, then
// redirected to the `return_url`.
if (error.type === "card_error" || error.type === "validation_error") {
showMessage(error.message);
} else {
showMessage("An unexpected error occured.");
}
setLoading(false);
}
// Fetches the payment intent status after payment submission
async function checkStatus() {
const clientSecret = new URLSearchParams(window.location.search).get(
"payment_intent_client_secret"
);
if (!clientSecret) {
return;
}
const { paymentIntent } = await stripe.retrievePaymentIntent(clientSecret);
switch (paymentIntent.status) {
case "succeeded":
showMessage("Payment succeeded!");
break;
case "processing":
showMessage("Your payment is processing.");
break;
case "requires_payment_method":
showMessage("Your payment was not successful, please try again.");
break;
default:
showMessage("Something went wrong.");
break;
}
}
// ------- UI helpers -------
function showMessage(messageText) {
const messageContainer = document.querySelector("#payment-message");
messageContainer.classList.remove("hidden");
messageContainer.textContent = messageText;
setTimeout(function () {
messageContainer.classList.add("hidden");
messageText.textContent = "";
}, 4000);
}
// Show a spinner on payment submission
function setLoading(isLoading) {
if (isLoading) {
// Disable the button and show a spinner
document.querySelector("#submit").disabled = true;
document.querySelector("#spinner").classList.remove("hidden");
document.querySelector("#button-text").classList.add("hidden");
} else {
document.querySelector("#submit").disabled = false;
document.querySelector("#spinner").classList.add("hidden");
document.querySelector("#button-text").classList.remove("hidden");
}
}
php :
<?php
require 'vendor/autoload.php';
// This is your test secret API key.
\Stripe\Stripe::setApiKey('sk_test_XXXXXX.........');
function calculateOrderAmount(array $items): int {
// Replace this constant with a calculation of the order's amount
// Calculate the order total on the server to prevent
// people from directly manipulating the amount on the client
return 1400;
}
header('Content-Type: application/json');
try {
// retrieve JSON from POST body
$jsonStr = file_get_contents('php://input');
$jsonObj = json_decode($jsonStr);
// Create a PaymentIntent with amount and currency
$paymentIntent = \Stripe\PaymentIntent::create([
'amount' => calculateOrderAmount($jsonObj->items),
'currency' => 'eur',
'automatic_payment_methods' => [
'enabled' => true,
],
]);
$output = [
'clientSecret' => $paymentIntent->client_secret,
];
echo json_encode($output);
} catch (Error $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}
为防止在客户端操纵价格,您应该将产品的 PHP 脚本 ID 传递给您,而不是直接传递价格。通过了解产品 ID,您可以在服务器端 search for prices。这就是 calculateOrderAmount
函数在您的示例代码中的用途。
由于您只打算销售一种类型的产品,其价格将取决于数量,因此您可能希望从客户那里发送多件商品。然后查看 tiered pricing 并使代码与您的定价模型相匹配。
我通过在 php 文件中执行 var_dump($items) 解决了这个问题,console.log 将其显示为 stdClass 对象。使用“$amount = $items[0]->amount;”在“函数 calculateOrderAmount(array $items)”中,现在为我提供了“$paymentIntent = \Stripe\PaymentIntent::create([ 'amount' => calculateOrderAmount($jsonObj->items),
谢谢贾斯汀回答我的问题。