【发布时间】:2021-01-17 23:53:49
【问题描述】:
我正在尝试使用 Stripe 的演示自定义表单在我的网站上工作。我没有使用本地服务器。我所做的一切都在我的虚拟主机上。
我已经尝试了预建页面,它工作正常,我正在从这些页面获得我的条带帐户的付款。当我使用他们的自定义付款表单时,我收到了一个错误。
https://stripe.com/docs/payments/integration-builder
我复制了他们在“自定义付款流程”部分提供的代码,但是当我尝试付款时,页面上显示错误 “没有这样的支付意图:'pi_1IAXXXXXXXXX'”。 在条带帐户上,付款显示为未完成并有此消息
The PaymentIntent requires a payment method
Set an existing payment method on the PaymentIntent or have the customer enter a new payment method.
Learn more
我尝试过代码,但我没有经验,所以我不明白。
我尝试了此处提供的解决方案,尝试在条带变量中提供我的 accountID
var stripe = Stripe('pk_test_xxxx', {stripeAccount: 'acct_xxxxx'});
但我仍然遇到同样的错误。 我仔细检查了我的钥匙,以确保它们是相同的,并且那里的一切都很好。
我不确定可能是什么问题。
这些是我拥有的文件,它们与他们网站上提供的演示完全相同,所以我不知道可能出了什么问题。
我下载了 Stripe API 并将其上传到我的虚拟主机。我正在使用
require 'stripe-php/stripe-php/init.php';
代替演示中提供的代码
require 'vendor/autoload.php';
因为我没有使用作曲家。
cpf_create.php(更改了这篇文章的密钥)
<?php
require 'stripe-php/stripe-php/init.php';
// This is your real test secret API key.
\Stripe\Stripe::setApiKey('sk_test_XXXXXXX');
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
// customers from directly manipulating the amount on the client
return 1400;
}
header('Content-Type: application/json');
try {
// retrieve JSON from POST body
$json_str = file_get_contents('php://input');
$json_obj = json_decode($json_str);
$paymentIntent = \Stripe\PaymentIntent::create([
'amount' => calculateOrderAmount($json_obj->items),
'currency' => 'usd',
]);
$output = [
'clientSecret' => $paymentIntent->client_secret,
];
echo json_encode($output);
} catch (Error $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}
cpf_client.js
// A reference to Stripe.js initialized with your real test publishable API key.
var stripe = Stripe("pk_test_XXXXXXXXX");
// The items the customer wants to buy
var purchase = {
items: [{ id: "xl-tshirt" }]
};
// Disable the button until we have Stripe set up on the page
document.querySelector("button").disabled = true;
fetch("/cpf_create.php", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(purchase)
})
.then(function(result) {
return result.json();
})
.then(function(data) {
var elements = stripe.elements();
var style = {
base: {
color: "#32325d",
fontFamily: 'Arial, sans-serif',
fontSmoothing: "antialiased",
fontSize: "16px",
"::placeholder": {
color: "#32325d"
}
},
invalid: {
fontFamily: 'Arial, sans-serif',
color: "#fa755a",
iconColor: "#fa755a"
}
};
var card = elements.create("card", { style: style });
// Stripe injects an iframe into the DOM
card.mount("#card-element");
card.on("change", function (event) {
// Disable the Pay button if there are no card details in the Element
document.querySelector("button").disabled = event.empty;
document.querySelector("#card-error").textContent = event.error ? event.error.message : "";
});
var form = document.getElementById("payment-form");
form.addEventListener("submit", function(event) {
event.preventDefault();
// Complete payment when the submit button is clicked
payWithCard(stripe, card, data.clientSecret);
});
});
// Calls stripe.confirmCardPayment
// If the card requires authentication Stripe shows a pop-up modal to
// prompt the user to enter authentication details without leaving your page.
var payWithCard = function(stripe, card, clientSecret) {
loading(true);
stripe
.confirmCardPayment(clientSecret, {
payment_method: {
card: card
}
})
.then(function(result) {
if (result.error) {
// Show error to your customer
showError(result.error.message);
} else {
// The payment succeeded!
orderComplete(result.paymentIntent.id);
}
});
};
/* ------- UI helpers ------- */
// Shows a success message when the payment is complete
var orderComplete = function(paymentIntentId) {
loading(false);
document
.querySelector(".result-message a")
.setAttribute(
"href",
"https://dashboard.stripe.com/test/payments/" + paymentIntentId
);
document.querySelector(".result-message").classList.remove("hidden");
document.querySelector("button").disabled = true;
};
// Show the customer the error from Stripe if their card fails to charge
var showError = function(errorMsgText) {
loading(false);
var errorMsg = document.querySelector("#card-error");
errorMsg.textContent = errorMsgText;
setTimeout(function() {
errorMsg.textContent = "";
}, 4000);
};
// Show a spinner on payment submission
var loading = function(isLoading) {
if (isLoading) {
// Disable the button and show a spinner
document.querySelector("button").disabled = true;
document.querySelector("#spinner").classList.remove("hidden");
document.querySelector("#button-text").classList.add("hidden");
} else {
document.querySelector("button").disabled = false;
document.querySelector("#spinner").classList.add("hidden");
document.querySelector("#button-text").classList.remove("hidden");
}
};
cpf_checkout.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Accept a card payment</title>
<meta name="description" content="A demo of a card payment on Stripe" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="cpf_global.css" />
<script src="https://js.stripe.com/v3/"></script>
<script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
<script src="/cpf_client.js" defer></script>
</head>
<body>
<!-- Display a payment form -->
<form id="payment-form">
<div id="card-element"><!--Stripe.js injects the Card Element--></div>
<button id="submit">
<div class="spinner hidden" id="spinner"></div>
<span id="button-text">Pay</span>
</button>
<p id="card-error" role="alert"></p>
<p class="result-message hidden">
Payment succeeded, see the result in your
<a href="" target="_blank">Stripe dashboard.</a> Refresh the page to pay again.
</p>
</form>
</body>
</html>
【问题讨论】:
-
在这个问题上运气好吗?我也有同样的问题,这让我很烦
-
很遗憾没有,在他们更新了一些东西以满足我的需要后,我开始使用他们的预制页面。
标签: stripe-payments