【问题标题】:How does the Stripe API update customer email?Stripe API 如何更新客户电子邮件?
【发布时间】:2019-09-05 22:00:57
【问题描述】:

我无法让 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 来自我的数据库,错误例程是从我的卡更新代码中粘贴的。

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

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

脚本运行时出现致命错误:

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'],
  ]
);

【问题讨论】:

    标签: php stripe-payments


    【解决方案1】:

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

      "email": "myemail@mydomain.com"
    

    完全一样documented:

    电子邮件可选

    客户的电子邮件地址。它显示在客户旁边 您的仪表板,可用于搜索和跟踪。这可能 最多 512 个字符。这可以通过将值更新为来取消设置 null 然后保存。

    【讨论】:

    • 谢谢。请参阅上面的更新代码,并提出 cmets/suggesions。我仍然无法让它工作。
    【解决方案2】:

    以下是有效的:

    \Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxxx");
    
        \Stripe\Customer::update(
            $sc_customer_id,
            [
                'email' => $newemail,
            ]
        );
    

    【讨论】:

      猜你喜欢
      • 2021-04-09
      • 1970-01-01
      • 2018-09-26
      • 2014-12-01
      • 2022-11-25
      • 1970-01-01
      • 2019-08-14
      • 2020-11-15
      • 1970-01-01
      相关资源
      最近更新 更多