gpt4 book ai didi

php - Stripe API 如何更新客户电子邮件?

转载 作者:行者123 更新时间:2023-12-05 03:01:28 26 4
gpt4 key购买 nike

我很难让 Stripe API 更新客户电子邮件。当用户登录时,有一个表单可以更新他们的电子邮件地址。电子邮件地址已在我的数据库表中更新,但我无法获取 Stripe API 来更新客户。

这是我使用的代码:

if (isset($customerid)) {
try {
$cu = \Stripe\Customer::update(
$customer_id,
[
'email' => $_SESSION['email'],
]
);

$output = "<p>Success!</p>";
} catch (\Stripe\Error\Card $e) {

// Use the variable $error to save any errors
// To be displayed to the customer later in the page
$body = $e->getJsonBody();
$err = $body['error'];
$error = $err['message'];

$output = "Error: $error";
}
// Add additional error handling here as needed
}

$customerid 来 self 的数据库,错误例程是从我的卡片更新代码中粘贴的。

这是来自日志的请求 POST 正文:

{
"email": {
"0": "myemail@mydomain.com"
}
}

脚本运行时出现 fatal error :

Fatal error: Uncaught Stripe\Error\InvalidRequest: Invalid string: {:"0"=>"myemail@mydomain.com"}

以及日志中的以下内容:

{
"error": {
"message": "Invalid string: {:"0"=>"myemail@mydomain.com"}",
"param": "email",
"type": "invalid_request_error"
}
}

有什么想法或建议吗?


按照 Marcin 的说法,PHP 代码应该是这样的吗?显然我是一个新手。

if (isset($customerid)) {
try {
$cu = \Stripe\Customer::update(
$customer_id,
[
"email": "myemail@mydomain.com",
]
);

$output = "<p>Success!</p>";
} catch (\Stripe\Error\Card $e) {

// Use the variable $error to save any errors
// To be displayed to the customer later in the page
$body = $e->getJsonBody();
$err = $body['error'];
$error = $err['message'];

$output = "Error: $error";
}
// Add additional error handling here as needed
}

我最初的做法是根据 API 引用并尝试模仿他们的示例:

\Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxx");

\Stripe\Customer::update(
'cus_ElRYM0KexefrGt',
[
'metadata' => ['order_id' => '6735'],
]
);

最佳答案

这里的错误信息很容易解释。您传递 JSON 对象,然后将其转换为字符串 {:"0"=>"myemail@mydomain.com"}。这不是有效的电子邮件语法,因此您会看到错误。您必须只传递电子邮件地址:

  "email": "myemail@mydomain.com"

完全一样 documented :

email optional

Customer’s email address. It’s displayed alongside the customer in your dashboard and can be useful for searching and tracking. This may be up to 512 characters. This can be unset by updating the value to null and then saving.

关于php - Stripe API 如何更新客户电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55697343/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com