stripe / stripe-php

PHP library for the Stripe API.
https://stripe.com
MIT License
3.75k stars 848 forks source link

Account Retrieve Initiates Empty Objects (Prevents Save) #380

Closed JoshuaBedford closed 7 years ago

JoshuaBedford commented 7 years ago

Howdy. I'm currently developing a PHP application using Laravel and Stripe Connect.

I can successfully create a customer, but when i go to UPDATE the customer (following this documentation) it says there are empty values that I never specified (see error below):

You passed an empty string for 'legal_entity[personal_address]'. We assume empty values are an attempt to unset a parameter; however 'legal_entity[personal_address]' cannot be unset. You should remove 'legal_entity[personal_address]' from your request or supply a non-empty value.

In the documentation, it says that is an optional field, but retrieving the account generates an object with it as an empty value, which it won't accept to save the updated information.

For reference, here is my code.

public function verifyAccount(Request $request)
{
    $input = $request->all();
    $token = $input['token'];
    $account = $input['account'];

    $user = \Auth::user();

    $acct = $user->getStripeAccount();

    $acct->external_accounts->create(["external_account" => $token['id']]);

    $acct->legal_entity = [
        'type' => 'individual',
        'first_name' => $account['legal_entity']['first_name'],
        'last_name' => $account['legal_entity']['last_name'],
        'ssn_last_4' => $account['legal_entity']['ssn_last_4'],
        'address' => [
            "line1" => $account['legal_entity']['address']['line1'],
            "line2" => $account['legal_entity']['address']['line2'],
            "city" => $account['legal_entity']['address']['city'],
            "state" => $account['legal_entity']['address']['state'],
            "postal_code" => $account['legal_entity']['address']['postal_code'],
        ],
        'dob' => [
            "day" => $account['legal_entity']['dob']['day'],
            "month" => $account['legal_entity']['dob']['month'],
            "year" => $account['legal_entity']['dob']['year']
        ],
        'tos_acceptance' => [
            "date" => $account['legal_entity']['tos_acceptance']['date'],
            "ip" => $account['legal_entity']['tos_acceptance']['ip'],
        ]
    ];

    $user->card_brand = $token['card']['brand'];
    $user->card_last_four = $token['card']['last4'];

    if($acct->save() && $user->save()){
        return "success";
    } else{
        return "error";
    }
}

I'm sure this is all due to some sort of misunderstanding on my part, but I see no reason why this should not work. Your help is much appreciated.

P.S. I am on PHP 7.1.6 and Stripe 5.2.

remi-stripe commented 7 years ago

@JoshuaBedford The issue here is that you should set each field in the legal_entity hash directly instead of "overwriting" the whole object.

Your code should look like this:

    $acct->legal_entity->type = 'individual';
    $acct->legal_entity->first_name = $first_name;
    $acct->legal_entity->last_name = $last_name;
    $acct->legal_entity->ssn_last_4 = $ssn_last4;
    $acct->tos_Acceptance->date = time();
    $acct->save();

you set the fields you want to set. You don't need to copy anything that is already set. You also should only set non empty values.

JoshuaBedford commented 7 years ago

@remi-stripe Thanks! Knew i was having a bad moment there. Seems pretty obvious doesn't it? Thanks for the prompt response. I'll try that out.