繁体   English   中英

提交表单两次,并向客户收费两次(条带化-PHP)

[英]Form is submitted twice and the customer is charged twice (Stripe - PHP)

尝试向客户端收费时,Stripe系统出现问题。 交易同时进行两次。

我不确定这是Stripe脚本还是我的表单出现问题。

这是证明: 在此处输入图片描述

这是结帐表格:

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

谢谢!

Stripe支持Idempotency-Key标头,您可以在其中定义该费用的唯一令牌,以在24小时内最多“处理一次”。 参见https://stripe.com/docs/api/php#idempotent_requests

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM