【问题标题】:Stripe check for existing card现有卡的条纹检查
【发布时间】:2017-03-09 03:58:07
【问题描述】:

我希望在客户提交信用卡时执行以下序列(使用 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);

感谢任何帮助。

【问题讨论】:

  • 我会仔细检查$chargeArgs 中的$source。如果我没记错的话,它需要一个代表令牌的字符串(例如:'tok_189fx52eZvKYlo2CrxQsq5zF')。验证 $source = $customer->sources->create(array('source' => $token));$source = $thisCard['id']; 是否都返回此
  • 好主意。如果还提供了客户 (stripe.com/docs/api#create_charge),源也可以获取卡 ID。看起来我在一个地方获得了卡片对象(而不是 ID),所以我更新了它,但仍然没有运气。
  • 啊,我明白了。好吧,另一个想法:如果您登录到 Stripe 仪表板,在 API 下有一个日志选项卡,它应该包含问题的确切来源 - 至少就 Stripe 后端而言。
  • 这当然很有帮助!我没有意识到这些日志的存在。
  • 太棒了,很高兴它有帮助! :)

标签: php stripe-payments


【解决方案1】:

问题原来是这个部分:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-08
    • 2021-02-10
    • 2017-12-10
    • 1970-01-01
    • 2015-11-10
    • 2020-08-16
    • 2023-03-05
    • 2020-03-26
    相关资源
    最近更新 更多