【发布时间】:2017-09-08 12:28:15
【问题描述】:
我无法在条带结帐后重定向到确认页面。事情与下面的代码不同。如果我删除标题部分,一切都会正常工作。无论哪种方式,费用都会发布,但无论如何,所有内容都保留在结帐页面上。我是不是做错了什么?
我找到了a SO answer by one of their developers,但不幸的是它不起作用。奇怪的是,Stripe for PHP 根本没有记录该操作,并且支持令人愤怒地给了我与标题重定向相同的确切答案。
PHP
<?php
require_once('./config.php');
// Variables passed by Ajax request. Shipping address, selection, and message attached via metadata to Stripe Tx.
$token = $_POST['token'];
$price = $_POST['price'];
$email = $_POST['email'];
$metadata = $_POST['metadata'];
// Post charge to Stripe with the variables.
try {
$customer = \Stripe\Customer::create(array(
'email' => $email,
'source' => $token
));
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $price,
'currency' => 'usd',
'metadata' => array("metadata" => $metadata)
));
//Ajax hears and prints in console.log that Tx successful. Then page redirects.
echo 'success';
header('Location: /success.php');
} catch(Stripe\CardError $e) {
// When the card declines.
echo $token;
}
?>
JS
var handler = StripeCheckout.configure({
key: 'pk_test_00000000',
image: '/logo.png',
locale: 'auto',
token: function(token, args) {
// Use the token to create the charge with a server-side script.
// You can access the token ID with `token.id`
console.log(token)
$.ajax({
url: '/charge.php',
type: 'POST',
data: { token: token.id, email: token.email, price: price, metadata: metadata },
success: function(data) {
if (data == 'success') {
console.log("Things went well for once, success!");
console.log(metadata);
} else {
console.log("Charge Error");
console.log(metadata);
}
},
error: function(data) {
console.log("Ajax Error");
console.log(data);
}
}); // end ajax call
}
});
【问题讨论】:
标签: php stripe-payments