在PHP中设置条带元素?好像我的PHP代码与我的JS无法正常通信

时间:2017-10-30 23:27:44

标签: javascript php stripe-payments stripe.js

我试图设置Stripe Elements,我对此很陌生。

所以,我包含了关于元素的条带文档中的每一个JS,我安装了Stripe API,并且需要它,也链接到Stripe JS。这是我走了多远,PHP在index.php文件的顶部,它还包含PHP下面的HTML和js。

PHP:

<?php 

require('C:/xampp2/htdocs/stripe/vendor/stripe/stripe-php/init.php');


// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("my_test_key");

// Token is created using Checkout or Elements!
// Get the payment token ID submitted by the form:
$token = $_POST['stripeToken'];

// Charge the user's card:
$charge = \Stripe\Charge::create(array(
  "amount" => 1000,
  "currency" => "usd",
  "description" => "Example charge",
  "source" => $token,
));

?>

HTML:

<form action="/charge" 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 Element errors -->
    <div id="card-errors" role="alert"></div>
  </div>

  <button>Submit Payment</button>
</form>

JS:

<script src="https://js.stripe.com/v3/"></script>
<script type="text/javascript">
var stripe = Stripe('pk_test_wzqlUMA7qtsfN4RZnm9IOSkQ');
var elements = stripe.elements();

// Custom styling can be passed to options when creating an Element.
var style = {
  base: {
    // Add your base input styles here. For example:
    fontSize: '16px',
    lineHeight: '24px'
  }
};

// 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');

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

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

on 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>

我有点迷失在这一切......你能给我一些白痴指导吗?

我还尝试将自定义.php文件的操作更改为包含上面的php代码,但结果相同。好像我的php代码无法与javascript通信。 Chrome devtool控制台没有显示常规的javascript错误。

提前感谢您提供任何帮助

1 个答案:

答案 0 :(得分:0)

我偶然发现了this guide,删除了我从条纹文档中收集的所有内容,并根据它的工作指南重新开始。