条纹检查现有卡

时间:2017-03-09 03:58:08

标签: php stripe-payments

当客户提交信用卡时,我希望执行以下序列(使用Stripe API):

  1. 检查用户的元
  2. 中是否有条带客户ID
  3. 如果他们没有,请创建新客户,将输入的卡片保存到该用户
  4. 如果用户已拥有客户ID,请检查输入的卡是否已经是其中一张已保存的卡。
  5. 如果是,请对该卡充电
  6. 如果不是,请将新卡添加到客户对象,然后为该卡充电。
  7. 在我当前的代码中,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);
    

    感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

问题原来是这一部分:

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

如果指纹匹配成功,我需要获取已经在用户的元中的卡的ID,而不是输入的匹配卡。所以,这有效:

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