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