我尝试在不使用 ajax 的情况下向 charge.php 提交 stripeToken,但收到以下错误:
Fatal error: Uncaught exception 'Stripe\Error\InvalidRequest' with message 'Invalid source object: must be a dictionary or a non-empty string.
我相信这与 token 未附加到输入有关。我尝试了几种不同的方法但没有成功。
最低加价
<form action="charge.php" method="POST" id="payment-form">
//rest of the form
<input id="stripeToken" type="hidden" name="stripeToken" value="">
<input type="submit" class="submit" value="Submit Payment">
</form>
javascript
Stripe.setPublishableKey('pk_test_PUBLISHABLE'); //publishable key
var $form = $('#payment-form'); // declare form variable
$(function() {
$form.submit(function(event) {
// Disable the submit button to prevent repeated clicks:
$form.find('.submit').prop('disabled', true);
// Request a token from Stripe:
Stripe.card.createToken($form, stripeResponseHandler);
// Prevent the form from being submitted:
return false;
});
});
function stripeResponseHandler(status, response) {
if (response.error) {
// display errors in form
} else {
// Get the token
var token = response.id;
// Insert the token in the form:
document.getElementById('stripeToken').value=token;
// $form.append($('<input type="hidden" name="stripeToken">').val(token));
// Submit the form:
$form.get(0).submit();
}
};
charge.php
require_once('config.php');
\Stripe\Stripe::setApiKey($mysk_key);
$stripeToken=$_POST['stripeToken'];//line of the error
$customer = \Stripe\Customer::create(array(
'email' => $email,
'source' =>$stripeToken, //line of the error
'plan' => $myplan
));
更新config.php
<?php
require_once('vendor/init.php');
$test_sk="sk_test_xxx";
$live_sk="sk_live_xxx";
$mykey_sk=$test_sk;
$test_pk="pk_test_xxx";
$live_pk="pk_live_xxx";
$mykey_pk=$test_pk;
$stripe = array(
"secret_key" =>$mykey_sk,
"publishable_key" =>$mykey_pk
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
?>
最佳答案
您已经使用了 jQuery,因此您不妨提取 token 并使用 jQuery 将其分配给输入。
确保response.id不为空,然后使用jQuery编辑值。
$('#stripeToken').val(token);
如果您传递空字符串,stripe api 会提示,因此确保您的response.id 不是空字符串非常重要。
关于javascript - Stripe fatal error 源对象无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45701453/