src/Services/StripeConnectService.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use App\Entity\Command;
  4. use App\Entity\Company;
  5. use App\Entity\Location;
  6. use App\Entity\Reservation;
  7. use MangoPay\Check;
  8. use Stripe\Account;
  9. use Stripe\AccountLink;
  10. use Stripe\Checkout\Session;
  11. use Stripe\Refund;
  12. use Stripe\Stripe;
  13. class StripeConnectService extends StripeClient
  14. {
  15.     public function __construct(string $stripe_sk)
  16.     {
  17.         parent::__construct($stripe_sk);
  18.     }
  19.     /**
  20.      * Create a new Stripe account for a connected user.
  21.      *
  22.      * @param string $type
  23.      * @param string $country
  24.      * @param string $email
  25.      * @return Account
  26.      */
  27.     public function createAccount(string $typestring $countrystring $email): Account
  28.     {
  29.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  30.         $account $stripe->accounts->create([
  31.             'type' => $type,
  32.             'country' => $country,
  33.             'email' => $email,
  34.         ]);
  35.         return $account;
  36.     }
  37.     /**
  38.      * Retrieve a Stripe account by its ID.
  39.      *
  40.      * @param string $accountId
  41.      * @return Account
  42.      */
  43.     public function getAccount(string $accountId): Account
  44.     {
  45.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  46.         $account $stripe->accounts->retrieve($accountId);
  47.         return $account;
  48.     }
  49.     
  50.     public function createAccountLink(string $accountIdstring $returnUrlstring $refreshUrl): AccountLink
  51.     {
  52.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  53.         $accountLink $stripe->accountLinks->create([
  54.             'account' => $accountId,
  55.             'refresh_url' => $refreshUrl,
  56.             'return_url' => $returnUrl,
  57.             'type' => 'account_onboarding',
  58.         ]);
  59.         return $accountLink;
  60.     }
  61.     
  62.     public function updateAccount(string $accountId, array $params): Account
  63.     {
  64.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  65.         $account $stripe->accounts->update($accountId$params);
  66.         return $account;
  67.     }
  68.     public function deleteAccount(string $accountId): Account
  69.     {
  70.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  71.         $account $stripe->accounts->delete($accountId);
  72.         return $account;
  73.     }
  74.     public function getAccountBalance(string $accountId)
  75.     {
  76.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  77.         $balance $stripe->balances->retrieve(['stripe_account' => $accountId]);
  78.         return $balance;
  79.     }
  80.     public function createCheckoutSession(
  81.         string $connectedAccountID,
  82.         string $price,
  83.         string $successUrl,
  84.         string $cancelUrl
  85.     ): Session {
  86.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  87.         $session $stripe->checkout->sessions->create([
  88.             'payment_method_types' => ['card'],
  89.             'mode' => 'payment',
  90.             'line_items' => [[
  91.                 'price_data' => [
  92.                     'currency' => 'eur',
  93.                     'product_data' => [
  94.                         'name' => 'Custom Payment',
  95.                     ],
  96.                     'unit_amount' => $price// amount in cents
  97.                 ],
  98.                 'quantity' => 1,
  99.             ]],
  100.             'payment_intent_data' => [
  101.                 // 'application_fee_amount' => 123,
  102.                 'transfer_data' => ['destination' => $connectedAccountID],
  103.             ],
  104.             'success_url' => $successUrl "?session_id={CHECKOUT_SESSION_ID}",
  105.             'cancel_url' => $cancelUrl
  106.         ]);
  107.         return $session;
  108.     }
  109.     /**
  110.      * Creates a checkout session for locations associated with connected accounts.
  111.      */
  112.     public function createCheckoutSessionForLocations(
  113.         Location $location,
  114.         string $connectedAccountID,
  115.         string $price,
  116.         string $successUrl,
  117.         string $cancelUrl
  118.     ): Session {
  119.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  120.         $appFeeAmount 0;
  121.         
  122.         foreach ($location->getAppFees() as $appFee) {
  123.             $appFeeAmount += $appFee->getValue();
  124.         }
  125.         
  126.         $location_cautions_amount 0
  127.         
  128.         foreach ($location->getCautions() as $caution) {
  129.             $location_cautions_amount += $caution->getMontant();
  130.         }
  131.         $session $stripe->checkout->sessions->create([
  132.             'mode' => 'payment',
  133.             'payment_method_types' => ['card'],
  134.             'line_items' => [[
  135.                 'price_data' => [
  136.                     'currency' => 'eur',
  137.                     'product_data' => [
  138.                         'name' => $location->getProduct()->getName(),
  139.                     ],
  140.                     'unit_amount' => $price// amount in cents
  141.                 ],
  142.                 'quantity' => 1,
  143.             ]],
  144.             'metadata' => ['location_id' => $location->getId()],
  145.             'payment_intent_data' => [
  146.                 'transfer_group' => 'location_' $location->getId(),
  147.                 // 💰 collect a platform fee (in cents)
  148.                 'application_fee_amount' => $appFeeAmount*100 $location_cautions_amount*100,
  149.                 // 🧾 send the remaining amount to the connected account
  150.                 'transfer_data' => [
  151.                     'destination' => $connectedAccountID// connected account ID
  152.                 ]
  153.             ],
  154.             'success_url' => $successUrl "?session_id={CHECKOUT_SESSION_ID}&client_id=" $location->getClient()->getId(),
  155.             'cancel_url' => $cancelUrl
  156.         ]);
  157.         return $session;
  158.     }
  159.     public function createCheckoutSessionForProductCommands(
  160.         Command $command,
  161.         array $line_items,
  162.         string $transfer_group,
  163.         string $successUrl,
  164.         string $cancelUrl
  165.     ): Session {
  166.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  167.         $session $stripe->checkout->sessions->create([
  168.             'line_items' => $line_items,
  169.             'payment_intent_data' => ['transfer_group' => $transfer_group],
  170.             'metadata' => [
  171.                 'command_id' => $command->getId(),
  172.                 'client_id' => $command->getClient()->getId(),
  173.             ], 
  174.             'mode' => 'payment',
  175.             'success_url' => $successUrl "?session_id={CHECKOUT_SESSION_ID}",
  176.             'cancel_url' => $cancelUrl
  177.         ]);
  178.         return $session;
  179.     }
  180.     public function createCheckoutSessionForReservations(
  181.         Reservation $reservation,
  182.         string $connectedAccountID,
  183.         string $price,
  184.         string $successUrl,
  185.         string $cancelUrl
  186.     ):Session {
  187.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  188.         $appFeeAmount 0;
  189.         foreach ($reservation->getAppFees() as $appFee) {
  190.             $appFeeAmount += $appFee->getValue();
  191.         }
  192.         
  193.         $session $stripe->checkout->sessions->create([
  194.             'line_items' => [[
  195.                 'price_data' => [
  196.                     'currency' => 'eur',
  197.                     'product_data' => [
  198.                         'name' => $reservation->getProduct()->getName(),
  199.                     ],
  200.                     'unit_amount' => $price// amount in cents
  201.                 ],
  202.                 'quantity' => 1,
  203.             ]],
  204.             'payment_intent_data' => [
  205.                 'on_behalf_of' => $connectedAccountID,
  206.                 'transfer_group' => 'RESERVATION' $reservation->getId(),
  207.                 // 💰 collect a platform fee (in cents)
  208.                 'application_fee_amount' => $appFeeAmount*100,
  209.                 // 🧾 send the remaining amount to the connected account
  210.                 'transfer_data' => [
  211.                     'destination' => $connectedAccountID// connected account ID
  212.                 ]],
  213.             'mode' => 'payment',
  214.             'success_url' => $successUrl "?session_id={CHECKOUT_SESSION_ID}",
  215.             'cancel_url' => $cancelUrl
  216.         ]);
  217.         return $session;
  218.     }
  219.     public function retrieveCheckoutSession(string $sessionId): Session
  220.     {
  221.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  222.         $session $stripe->checkout->sessions->retrieve($sessionId);
  223.         return $session;
  224.     }
  225.     public function retrieveInvoice(string $invoiceId)
  226.     {
  227.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  228.         $invoice $stripe->invoices->retrieve($invoiceId);
  229.         return $invoice;
  230.     }
  231.     public function createTransfer(string $amountstring $destinationAccountIdstring $chargeId, ?string $currency 'eur', ?string $description null)
  232.     {
  233.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  234.         $transfer $stripe->transfers->create([
  235.             'amount' => $amount,
  236.             'currency' => $currency,
  237.             'source_transaction' => $chargeId,
  238.             'destination' => $destinationAccountId,
  239.             'description' => $description,
  240.         ]);
  241.         return $transfer;
  242.     }
  243.     public function createTransferReversal(string $transferIdint $amount): \Stripe\TransferReversal
  244.     {
  245.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  246.         $reversal $stripe->transfers->createReversal($transferId, [
  247.             'amount' => $amount,
  248.         ]);
  249.         return $reversal;
  250.     }
  251.     public function retrieveTransfer(string $transferId)
  252.     {
  253.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  254.         $transfer $stripe->transfers->retrieve($transferId);
  255.         return $transfer;
  256.     }
  257.     public function retrieveLatestCharge(string $latestChargeId)
  258.     {
  259.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  260.         $charge $stripe->charges->retrieve($latestChargeId);
  261.         return $charge;
  262.     }
  263.     public function createChargeReversal(string $chargeIdint $amount): Refund
  264.     {
  265.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  266.         $charge $stripe->refunds->create(['charge' => $chargeId'amount' => $amount]);
  267.         return $charge;
  268.     }
  269.     /**
  270.      * Refund part of an application fee.
  271.      *
  272.      * @param string $applicationFeeId The fee ID (fee_xxx)
  273.      * @param int $amount Amount to refund in cents
  274.      */
  275.     public function refundApplicationFee(
  276.         string $applicationFeeId,
  277.         int $amount
  278.     ) {
  279.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  280.         return $stripe->applicationFees->createRefund(
  281.             $applicationFeeId,
  282.             [
  283.                 'amount' => $amount,
  284.             ]
  285.         );
  286.     }
  287.     public function createStripeLoginLink(string $accountId): \Stripe\LoginLink
  288.     {
  289.         $stripe = new \Stripe\StripeClient($this->stripe_sk);
  290.         $loginLink $stripe->accounts->createLoginLink($accountId);
  291.         return $loginLink;
  292.     }
  293.     public function getMissingRequirements(Account $account): array
  294.     {
  295.         return [
  296.             'currently_due' => $account->requirements->currently_due,
  297.             'past_due' => $account->requirements->past_due,
  298.             'eventually_due' => $account->requirements->eventually_due,
  299.             'pending_verification' => $account->requirements->pending_verification,
  300.             'current_deadline' => $account->requirements->current_deadline date('d-m-Y'$account->requirements->current_deadline) : null,
  301.         ];
  302.     }
  303.     public function hasMissingRequirements(Account $account): bool
  304.     {
  305.         return !empty($account->requirements->currently_due)
  306.             || !empty($account->requirements->past_due
  307.             || !empty($account->requirements->eventually_due);
  308.     }
  309.     public function formatRequirementsMessages(array $requirementsstring $url): array
  310.     {
  311.         $messages = [];
  312.         
  313.         foreach ($requirements as $category => $fields) {
  314.             if ($category == 'currently_due' && !empty($fields)) {
  315.                 $messages['warning'] = '<p>Votre compte Stripe a des exigences actuellement dues ' $requirements['current_deadline'] . '.</p> <a href="' $url '" class="btn btn-warning">Veuillez les remplir</a></p>';
  316.             }
  317.             
  318.             if ($category == 'past_due' && !empty($fields)) {
  319.                 $messages['danger'] = '<p>Votre compte Stripe a des exigences passées dues.</p> <a href="' $url '" class="btn btn-danger">Veuillez les remplir</a>';
  320.             } 
  321.             
  322.             if ($category == 'eventually_due' && !empty($fields)) {
  323.                 $messages['info'] = '<p>Votre compte Stripe a des exigences éventuellement dues.</p> <a href="' $url '" class="btn btn-info">Veuillez les remplir</a>';
  324.             }
  325.             
  326.             if ($category == 'pending_verification' && !empty($fields)) {
  327.                 $messages['info'] = '<p>Votre compte Stripe a des exigences en attente de vérification.</p> <a href="' $url '" class="btn btn-info">Veuillez les vérifier.</a>';
  328.             }
  329.         }
  330.         return $messages;
  331.     }
  332.     public function isFullyEnabled(Account $account): bool
  333.     {
  334.         return $account->charges_enabled && $account->payouts_enabled;
  335.     }
  336. }