条带 api 检查现有卡

stripe api checking for existing card

我确定我在这里遗漏了一些明显的东西,但我无法理解如何根据客户检查现有卡。

我在laravel应用中使用stripe connect api代管支付,基本流程如下:

目前,如果客户 returns 并使用不同的卡,由于费用仅包括客户,而不是来源,因此无论如何都会从他们的默认卡中扣除。

我想做的是:

简单的说:我看不到进程中哪里产生了一个持久化的card_id;在 stripe.js 响应中使用的以及在条纹仪表板中创建的那些看起来都是独一无二的,这意味着每次收费都会在条纹中创建一个全新的卡片对象。

我知道我可以检索存储在客户帐户中的卡列表 - 但我从哪里获得初始 card_id 以进行搜索?

我在这里看到了一个涉及此问题的问题 - Can I check whether stripe a card is already existed before going to create new one? - 但我不知道 Ruby,所以无法弄清楚它。

编辑:

更简单的版本 - 有没有办法获得 fingerprint,如此处的条带文档中所述 - https://stripe.com/docs/api/php#card_object - 无需先创建卡片对象?

所以这里的想法是在 Card object or the Token 对象上使用 fingerprint 而不是 id 本身,因为如果多次添加同一张卡,它们会有所不同。

当您获得新的卡片令牌时,您可以通过 Retrieve Token API 检索它并在 card 哈希中查找 fingerprint

您将在数据库中保留与特定客户 and/or 卡关联的已知指纹列表,以便您可以检测重复的卡。

注意:确保您使用密钥来获取这些信息。否则,如果您使用的是可发布密钥,则可能无法获得指纹值。

我创建了一个函数来执行此操作:

  • $customer是条带客户对象
  • $stripe_account 是您帐户的条带 ID 或关联帐户的条带 ID
  • $token 来自 stripe.js 个元素
  • $check_exp 允许您决定是否还要检查卡的有效期,因为如果卡号相同,指纹不会改变
  • 条纹PHPAPI7.0.0

    function check_duplicate_card($customer, $stripe_account, $token, $check_exp) {
    $loc = "check_duplicate_card >> ";
    $debug = true;
    
    if ($debug) {
        // see here for an explanation for logging: http://php.net/set_error_handler >> Examples
        trigger_error("$loc started", E_USER_NOTICE);
    }
    
    try
    {
        // get token data
        $response = \Stripe\Token::retrieve(
            $token,
            ["stripe_account" => $stripe_account]
        );
        $token_fingerprint = $response->card->fingerprint;
        $token_exp_month = $response->card->exp_month;
        $token_exp_year = $response->card->exp_year;
        if ($debug) {
            trigger_error("$loc token_fingerprint = $token_fingerprint; token_exp_month = $token_exp_month; token_exp_year = $token_exp_year", E_USER_NOTICE);
        }
    
        // check for duplicate source
        if ($debug) {
            trigger_error("$loc customer sources = " . json_encode($customer->sources), E_USER_NOTICE);
        }
        $duplicate_found = false;
        foreach ($customer->sources->data as &$value) {
            // get data
            $fingerprint = $value->fingerprint;
            $exp_month = $value->exp_month;
            $exp_year = $value->exp_year;
    
            if ($fingerprint == $token_fingerprint) {
                if ($check_exp) {
                    if (($exp_month == $token_exp_month) && ($exp_year == $token_exp_year)) {
                        $duplicate_found = true;
                        break;
                    }
                } else {
                    $duplicate_found = true;
                    break;    
                }
            }
        }
        if ($debug) {
            trigger_error("$loc duplicate_found = " . json_encode($duplicate_found), E_USER_NOTICE);
        }
    } catch (Exception $e) {
        if ($e instanceof \Stripe\Exception\ApiErrorException) {
            $return_array = [
                "status" => $e->getHttpStatus(),
                "type" => $e->getError()->type,
                "code" => $e->getError()->code,
                "param" => $e->getError()->param,
                "message" => $e->getError()->message,
            ];
            $return_str = json_encode($return_array);
            trigger_error("$loc $return_str", E_USER_WARNING);
            http_response_code($e->getHttpStatus());
            echo $return_str;
        } else {
            $return_array = [
                "message" => $e->getMessage(),
            ];
            $return_str = json_encode($return_array);
            trigger_error("$loc $return_str", E_USER_ERROR);
            http_response_code(500); // Internal Server Error
            echo $return_str;
        }
    }
    
    if ($debug) {
        trigger_error("$loc ended", E_USER_NOTICE);
    }
    
    return $duplicate_found;
    }