Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ManagesCustomer to include optional metadata when creating or syncing a user #1543

Merged
merged 1 commit into from
Jun 21, 2023

Conversation

dbhynds
Copy link
Contributor

@dbhynds dbhynds commented Jun 20, 2023

Our User model has the Billable trait applied to it. In order to make it easier to search Stripe for the correct user, we've been syncing the user ID in the metadata. I've noticed this has lead to a decent amount of code duplication.

class User extends Model
{
    public function refreshStripeData()
    {
        $options = [
            'metadata' => [
                'user_id' => $this->id,
            ],
        ];

        if ($this->hasStripeId()) {
            $this->syncStripeCustomerDetails($options);
        } else {
            $this->createAsStripeCustomer($options);
        }
        $this->updateDefaultPaymentMethodFromStripe();
    }

    public function ensureStripeUserExists()
    {

        if (!$this->hasStripeId()) {
            $this->createAsStripeCustomer([
                'metadata' => [
                    'user_id' => $this->id,
                ],
            ]);
        }
    }
}

This also means that if someone else is writing code that creates/syncs users in Stripe, they need to 1) know it won't send this metadata over and 2) remember to include it. It would be much easier if I could just define the metadata that should be included once on the User model and know that it would be included consistently every time.

This PR introduces a stripeMetadata() method to the ManagesCustomers concern. By default, it will just send over a null object and not set the metadata. However, if I wanted to specify some custom metadata on my Billable object, all I would need to do is add:

    public function stripeMetadata()
    {
        return [ 'user_id' => $this->id ];
    }

I haven't written any automated tests for this functionality, as I didn't see any for similar methods (e.g. stripeName(), stripeEmail(), etc.). I did manually test it and verified that it worked as expected for when no metadata is specified, and when custom metadata is provided.

@clementmas
Copy link
Contributor

clementmas commented Jun 21, 2023

I'd like to see that PR merged so I could also replace:

$user->createOrGetStripeCustomer([
    'metadata' => $user->stripeMetadata()
]);

with

$user->createOrGetStripeCustomer();

@driesvints driesvints merged commit 02c4656 into laravel:14.x Jun 21, 2023
8 checks passed
@driesvints
Copy link
Member

Looks good! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants