Stripe 更新客户默认卡 (PHP)

Stripe update customer default card (PHP)

我想为使用 Stripe 的客户更新默认卡。

update customer API docs 中,不清楚为 card 参数输入什么。

在 PHP 中,我试过根据 retrieve card 方法设置 card,如下所示:

$customer->card=$card['id']

但这似乎不起作用。也不会像这样使用令牌:

$customer->source=$_POST['stripe_token]

所以我有点不知所措。想法?

我能够在 Stripe 的支持下回答我自己的问题 IRC #stripe channel:

card 参数由 default_source 指定,如下所示:

Stripe::setApiKey($stripe_private_key);
$customer = Stripe_Customer::retrieve($stripe_cus_id);
$customer->default_source=$card['id'];
$customer->save();  

1。概述

@tim peterson 的回答是完整的并且满足最初提出的问题。然而,这个 Whosebug 问题是为 Stripe 设置默认卡的第一个问题 - 所以我想更详细地记录我自己的发现。

2。流量

首先要了解保存卡的流程,这对于安全地存储 references 信用卡也很重要。

通常您会添加一张卡并将其当场设置为默认卡,或者将信用卡 ID(不是信用卡号!)存储在您的数据库中以执行操作。

注意当给用户添加第一张卡片时,它会默认,好吧,默认。

2.1。客户创建

  1. 从您的 app 通过 api
  2. Stripe 中创建一个客户
  3. 客户对象由 Stripe 编辑,包括 customer->id
  4. 至少,将 customer->id 存储在您的 app 中。这通常在 user table 或类似的地方。

A VARCHAR at 255 characters seems appropriate - however, after chatting with Stripe directly, they don't have a document length of id's in their system.

2.2。卡片创建

稍后,您会想要添加一张卡片。

  1. 首先使用 Stripe JS 库以及在其 site 上记录的库。这里的想法是表单的提交操作直接指向 Stripe 服务器,因此当提交表单时,真正的信用卡详细信息永远不会到达您的服务器。尽管在技术上可以不使用 Stripe JS 创建卡片,但除非有充分的理由,否则我会坚持推荐的方式并让它完成繁重的工作。
  2. 用户在您的信用卡表单上,发生以下情况:
  3. 他们将信用卡详细信息输入您的表格
  4. 他们点击提交
  5. 所有表单输入都通过 ajax
  6. 发送到 Stripe
  7. 成功后 Stripe JS 将 return 包含以下数据的响应:

    • response.id
    • response.card.id
    • response.card.last4
    • response.card.品牌
    • response.card.exp_month
    • response.card.exp_year
  8. 在Stripe的例子中,他们追加到DOM一堆隐藏的表单元素,填充上面的数据,然后"submit the form for realz this time".

  9. 您的后端,无论是什么,都会收到上述数据,您可以根据需要存储它。

2.3。设置默认值

现在回到最初的问题。在某个阶段,您可能有一个用户有多张卡,需要将其中一张设置为默认卡。

因为您存储了上述表格中的 return 数据,您现在应该在您的数据库中有一个卡片列表、卡片 ID 以及诸如此类的东西。所以简单地遍历它们,用户点击哪张卡作为默认卡,获取卡 ID,并使用卡 ID 值针对客户对象更新 default_source 属性。

3 个例子

这将通过一些非常松散的代码片段来反映上述三个步骤(使用 PHP 但应该很容易跟进)。

注意 为了简洁起见,我跳过错误捕获和异常。在与外部源进行任何交互时,勤于处理异常始终是一个好习惯——基本上假设服务会失败。

3.1 客户创建

// Send details to Stripe
$customer = \Stripe\Customer::create([
    'email' => $this->user->email,
    'description' => $this->user->name,
]);

// Then update our application
$this->user->stripe_customer_id = $customer->id;
$this->user->save();

3.2 卡片制作

3.2.1 Javascript

module.exports = (function() {
    function CreditCard() {
        $('#payment-form').submit(function(e) {
            var $form = $(this);

            // Disable the submit button to prevent repeated clicks
            $form.find('button').prop('disabled', true);

            Stripe.card.createToken($form, function(status, response) {
                var $form = $('#payment-form');
                if (response.error) {
                    // Show the errors on the form
                    $form.find('.payment-errors').text(response.error.message);
                    $form.find('.payment-errors').parents(".row").show();
                    $form.find('button').prop('disabled', false);
                } else {
                    // token contains id, last4, and card type
                    var token = response.id;
                    var cardId = response.card.id;
                    var last4 = response.card.last4;
                    var brand = response.card.brand;
                    var expMonth = response.card.exp_month;
                    var expYear = response.card.exp_year;

                    // Insert the token into the form so it gets submitted to the server
                    $form.append($('<input type="hidden" name="stripeToken" />').val(token));
                    $form.append($('<input type="hidden" name="cardId" />').val(cardId));
                    $form.append($('<input type="hidden" name="last4" />').val(last4));
                    $form.append($('<input type="hidden" name="brand" />').val(brand));
                    $form.append($('<input type="hidden" name="expMonth" />').val(expMonth));
                    $form.append($('<input type="hidden" name="expYear" />').val(expYear));

                    // and re-submit
                    $form.get(0).submit();
                }
            });

            // Prevent the form from submitting with the default action
            return false;
        });
    }

    return CreditCard;
})();

3.2.2 后端

public function save(string $token, string $cardId, string $last4, string $brand, int $expMonth, int $expYear)
{
    // Store in our application
    $creditCard = $this->user->creditCards()->create([
        'token_id' => $token,
        'card_id' => $cardId,
        'last4' => $last4,
        'brand' => $brand,
        'exp_month' => $expMonth,
        'exp_year' => $expYear
    ]);
}

3.3 设置默认值

    Customer::retrieve($this->user->stripe_customer_id);
    $customer->default_source = $cardId;
    $customer->save();

创建新卡片并将卡片指定为默认来源

如果您想创建一张新卡并将该新卡设置为默认卡,请尝试此操作。

*请注意,条带令牌应来自条带 js 请求,条带客户 ID 可能来自经过身份验证的用户。

// Get the customer
$customer = Customer::retrieve("cus_9jF6ku4f2pztRo");

// Add a new card to the customer
$card = $customer->sources->create(['source' => "tok_19PmcMI94XzVK71QeIwtUJmM"]);

// Set the new card as the customers default card
$customer->default_source = $card->id;
$customer->save();