我正在寻找一种方法来为客户创建新来源,然后将该来源设置为客户的默认来源。我当前面临的问题是,创建源的响应并没有为我提供一种简单的方法来识别新创建的源的 id,以便我可以将该源设置为默认值。
下面是我的代码,没有任何 API key 和客户 ID:
\Stripe\Stripe::setApiKey($stripe_api_key);
$customer = \Stripe\Customer::retrieve($_SESSION['parent']['stripe_customer_id']);
$customer->sources->create(array('source' => array("object" => "card", 'exp_month' => $expire_month, 'exp_year' => $expire_year, 'number' => $card_number, 'address_line1' => $billing_address1, 'address_line2' => $billing_address2, 'address_city' => $billing_city, 'address_zip' => $billing_postal_code, 'address_country' => $billing_country, 'currency' => 'GBP', 'cvc' => $security_code, 'name' => $_SESSION['parent']['firstname'] . ' ' . $_SESSION['parent']['lastname'])))->__toArray(true);
//set as default
if (isset($_POST['check_default_source'])) {
$customer->default_source=$customer['id'];
$customer->save();
}
最佳答案
我也遇到了这个问题,略有不同。
我从 token_id 添加了新源。
getLastResponse
方法在我的 API 或库版本中不可用。
但是使用 token 对象 (Stripe::Token::retreive( token_id )),您实际上可以检索card_id,并使用它来更新default_source。
我花了很长时间才弄清楚这个问题,所以我把它贴在这里。也许它对将来的某人有帮助......
关于php - 如何在 Stripe API 上创建新源并设置为默认源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48283787/