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

Use service container to resolve StripeClient? #1559

Closed
martinbean opened this issue Aug 30, 2023 · 4 comments
Closed

Use service container to resolve StripeClient? #1559

martinbean opened this issue Aug 30, 2023 · 4 comments

Comments

@martinbean
Copy link

martinbean commented Aug 30, 2023

I’ve noticed in Laravel\Cashier\Cashier, the StripeClient instance is just manually instantiated:

return new StripeClient(array_merge([
    'api_key' => $options['api_key'] ?? config('cashier.secret'),
    'stripe_version' => static::STRIPE_VERSION,
    'api_base' => static::$apiBaseUrl,
], $options));

Would it be possible to use the service container to build this StripeClient instance instead? As I’m having difficulty testing code that uses Cashier methods when I don’t have a STRIPE_SECRET set in my testing environment:

The following exception occurred during the last request:

Stripe\Exception\InvalidArgumentException: api_key cannot be the empty string in /var/www/html/vendor/stripe/stripe-php/lib/BaseStripeClient.php:257

It would be nice if the StripeClient was instead resolved using the service container:

return resolve(StripeClient::class, array_merge([
    'api_key' => $options['api_key'] ?? config('cashier.secret'),
    'stripe_version' => static::STRIPE_VERSION,
    'api_base' => static::$apiBaseUrl,
], $options)));

I would then be able to mock any StripeClient method calls in my tests:

$this->mock(StripeClient::class, function (MockInterface $mock) {
    // Great expectations...
});

Or even just swap it out entirely for a fake:

$this->swap(StripeClient::class, FakeStripeClient::class);
@github-actions
Copy link

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

@driesvints
Copy link
Member

Yeah I think we could maybe do this (use app instead of resolve though).

Does overwriting the stripe method on your billable help as well maybe?

@martinbean
Copy link
Author

martinbean commented Sep 1, 2023

Does overwriting the stripe method on your billable help as well maybe?

@driesvints Somewhat. It allows me to mock the immediate StripeClient call, but doesn’t help with deeper calls, such as to get or create customer, as that gets called on the Cashier class.

@driesvints
Copy link
Member

Okay. Can you send in a PR? We'll get that in then.

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

No branches or pull requests

3 participants