php - 如何在 Stripe API 上创建新源并设置为默认源

标签 php api stripe-payments

我正在寻找一种方法来为客户创建新来源,然后将该来源设置为客户的默认来源。我当前面临的问题是,创建源的响应并没有为我提供一种简单的方法来识别新创建的源的 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/

相关文章:

javascript - 将 strip 支付(JS 和 PHP)与自定义金额(JS 变量)集成

arrays - forEach如何将多条数据保存到MongoDB中

php - 插入选定复选框的隐藏字段

php - Slim Framework 3 在运行时输入和输出到 cli

jquery - Ajax 必然导致公共(public) API 吗?

javascript - Node 休息客户端获取局部变量

stripe-payments - STRIPE - 在 charge.php 上调用时动态设置的金额返回 null

php - 如何使用 mysql_query 在这种方法中添加 "no record found"

php - 将用户数据从js发送到php

php - 如何在 PHP 中集成谷歌地图并用标志标记地点