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

标签 php stripe-payments

我很难让 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/

相关文章:

stripe-payments - 使用连接的帐户进行 Stripe Checkout 时出现 "Invalid payment_page id"错误

php - 从php数组自动完成显示后点击事件

Swift Stripe 访问 DidCreateToken

javascript - 自定义 Stripe 结账按钮无法在移动设备上使用

PHP和mySQL Task Scheduler程序和数据库原理?

ruby-on-rails - rails 4 - stripe_event 函数

stripe-payments - Stripe 中的批处理事务

php - 如何回显多维数组?

javascript - 停止 Ajax 循环 JQuery

php - 如何在PHP中生成大量 session ID?