104 lines
3.6 KiB
PHP
104 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace KupShop\GTMBundle\ServerSideGTMEvent;
|
|
|
|
use KupShop\GTMBundle\Utils\DataLoaders\PurchaseState;
|
|
use KupShop\GTMBundle\Utils\PriceComputer;
|
|
use KupShop\OrderingBundle\Util\Order\DeliveryInfo;
|
|
use KupShop\OrderingBundle\Util\Order\OrderInfo;
|
|
use Symfony\Contracts\EventDispatcher\Event;
|
|
use Symfony\Contracts\Service\Attribute\Required;
|
|
|
|
class OrderRefundBaseEvent extends AbstractServerSideGTMEvent
|
|
{
|
|
public string $refundType;
|
|
|
|
#[Required]
|
|
public PriceComputer $priceComputer;
|
|
|
|
#[Required]
|
|
public DeliveryInfo $deliveryInfo;
|
|
|
|
#[Required]
|
|
public PurchaseState $purchaseState;
|
|
|
|
public function getPayload(Event $event): array
|
|
{
|
|
$dbcfg = \Settings::getDefault();
|
|
$order = $event->getOrder();
|
|
|
|
$items = [];
|
|
|
|
$index = 1;
|
|
foreach ($order->fetchItems() as $item) {
|
|
if (empty($item['product'])) {
|
|
continue;
|
|
}
|
|
|
|
/** @var \Product|\Variation $product */
|
|
$product = $item['product'];
|
|
$product->fetchProducer();
|
|
|
|
$newItem = $this->productLoader->loadProductObject($product);
|
|
$newItem['index'] = $index++;
|
|
$newItem['discount'] = $product->discount->asFloat();
|
|
$newItem['quantity'] = $item['pieces'];
|
|
|
|
$prices = $this->purchaseState->getPrices($item);
|
|
$newItem['price'] = $prices['price'];
|
|
$newItem['price_with_vat'] = $prices['priceWithVat'];
|
|
$newItem['price_without_vat'] = $prices['priceWithoutVat'];
|
|
$newItem['price_vat'] = $prices['priceVat'];
|
|
$newItem['vat_rate'] = $prices['vatRate'];
|
|
$items[] = $newItem;
|
|
}
|
|
|
|
$deliveryType = $order->getDeliveryType();
|
|
$shippingPrice = $this->getDeliveryPrice($order);
|
|
$shippinPrice = $this->priceComputer->getPrice($shippingPrice ?? toDecimal(0));
|
|
|
|
if (($dbcfg->analytics['google_tag_manager']['withoutDelivery'] ?? 'N') == 'Y') {
|
|
$tp = toDecimal($this->priceComputer->getPrice($order->getTotalPrice()));
|
|
$shipp = toDecimal($shippinPrice);
|
|
$totalPrice = $tp->sub($shipp);
|
|
} else {
|
|
$totalPrice = $order->getTotalPrice();
|
|
}
|
|
|
|
if ($coupons = OrderInfo::getUsedCoupons($order)) {
|
|
$coupons = join(',', $coupons);
|
|
}
|
|
|
|
$params = [
|
|
'currency' => $order->currency,
|
|
'transaction_id' => $order->order_no,
|
|
'value' => $this->priceComputer->getPrice($totalPrice),
|
|
'coupon' => $coupons ?: '',
|
|
'shipping' => $this->priceComputer->getPrice($order->getDeliveryPrice()),
|
|
'tax' => $this->priceComputer->getPrice($order->total_price_array['value_vat'], null, false),
|
|
'delivery_type' => $deliveryType->getDelivery()?->getClassName() ?? '',
|
|
'payment_type' => $deliveryType->getPayment()?->getClassName() ?? '',
|
|
'refund_type' => $this->refundType,
|
|
'items' => $items,
|
|
'user_data' => array_merge(['sha_email' => hash('sha256', $order->invoice_email.($dbcfg->analytics['google_tag_manager']['sha_salt'] ?? false ?: ''))], $this->getUserData($event)),
|
|
'cjevent' => $order->getData('cjevent'),
|
|
];
|
|
|
|
return [
|
|
'events' => [
|
|
[
|
|
'name' => 'refund',
|
|
'params' => $params,
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
public function getDeliveryPrice(\Order $order)
|
|
{
|
|
$delivery_item = $this->deliveryInfo->getDeliveryItem($order);
|
|
|
|
return $delivery_item ? $order->fetchItems()[$delivery_item]['total_price'] : null;
|
|
}
|
|
}
|