提问者:小点点

现有卡的条纹检查


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

  1. 检查用户的元数据中是否有条带客户id
  2. 如果没有,则创建一个新客户,将输入的卡保存给该用户
  3. 如果用户已经有客户id,请检查输入的卡是否已经是其保存的卡之一
  4. 如果是,则对该卡收费
  5. 如果不是,请将新卡添加到客户对象,然后对该卡收费

在我当前的代码中,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个答案

匿名用户

问题原来是这一节:

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

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

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