现有卡的条纹检查 [英] Stripe check for existing card

查看:23
本文介绍了现有卡的条纹检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在客户提交信用卡时执行以下操作(使用 Stripe API):

I'm looking to do the following sequence (using the Stripe API) when a customer submits a credit card:

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

在我当前的代码中,Stripe 在尝试创建费用时返回 invalid_request 错误.这是相关的代码部分:

In my current code, Stripe is returning an invalid_request error upon trying to create the charge. Here's the section of code that relates:

//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);

感谢任何帮助.

推荐答案

原来是这个部分的问题:

The issue turned out to be this section:

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

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

If a fingerprint match is successful, I need to grab the id of the card already in the users's meta, not the matching card that was inputted. So, this works:

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

这篇关于现有卡的条纹检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆