我确信我在这里遗漏了一些显而易见的东西,但我不知道如何针对客户检查现有卡。
我在laravel应用程序中使用stripe connect api代表他人管理付款,基本流程如下:
目前,如果客户返回并使用不同的卡,由于费用只包括客户,而不是来源,因此无论如何都将从他们的默认卡中收取。
我想做的是:
令牌
客户
对本地数据库等卡
指纹与客户的卡卡
客户
和卡
id简单地说:我看不到在过程中的哪个位置生成了持久的card_id
;条纹中使用的那些.js
响应,以及在条纹仪表板中创建时,看起来都是唯一的,这意味着每次充电都会在条纹中创建一个全新的卡对象。
我知道我可以检索存储在客户账户上的卡的列表,但是我从哪里获得初始的< code>card_id来进行搜索呢?
我在这里看到了一个涉及到这个的问题——在创建新的卡片之前,我能检查条纹卡片是否已经存在吗?-但是我不知道Ruby,所以搞不清楚它的来龙去脉。
编辑:
更简单的版本-是否有方法获得指纹
,如这里的stripe docs所述-https://stripe.com/docs/api/php#card_object-无需首先创建卡对象?
因此,这里的想法是在卡对象或Token对象上使用指纹
,而不是ID本身,因为如果您多次添加同一张卡,它们会有所不同。
当您获得新卡令牌时,您可以通过Retrieve Token API检索它并在card
哈希中查找指纹
。
您可以在数据库中保存一份与特定客户和/或卡相关的已知指纹列表,以便检测重复的卡。
注意:确保使用密钥来获取这些信息。否则,如果您使用可发布密钥,您可能无法获得指纹值。
我创建了一个函数来执行此操作:
>
$customer
是条带客户对象$stripe_account
是您的帐户的条带ID或连接的帐户的条纹ID$token
来自stripe。js元素$check_exp
允许您决定是否还要检查卡的到期日期,因为如果卡的号码相同,指纹不会改变条带式PHP API 7.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;
}