当客户提交信用卡时,我希望执行以下序列(使用Stripe API):
在我当前的代码中,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);
感谢任何帮助。
答案 0 :(得分:1)
问题原来是这一部分:
if ($card['fingerprint'] == $thisCard['fingerprint']) {
$source = $thisCard['id'];
}
如果指纹匹配成功,我需要获取已经在用户的元中的卡的ID,而不是输入的匹配卡。所以,这有效:
if ($card['fingerprint'] == $thisCard['fingerprint']) {
$source = $card['id'];
}