Skip to content

Commit

Permalink
Generate AccountingTransactions for the each charge on a paid stripe …
Browse files Browse the repository at this point in the history
…checkout session
  • Loading branch information
subiabre committed Jul 11, 2024
1 parent 8db00c7 commit 2430e51
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/Library/Economy/Payment/StripeGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace App\Library\Economy\Payment;

use App\Entity\AccountingTransaction;
use App\Entity\GatewayChargeType;
use App\Entity\GatewayCheckout;
use App\Entity\GatewayCheckoutStatus;
use App\Repository\GatewayCheckoutRepository;
use Doctrine\ORM\EntityManagerInterface;
use Stripe\Checkout\Session as StripeSession;
use Stripe\StripeClient;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -18,7 +20,8 @@ class StripeGateway implements GatewayInterface
public function __construct(
private string $stripeApiKey,
private RouterInterface $router,
private GatewayCheckoutRepository $gatewayCheckoutRepository
private GatewayCheckoutRepository $gatewayCheckoutRepository,
private EntityManagerInterface $entityManager
) {
$this->stripe = new StripeClient($stripeApiKey);
}
Expand Down Expand Up @@ -58,12 +61,38 @@ public function create(GatewayCheckout $checkout): GatewayCheckout

public function handleRedirect(Request $request): GatewayCheckout
{
$sessionId = $request->query->get('session_id');

$session = $this->stripe->checkout->sessions->retrieve($sessionId);
$checkout = $this->gatewayCheckoutRepository->findOneBy(
['gatewayReference' => $request->query->get('session_id')]
['gatewayReference' => $sessionId]
);

if ($checkout === null) {
throw new \Exception(sprintf("Could not find GatewayCheckout with gatewayReference '%s'", $sessionId));
}

if ($session->payment_status !== StripeSession::PAYMENT_STATUS_PAID) {
return $checkout;
}

$checkout->setStatus(GatewayCheckoutStatus::Charged);

foreach ($checkout->getCharges() as $charge) {
$transaction = new AccountingTransaction();
$transaction->setMoney($charge->getMoney());
$transaction->setOrigin($checkout->getOrigin());
$transaction->setTarget($charge->getTarget());

$this->entityManager->persist($transaction);

$charge->setTransaction($transaction);

$this->entityManager->persist($charge);
}

$this->entityManager->flush();

return $checkout;
}

Expand Down

0 comments on commit 2430e51

Please sign in to comment.