gpt4 book ai didi

javascript - 无法在 stripe PaymentIntent 中使用 fetch API 获取 Payment_method_id

转载 作者:行者123 更新时间:2023-11-29 10:26:32 26 4
gpt4 key购买 nike

我正在使用 PaymentIntent API 通过 stripe-php SDK 和 Stripe.js V3 集成 Stripe 支付。遵循本指南 https://stripe.com/docs/payments/payment-intents/migration#saving-cards-checkout .我在我的 Stripe Dashboard 中使用不需要 3d 安全的测试卡获得了成功的付款。但是 Stripe 的新 SCA 3d 安全弹出窗口(根据他们的文档。)没有弹出,这导致使用 3dsecure ENABLED 卡完成的付款到 stripe 仪表板中的“未完成付款”选项卡。

我已经多次彻底检查代码并进行了测试。我注意到我的代码跳过(有时)或在客户端代码的“获取部分”中抛出错误“JSON 输入的意外结束”。这导致 3d 安全卡支付不完整。JavaScript 获取功能未从指定文件 (url) 获取“payment_method_id”。

我的 Payment.js 文件:

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();
}
}

我的 Index.php 文件

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()
]);
}


}
?>

可以看出,我的 stripeTokenHandler 将 payment_method.id 附加到 HTML 表单中,并且该过程继续进行。但是 JS 代码的“fetch”部分应该获取 payment_method_id 以生成“Response”并在支付状态为“requires_action”时继续执行“next_action”。

最佳答案

所以,为了实现我想要的,我所做的是

- 删除了 stripeTokenHandler()

因为我在之前的收费 API 集成中使用过它,并且认为它可以与新的 PaymentIntent 一起使用。而且我猜很多人误解或误导了 stripe 在它的文档中购买了一堆不同的方法。我在互联网上看到很多人提示 stripe 的“管理不善”的文档让他们感到困惑。

- 学习了 Fetch Api。

作为一个新手,我不知道它是干什么用的。

- 从我的 php 代码中删除了 isset post submit

原因:payment.js 无法使用 Fetch Api 将 paymentMethod.id 发布到服务器并从服务器获取响应主体以进一步处理代码。

我必须说,Stripe 需要改进其关于这个 SCA 就绪 PaymentIntent 的文档。

关于javascript - 无法在 stripe PaymentIntent 中使用 fetch API 获取 Payment_method_id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57810332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com