简体   繁体   中英

How does the Stripe API update customer email?

I am having difficulty getting the Stripe API to update the customer email. When a user is logged in, there is a form to update their email address. The email address is updated in my database table, but I cannot get the Stripe API to update the customer.

Here's the code I'm using:

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 comes from my database, and the error routine is pasted from my card update code.

Here is the request POST body from the logs:

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

I am getting a fatal error when the script runs:

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

and the following from the logs:

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

Any ideas or suggestions?


Is this what the php code should look like per Marcin? Obviously I'm pretty much a noob.

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
    }

The way I did it at first was from the API reference and I tried to mimic their example:

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

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

Error message is pretty self explanatory here. You pass JSON object which then is converted to string as {:"0"=>"myemail@mydomain.com"} . This is not a valid email syntax so you see the error. You must just pass email address alone:

  "email": "myemail@mydomain.com"

exactly as 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.

Here's what worked:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM