laravel 条纹 api 检查现有卡
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29222249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
stripe api checking for existing card
提问by taekni
I'm sure I'm missing something obvious here, but I can't get my head around how to check for an existing card against a customer.
我确定我在这里遗漏了一些明显的东西,但我无法理解如何针对客户检查现有卡。
I'm using the stripe connect api within an laravel app to manage payments on behalf of others, and the basic process is as follows:
我在laravel应用程序中使用stripe connect api代表他人管理付款,基本过程如下:
- a stripe
token
is created viastripe.js
and submitted with the payment form - if the customer exists in the local database, I grab their
stripe_id
, otherwise a new customer is created using the token as the source/card - a
charge
is then created using the retrieved or new customerstripe_id
token
通过创建条带stripe.js
并与付款表一起提交- 如果客户存在于本地数据库中,我获取他们的
stripe_id
,否则使用令牌作为源/卡创建一个新客户 charge
然后使用检索到的或新的客户创建astripe_id
Currently, if the customer returns and uses a different card, as the charge only includes a customer, not source, it'll be charged against their default card regardless.
目前,如果客户返回并使用不同的卡,因为费用只包括客户,而不是来源,无论如何都会从他们的默认卡中收取费用。
What I'd like to do is:
我想做的是:
- create a stripe
token
- check
customer
against local database etc - check
card
fingerprint against customer's cards - if necessary, create new
card
on customer's record - create charge using both
customer
andcard
ids
- 创建条纹
token
- 检查
customer
本地数据库等 - 检查
card
客户卡上的指纹 - 如有必要,
card
在客户记录上创建新的 - 使用
customer
和card
id创建费用
Simply put:I can't see where in the process a persistent card_id
is generated; both those used in the stripe.js
response, and when created in the stripe dashboard, appear to be unique, meaning every charge creates a brand-new card object in stripe.
简单地说:我看不到在过程中的哪个地方card_id
生成了持久化;stripe.js
响应中使用的以及在条带仪表板中创建的那些看起来都是独一无二的,这意味着每次充电都会创建一个全新的条带卡对象。
I know I can retrieve a list of cards stored against a customer's account - but where do I get the initial card_id
from to search against?
我知道我可以检索针对客户帐户存储的卡片列表 - 但是我card_id
从哪里获取初始信息以进行搜索?
I've seen a question that touches on this here - Can I check whether stripe a card is already existed before going to create new one?- but I don't know Ruby, so can't make head nor tail of it.
我在这里看到了一个与此相关的问题 -在创建新卡之前,我可以检查卡条是否已经存在吗?- 但我不知道 Ruby,所以无法理解它。
EDIT:
编辑:
Simpler version - is there a way to get the fingerprint
as described in the stripe docs here - https://stripe.com/docs/api/php#card_object- without having to first create a card object ?
更简单的版本 - 有没有办法获得fingerprint
这里的条纹文档中描述的内容 - https://stripe.com/docs/api/php#card_object- 而不必先创建卡片对象?
回答by koopajah
So the idea here would be to use the fingerprint
on the Cardobject or the Tokenobject and not the id itself as those would be different if you add the same card multiple times.
所以这里的想法是fingerprint
在Card对象或Token对象上使用 ,而不是 id 本身,因为如果多次添加相同的卡片,它们会有所不同。
When you get a new card token you can retrieve it through the Retrieve TokenAPI and look for the fingerprint
in the card
hash.
当您获得新的卡令牌时,您可以通过检索令牌API检索它并fingerprint
在card
哈希中查找。
You would keep a list of known fingerprints in your database associated with a specific customer and/or card so that you can detect duplicate cards.
您将在与特定客户和/或卡相关联的数据库中保留一个已知指纹列表,以便您可以检测到重复的卡。
NOTE:make sure you are using the secret keys to get those information. otherwise if you are using the publishable key, you might not get the fingerprint value.
注意:确保您使用密钥来获取这些信息。否则,如果您使用的是可发布密钥,您可能无法获得指纹值。
回答by xinthose
I created a function to do this:
我创建了一个函数来做到这一点:
$customer
is the stripe customer object$stripe_account
is either your account's stripe ID or the connected account's stripe ID$token
comes from stripe.js elements$check_exp
allows you to decide if you want to check the card's expiration date as well, because the fingerprint does not change if the card's number is the samestripe 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; }
$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; }