gpt4 book ai didi

php - Stripe API 收取的金额不正确

转载 作者:行者123 更新时间:2023-12-04 03:01:08 25 4
gpt4 key购买 nike

我正在使用 Laravel 开发一个网站。我需要将 Stripe 支付网关集成到我的网站中。我成功实现了付款流程,但收取的金额不正确。

这是我的 HTML 表单和 JavaScript:

<script src="https://js.stripe.com/v3/"></script>
<div class="container">
<div style="margin-top: 100px;">
{!! Form::open([ 'url' => url('checkout/charge'), 'id' => 'payment-form' ]) !!}
<div style="color: red" id="card-errors">

</div>
<div class="form-row">
<label for="card-element">
Credit or debit card
</label>
<div id="card-element">
<!-- A Stripe Element will be inserted here. -->
</div>

<!-- Used to display form errors. -->
<div id="card-errors" role="alert"></div>
</div>

<button>Submit Payment</button>
{!! Form::close() !!}
</div>
</div>
<script type="text/javascript">
$(function(){
var secret = $('#stripe-secret').val();
var stripe = Stripe(secret);
// Create an instance of Elements.
var elements = stripe.elements();

// Custom styling can be passed to options when creating an Element.
// (Note that this demo uses a wider set of styles than the guide below.)
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'
}
};

// Create an instance of the card Element.
var card = elements.create('card', {style: style});

// Add an instance of the card Element into the `card-element` <div>.
card.mount('#card-element');


//Error live
card.addEventListener('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});



//Create token
// Create a token or display an error when the form is submitted.
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();

stripe.createToken(card).then(function(result) {
if (result.error) {
// Inform the customer that there was an error.
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Send the token to your server.
stripeTokenHandler(result.token);
}
});
});


function stripeTokenHandler(token) {
// Insert the token ID into the form so it gets submitted to the server
var form = document.getElementById('payment-form');
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'stripeToken');
hiddenInput.setAttribute('value', token.id);

form.appendChild(hiddenInput);

// Submit the form
form.submit();
}
})
</script>

如您所见,Stripe 的 token 随后被提交到服务器。为了能够从服务器端使用 Stripe 客户端,我运行此命令安装它。

composer require stripe/stripe-php

这是我的充电 Action

function charge(Request $request)
{
Stripe::setApiKey(env('STRIPE_SECRET'));

$customer = Customer::create(array(
'email' => "my-email@gmail.com",
'source' => $request->stripeToken
));


$charge = Charge::create(array(
'customer' => $customer->id,
'amount' => 70,
'currency' => 'usd'
));

return "Charge successful, you get the course";
}

支付实现工作正常。但问题是,正如您在代码中看到的那样,我传递了 70 的金额。我正在尝试收取 70 美元。货币为美元。但是当我提交表格并查看仪表板时,它只收取 0.7 美元。如果我超过 5 美元,它会收取 0.5。它将实际金额乘以 0.01。

这是截图:

enter image description here

如何修正我的代码以收取正确的金额?

最佳答案

Stripe 确实以美分收费。所以如果你想要 70 美元,你需要做 7000 美分

关于php - Stripe API 收取的金额不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49155774/

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