Skip to content

Commit

Permalink
Merge pull request #59 from NicolasBarbey/main
Browse files Browse the repository at this point in the history
fix payment capture
  • Loading branch information
NicolasBarbey authored Oct 31, 2024
2 parents 6c0d44b + ee7b8d9 commit ec9c006
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
13 changes: 12 additions & 1 deletion Controller/PayPalApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Thelia\Controller\Front\BaseFrontController;
use Thelia\Core\Event\Order\OrderEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Log\Tlog;
use Thelia\Model\Base\OrderStatusQuery;
use Thelia\Model\CurrencyQuery;
use Thelia\Model\OrderQuery;
use Thelia\Model\OrderStatus;

#[Route("/paypal/api", name: "paypal_api_")]
class PayPalApiController extends BaseFrontController
Expand Down Expand Up @@ -94,7 +99,7 @@ public function createOrder(Request $request, PayPalApiService $payPalApiService
}

#[Route("/capture", name: "capture", methods: "POST")]
public function captureOrder(Request $request, PayPalApiService $payPalApiService)
public function captureOrder(Request $request, PayPalApiService $payPalApiService, EventDispatcherInterface $dispatcher)
{
try {
$data = json_decode($request->getContent(), true);
Expand All @@ -107,6 +112,12 @@ public function captureOrder(Request $request, PayPalApiService $payPalApiServic
);

$responseContent = $response->getContent();
$status = json_decode($responseContent, true)['status'];
if ("COMPLETED" === $status){
$event = new OrderEvent($paypalOrder->getOrder());
$event->setStatus(OrderStatusQuery::create()->filterByCode(OrderStatus::CODE_PAID)->findOne()?->getId());
$dispatcher->dispatch($event, TheliaEvents::ORDER_UPDATE_STATUS);
}
return new JsonResponse($responseContent);
} catch (\Exception $exception) {
Tlog::getInstance()->error($exception->getMessage());
Expand Down
40 changes: 36 additions & 4 deletions templates/frontOffice/default/paypal/paypal-payment.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,46 @@ <h1 class="text-2xl">{intl l="PayPal - Sélection de paiement"}</h1>
}
}

async function capturePaypalOrder(orderId)
{
try {
const response = await fetch("/paypal/api/capture", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
order_id: orderId
}),
});

const order = await response.json();

if (!response.ok) {
console.error('Error:', order.error.message);
return null;
}

const parsedOrder = JSON.parse(order);

if (parsedOrder.status === "COMPLETED") {
window.location.href = `/order/placed/{$order_id}`;
} else {
window.location.href = `/order-failed`;
}

} catch (error) {
console.error('Error:', error);
return null;
}
}

paypal.Buttons({
createOrder: function(data, actions) {
return createPaypalOrder(orderId)
return createPaypalOrder(orderId);
},
onApprove: function (data, actions) {
console.log('test Paypal')
console.log(data.orderID)
window.location.href = `/order/placed/{$order_id}`;
capturePaypalOrder(orderId);
}
}).render('#paypal-button-container');

Expand Down

0 comments on commit ec9c006

Please sign in to comment.