PHP Stripe 获取购买成功后的交易id
PHP Stripe get the transaction id after successful purchase
我正在尝试获取购买后的交易 ID,但它 returns 在谢谢页面上是空的
order.php
<?php
\Stripe\Stripe::setApiKey('<test token>');
$amount = 100;
$card = $_POST['stripeToken'];
// Create a Customer
$customer = \Stripe\Customer::create(array(
"source" => $card,
"email" => $email,
"description" => "Example description")
);
// Charge the Customer instead of the card
$charge = \Stripe\Charge::create(array(
"amount" => 100,
"currency" => "usd",
"customer" => $customer->id)
);
// Save the billing info
set_billing([
'customer_id' => $customer->id,
'address' => $address,
'address2' => $address2,
'city' => $city,
'state' => $state,
'country' => $country,
'postal' => $postal,
'trans_id' => $charge->id // Save the transaction id
]);
function set_billing($fields = array())
{
$bdate = time();
$query = "INSERT INTO billings (
customer_id, address, address2, city,
state, postal, country, billing_date, trans_id
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $GLOBALS['sqlConnection']->prepare($query);
$stmt->bind_param(
'sssssssis', $fields['customer_id'], $fields['address'], $fields['address2'], $fields['city'], $fields['state'], $fields['postal'], $fields['country'], $bdate, $fields['trans_id']);
$stmt->execute();
}
// If successful, redirect to thank you page
header('Location: /thankyou.php?id=' . $user_id);
exit;
thankyou.php
<?php
$s = $sql->prepare('SELECT trans_id FROM billings WHERE user_id = ?');
$s->bind_param('i', $_GET['id']);
$s->execute();
$s->bind_result($trans_id);
$s->fetch();
?>
<ul>
<li>Transaction id: <?php echo $trans_id ?></li>
</ul>
我的代码有问题吗?
可以直接使用$charge->id
在 order.php
中更新您的行
// If successful, redirect to thank you page
header('Location: /thankyou.php?id=' . $charge->id );
在 thankyou.php
中更新您的代码
<ul>
<li>Transaction id: <?php echo $_GET['id'] ?></li>
</ul>
当您使用此向客户收费时
$charge = \Stripe\Charge::create(array(
"amount" => 100,
"currency" => "usd",
"customer" => $customer->id)
);
也试试print_r($charge); // to check the output variables
您将收到其响应 $charge->id
这只不过是 交易 ID ,请将此交易 ID 插入您的数据库以备将来使用。然后应用您的进一步代码。
然后在thankyou.php中获取您插入的值
我正在尝试获取购买后的交易 ID,但它 returns 在谢谢页面上是空的
order.php
<?php
\Stripe\Stripe::setApiKey('<test token>');
$amount = 100;
$card = $_POST['stripeToken'];
// Create a Customer
$customer = \Stripe\Customer::create(array(
"source" => $card,
"email" => $email,
"description" => "Example description")
);
// Charge the Customer instead of the card
$charge = \Stripe\Charge::create(array(
"amount" => 100,
"currency" => "usd",
"customer" => $customer->id)
);
// Save the billing info
set_billing([
'customer_id' => $customer->id,
'address' => $address,
'address2' => $address2,
'city' => $city,
'state' => $state,
'country' => $country,
'postal' => $postal,
'trans_id' => $charge->id // Save the transaction id
]);
function set_billing($fields = array())
{
$bdate = time();
$query = "INSERT INTO billings (
customer_id, address, address2, city,
state, postal, country, billing_date, trans_id
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $GLOBALS['sqlConnection']->prepare($query);
$stmt->bind_param(
'sssssssis', $fields['customer_id'], $fields['address'], $fields['address2'], $fields['city'], $fields['state'], $fields['postal'], $fields['country'], $bdate, $fields['trans_id']);
$stmt->execute();
}
// If successful, redirect to thank you page
header('Location: /thankyou.php?id=' . $user_id);
exit;
thankyou.php
<?php
$s = $sql->prepare('SELECT trans_id FROM billings WHERE user_id = ?');
$s->bind_param('i', $_GET['id']);
$s->execute();
$s->bind_result($trans_id);
$s->fetch();
?>
<ul>
<li>Transaction id: <?php echo $trans_id ?></li>
</ul>
我的代码有问题吗?
可以直接使用$charge->id
在 order.php
中更新您的行// If successful, redirect to thank you page
header('Location: /thankyou.php?id=' . $charge->id );
在 thankyou.php
中更新您的代码<ul>
<li>Transaction id: <?php echo $_GET['id'] ?></li>
</ul>
当您使用此向客户收费时
$charge = \Stripe\Charge::create(array(
"amount" => 100,
"currency" => "usd",
"customer" => $customer->id)
);
也试试print_r($charge); // to check the output variables
您将收到其响应 $charge->id
这只不过是 交易 ID ,请将此交易 ID 插入您的数据库以备将来使用。然后应用您的进一步代码。
然后在thankyou.php中获取您插入的值