2017-03-09 33 views
0

我希望做下面的序列(使用條紋API),當客戶提交信用卡:條紋檢查現有的卡

  1. 檢查,如果用戶在他們的元
  2. 條紋客戶ID
  3. 如果他們沒有,創建一個新客戶,將輸入的卡保存到該用戶
  4. 如果用戶已經有客戶ID,請檢查輸入的卡是否已經是他們保存的卡之一。
  5. 如果是,則收取卡
  6. 如果不是,則將新卡添加到客戶對象,然後爲該卡收費。

在我當前的代碼中,Stripe在嘗試創建費用時返回invalid_request錯誤。以下是相關代碼部分:

//See if our user already has a customer ID, if not create one 
$stripeCustID = get_user_meta($current_user->ID, 'stripeCustID', true); 
if (!empty($stripeCustID)) { 
    $customer = \Stripe\Customer::retrieve($stripeCustID); 
} else { 
    // Create a Customer: 
    $customer = \Stripe\Customer::create(array(
     'email' => $current_user->user_email, 
     'source' => $token, 
    )); 
    $stripeCustID = $customer->id; 

    //Add to user's meta 
    update_user_meta($current_user->ID, 'stripeCustID', $stripeCustID); 
} 

//Figure out if the user is using a stored card or a new card by comparing card fingerprints 
$tokenData = \Stripe\Token::retrieve($token); 
$thisCard = $tokenData['card']; 

$custCards = $customer['sources']['data']; 
foreach ($custCards as $card) { 
    if ($card['fingerprint'] == $thisCard['fingerprint']) { 
     $source = $thisCard['id']; 
    } 
} 
//If this card is not an existing one, we'll add it 
if ($source == false) { 
    $newSource = $customer->sources->create(array('source' => $token)); 
    $source=$newSource['id']; 
} 

// Try to authorize the card 
$chargeArgs = array(
    'amount' => $cartTotal, 
    'currency' => 'usd', 
    'description' => 'TPS Space Rental', 
    'customer' => $stripeCustID, 
    'source' => $source, 
    'capture' => false, //this pre-authorizes the card for 7 days instead of charging it immedietely 
    ); 

try { 
    $charge = \Stripe\Charge::create($chargeArgs); 

任何幫助表示讚賞。

+0

我會再仔細檢查'$ chargeArgs'中的'$ source'。如果我沒有記錯,它需要一個表示令牌的字符串(例如:'tok_189fx52eZvKYlo2CrxQsq5zF')。確認'$ source = $ customer-> sources-> create(array('source'=> $ token));'和$ thisCard ['id'];'return this – avip

+0

好想法。如果還提供了客戶,則來源還可以獲取卡ID(https://stripe.com/docs/api#create_charge)。看起來我在一個地方得到了卡片對象(不是ID),所以我更新了它,但仍然沒有運氣。 – Eckstein

+1

啊,我明白了。好吧,另一個想法是:如果你登錄到你的Stripe儀表板,API下有一個日誌選項卡,它應該包含問題的確切來源 - 至少就Stripe後端看到它而言。 – avip

回答

0

問題竟然是本節:

if ($card['fingerprint'] == $thisCard['fingerprint']) { 
    $source = $thisCard['id']; 
} 

如果指紋匹配成功,我需要抓住插卡的ID已經在用戶的元,不匹配的卡,這是輸入。所以,這個作品:

if ($card['fingerprint'] == $thisCard['fingerprint']) { 
    $source = $card['id']; 
}