| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers; |
| 4 |
|
| 5 |
|
| 6 |
use FluentCart\Api\Checkout\CheckoutApi; |
| 7 |
use FluentCart\App\Helpers\CartHelper; |
| 8 |
use FluentCart\App\Helpers\Helper; |
| 9 |
use FluentCart\App\Models\ShippingMethod; |
| 10 |
use FluentCart\App\Services\RateLimiter; |
| 11 |
use FluentCart\App\Modules\PaymentMethods\Core\GatewayManager; |
| 12 |
use FluentCart\Framework\Http\Request\Request; |
| 13 |
|
| 14 |
class CheckoutController extends Controller |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @throws \Exception |
| 18 |
*/ |
| 19 |
public function placeOrder(Request $request) |
| 20 |
{ |
| 21 |
RateLimiter::isSpamming('place_order_attempt', 5, 60, true); |
| 22 |
|
| 23 |
CheckoutApi::placeOrder($request->all(), true); |
| 24 |
} |
| 25 |
|
| 26 |
public function getCheckoutSummary(Request $request) |
| 27 |
{ |
| 28 |
$checkOutHelper = \FluentCart\App\Helpers\CartCheckoutHelper::make(); |
| 29 |
$shippingMethodId = $request->getSafe('shipping_method_id', 'sanitize_text_field'); |
| 30 |
|
| 31 |
|
| 32 |
$charge = 0; |
| 33 |
$shippingMethod = ShippingMethod::query()->find($shippingMethodId); |
| 34 |
if (!empty($shippingMethod)) { |
| 35 |
$charge = CartHelper::calculateShippingMethodCharge($shippingMethod); |
| 36 |
} |
| 37 |
|
| 38 |
ob_start(); |
| 39 |
do_action('fluent_cart/views/checkout_page_cart_item_list', [ |
| 40 |
'checkout' => $checkOutHelper, |
| 41 |
'items' => $checkOutHelper->getItems() |
| 42 |
]); |
| 43 |
$views = ob_get_clean(); |
| 44 |
|
| 45 |
$items['views'] = $views; |
| 46 |
$items['subtotal'] = $checkOutHelper->getItemsAmountSubtotal(true, true); |
| 47 |
|
| 48 |
$total = $checkOutHelper->getItemsAmountTotal(false, false); |
| 49 |
|
| 50 |
$items['has_subscriptions'] = $checkOutHelper->hasSubscription() === 'yes'; |
| 51 |
$items['shipping_charge'] = $charge; |
| 52 |
$items['unformatted_total'] = $total + $charge; |
| 53 |
$formatted = Helper::toDecimal($total + $charge, true); |
| 54 |
$items['total'] = $formatted; |
| 55 |
$items['shipping_charge_formated'] = Helper::toDecimal($charge, true); |
| 56 |
$items['shipping_method_id'] = $shippingMethodId; |
| 57 |
|
| 58 |
return [ |
| 59 |
'items' => $items |
| 60 |
]; |
| 61 |
} |
| 62 |
|
| 63 |
public function getOrderInfo(Request $request) |
| 64 |
{ |
| 65 |
$paymentManager = GatewayManager::getInstance()->get($request->getSafe('method', 'sanitize_text_field')); |
| 66 |
return $paymentManager->getOrderInfo($request->all()); |
| 67 |
} |
| 68 |
} |
| 69 |
|