stripe-payments - 在 PHP 中使用 Stripe API 创建客户时,为什么我的数组不返回任何数据?
问题描述
我正在尝试在 Stripe 中创建客户和收费。但是,当我尝试 print_r 我的 $customer 数组变量中的数组数据时,我得到一个空白页。
我还尝试将字符串回显到charge.php 页面以确保收费页面正常工作(回显“页面工作”);它确实如此。所以我知道这不是问题的一部分。
权限似乎没问题。
这些是相关文件的权限 -rw-rw-r-- 1 sandbox admin 682 Apr 7 15:55 charge.php -rw-rw-r-- 1 sandbox admin 1612 Apr 7 17:01 index.php
我已经检查了我的代码几次,但没有运气。有人可以帮助我了解我可能做错了什么吗?
注意:下面显示的代码都在相应文件类型的单独文件中,即所有 PHP 代码都在不同的 .php 文件中,而 JS 则在一个 .js 文件中。注释标签不在实际代码中。它们只是为了进行此对话而指示每个文件从何处开始的指示符。
<!-- index.php code below -->
<html>
<body>
<div class="container">
<h2 class="my-4 text-center">Title of Product Page Here</h2>
<form action="./charge.php" method="post" id="payment-form">
<div class="form-row">
<input type="text" class="form-control mb-3 StripeElement StripeElement--empty" name="first_name" placeholder="First Name">
<input type="text" class="form-control mb-3 StripeElement StripeElement--empty" name="last_name" placeholder="Last Name">
<input type="email" class="form-control mb-3 StripeElement StripeElement--empty" name="email" placeholder="Email Address">
<div id="card-element" class="form-control">
<!-- 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>
</div>
<script src="https://js.stripe.com/v3/"></script>
<script src="js/charge.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</body>
</html>
<!-- charge.php code below -->
<!-- Note: Both the Stripe test Key and Stripe PW were masked per Stripe recommendation. Actual test key and password used in real code -->
<?php
require_once('vendor/autoload.php');
\Stripe\Stripe::setApiKey('sk_test_################');
// Sanitize
$POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);
$first_name = $POST['first_name'];
$last_name = $POST['last_name'];
$email = $POST['email'];
$token = $POST['stripeToken'];
// Create customer
$customer = \Stripe\Customer::create(array(
"email" => $email,
"source" => $token
));
// Charge customer
$charge = \Stripe\Charge::create(array(
"amount" => 5000,
"currency" => "usd",
"description" => "Audio Loops Pack 2019",
"customer" => $customer->id
));
print_r($customer);
<!-- charge.js code below -->
// Create a Stripe client.
var stripe = Stripe('pk_test_################');
// 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',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: 'antialiased',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
}
},
invalid: {
color: '#fa755a',
iconColor: '#fa755a'
}
};
//Style button
document.querySelector('#payment-form button').classList ='btn btn-primary btn-block mt-4';
// 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 {
// Send the token to your server.
stripeTokenHandler(result.token);
}
});
});
// Submit the form with the token ID.
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();
}
我希望客户和费用数组返回数据,但屏幕上什么也没有,甚至没有一个空数组。
解决方案
推荐阅读
- html - 使用 CSS,有没有办法确定 HTML 元素是否具有属性集?
- typescript - ngrx 8 createAction 语法
- python - 如何从python中的列表中获取随机元素?
- sql - 具有多个字段的搜索函数存储过程未返回预期结果
- python - 如何连接到托管在私有树莓派服务器上的远程 MySQL 数据库?
- postgresql - 如何在 AWS Aurora postgres 中终止长时间运行的查询
- css - 当用户在功能性反应组件中使用触摸屏时,用按钮隐藏 div
- r - 与 R 中的条件重复
- python-3.x - RNNLanguageModel 的 forward 方法的作用是什么?
- docker - GitLab CD 工作流将 Docker 应用程序部署到登台和生产中