src/Application/UseCase/Product/GetCartOrderForCheckoutHandler.php line 38

Open in your IDE?
  1. <?php
  2. namespace Whater\Application\UseCase\Product;
  3. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  4. use Symfony\Component\Routing\Route;
  5. use Whater\Application\UseCase\AbstractCommandHandler;
  6. use Whater\Application\UseCase\Product\CommandRequest\GetCartOrderForCheckoutCommand;
  7. use Whater\Domain\Product\Exception\InvalidCartOrderAccessException;
  8. use Whater\Domain\User\Model\Role;
  9. use Whater\Domain\Product\Model\CartOrder;
  10. use Whater\Domain\Product\Repository\CartOrderRepositoryInterface;
  11. /**
  12. * Class GetCartOrderForCheckoutHandler
  13. * @package Whater\Application\UseCase\Whater
  14. */
  15. class GetCartOrderForCheckoutHandler extends AbstractCommandHandler
  16. {
  17. /**
  18. * @var CartOrderRepositoryInterface
  19. */
  20. private $repositoryCartOrder;
  21. /**
  22. * @param CartOrderRepositoryInterface $repositoryCartOrder
  23. */
  24. public function __construct(CartOrderRepositoryInterface $repositoryCartOrder)
  25. {
  26. $this->repositoryCartOrder = $repositoryCartOrder;
  27. }
  28. /**
  29. * @param GetCartOrderForCheckoutCommand $getCartOrderForCheckoutCommand
  30. * @return null|CartOrder
  31. */
  32. public function handle(GetCartOrderForCheckoutCommand $getCartOrderForCheckoutCommand): ?CartOrder
  33. {
  34. $grantUser = $getCartOrderForCheckoutCommand->user();
  35. $cartOrder = $this->repositoryCartOrder->findOneById(
  36. $getCartOrderForCheckoutCommand->cartOrderId()
  37. );
  38. if ($cartOrder->user() != null && ($grantUser == null || !$cartOrder->user()->equals($grantUser))) {
  39. throw new InvalidCartOrderAccessException();
  40. }
  41. if ($getCartOrderForCheckoutCommand->renewPaymentIntent() && $cartOrder->status() == CartOrder::ORDER_STATUS_PENDING) {
  42. if ($cartOrder->totalEuros() > 0) {
  43. $stripeSecretkey = $this->parameterBag()->get('stripe_private_key');
  44. $stripe = new \Stripe\StripeClient($stripeSecretkey);
  45. if ($cartOrder->cartOrderType() == CartOrder::ORDER_TYPE_LICENSE) {
  46. // Suscription
  47. $stripeCustomerId = $grantUser->stripeCustomerId();
  48. if ($stripeCustomerId == null) {
  49. $stripeCustomer = $stripe->customers->create([
  50. 'email' => $grantUser->email(),
  51. 'name' => $grantUser->fullName()
  52. ]);
  53. $stripeCustomerId = $stripeCustomer->id;
  54. }
  55. $interval = 'month';
  56. if ($cartOrder->cartItem(0)->meta()['payment_plan'] == 'LICENSE_PAYMENT_PLAN_ANNUAL') {
  57. $interval = 'year';
  58. }
  59. $stripeProduct = $stripe->products->create([
  60. 'name' => 'Suscripción a whater.app',
  61. 'shippable' => false,
  62. 'metadata' => [
  63. 'whater_product_slug' => $cartOrder->cartItem(0)->product()->productSlug(),
  64. ]
  65. // Puedes agregar más detalles del producto si es necesario
  66. ]);
  67. $stripePrice = $stripe->prices->create([
  68. 'product' => $stripeProduct->id,
  69. 'unit_amount' => $cartOrder->totalEuros() * 100,
  70. 'currency' => 'EUR',
  71. 'recurring' => ['interval' => $interval],
  72. 'metadata' => [
  73. 'cart_order_id' => $cartOrder->id(),
  74. ]
  75. ]);
  76. $stripeSubscription = $stripe->subscriptions->create([
  77. 'customer' => $stripeCustomerId,
  78. 'items' => [['price' => $stripePrice->id]],
  79. 'metadata' => [
  80. 'cart_order_id' => $cartOrder->id(),
  81. 'cart_url' => $this->router()->generate('web_public_cart_order_checkout', ['cartOrderId' => $cartOrder->id()], UrlGeneratorInterface::ABSOLUTE_URL)
  82. ],
  83. 'payment_behavior' => 'default_incomplete',
  84. 'payment_settings' => ['save_default_payment_method' => 'on_subscription'],
  85. 'expand' => ['latest_invoice.payment_intent']
  86. ]);
  87. $paymentIntent = $stripeSubscription->latest_invoice->payment_intent;
  88. $cartOrder->updateStripeInfo(
  89. json_decode(json_encode($paymentIntent), true),
  90. json_decode(json_encode($stripeSubscription), true)
  91. );
  92. } else {
  93. // One payment
  94. $params = [
  95. 'amount' => $cartOrder->totalEuros() * 100,
  96. 'description' => 'whater.app - ' . $cartOrder->id(),
  97. 'metadata' => [
  98. 'cart_order_id' => $cartOrder->id(),
  99. 'cart_url' => $this->router()->generate('web_public_cart_order_checkout', ['cartOrderId' => $cartOrder->id()], UrlGeneratorInterface::ABSOLUTE_URL)
  100. ],
  101. 'currency' => 'eur',
  102. 'automatic_payment_methods' => [
  103. 'enabled' => false,
  104. ],
  105. 'payment_method_types' => ['card']
  106. ];
  107. if ($cartOrder->cartOrderType() == CartOrder::ORDER_TYPE_CATALOG_PRODUCT) {
  108. $stripeExpressAccountId = $cartOrder->cartItem(0)->product()->whaterOrganization()->stripeExpressAccountId();
  109. $params['on_behalf_of'] = $stripeExpressAccountId;
  110. $whaterCommisionPercent = $cartOrder->cartItem(0)->product()->whaterCommisionPercent();
  111. $whaterFee = $cartOrder->totalEuros() * ($whaterCommisionPercent) / 100;
  112. $params['application_fee_amount'] = round($whaterFee * 100);
  113. $params['transfer_data'] = [
  114. 'destination' => $stripeExpressAccountId
  115. ];
  116. }
  117. $paymentIntent = $stripe->paymentIntents->create($params);
  118. $cartOrder->updateStripeInfo(
  119. json_decode(json_encode($paymentIntent), true)
  120. );
  121. }
  122. }
  123. $this->repositoryCartOrder->add($cartOrder);
  124. }
  125. return $cartOrder;
  126. }
  127. }