条纹更新客户默认卡(PHP)

tim*_*son 17 php stripe-payments

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

update customerAPI文档中,不清楚要为card参数提供什么.

在PHP中,我尝试设置card基于这样的retrieve card方法:

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

但这似乎不起作用.也没有像这样使用令牌:

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

所以我有点不知所措.思考?

tim*_*son 19

我能够通过Stripe对IRC#stripe频道的支持回答我自己的问题:

card参数由指定default_source这样的:

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


Chr*_*ris 13

1.概述

@tim peterson的答案是完整的,并且满足了最初提出的问题.但是,这个Stackoverflow问题是为Stripe设置默认卡的第一个命中之一 - 所以我想更详细地记录我自己的发现.

 

2.流动

首先要了解保存卡的流程,并且安全存储信用卡的引用非常重要.

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

请注意,在向用户添加第一张卡时,默认情况下它将是默认值.

 

2.1.客户创造

  1. 从您的应用程序通过api 在Stripe中创建客户
  2. Stripe返回一个客户对象,包括:customer->id
  3. 至少,存储customer->id在您的应用中.这通常在user表格或类似内容中.

一个255个字符的VARCHAR似乎是合适的 - 但是,在与Stripe直接聊天后,它们的系统中没有id的文档长度.

 

2.2.卡片创作

在稍后阶段,您将需要添加一张卡片.

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

    • response.id
    • response.card.id
    • response.card.last4
    • response.card.brand
    • response.card.exp_month
    • response.card.exp_year
  7. 在Stripe的例子中,他们向DOM附加了一堆隐藏的表单元素,填充上面的数据,然后"这次提交表单为realz".

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

 

2.3.设置默认值

现在回到原来的问题.在某个阶段,您可能有一个拥有多张卡的用户,需要将其设置为默认值.

因为您存储了上述表单中的返回数据,所以现在应该有一个列表,包括卡片,卡片ID以及数据库中的内容.因此,只需循环遍历它们,并将用户单击的卡作为默认值,获取卡ID,并default_source使用卡ID值更新客户对象的属性.

 

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();
Run Code Online (Sandbox Code Playgroud)

 

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;
})();
Run Code Online (Sandbox Code Playgroud)

 

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
    ]);
}
Run Code Online (Sandbox Code Playgroud)

 

3.3设置默认值

    Customer::retrieve($this->user->stripe_customer_id);
    $customer->default_source = $cardId;
    $customer->save();
Run Code Online (Sandbox Code Playgroud)

  • Stripe的流程过于复杂,我们应该能够在添加卡时传递"default_card"=> true参数.谢谢! (3认同)

Rob*_*Rob 13

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

如果您要创建新卡并将新卡设置为默认卡,请尝试此操作.

*请注意条带标记应来自条带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();
Run Code Online (Sandbox Code Playgroud)