2016-06-07 36 views
1

我試圖讓購買後事務ID,但它三江源頁面上返回空PHP條紋得到事務ID成功購買後

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> 

我的代碼有問題嗎?

+0

正如你從賬單表中讀取TRANS_ID ......在哪個文件要保存交易明細賬單中的表中的值? –

+0

當你給客戶收費時,你會得到一個'message array'嘗試打印它,並且在那裏你也會得到'transaction_id'。就像'$ charge_resp = $ charge ['msg']; $ transactionid = $ charge_resp-> id;' – Nehal

+0

@keziah,而不是提取交易ID,你必須先保存它。你在哪裏插入了'transaction id'在你的數據庫中? – Nehal

回答

4

當你充電使用此的客戶

$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獲取已插入

+0

@keziah,你的問題完全解決了。如果是的話,如果它有幫助,或者你發現它有用,隨意投票/接受。因爲它對其他用戶也很有用 – Nehal

1

您可以直接使用$電荷> 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> 
+0

的where子句中使用此項您應該使用$ charge-> id作爲事務ID,因爲balance_transaction是餘額交易的ID,用於描述此費用對您帳戶餘額的影響。 –

+0

請檢查此鏈接https://stripe.com/docs/api#charge_object-balance_transaction –

+0

我認爲你不應該在你的末端存儲卡的細節。你可以做一件事存儲卡的最後4位數字在你的結束與條紋給出的卡片ID。 Stripe將會處理所有事情。您可以使用條帶用戶ID獲取與用戶關聯的所有卡片 –