diff --git a/composer.json b/composer.json index 88f2f63..efa460f 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ ], "require": { "payum/core": "^1.6", - "stripe/stripe-php": "^9" + "stripe/stripe-php": "^12" }, "require-dev": { "phpunit/phpunit": "^8|^9", diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 1dc72d8..75d6314 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -17,17 +17,32 @@ parameters: message: '/Parameter #2 (\$arr2|\.\.\.\$replacements) of function array_replace expects array, mixed given\./' path: src/AbstractStripeGatewayFactory.php - - message: '/Parameter #[12] \$[^ ]+ of class FluxSE\\PayumStripe\\Api\\StripeCheckoutSessionApi constructor expects string, mixed given\./' + message: '/Parameter #[12] \$[^ ]+ of class FluxSE\\PayumStripe\\Api\\Stripe(CheckoutSession|Js)Api constructor expects string, mixed given\./' paths: - src/StripeCheckoutSessionGatewayFactory.php - src/StripeJsGatewayFactory.php - - message: '/Parameter #3 \$webhookSecretKeys of class FluxSE\\PayumStripe\\Api\\StripeCheckoutSessionApi constructor expects array, mixed given\./' + message: '/Parameter #3 \$webhookSecretKeys of class FluxSE\\PayumStripe\\Api\\Stripe(CheckoutSession|Js)Api constructor expects array, mixed given\./' paths: - src/StripeCheckoutSessionGatewayFactory.php - src/StripeJsGatewayFactory.php - - message: '/Parameter #4 \$paymentMethodTypes of class FluxSE\\PayumStripe\\Api\\StripeCheckoutSessionApi constructor expects array, mixed given\./' + message: '/Parameter #4 \$clientId of class FluxSE\\PayumStripe\\Api\\Stripe(CheckoutSession|Js)Api constructor expects string\|null, mixed given\./' + paths: + - src/StripeCheckoutSessionGatewayFactory.php + - src/StripeJsGatewayFactory.php + - + message: '/Parameter #5 \$stripeAccount of class FluxSE\\PayumStripe\\Api\\Stripe(CheckoutSession|Js)Api constructor expects string\|null, mixed given\./' + paths: + - src/StripeCheckoutSessionGatewayFactory.php + - src/StripeJsGatewayFactory.php + - + message: '/Parameter #6 \$stripeVersion of class FluxSE\\PayumStripe\\Api\\Stripe(CheckoutSession|Js)Api constructor expects string, mixed given\./' + paths: + - src/StripeCheckoutSessionGatewayFactory.php + - src/StripeJsGatewayFactory.php + - + message: '/Parameter #7 \$paymentMethodTypes of class FluxSE\\PayumStripe\\Api\\StripeCheckoutSessionApi constructor expects array, mixed given\./' path: src/StripeCheckoutSessionGatewayFactory.php - message: '/Parameter #1 \$templateName of class FluxSE\\PayumStripe\\Action\\StripeJs\\Api\\RenderStripeJsAction constructor expects string, mixed given\./' diff --git a/src/AbstractStripeGatewayFactory.php b/src/AbstractStripeGatewayFactory.php index beea296..e8a8654 100644 --- a/src/AbstractStripeGatewayFactory.php +++ b/src/AbstractStripeGatewayFactory.php @@ -65,6 +65,7 @@ use FluxSE\PayumStripe\Api\KeysAwareInterface; use Payum\Core\Bridge\Spl\ArrayObject; use Payum\Core\GatewayFactory; +use Stripe\Util\ApiVersion; abstract class AbstractStripeGatewayFactory extends GatewayFactory { @@ -182,6 +183,9 @@ protected function getStripeDefaultOptions(): array 'publishable_key' => '', 'secret_key' => '', 'webhook_secret_keys' => [], + 'client_id' => null, + 'stripe_account' => null, + 'stripe_version' => ApiVersion::CURRENT, ]; } diff --git a/src/Action/Api/Resource/AbstractAllAction.php b/src/Action/Api/Resource/AbstractAllAction.php index 33361b0..c76c3e0 100644 --- a/src/Action/Api/Resource/AbstractAllAction.php +++ b/src/Action/Api/Resource/AbstractAllAction.php @@ -8,9 +8,7 @@ use FluxSE\PayumStripe\Request\Api\Resource\AllInterface; use Payum\Core\Exception\LogicException; use Payum\Core\Exception\RequestNotSupportedException; -use Stripe\ApiOperations\All; use Stripe\Collection; -use Stripe\Stripe; abstract class AbstractAllAction implements AllResourceActionInterface { @@ -34,15 +32,12 @@ public function execute($request): void */ public function allApiResource(AllInterface $request): Collection { - $apiResourceClass = $this->getApiResourceClass(); - if (false === method_exists($apiResourceClass, 'all')) { - throw new LogicException(sprintf('This class "%s" is not an instance of "%s" !', $apiResourceClass, All::class)); + $service = $this->getService(); + if (false === method_exists($service, 'all')) { + throw new LogicException('This Stripe service does not have "all" method !'); } - Stripe::setApiKey($this->api->getSecretKey()); - - /* @see All::all() */ - return $apiResourceClass::all( + return $service->all( $request->getParameters(), $request->getOptions() ); diff --git a/src/Action/Api/Resource/AbstractCreateAction.php b/src/Action/Api/Resource/AbstractCreateAction.php index 4973e36..a8df798 100644 --- a/src/Action/Api/Resource/AbstractCreateAction.php +++ b/src/Action/Api/Resource/AbstractCreateAction.php @@ -8,9 +8,7 @@ use FluxSE\PayumStripe\Request\Api\Resource\CreateInterface; use Payum\Core\Exception\LogicException; use Payum\Core\Exception\RequestNotSupportedException; -use Stripe\ApiOperations\Create; use Stripe\ApiResource; -use Stripe\Stripe; abstract class AbstractCreateAction implements CreateResourceActionInterface { @@ -34,15 +32,12 @@ public function execute($request): void */ public function createApiResource(CreateInterface $request): ApiResource { - $apiResourceClass = $this->getApiResourceClass(); - if (false === method_exists($apiResourceClass, 'create')) { - throw new LogicException(sprintf('This class "%s" is not an instance of "%s" !', $apiResourceClass, Create::class)); + $service = $this->getService(); + if (false === method_exists($service, 'create')) { + throw new LogicException('This Stripe service does not have "create" method !'); } - Stripe::setApiKey($this->api->getSecretKey()); - - /* @see Create::create() */ - return $apiResourceClass::create( + return $service->create( $request->getParameters(), $request->getOptions() ); diff --git a/src/Action/Api/Resource/AbstractDeleteAction.php b/src/Action/Api/Resource/AbstractDeleteAction.php index 810dfe4..d957266 100644 --- a/src/Action/Api/Resource/AbstractDeleteAction.php +++ b/src/Action/Api/Resource/AbstractDeleteAction.php @@ -8,10 +8,7 @@ use FluxSE\PayumStripe\Request\Api\Resource\DeleteInterface; use Payum\Core\Exception\LogicException; use Payum\Core\Exception\RequestNotSupportedException; -use Stripe\ApiOperations\Delete; -use Stripe\ApiOperations\Retrieve; use Stripe\ApiResource; -use Stripe\Stripe; abstract class AbstractDeleteAction implements DeleteResourceActionInterface { @@ -32,24 +29,20 @@ public function execute($request): void public function deleteApiResource(DeleteInterface $request): ApiResource { - $apiResourceClass = $this->getApiResourceClass(); - if (false === method_exists($apiResourceClass, 'retrieve')) { - throw new LogicException(sprintf('This class "%s" is not an instance of "%s" !', $apiResourceClass, Retrieve::class)); + $service = $this->getService(); + if (false === method_exists($service, 'retrieve')) { + throw new LogicException('This Stripe service does not have "retrieve" method !'); } - if (false === method_exists($apiResourceClass, 'delete')) { - throw new LogicException(sprintf('This class "%s" is not an instance of "%s" !', $apiResourceClass, Delete::class)); + if (false === method_exists($service, 'delete')) { + throw new LogicException('This Stripe service does not have "delete" method !'); } - Stripe::setApiKey($this->api->getSecretKey()); - - /** @see Retrieve::retrieve() */ - $apiResource = $apiResourceClass::retrieve( + $apiResource = $service->retrieve( $request->getId(), $request->getOptions() ); - /* @see Delete::delete() */ return $apiResource->delete(); } diff --git a/src/Action/Api/Resource/AbstractRetrieveAction.php b/src/Action/Api/Resource/AbstractRetrieveAction.php index 25cf0b1..04901fb 100644 --- a/src/Action/Api/Resource/AbstractRetrieveAction.php +++ b/src/Action/Api/Resource/AbstractRetrieveAction.php @@ -8,9 +8,7 @@ use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInterface; use Payum\Core\Exception\LogicException; use Payum\Core\Exception\RequestNotSupportedException; -use Stripe\ApiOperations\Retrieve; use Stripe\ApiResource; -use Stripe\Stripe; abstract class AbstractRetrieveAction implements RetrieveResourceActionInterface { @@ -31,15 +29,12 @@ public function execute($request): void public function retrieveApiResource(RetrieveInterface $request): ApiResource { - $apiResourceClass = $this->getApiResourceClass(); - if (false === method_exists($apiResourceClass, 'retrieve')) { - throw new LogicException(sprintf('This class "%s" is not an instance of "%s" !', $apiResourceClass, Retrieve::class)); + $service = $this->getService(); + if (false === method_exists($service, 'retrieve')) { + throw new LogicException('This Stripe service does not have "retrieve" method !'); } - Stripe::setApiKey($this->api->getSecretKey()); - - /* @see Retrieve::retrieve() */ - return $apiResourceClass::retrieve( + return $service->retrieve( $request->getId(), $request->getOptions() ); diff --git a/src/Action/Api/Resource/AbstractUpdateAction.php b/src/Action/Api/Resource/AbstractUpdateAction.php index eeb88b5..bdbe2c3 100644 --- a/src/Action/Api/Resource/AbstractUpdateAction.php +++ b/src/Action/Api/Resource/AbstractUpdateAction.php @@ -8,9 +8,7 @@ use FluxSE\PayumStripe\Request\Api\Resource\UpdateInterface; use Payum\Core\Exception\LogicException; use Payum\Core\Exception\RequestNotSupportedException; -use Stripe\ApiOperations\Update; use Stripe\ApiResource; -use Stripe\Stripe; abstract class AbstractUpdateAction implements UpdateResourceActionInterface { @@ -31,15 +29,13 @@ public function execute($request): void public function updateApiResource(UpdateInterface $request): ApiResource { - $apiResourceClass = $this->getApiResourceClass(); - if (false === method_exists($apiResourceClass, 'update')) { - throw new LogicException(sprintf('This class "%s" is not an instance of "%s" !', $apiResourceClass, Update::class)); - } + $service = $this->getService(); - Stripe::setApiKey($this->api->getSecretKey()); + if (false === method_exists($service, 'update')) { + throw new LogicException('This Stripe service does not have "update" method !'); + } - /* @see Update::update() */ - return $apiResourceClass::update( + return $service->update( $request->getId(), $request->getParameters(), $request->getOptions() diff --git a/src/Action/Api/Resource/AllCouponAction.php b/src/Action/Api/Resource/AllCouponAction.php index 60b839b..2402fe4 100644 --- a/src/Action/Api/Resource/AllCouponAction.php +++ b/src/Action/Api/Resource/AllCouponAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\AllCoupon; use FluxSE\PayumStripe\Request\Api\Resource\AllInterface; -use Stripe\Coupon; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class AllCouponAction extends AbstractAllAction { - protected $apiResourceClass = Coupon::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->coupons; + } public function supportAlso(AllInterface $request): bool { diff --git a/src/Action/Api/Resource/AllCustomerAction.php b/src/Action/Api/Resource/AllCustomerAction.php index 6532d5b..eeda501 100644 --- a/src/Action/Api/Resource/AllCustomerAction.php +++ b/src/Action/Api/Resource/AllCustomerAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\AllCustomer; use FluxSE\PayumStripe\Request\Api\Resource\AllInterface; -use Stripe\Customer; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class AllCustomerAction extends AbstractAllAction { - protected $apiResourceClass = Customer::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->customers; + } public function supportAlso(AllInterface $request): bool { diff --git a/src/Action/Api/Resource/AllInvoiceAction.php b/src/Action/Api/Resource/AllInvoiceAction.php index 1d6a0aa..9eec7b4 100644 --- a/src/Action/Api/Resource/AllInvoiceAction.php +++ b/src/Action/Api/Resource/AllInvoiceAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\AllInterface; use FluxSE\PayumStripe\Request\Api\Resource\AllInvoice; -use Stripe\Invoice; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class AllInvoiceAction extends AbstractAllAction { - protected $apiResourceClass = Invoice::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->invoices; + } public function supportAlso(AllInterface $request): bool { diff --git a/src/Action/Api/Resource/AllSessionAction.php b/src/Action/Api/Resource/AllSessionAction.php index e0e0800..dfd86f1 100644 --- a/src/Action/Api/Resource/AllSessionAction.php +++ b/src/Action/Api/Resource/AllSessionAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\AllInterface; use FluxSE\PayumStripe\Request\Api\Resource\AllSession; -use Stripe\Checkout\Session; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class AllSessionAction extends AbstractAllAction { - protected $apiResourceClass = Session::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->checkout->sessions; + } public function supportAlso(AllInterface $request): bool { diff --git a/src/Action/Api/Resource/AllTaxRateAction.php b/src/Action/Api/Resource/AllTaxRateAction.php index 0f1232d..819f835 100644 --- a/src/Action/Api/Resource/AllTaxRateAction.php +++ b/src/Action/Api/Resource/AllTaxRateAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\AllInterface; use FluxSE\PayumStripe\Request\Api\Resource\AllTaxRate; -use Stripe\TaxRate; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class AllTaxRateAction extends AbstractAllAction { - protected $apiResourceClass = TaxRate::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->taxRates; + } public function supportAlso(AllInterface $request): bool { diff --git a/src/Action/Api/Resource/CancelPaymentIntentAction.php b/src/Action/Api/Resource/CancelPaymentIntentAction.php index 01fdd51..be0f7c3 100644 --- a/src/Action/Api/Resource/CancelPaymentIntentAction.php +++ b/src/Action/Api/Resource/CancelPaymentIntentAction.php @@ -9,10 +9,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInterface; use Stripe\ApiResource; use Stripe\PaymentIntent; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class CancelPaymentIntentAction extends AbstractRetrieveAction { - protected $apiResourceClass = PaymentIntent::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->paymentIntents; + } public function supportAlso(RetrieveInterface $request): bool { diff --git a/src/Action/Api/Resource/CancelSetupIntentAction.php b/src/Action/Api/Resource/CancelSetupIntentAction.php index 7b431b1..5f5a75f 100644 --- a/src/Action/Api/Resource/CancelSetupIntentAction.php +++ b/src/Action/Api/Resource/CancelSetupIntentAction.php @@ -8,11 +8,16 @@ use FluxSE\PayumStripe\Request\Api\Resource\CustomCallInterface; use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInterface; use Stripe\ApiResource; +use Stripe\Service\AbstractService; use Stripe\SetupIntent; +use Stripe\StripeClient; final class CancelSetupIntentAction extends AbstractRetrieveAction { - protected $apiResourceClass = SetupIntent::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->setupIntents; + } public function supportAlso(RetrieveInterface $request): bool { diff --git a/src/Action/Api/Resource/CancelSubscriptionAction.php b/src/Action/Api/Resource/CancelSubscriptionAction.php index 93753b4..049ba5e 100644 --- a/src/Action/Api/Resource/CancelSubscriptionAction.php +++ b/src/Action/Api/Resource/CancelSubscriptionAction.php @@ -8,11 +8,16 @@ use FluxSE\PayumStripe\Request\Api\Resource\CustomCallInterface; use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInterface; use Stripe\ApiResource; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; use Stripe\Subscription; final class CancelSubscriptionAction extends AbstractRetrieveAction { - protected $apiResourceClass = Subscription::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->subscriptions; + } public function supportAlso(RetrieveInterface $request): bool { diff --git a/src/Action/Api/Resource/CapturePaymentIntentAction.php b/src/Action/Api/Resource/CapturePaymentIntentAction.php index 8b362b4..5480dce 100644 --- a/src/Action/Api/Resource/CapturePaymentIntentAction.php +++ b/src/Action/Api/Resource/CapturePaymentIntentAction.php @@ -9,10 +9,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInterface; use Stripe\ApiResource; use Stripe\PaymentIntent; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class CapturePaymentIntentAction extends AbstractRetrieveAction { - protected $apiResourceClass = PaymentIntent::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->paymentIntents; + } public function supportAlso(RetrieveInterface $request): bool { diff --git a/src/Action/Api/Resource/CreateCouponAction.php b/src/Action/Api/Resource/CreateCouponAction.php index 678ec3c..138e2b8 100644 --- a/src/Action/Api/Resource/CreateCouponAction.php +++ b/src/Action/Api/Resource/CreateCouponAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\CreateCoupon; use FluxSE\PayumStripe\Request\Api\Resource\CreateInterface; -use Stripe\Coupon; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class CreateCouponAction extends AbstractCreateAction { - protected $apiResourceClass = Coupon::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->coupons; + } public function supportAlso(CreateInterface $request): bool { diff --git a/src/Action/Api/Resource/CreateCustomerAction.php b/src/Action/Api/Resource/CreateCustomerAction.php index 01e8473..7941336 100644 --- a/src/Action/Api/Resource/CreateCustomerAction.php +++ b/src/Action/Api/Resource/CreateCustomerAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\CreateCustomer; use FluxSE\PayumStripe\Request\Api\Resource\CreateInterface; -use Stripe\Customer; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class CreateCustomerAction extends AbstractCreateAction { - protected $apiResourceClass = Customer::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->customers; + } public function supportAlso(CreateInterface $request): bool { diff --git a/src/Action/Api/Resource/CreatePaymentIntentAction.php b/src/Action/Api/Resource/CreatePaymentIntentAction.php index 11dd76a..d0e382f 100644 --- a/src/Action/Api/Resource/CreatePaymentIntentAction.php +++ b/src/Action/Api/Resource/CreatePaymentIntentAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\CreateInterface; use FluxSE\PayumStripe\Request\Api\Resource\CreatePaymentIntent; -use Stripe\PaymentIntent; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class CreatePaymentIntentAction extends AbstractCreateAction { - protected $apiResourceClass = PaymentIntent::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->paymentIntents; + } public function supportAlso(CreateInterface $request): bool { diff --git a/src/Action/Api/Resource/CreatePaymentMethodAction.php b/src/Action/Api/Resource/CreatePaymentMethodAction.php index dc3331c..82c7b48 100644 --- a/src/Action/Api/Resource/CreatePaymentMethodAction.php +++ b/src/Action/Api/Resource/CreatePaymentMethodAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\CreateInterface; use FluxSE\PayumStripe\Request\Api\Resource\CreatePaymentMethod; -use Stripe\PaymentMethod; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class CreatePaymentMethodAction extends AbstractCreateAction { - protected $apiResourceClass = PaymentMethod::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->paymentMethods; + } public function supportAlso(CreateInterface $request): bool { diff --git a/src/Action/Api/Resource/CreatePlanAction.php b/src/Action/Api/Resource/CreatePlanAction.php index 36bf0db..7f638dd 100644 --- a/src/Action/Api/Resource/CreatePlanAction.php +++ b/src/Action/Api/Resource/CreatePlanAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\CreateInterface; use FluxSE\PayumStripe\Request\Api\Resource\CreatePlan; -use Stripe\Plan; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class CreatePlanAction extends AbstractCreateAction { - protected $apiResourceClass = Plan::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->plans; + } public function supportAlso(CreateInterface $request): bool { diff --git a/src/Action/Api/Resource/CreateRefundAction.php b/src/Action/Api/Resource/CreateRefundAction.php index 9ba6988..7a58512 100644 --- a/src/Action/Api/Resource/CreateRefundAction.php +++ b/src/Action/Api/Resource/CreateRefundAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\CreateInterface; use FluxSE\PayumStripe\Request\Api\Resource\CreateRefund; -use Stripe\Refund; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class CreateRefundAction extends AbstractCreateAction { - protected $apiResourceClass = Refund::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->refunds; + } public function supportAlso(CreateInterface $request): bool { diff --git a/src/Action/Api/Resource/CreateSessionAction.php b/src/Action/Api/Resource/CreateSessionAction.php index 2ab2b44..972fdb9 100644 --- a/src/Action/Api/Resource/CreateSessionAction.php +++ b/src/Action/Api/Resource/CreateSessionAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\CreateInterface; use FluxSE\PayumStripe\Request\Api\Resource\CreateSession; -use Stripe\Checkout\Session; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class CreateSessionAction extends AbstractCreateAction { - protected $apiResourceClass = Session::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->checkout->sessions; + } public function supportAlso(CreateInterface $request): bool { diff --git a/src/Action/Api/Resource/CreateSetupIntentAction.php b/src/Action/Api/Resource/CreateSetupIntentAction.php index 262084f..1de570b 100644 --- a/src/Action/Api/Resource/CreateSetupIntentAction.php +++ b/src/Action/Api/Resource/CreateSetupIntentAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\CreateInterface; use FluxSE\PayumStripe\Request\Api\Resource\CreateSetupIntent; -use Stripe\SetupIntent; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class CreateSetupIntentAction extends AbstractCreateAction { - protected $apiResourceClass = SetupIntent::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->setupIntents; + } public function supportAlso(CreateInterface $request): bool { diff --git a/src/Action/Api/Resource/CreateSubscriptionAction.php b/src/Action/Api/Resource/CreateSubscriptionAction.php index 8bc4ef4..65b7638 100644 --- a/src/Action/Api/Resource/CreateSubscriptionAction.php +++ b/src/Action/Api/Resource/CreateSubscriptionAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\CreateInterface; use FluxSE\PayumStripe\Request\Api\Resource\CreateSubscription; -use Stripe\Subscription; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class CreateSubscriptionAction extends AbstractCreateAction { - protected $apiResourceClass = Subscription::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->subscriptions; + } public function supportAlso(CreateInterface $request): bool { diff --git a/src/Action/Api/Resource/CreateTaxRateAction.php b/src/Action/Api/Resource/CreateTaxRateAction.php index 6519d84..dcb1c17 100644 --- a/src/Action/Api/Resource/CreateTaxRateAction.php +++ b/src/Action/Api/Resource/CreateTaxRateAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\CreateInterface; use FluxSE\PayumStripe\Request\Api\Resource\CreateTaxRate; -use Stripe\TaxRate; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class CreateTaxRateAction extends AbstractCreateAction { - protected $apiResourceClass = TaxRate::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->taxRates; + } public function supportAlso(CreateInterface $request): bool { diff --git a/src/Action/Api/Resource/DeleteCouponAction.php b/src/Action/Api/Resource/DeleteCouponAction.php index d0cc6c1..ca48405 100644 --- a/src/Action/Api/Resource/DeleteCouponAction.php +++ b/src/Action/Api/Resource/DeleteCouponAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\DeleteCoupon; use FluxSE\PayumStripe\Request\Api\Resource\DeleteInterface; -use Stripe\Coupon; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class DeleteCouponAction extends AbstractDeleteAction { - protected $apiResourceClass = Coupon::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->coupons; + } public function supportAlso(DeleteInterface $request): bool { diff --git a/src/Action/Api/Resource/DeletePlanAction.php b/src/Action/Api/Resource/DeletePlanAction.php index 7554a4f..fadd1de 100644 --- a/src/Action/Api/Resource/DeletePlanAction.php +++ b/src/Action/Api/Resource/DeletePlanAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\DeleteInterface; use FluxSE\PayumStripe\Request\Api\Resource\DeletePlan; -use Stripe\Plan; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class DeletePlanAction extends AbstractDeleteAction { - protected $apiResourceClass = Plan::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->plans; + } public function supportAlso(DeleteInterface $request): bool { diff --git a/src/Action/Api/Resource/ExpireSessionAction.php b/src/Action/Api/Resource/ExpireSessionAction.php index 47e56f9..e603c33 100644 --- a/src/Action/Api/Resource/ExpireSessionAction.php +++ b/src/Action/Api/Resource/ExpireSessionAction.php @@ -9,10 +9,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInterface; use Stripe\ApiResource; use Stripe\Checkout\Session; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class ExpireSessionAction extends AbstractRetrieveAction { - protected $apiResourceClass = Session::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->checkout->sessions; + } public function supportAlso(RetrieveInterface $request): bool { diff --git a/src/Action/Api/Resource/ResourceActionInterface.php b/src/Action/Api/Resource/ResourceActionInterface.php index 4fe6b45..d8419d7 100644 --- a/src/Action/Api/Resource/ResourceActionInterface.php +++ b/src/Action/Api/Resource/ResourceActionInterface.php @@ -6,10 +6,10 @@ use Payum\Core\Action\ActionInterface; use Payum\Core\ApiAwareInterface; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; interface ResourceActionInterface extends ActionInterface, ApiAwareInterface { - public function getApiResourceClass(): string; - - public function setApiResourceClass(string $apiResourceClass): void; + public function getStripeService(StripeClient $stripeClient): AbstractService; } diff --git a/src/Action/Api/Resource/ResourceAwareActionTrait.php b/src/Action/Api/Resource/ResourceAwareActionTrait.php index 60c69ea..327ed0f 100644 --- a/src/Action/Api/Resource/ResourceAwareActionTrait.php +++ b/src/Action/Api/Resource/ResourceAwareActionTrait.php @@ -4,18 +4,24 @@ namespace FluxSE\PayumStripe\Action\Api\Resource; +use FluxSE\PayumStripe\Api\StripeClientAwareInterface; +use Payum\Core\Exception\LogicException; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; + +/** + * @property StripeClientAwareInterface $api + */ trait ResourceAwareActionTrait { - /** @var string */ - protected $apiResourceClass = ''; + abstract public function getStripeService(StripeClient $stripeClient): AbstractService; - public function getApiResourceClass(): string + protected function getService(): AbstractService { - return $this->apiResourceClass; - } + if (null === $this->api) { + throw new LogicException('Api class not found !'); + } - public function setApiResourceClass(string $apiResourceClass): void - { - $this->apiResourceClass = $apiResourceClass; + return $this->getStripeService($this->api->getStripeClient()); } } diff --git a/src/Action/Api/Resource/RetrieveChargeAction.php b/src/Action/Api/Resource/RetrieveChargeAction.php index 6f9f235..6662d3e 100644 --- a/src/Action/Api/Resource/RetrieveChargeAction.php +++ b/src/Action/Api/Resource/RetrieveChargeAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\RetrieveCharge; use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInterface; -use Stripe\Charge; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class RetrieveChargeAction extends AbstractRetrieveAction { - protected $apiResourceClass = Charge::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->charges; + } public function supportAlso(RetrieveInterface $request): bool { diff --git a/src/Action/Api/Resource/RetrieveCouponAction.php b/src/Action/Api/Resource/RetrieveCouponAction.php index 72431b7..e18ebc8 100644 --- a/src/Action/Api/Resource/RetrieveCouponAction.php +++ b/src/Action/Api/Resource/RetrieveCouponAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\RetrieveCoupon; use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInterface; -use Stripe\Coupon; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class RetrieveCouponAction extends AbstractRetrieveAction { - protected $apiResourceClass = Coupon::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->coupons; + } public function supportAlso(RetrieveInterface $request): bool { diff --git a/src/Action/Api/Resource/RetrieveCustomerAction.php b/src/Action/Api/Resource/RetrieveCustomerAction.php index e8a5e5c..0025111 100644 --- a/src/Action/Api/Resource/RetrieveCustomerAction.php +++ b/src/Action/Api/Resource/RetrieveCustomerAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\RetrieveCustomer; use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInterface; -use Stripe\Customer; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class RetrieveCustomerAction extends AbstractRetrieveAction { - protected $apiResourceClass = Customer::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->customers; + } public function supportAlso(RetrieveInterface $request): bool { diff --git a/src/Action/Api/Resource/RetrieveInvoiceAction.php b/src/Action/Api/Resource/RetrieveInvoiceAction.php index cc684d5..22687fa 100644 --- a/src/Action/Api/Resource/RetrieveInvoiceAction.php +++ b/src/Action/Api/Resource/RetrieveInvoiceAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInterface; use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInvoice; -use Stripe\Invoice; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class RetrieveInvoiceAction extends AbstractRetrieveAction { - protected $apiResourceClass = Invoice::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->invoices; + } public function supportAlso(RetrieveInterface $request): bool { diff --git a/src/Action/Api/Resource/RetrievePaymentIntentAction.php b/src/Action/Api/Resource/RetrievePaymentIntentAction.php index 4f01ac0..ba962a8 100644 --- a/src/Action/Api/Resource/RetrievePaymentIntentAction.php +++ b/src/Action/Api/Resource/RetrievePaymentIntentAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInterface; use FluxSE\PayumStripe\Request\Api\Resource\RetrievePaymentIntent; -use Stripe\PaymentIntent; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class RetrievePaymentIntentAction extends AbstractRetrieveAction { - protected $apiResourceClass = PaymentIntent::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->paymentIntents; + } public function supportAlso(RetrieveInterface $request): bool { diff --git a/src/Action/Api/Resource/RetrievePaymentMethodAction.php b/src/Action/Api/Resource/RetrievePaymentMethodAction.php index 35ac30f..527f005 100644 --- a/src/Action/Api/Resource/RetrievePaymentMethodAction.php +++ b/src/Action/Api/Resource/RetrievePaymentMethodAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInterface; use FluxSE\PayumStripe\Request\Api\Resource\RetrievePaymentMethod; -use Stripe\PaymentMethod; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class RetrievePaymentMethodAction extends AbstractRetrieveAction { - protected $apiResourceClass = PaymentMethod::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->paymentMethods; + } public function supportAlso(RetrieveInterface $request): bool { diff --git a/src/Action/Api/Resource/RetrievePlanAction.php b/src/Action/Api/Resource/RetrievePlanAction.php index a683f51..1aee8ad 100644 --- a/src/Action/Api/Resource/RetrievePlanAction.php +++ b/src/Action/Api/Resource/RetrievePlanAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInterface; use FluxSE\PayumStripe\Request\Api\Resource\RetrievePlan; -use Stripe\Plan; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class RetrievePlanAction extends AbstractRetrieveAction { - protected $apiResourceClass = Plan::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->plans; + } public function supportAlso(RetrieveInterface $request): bool { diff --git a/src/Action/Api/Resource/RetrieveProductAction.php b/src/Action/Api/Resource/RetrieveProductAction.php index a818354..22201ad 100644 --- a/src/Action/Api/Resource/RetrieveProductAction.php +++ b/src/Action/Api/Resource/RetrieveProductAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInterface; use FluxSE\PayumStripe\Request\Api\Resource\RetrieveProduct; -use Stripe\Product; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class RetrieveProductAction extends AbstractRetrieveAction { - protected $apiResourceClass = Product::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->products; + } public function supportAlso(RetrieveInterface $request): bool { diff --git a/src/Action/Api/Resource/RetrieveSessionAction.php b/src/Action/Api/Resource/RetrieveSessionAction.php index 442d062..004083c 100644 --- a/src/Action/Api/Resource/RetrieveSessionAction.php +++ b/src/Action/Api/Resource/RetrieveSessionAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInterface; use FluxSE\PayumStripe\Request\Api\Resource\RetrieveSession; -use Stripe\Checkout\Session; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class RetrieveSessionAction extends AbstractRetrieveAction { - protected $apiResourceClass = Session::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->checkout->sessions; + } public function supportAlso(RetrieveInterface $request): bool { diff --git a/src/Action/Api/Resource/RetrieveSetupIntentAction.php b/src/Action/Api/Resource/RetrieveSetupIntentAction.php index 7135d2d..459b576 100644 --- a/src/Action/Api/Resource/RetrieveSetupIntentAction.php +++ b/src/Action/Api/Resource/RetrieveSetupIntentAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInterface; use FluxSE\PayumStripe\Request\Api\Resource\RetrieveSetupIntent; -use Stripe\SetupIntent; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class RetrieveSetupIntentAction extends AbstractRetrieveAction { - protected $apiResourceClass = SetupIntent::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->setupIntents; + } public function supportAlso(RetrieveInterface $request): bool { diff --git a/src/Action/Api/Resource/RetrieveSubscriptionAction.php b/src/Action/Api/Resource/RetrieveSubscriptionAction.php index f2b8f78..c40f586 100644 --- a/src/Action/Api/Resource/RetrieveSubscriptionAction.php +++ b/src/Action/Api/Resource/RetrieveSubscriptionAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\RetrieveInterface; use FluxSE\PayumStripe\Request\Api\Resource\RetrieveSubscription; -use Stripe\Subscription; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class RetrieveSubscriptionAction extends AbstractRetrieveAction { - protected $apiResourceClass = Subscription::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->subscriptions; + } public function supportAlso(RetrieveInterface $request): bool { diff --git a/src/Action/Api/Resource/UpdateCouponAction.php b/src/Action/Api/Resource/UpdateCouponAction.php index 60acd44..74fe45a 100644 --- a/src/Action/Api/Resource/UpdateCouponAction.php +++ b/src/Action/Api/Resource/UpdateCouponAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\UpdateCoupon; use FluxSE\PayumStripe\Request\Api\Resource\UpdateInterface; -use Stripe\Coupon; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class UpdateCouponAction extends AbstractUpdateAction { - protected $apiResourceClass = Coupon::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->coupons; + } public function supportAlso(UpdateInterface $request): bool { diff --git a/src/Action/Api/Resource/UpdatePaymentIntentAction.php b/src/Action/Api/Resource/UpdatePaymentIntentAction.php index be74308..fe73e18 100644 --- a/src/Action/Api/Resource/UpdatePaymentIntentAction.php +++ b/src/Action/Api/Resource/UpdatePaymentIntentAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\UpdateInterface; use FluxSE\PayumStripe\Request\Api\Resource\UpdatePaymentIntent; -use Stripe\PaymentIntent; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class UpdatePaymentIntentAction extends AbstractUpdateAction { - protected $apiResourceClass = PaymentIntent::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->paymentIntents; + } public function supportAlso(UpdateInterface $request): bool { diff --git a/src/Action/Api/Resource/UpdateSubscriptionAction.php b/src/Action/Api/Resource/UpdateSubscriptionAction.php index 781dba7..563e9cd 100644 --- a/src/Action/Api/Resource/UpdateSubscriptionAction.php +++ b/src/Action/Api/Resource/UpdateSubscriptionAction.php @@ -6,11 +6,15 @@ use FluxSE\PayumStripe\Request\Api\Resource\UpdateInterface; use FluxSE\PayumStripe\Request\Api\Resource\UpdateSubscription; -use Stripe\Subscription; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; final class UpdateSubscriptionAction extends AbstractUpdateAction { - protected $apiResourceClass = Subscription::class; + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return $stripeClient->subscriptions; + } public function supportAlso(UpdateInterface $request): bool { diff --git a/src/Action/Api/StripeApiAwareTrait.php b/src/Action/Api/StripeApiAwareTrait.php index 8292933..80eaa8d 100644 --- a/src/Action/Api/StripeApiAwareTrait.php +++ b/src/Action/Api/StripeApiAwareTrait.php @@ -4,11 +4,11 @@ namespace FluxSE\PayumStripe\Action\Api; -use FluxSE\PayumStripe\Api\KeysAwareInterface; +use FluxSE\PayumStripe\Api\StripeClientAwareInterface; use Payum\Core\ApiAwareTrait; /** - * @property KeysAwareInterface $api + * @property StripeClientAwareInterface $api */ trait StripeApiAwareTrait { @@ -21,7 +21,7 @@ public function __construct() protected function initApiClass(): void { - $this->apiClass = KeysAwareInterface::class; + $this->apiClass = StripeClientAwareInterface::class; } /** diff --git a/src/Api/KeysAwareTrait.php b/src/Api/KeysAwareTrait.php index e655c27..d51d802 100644 --- a/src/Api/KeysAwareTrait.php +++ b/src/Api/KeysAwareTrait.php @@ -8,8 +8,10 @@ trait KeysAwareTrait { /** @var string[] */ private $webhookSecretKeys; + /** @var string */ private $publishable; + /** @var string */ private $secret; @@ -43,7 +45,7 @@ public function getWebhookSecretKeys(): array public function hasWebhookSecretKey(string $webhookSecretKey): bool { - return in_array($webhookSecretKey, $this->webhookSecretKeys); + return in_array($webhookSecretKey, $this->webhookSecretKeys, true); } public function addWebhookSecretKey(string $webhookSecretKey): void diff --git a/src/Api/StripeCheckoutSessionApi.php b/src/Api/StripeCheckoutSessionApi.php index 74f5a35..7d56018 100644 --- a/src/Api/StripeCheckoutSessionApi.php +++ b/src/Api/StripeCheckoutSessionApi.php @@ -4,10 +4,12 @@ namespace FluxSE\PayumStripe\Api; +use Stripe\Util\ApiVersion; + final class StripeCheckoutSessionApi implements StripeCheckoutSessionApiInterface { - use KeysAwareTrait { - KeysAwareTrait::__construct as private __keysAwareTraitConstruct; + use StripeClientAwareTrait { + StripeClientAwareTrait::__construct as private __stripeClientAwareTraitConstruct; } use PaymentMethodTypesAwareTrait { @@ -21,9 +23,19 @@ public function __construct( string $publishable, string $secret, array $webhookSecretKeys = [], + ?string $clientId = null, + ?string $stripeAccount = null, + string $stripeVersion = ApiVersion::CURRENT, array $paymentMethodTypes = self::DEFAULT_PAYMENT_METHOD_TYPES ) { - $this->__keysAwareTraitConstruct($publishable, $secret, $webhookSecretKeys); + $this->__stripeClientAwareTraitConstruct( + $publishable, + $secret, + $webhookSecretKeys, + $clientId, + $stripeAccount, + $stripeVersion + ); $this->__paymentMethodTypesAwareTraitConstruct($paymentMethodTypes); } } diff --git a/src/Api/StripeCheckoutSessionApiInterface.php b/src/Api/StripeCheckoutSessionApiInterface.php index 2571d15..764dab1 100644 --- a/src/Api/StripeCheckoutSessionApiInterface.php +++ b/src/Api/StripeCheckoutSessionApiInterface.php @@ -4,7 +4,7 @@ namespace FluxSE\PayumStripe\Api; -interface StripeCheckoutSessionApiInterface extends KeysAwareInterface, PaymentMethodTypesAwareInterface +interface StripeCheckoutSessionApiInterface extends StripeClientAwareInterface, PaymentMethodTypesAwareInterface { public const DEFAULT_PAYMENT_METHOD_TYPES = []; } diff --git a/src/Api/StripeClientAwareInterface.php b/src/Api/StripeClientAwareInterface.php new file mode 100644 index 0000000..c08d5f4 --- /dev/null +++ b/src/Api/StripeClientAwareInterface.php @@ -0,0 +1,12 @@ +__keysAwareTraitConstruct($publishable, $secret, $webhookSecretKeys); + $this->clientId = $clientId; + $this->stripeAccount = $stripeAccount; + $this->stripeVersion = $stripeVersion; + } + + public function getStripeClient(): StripeClient + { + if (null === $this->stripeClient) { + $this->stripeClient = new StripeClient([ + 'api_key' => $this->getSecretKey(), + 'client_id' => $this->getClientId(), + 'stripe_account' => $this->getStripeAccount(), + 'stripe_version' => $this->getStripeVersion(), + ]); + } + + return $this->stripeClient; + } + + public function getClientId(): ?string + { + return $this->clientId; + } + + public function getStripeAccount(): ?string + { + return $this->stripeAccount; + } + + public function getStripeVersion(): string + { + return $this->stripeVersion; + } +} diff --git a/src/Api/StripeJsApi.php b/src/Api/StripeJsApi.php index 77551aa..a6fcfdd 100644 --- a/src/Api/StripeJsApi.php +++ b/src/Api/StripeJsApi.php @@ -6,5 +6,5 @@ final class StripeJsApi implements StripeJsApiInterface { - use KeysAwareTrait; + use StripeClientAwareTrait; } diff --git a/src/Api/StripeJsApiInterface.php b/src/Api/StripeJsApiInterface.php index 146b9cd..9aa7106 100644 --- a/src/Api/StripeJsApiInterface.php +++ b/src/Api/StripeJsApiInterface.php @@ -4,6 +4,6 @@ namespace FluxSE\PayumStripe\Api; -interface StripeJsApiInterface extends KeysAwareInterface +interface StripeJsApiInterface extends StripeClientAwareInterface { } diff --git a/src/StripeCheckoutSessionGatewayFactory.php b/src/StripeCheckoutSessionGatewayFactory.php index f63415a..acf7bb4 100644 --- a/src/StripeCheckoutSessionGatewayFactory.php +++ b/src/StripeCheckoutSessionGatewayFactory.php @@ -64,6 +64,9 @@ protected function initApi(ArrayObject $config): KeysAwareInterface $config['publishable_key'], $config['secret_key'], $config['webhook_secret_keys'], + $config['client_id'], + $config['stripe_account'], + $config['stripe_version'], $config['payment_method_types'] ); } diff --git a/src/StripeJsGatewayFactory.php b/src/StripeJsGatewayFactory.php index a97e966..42dc96a 100644 --- a/src/StripeJsGatewayFactory.php +++ b/src/StripeJsGatewayFactory.php @@ -10,6 +10,7 @@ use FluxSE\PayumStripe\Action\StripeJs\ConvertPaymentAction; use FluxSE\PayumStripe\Api\KeysAwareInterface; use FluxSE\PayumStripe\Api\StripeCheckoutSessionApi; +use FluxSE\PayumStripe\Api\StripeJsApi; use Payum\Core\Bridge\Spl\ArrayObject; use Stripe\PaymentIntent; @@ -42,10 +43,13 @@ protected function populateConfig(ArrayObject $config): void protected function initApi(ArrayObject $config): KeysAwareInterface { - return new StripeCheckoutSessionApi( + return new StripeJsApi( $config['publishable_key'], $config['secret_key'], - $config['webhook_secret_keys'] + $config['webhook_secret_keys'], + $config['client_id'], + $config['stripe_account'], + $config['stripe_version'] ); } } diff --git a/tests/Action/Api/ApiAwareActionTestTrait.php b/tests/Action/Api/ApiAwareActionTestTrait.php index f108e48..1632b0e 100644 --- a/tests/Action/Api/ApiAwareActionTestTrait.php +++ b/tests/Action/Api/ApiAwareActionTestTrait.php @@ -4,8 +4,10 @@ namespace Tests\FluxSE\PayumStripe\Action\Api; -use FluxSE\PayumStripe\Api\KeysAwareInterface; +use FluxSE\PayumStripe\Api\StripeClientAwareInterface; use PHPUnit\Framework\MockObject\MockObject; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; trait ApiAwareActionTestTrait { @@ -21,25 +23,23 @@ trait ApiAwareActionTestTrait abstract protected function createMock(string $originalClassName): MockObject; /** - * @return MockObject&KeysAwareInterface + * @return MockObject&StripeClientAwareInterface */ - protected function createApiMock(bool $shouldGetSecretKey = true): KeysAwareInterface + protected function createApiMock(): StripeClientAwareInterface { + $stripeClient = new StripeClient('sk_test_123'); $apiMock = $this->createMock($this->getApiClass()); - if ($shouldGetSecretKey) { - $apiMock - ->expects($this->atLeastOnce()) - ->method('getSecretKey') - ->willReturn('sk_test_123') - ; - } + $apiMock + ->method('getStripeClient') + ->willReturn($stripeClient) + ; return $apiMock; } protected function getApiClass(): string { - return KeysAwareInterface::class; + return StripeClientAwareInterface::class; } } diff --git a/tests/Action/Api/ResolveWebhookEventActionTest.php b/tests/Action/Api/ResolveWebhookEventActionTest.php index 44e48dd..74ec069 100644 --- a/tests/Action/Api/ResolveWebhookEventActionTest.php +++ b/tests/Action/Api/ResolveWebhookEventActionTest.php @@ -43,7 +43,7 @@ public function testShouldThrowLogicExceptionWhenNoStripeSignatureIsFound(): voi ->method('execute') ->with($this->isInstanceOf(GetHttpRequest::class)); - $apiMock = $this->createApiMock(false); + $apiMock = $this->createApiMock(); $action->setApiClass(KeysAwareInterface::class); $action->setGateway($gatewayMock); @@ -81,7 +81,7 @@ public function testShouldThrowExceptionWhenSignatureFailed(): void $this->throwException(SignatureVerificationException::factory('')) ); - $apiMock = $this->createApiMock(false); + $apiMock = $this->createApiMock(); $apiMock ->expects($this->once()) ->method('getWebhookSecretKeys') @@ -131,7 +131,7 @@ public function testShouldResolveWebhookEventWithSymfonyRequestBridge(): void }) ); - $apiMock = $this->createApiMock(false); + $apiMock = $this->createApiMock(); $apiMock ->expects($this->once()) ->method('getWebhookSecretKeys') @@ -182,7 +182,7 @@ public function testShouldResolveWebhookEventWithPlainPHP(): void }) ); - $apiMock = $this->createApiMock(false); + $apiMock = $this->createApiMock(); $apiMock ->expects($this->once()) ->method('getWebhookSecretKeys') @@ -230,7 +230,7 @@ public function testShouldRequestNotSupportedExceptionWhenTheWebhookCanNotBeReso }) ); - $apiMock = $this->createApiMock(false); + $apiMock = $this->createApiMock(); $apiMock ->expects($this->once()) ->method('getWebhookSecretKeys') diff --git a/tests/Action/Api/Resource/AllActionTest.php b/tests/Action/Api/Resource/AllActionTest.php index 4fcde0c..d4776fb 100644 --- a/tests/Action/Api/Resource/AllActionTest.php +++ b/tests/Action/Api/Resource/AllActionTest.php @@ -29,7 +29,8 @@ use Stripe\Coupon; use Stripe\Customer; use Stripe\Invoice; -use Stripe\Issuing\CardDetails; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; use Stripe\TaxRate; use Tests\FluxSE\PayumStripe\Action\Api\ApiAwareActionTestTrait; use Tests\FluxSE\PayumStripe\Stripe\StripeApiTestHelper; @@ -68,7 +69,6 @@ public function testShouldAllAPaymentIntent( $action = new $allActionClass(); $action->setApiClass(KeysAwareInterface::class); $action->setApi($apiMock); - $this->assertEquals($allClass, $action->getApiResourceClass()); /** @var AbstractAll $request */ $request = new $allRequestClass(); @@ -115,10 +115,13 @@ public function supportAlso(AllInterface $request): bool { return true; } - }; - $action->setApiResourceClass(CardDetails::class); - $this->assertEquals(CardDetails::class, $action->getApiResourceClass()); + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return new class($stripeClient) extends AbstractService { + }; + } + }; $request = new class() extends AbstractAll { }; diff --git a/tests/Action/Api/Resource/CreateActionTest.php b/tests/Action/Api/Resource/CreateActionTest.php index 2fba757..92d8966 100644 --- a/tests/Action/Api/Resource/CreateActionTest.php +++ b/tests/Action/Api/Resource/CreateActionTest.php @@ -41,7 +41,9 @@ use Stripe\PaymentMethod; use Stripe\Plan; use Stripe\Refund; +use Stripe\Service\AbstractService; use Stripe\SetupIntent; +use Stripe\StripeClient; use Stripe\Subscription; use Stripe\TaxRate; use Tests\FluxSE\PayumStripe\Action\Api\ApiAwareActionTestTrait; @@ -82,7 +84,6 @@ public function testShouldCreateAnApiRessource( /** @var AbstractCreateAction $action */ $action = new $createActionClass(); $action->setApiClass(KeysAwareInterface::class); - $this->assertEquals($createClass, $action->getApiResourceClass()); $action->setApi($apiMock); /** @var AbstractCreate $request */ @@ -119,10 +120,13 @@ public function supportAlso(CreateInterface $request): bool { return true; } - }; - $action->setApiResourceClass(CardDetails::class); - $this->assertEquals(CardDetails::class, $action->getApiResourceClass()); + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return new class() extends AbstractService { + }; + } + }; $request = new class($model) extends AbstractCreate { }; diff --git a/tests/Action/Api/Resource/CustomCallActionTest.php b/tests/Action/Api/Resource/CustomCallActionTest.php index 6774f28..d45b901 100644 --- a/tests/Action/Api/Resource/CustomCallActionTest.php +++ b/tests/Action/Api/Resource/CustomCallActionTest.php @@ -71,7 +71,6 @@ public function testShouldCallCustom( $action = new $customCallActionClass(); $action->setApiClass(KeysAwareInterface::class); $action->setApi($apiMock); - $this->assertEquals($customCallClass, $action->getApiResourceClass()); /** @var AbstractCustomCall $request */ $request = new $customCallRequestClass($id); diff --git a/tests/Action/Api/Resource/DeleteActionTest.php b/tests/Action/Api/Resource/DeleteActionTest.php index 121d08b..7191e34 100644 --- a/tests/Action/Api/Resource/DeleteActionTest.php +++ b/tests/Action/Api/Resource/DeleteActionTest.php @@ -22,7 +22,9 @@ use Stripe\Issuing\Card; use Stripe\Issuing\CardDetails; use Stripe\Plan; +use Stripe\Service\AbstractService; use Stripe\Stripe; +use Stripe\StripeClient; use Tests\FluxSE\PayumStripe\Action\Api\ApiAwareActionTestTrait; use Tests\FluxSE\PayumStripe\Stripe\StripeApiTestHelper; @@ -62,7 +64,6 @@ public function testShouldBeDeleted( $action = new $deleteActionClass(); $action->setApiClass(KeysAwareInterface::class); $action->setApi($apiMock); - $this->assertEquals($deleteClass, $action->getApiResourceClass()); /** @var AbstractDelete $request */ $request = new $deleteRequestClass($id); @@ -124,10 +125,13 @@ public function supportAlso(DeleteInterface $request): bool { return true; } - }; - $action->setApiResourceClass($faultClass); - $this->assertEquals($faultClass, $action->getApiResourceClass()); + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return new class() extends AbstractService { + }; + } + }; $request = new class($id) extends AbstractDelete { }; diff --git a/tests/Action/Api/Resource/RetrieveActionTest.php b/tests/Action/Api/Resource/RetrieveActionTest.php index 9562d73..c4376c3 100644 --- a/tests/Action/Api/Resource/RetrieveActionTest.php +++ b/tests/Action/Api/Resource/RetrieveActionTest.php @@ -45,7 +45,9 @@ use Stripe\PaymentMethod; use Stripe\Plan; use Stripe\Product; +use Stripe\Service\AbstractService; use Stripe\SetupIntent; +use Stripe\StripeClient; use Stripe\Subscription; use Tests\FluxSE\PayumStripe\Action\Api\ApiAwareActionTestTrait; use Tests\FluxSE\PayumStripe\Stripe\StripeApiTestHelper; @@ -86,7 +88,6 @@ public function testShouldBeRetrieved( $action = new $retrieveActionClass(); $action->setApiClass(KeysAwareInterface::class); $action->setApi($apiMock); - $this->assertEquals($retrieveClass, $action->getApiResourceClass()); /** @var AbstractRetrieve $request */ $request = new $retrieveRequestClass($id); @@ -122,10 +123,13 @@ public function supportAlso(RetrieveInterface $request): bool { return true; } - }; - $action->setApiResourceClass(CardDetails::class); - $this->assertEquals(CardDetails::class, $action->getApiResourceClass()); + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return new class() extends AbstractService { + }; + } + }; $request = new class($id) extends AbstractRetrieve { }; diff --git a/tests/Action/Api/Resource/UpdateActionTest.php b/tests/Action/Api/Resource/UpdateActionTest.php index 9b804c8..74231b1 100644 --- a/tests/Action/Api/Resource/UpdateActionTest.php +++ b/tests/Action/Api/Resource/UpdateActionTest.php @@ -22,6 +22,8 @@ use Stripe\Coupon; use Stripe\Issuing\CardDetails; use Stripe\PaymentIntent; +use Stripe\Service\AbstractService; +use Stripe\StripeClient; use Stripe\Subscription; use Tests\FluxSE\PayumStripe\Action\Api\ApiAwareActionTestTrait; use Tests\FluxSE\PayumStripe\Stripe\StripeApiTestHelper; @@ -63,7 +65,6 @@ public function testShouldUpdateAPaymentIntent( $action = new $updateActionClass(); $action->setApiClass(KeysAwareInterface::class); $action->setApi($apiMock); - $this->assertEquals($updateClass, $action->getApiResourceClass()); /** @var AbstractUpdate $request */ $request = new $updateRequestClass($id, $parameters); @@ -100,10 +101,13 @@ public function supportAlso(UpdateInterface $request): bool { return true; } - }; - $action->setApiResourceClass(CardDetails::class); - $this->assertEquals(CardDetails::class, $action->getApiResourceClass()); + public function getStripeService(StripeClient $stripeClient): AbstractService + { + return new class() extends AbstractService { + }; + } + }; $request = new class($id, $parameters) extends AbstractUpdate { }; diff --git a/tests/Action/Api/StripeApiAwareTraitTest.php b/tests/Action/Api/StripeApiAwareTraitTest.php index d665f93..f079753 100644 --- a/tests/Action/Api/StripeApiAwareTraitTest.php +++ b/tests/Action/Api/StripeApiAwareTraitTest.php @@ -5,8 +5,8 @@ namespace Tests\FluxSE\PayumStripe\Action\Api; use FluxSE\PayumStripe\Action\Api\StripeApiAwareTrait; -use FluxSE\PayumStripe\Api\KeysAwareInterface; -use FluxSE\PayumStripe\Api\KeysAwareTrait; +use FluxSE\PayumStripe\Api\StripeClientAwareInterface; +use FluxSE\PayumStripe\Api\StripeClientAwareTrait; use PHPUnit\Framework\TestCase; final class StripeApiAwareTraitTest extends TestCase @@ -14,13 +14,13 @@ final class StripeApiAwareTraitTest extends TestCase public function testShouldGetApiClass(): void { $trait = $this->getObjectForTrait(StripeApiAwareTrait::class); - $this->assertEquals(KeysAwareInterface::class, $trait->getApiClass()); + $this->assertEquals(StripeClientAwareInterface::class, $trait->getApiClass()); } public function testShouldSetApiClass(): void { $trait = $this->getObjectForTrait(StripeApiAwareTrait::class); - $trait->setApiClass(KeysAwareTrait::class); - $this->assertEquals(KeysAwareTrait::class, $trait->getApiClass()); + $trait->setApiClass(StripeClientAwareTrait::class); + $this->assertEquals(StripeClientAwareTrait::class, $trait->getApiClass()); } } diff --git a/tests/Action/StripeCheckoutSession/ConvertPaymentActionTest.php b/tests/Action/StripeCheckoutSession/ConvertPaymentActionTest.php index f05ef7a..6be5825 100644 --- a/tests/Action/StripeCheckoutSession/ConvertPaymentActionTest.php +++ b/tests/Action/StripeCheckoutSession/ConvertPaymentActionTest.php @@ -48,7 +48,7 @@ public function testShouldCorrectlyConvertPaymentToDetailsAndSetItBack(): void $action = new ConvertPaymentAction(); - $apiMock = $this->createApiMock(false); + $apiMock = $this->createApiMock(); $apiMock ->expects($this->once()) ->method('getPaymentMethodTypes') @@ -105,7 +105,7 @@ public function testShouldNotOverwriteAlreadySetExtraDetails(): void $action = new ConvertPaymentAction(); - $apiMock = $this->createApiMock(false); + $apiMock = $this->createApiMock(); $apiMock ->expects($this->once()) ->method('getPaymentMethodTypes') @@ -143,7 +143,7 @@ public function testShouldNotOverwriteAlreadySetPaymentMethodTypes(): void $action = new ConvertPaymentAction(); - $apiMock = $this->createApiMock(false); + $apiMock = $this->createApiMock(); $apiMock ->expects($this->once()) ->method('getPaymentMethodTypes') @@ -181,7 +181,7 @@ public function testShouldNotOverwriteAlreadySetCustomerEmail(): void $action = new ConvertPaymentAction(); - $apiMock = $this->createApiMock(false); + $apiMock = $this->createApiMock(); $apiMock ->expects($this->once()) ->method('getPaymentMethodTypes') @@ -219,7 +219,7 @@ public function testShouldNotOverwriteAlreadySetLineItems(): void $action = new ConvertPaymentAction(); - $apiMock = $this->createApiMock(false); + $apiMock = $this->createApiMock(); $apiMock ->expects($this->once()) ->method('getPaymentMethodTypes') @@ -254,7 +254,7 @@ public function testPaymentMethodTypes(): void $action = new ConvertPaymentAction(); - $apiMock = $this->createApiMock(false); + $apiMock = $this->createApiMock(); $apiMock ->expects($this->once()) ->method('getPaymentMethodTypes') diff --git a/tests/Action/StripeJs/Api/RenderStripeJsActionTest.php b/tests/Action/StripeJs/Api/RenderStripeJsActionTest.php index 0c918c4..b95bdbc 100644 --- a/tests/Action/StripeJs/Api/RenderStripeJsActionTest.php +++ b/tests/Action/StripeJs/Api/RenderStripeJsActionTest.php @@ -83,7 +83,7 @@ public function testShouldNotSupportObtainTokenRequestWithNotArrayAccessModel(st $request->setResult(''); }); - $apiMock = $this->createApiMock(false); + $apiMock = $this->createApiMock(); $apiMock ->expects($this->once()) ->method('getPublishableKey') diff --git a/tests/StripeGatewayFactoryTest.php b/tests/StripeGatewayFactoryTest.php index 15020fa..1b07b13 100644 --- a/tests/StripeGatewayFactoryTest.php +++ b/tests/StripeGatewayFactoryTest.php @@ -9,8 +9,10 @@ use FluxSE\PayumStripe\Action\StripeCheckoutSession; use FluxSE\PayumStripe\Action\StripeJs; use FluxSE\PayumStripe\Action\StripeJs\Api\RenderStripeJsAction; -use FluxSE\PayumStripe\Api\KeysAwareInterface; +use FluxSE\PayumStripe\Api\StripeCheckoutSessionApi; use FluxSE\PayumStripe\Api\StripeCheckoutSessionApiInterface; +use FluxSE\PayumStripe\Api\StripeClientAwareInterface; +use FluxSE\PayumStripe\Api\StripeJsApi; use FluxSE\PayumStripe\StripeCheckoutSessionGatewayFactory; use FluxSE\PayumStripe\StripeJsGatewayFactory; use Payum\Core\Bridge\Spl\ArrayObject; @@ -18,7 +20,6 @@ use Payum\Core\GatewayFactoryInterface; use PHPUnit\Framework\TestCase; use Stripe\PaymentIntent; - use function strpos; final class StripeGatewayFactoryTest extends TestCase @@ -175,8 +176,9 @@ public function testShouldAcceptDefaultOptions(string $gatewayClass): void ], 'payment_method_types' => ['card'], ]); + $newCredentials->defaults($config); $api = $config['payum.api']($newCredentials); - $this->assertInstanceOf(KeysAwareInterface::class, $api); + $this->assertInstanceOf(StripeClientAwareInterface::class, $api); } public function gatewayList(): array @@ -189,7 +191,14 @@ public function gatewayList(): array public function testConfigurationForCheckoutSession(): void { - $factory = new StripeCheckoutSessionGatewayFactory(); + $defaults = [ + 'publishable_key' => '12345', + 'secret_key' => '12345', + 'webhook_secret_keys' => [ + '12345' + ], + ]; + $factory = new StripeCheckoutSessionGatewayFactory($defaults); $config = $factory->createConfig(); @@ -213,11 +222,21 @@ public function testConfigurationForCheckoutSession(): void $this->assertEquals(new StripeCheckoutSession\Api\RedirectToCheckoutAction(), $actualActions['payum.action.redirect_to_checkout']); $this->assertArrayHasKey('payum.action.cancel.payment_intent.automatic', $actualActions); $this->assertEquals(new StripeCheckoutSession\CancelAction(), $actualActions['payum.action.cancel.payment_intent.automatic']); + + $api = $config['payum.api'](ArrayObject::ensureArrayObject($config)); + $this->assertInstanceOf(StripeCheckoutSessionApi::class, $api); } public function testConfigurationForStripeJs(): void { - $factory = new StripeJsGatewayFactory(); + $defaults = [ + 'publishable_key' => '12345', + 'secret_key' => '12345', + 'webhook_secret_keys' => [ + '12345' + ], + ]; + $factory = new StripeJsGatewayFactory($defaults); $config = $factory->createConfig(); @@ -244,8 +263,10 @@ public function testConfigurationForStripeJs(): void /** @var RenderStripeJsAction $payAction */ $payAction = $config['payum.action.render_stripe_js.payment_intent'](ArrayObject::ensureArrayObject($config)); $this->assertInstanceOf(RenderStripeJsAction::class, $payAction); - $this->assertEquals(PaymentIntent::class, $payAction->getApiResourceClass()); $this->assertEquals($config['payum.template.render_stripe_js.payment_intent'], $payAction->getTemplateName()); + + $api = $config['payum.api'](ArrayObject::ensureArrayObject($config)); + $this->assertInstanceOf(StripeJsApi::class, $api); } private function configuredPayumActions(array $config): array