我在尝试为客户端充电时遇到Stripe系统问题。 交易同时进行两次。
我不确定Stripe脚本或我的表单是否存在问题。
以下是证据: enter image description here
以下是结帐时的表格:
<form action="charge.php" method="post" id="payment-form">
<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"></div>
</div>
<input type="text" class="hidden" name="amount" value="<?php echo $amount;?>">
<input type="text" class="hidden" name="item" value="<?php echo $item;?>">
<input type="submit" name="payment-form-submit" style="margin-left: -5px" class="prm-btn2" value="Submit Payment">
</form>
这是charge.php:
<?php
require_once('./vendor/autoload.php');
$amount = $_POST['amount']*100;
$item = $_POST['item'];
include($_SERVER['DOCUMENT_ROOT'].'/functions.php');
include($_SERVER['DOCUMENT_ROOT'].'/assets/includes/db.inc.php');
\Stripe\Stripe::setApiKey("sk_test_4gS2utD7Epld3B6bsZWhzecL");
// Get the token from the JS script
$token = $_POST['stripeToken'];
if(strlen(getStripeId($_COOKIE["username"]))<2){
// Create a Customer
$customer = \Stripe\Customer::create(array(
"email" => $_COOKIE['username'],
"source" => $token,
));
$customer_id = $customer->id;
// Insert the Stripe ID into the DB for this user
$ReadSql = "UPDATE users SET stripe_id = '".$customer_id."' WHERE username='".$_COOKIE['username']."'";
$res = mysqli_query($mysqli_conn, $ReadSql);
}
else {
$customer_id = getStripeId($_COOKIE["username"]);
}
//Charge him
$charge = \Stripe\Charge::create(array(
"amount" => $amount,
"currency" => "usd",
"customer" => $customer_id,
"description" => $item
));
这是charge.js
// Stripe API Key
var stripe = Stripe('pk_test_CoWQ9BDSFgGEPpMXNW1wmKSo');
var elements = stripe.elements();
// Custom Styling
var style = {
base: {
color: '#32325d',
lineHeight: '24px',
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');
// Handle real-time validation errors from the card Element.
card.addEventListener('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
// Handle form submission
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
stripe.createToken(card).then(function(result) {
if (result.error) {
// Inform the user if there was an error
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
stripeTokenHandler(result.token);
}
});
});
// Send Stripe Token to Server
function stripeTokenHandler(token) {
// Insert the token ID into the form so it gets submitted to the server
var form = document.getElementById('payment-form');
// Add Stripe Token to hidden input
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'stripeToken');
hiddenInput.setAttribute('value', token.id);
form.appendChild(hiddenInput);
// Submit form
//form.submit();
}
谢谢!
答案 0 :(得分:0)
Stripe支持Idempotency-Key标头,您可以在其中定义该费用的唯一标记,以便在24小时内最多“处理一次”。见https://stripe.com/docs/api/php#idempotent_requests