【发布时间】:2021-08-18 17:16:39
【问题描述】:
我为在我的 Laravel 应用中实现 Stripe 苦苦挣扎了很长一段时间,但我遇到了很多问题。我确实以某种方式实现了逻辑,但我不能或者我不知道如何发送当前订单总额来创建 paymentIntent,所有付款都存储在控制器中设置的默认金额 500。我必须提到,在条带成功响应后,当前订单应存储在数据库中,用户运输详细信息在第一种形式中找到,所有订购的产品都存储在会话中。让我向您展示以便更好地理解。
这是视图 (revieworder.blade.php),其中我有 2 个表单,一个包含用户的运输详细信息,以及 Stripe 支付表单,以及会话中的购物车产品列表:
<ul class="list-group mb-3">
<?php $total = 0 ?>
@if(session('cart'))
@foreach(session('cart') as $id => $details)
<?php $total += $details['price'] * $details['quantity'] ?>
<li class="list-group-item d-flex justify-content-between lh-condensed">
<img src="../img/{{ $details['image'] }}" alt="{{ $details['name'] }}" width="60" height="60">
<div>
<h6 class="my-0">{{ $details['name'] }}</h6>
<small class="text-muted">{{ __('Quantity') }}: {{ $details['quantity'] }}</small><br>
<small class="text-muted">{{ __('Unit price') }}: {{ $details['price'] }} RON</small>
</div>
<span class="text-muted">{{ $details['price'] * $details['quantity'] }} RON</span>
</li>
@endforeach
@endif
<li class="list-group-item d-flex justify-content-between">
<span>Total (RON)</span>
<strong id="total">{{ $total.' RON' }}</strong>
</li>
</ul>
<form id="payment-form">
@csrf
<div id="card-element"><!--Stripe.js injects the Card Element--></div>
<button id="submit" class="submit-id">
<div class="spinner hidden" id="spinner"></div>
<span id="button-text">Pay now</span>
</button>
<p id="card-error" role="alert"></p>
<p class="result-message hidden">
</p>
</form>
<script>
//Stripe script
var stripe = Stripe("pk_test_XXX");
// The items the customer wants to buy
var purchase = {
items: [{id: "prod"}] //sessions cart
};
console.log(purchase);
var elements = stripe.elements();
var style = {
base: { //some styling },
invalid: {
fontFamily: 'Arial, sans-serif',
color: "#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 : "";
});
// Disable the button until we have Stripe set up on the page
document.getElementsByClassName("submit-id").disabled = true;
$('#payment-form').submit(function(){
fetch("{{ url(app()->getLocale().'/revieworder') }}", {
method: "POST",
headers: {
"Content-Type": "application/json",
'X-CSRF-TOKEN': "{{ csrf_token() }}"
},
body: JSON.stringify(purchase)
})
.then(function(data) {
$('#payment-form').submit(function(event) {
event.preventDefault();
// Complete payment when the submit button is clicked
payWithCard(stripe, card, data.clientSecret);
});
});
// Calls stripe.confirmCardPayment
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!
// The order should be stored in the database
orderComplete(result.paymentIntent.id);
}
});
};
// 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.getElementsByClassName("submit-id").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.getElementsByClassName("submit-id").disabled = true;
document.querySelector("#spinner").classList.remove("hidden");
document.querySelector("#button-text").classList.add("hidden");
} else {
document.getElementsByClassName("submit-id").disabled = false;
document.querySelector("#spinner").classList.add("hidden");
document.querySelector("#button-text").classList.remove("hidden");
}
};
});
</script>
然后这是处理秘密客户端密钥和 paymentIntent (CheckoutController.php) 的控制器:
public function create(){
\Stripe\Stripe::setApiKey('sk_test_XXX');
header('Content-Type: application/json');
try {
$json_str = file_get_contents('php://input');
$json_obj = json_decode($json_str);
$paymentIntent = \Stripe\PaymentIntent::create([
'amount' => "500",
'currency' => 'ron',
]);
$output = [
'clientSecret' => $paymentIntent->client_secret,
];
echo json_encode($output);
} catch (Error $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}
}
所以,其中一个问题是,每当我要为订单付款时,无论我通过什么信用卡号,它总是成功,这是一个重大错误。第二个,我需要传递当前订单的总额,否则所有订单都会得到总额 500,默认值。我试图将会话购物车项目传递给 fetch,但它没有用。即使我无法将购物车中的所有商品都发送到 Intent,但至少总价格应该对应。
如果我对任何事情都不够清楚,请告诉我。我真的很感激任何建议。谢谢!
【问题讨论】:
-
如果您在测试环境中进行条带化,您有两个信用卡号:第一个始终成功,另一个始终失败。阅读文档以了解这些数字适合您。
-
无论我插入哪一个,总是成功,即使是随机数
-
那么你没有听到来自条纹的响应。您总是会收到他们的回复:在 json 中您有反馈(成功与否)
-
@LelioFaieta fetch 函数只发送金额和货币,没有关于卡的信息,所以作为响应接收到的所有响应体都有状态 OK。据我所知, fetch 负责付款。我按照文档中的所有步骤进行操作,不知道可能出了什么问题
标签: javascript php laravel stripe-payments