Javascript 无法使用stripe PaymentIntent中的fetch API获取付款方法id
我正在使用PaymentIntent API使用Stripe php SDK和Stripe.js V3集成条带支付。 遵循本指南。我在我的Stripe Dashboard中获得了成功付款所用的测试卡,这些测试卡不需要3d安全。但是Stripe的新SCA 3d安全弹出窗口(根据他们的文档)没有弹出,这会导致使用支持3dsecure的卡完成的付款进入Stripe仪表板中的“未完成付款”选项卡 我已经对代码进行了多次彻底的检查和测试。我注意到我的代码在客户端代码的“Fetch Part”中跳过(有时)或抛出错误“意外的JSON输入结束”。。这导致3d安全卡支付不完整。JavaScript获取函数未从指定文件(url)获取“支付方法id” My Payment.js文件:Javascript 无法使用stripe PaymentIntent中的fetch API获取付款方法id,javascript,php,stripe-payments,Javascript,Php,Stripe Payments,我正在使用PaymentIntent API使用Stripe php SDK和Stripe.js V3集成条带支付。 遵循本指南。我在我的Stripe Dashboard中获得了成功付款所用的测试卡,这些测试卡不需要3d安全。但是Stripe的新SCA 3d安全弹出窗口(根据他们的文档)没有弹出,这会导致使用支持3dsecure的卡完成的付款进入Stripe仪表板中的“未完成付款”选项卡 我已经对代码进行了多次彻底的检查和测试。我注意到我的代码在客户端代码的“Fetch Part”中跳过(有时)
var elements = stripe.elements();
var style = {
base: {
color: '#32325d',
lineHeight: '18px',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: 'antialiased',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
}
},
invalid: {
color: '#fa755a',
iconColor: '#fa755a'
}
};
var cardNumber = elements.create('cardNumber', {
style: style
});
cardNumber.mount('#cardNumber');
var cardExpiry = elements.create('cardExpiry', {
style: style
});
cardExpiry.mount('#cardExpiry');
var cardCvc = elements.create('cardCvc', {
style: style
});
cardCvc.mount('#cardCVC');
var cardholderName = $('#custName').val();
var amount = $('#amount').val();
$(document).ready(function () {
$("#paymentForm").submit(function (event) {
//event.preventDefault();
stripe.createPaymentMethod('card', cardNumber, {
billing_details: {name: cardholderName.value}
}).then(function (result) {
console.log(result);
if (result.error) {
var errorElement = document.getElementById('card-error');
errorElement.textContent = result.error.massage;
} else {
stripeTokenHandler(result);
fetch('example.com/stripe/index.php', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
payment_method_id: result.paymentMethod.id
})
}).then(function (result) {
// Handle server response (see Step 3)
result.json().then(function (result) {
handleServerResponse(result);
})
});
}
});
//return false;
});
function stripeTokenHandler(result) {
var payForm = $("#paymentForm");
var paymentMethodID = result.paymentMethod.id;
//set the token into the form hidden input to make payment
payForm.append("<input type='hidden' name='payment_method_id' value='" + paymentMethodID + "' />");
// payForm.submit();
payForm.submit();
}
}
如图所示,我的stripeTokenHandler正在将payment_method.id附加到HTML表单中,过程继续进行。但是JS代码的“fetch”部分应该获得payment\u method\u id,以生成“Response”,并在支付状态为“requires\u action”时继续执行“next\u action”。因此,为了实现我想要的,我所做的是 -已删除stripeTokenHandler() 因为我在以前的charges API集成中使用了它,并且认为它将与新的PaymentIntent一起工作。我猜很多人误解或误导了stripe在文档中使用的各种方法。我在网上看到很多问题,人们抱怨stripe的“管理不善”文档让他们感到困惑 -学习获取Api。 作为一个新手,我不知道这是为了什么 -从我的php代码中删除了ISSETPOST提交 原因:payment.js无法使用Fetch Api将paymentMethod.id发布到服务器,并从服务器获取响应正文以继续代码
我必须说,Stripe需要改进关于这个SCA就绪PaymentIntent的文档。当您看到这个错误时,JSON的内容是什么?堆栈跟踪会有所帮助,包括代码抛出错误的行。@taintedzodiac inside stripe.createPaymentmethod()我获得paymentMethod,并使用stripeTokenHandler函数将其发送到html表单。问题就在代码下面。“Fetch”应该从process.php文件中获取payment\u method\u id。但是它给了我一个承诺错误。然后()说“JSON输入的意外结束”。。我认为这意味着它没有得到任何回应。屏幕截图:好的,当前端得到JSON的意外端时,PHP后端返回的实际响应是什么?您的服务器中是否有显示任何错误的日志等?我换了一种不同的方法,并对当前的集成进行了一些更改。。这解决了所有问题,我在3d安全卡上成功付款。Stripe的3dsecure身份验证弹出窗口也在发挥作用。
header('Content-Type: application/json');
if(isset($_POST['submit'])){
//include Stripe PHP library
require_once('stripe-php/init.php');
//set Stripe Secret Key
\Stripe\Stripe::setApiKey('sk_test_key');
//add customer to stripe
$customer = \Stripe\Customer::create(array(
'email' => $custEmail,
));
function generatePaymentResponse($intent) {
if ($intent->status == 'requires_action' &&
$intent->next_action->type == 'use_stripe_sdk') {
# Tell the client to handle the action
echo json_encode([
'requires_action' => true,
'payment_intent_client_secret' => $intent->client_secret
]);
} else if ($intent->status == 'succeeded') {
# The payment didn’t need any additional actions and completed!
# Handle post-payment fulfillment
echo json_encode([
'success' => true
]);
} else {
# Invalid status
http_response_code(500);
echo json_encode(['error' => 'Invalid PaymentIntent status']);
}
}
# retrieve json from POST body
$json_str = file_get_contents('php://input');
$json1 = json_encode($_POST);
$json_obj = json_decode($json1);
$intent = null;
try {
if (isset($json_obj->payment_method_id)) {
$intent = \Stripe\PaymentIntent::create([
'payment_method' => $json_obj->payment_method_id,
'customer' => $customer->id,
'amount' => 1099,
'currency' => 'gbp',
'confirmation_method' => 'manual',
'confirm' => true,
]);
}
generatePaymentResponse($intent);
}catch (\Stripe\Error\Base $e) {
# Display error on client
echo json_encode([
'error' => $e->getMessage()
]);
}
}
?>