tim*_*son 17 php stripe-payments
我想使用Stripe更新客户的默认卡.
在update customer
API文档中,不清楚要为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
@tim peterson的答案是完整的,并且满足了最初提出的问题.但是,这个Stackoverflow问题是为Stripe设置默认卡的第一个命中之一 - 所以我想更详细地记录我自己的发现.
首先要了解保存卡的流程,并且安全存储信用卡的引用非常重要.
通常,您可以在现场添加一张卡并将其设置为默认值,或者将信用卡ID(不是信用卡号!)存储在您的数据库中以执行操作.
请注意,在向用户添加第一张卡时,默认情况下它将是默认值.
customer->id
customer->id
在您的应用中.这通常在user
表格或类似内容中.一个255个字符的VARCHAR似乎是合适的 - 但是,在与Stripe直接聊天后,它们的系统中没有id的文档长度.
在稍后阶段,您将需要添加一张卡片.
成功后,Stripe JS将返回包含以下数据的响应:
在Stripe的例子中,他们向DOM附加了一堆隐藏的表单元素,填充上面的数据,然后"这次提交表单为realz".
现在回到原来的问题.在某个阶段,您可能有一个拥有多张卡的用户,需要将其设置为默认值.
因为您存储了上述表单中的返回数据,所以现在应该有一个列表,包括卡片,卡片ID以及数据库中的内容.因此,只需循环遍历它们,并将用户单击的卡作为默认值,获取卡ID,并default_source
使用卡ID值更新客户对象的属性.
这将使用一些非常松散的代码片段反映上述三个步骤(使用PHP,但应该很容易跟随).
注意为了简洁起见,我正在跳过错误捕获和异常.在与外部源进行任何交互时,总是很好地练习您的异常处理 - 基本上假设该服务将失败.
// 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)
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)
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)
Customer::retrieve($this->user->stripe_customer_id);
$customer->default_source = $cardId;
$customer->save();
Run Code Online (Sandbox Code Playgroud)
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)